Resultset class allowing chainable queries. Intended to be instanced internally. Collection.find(), Collection.where(), and Collection.chain() instantiate this.

Example

mycollection.chain()
.find({ 'doors' : 4 })
.where(function(obj) { return obj.name === 'Toyota' })
.data();

Param

The collection which this Resultset will query against.

Type Parameters

Hierarchy

  • ResultSet

Constructors

Properties

$and: ((expressionArray: any[]) => ResultSet<any>) = ResultSet.prototype.findAnd

Type declaration

    • (expressionArray: any[]): ResultSet<any>
    • findAnd() - oversee the operation of AND'ed query expressions. AND'ed expression evaluation runs each expression progressively against the full collection, internally utilizing existing chained resultset functionality. Only the first filter can utilize a binary index.

      Returns

      this resultset for further chain ops.

      Parameters

      • expressionArray: any[]

        array of expressions

      Returns ResultSet<any>

$or: ((expressionArray: any[]) => ResultSet<any>) = ResultSet.prototype.findOr

Type declaration

    • (expressionArray: any[]): ResultSet<any>
    • findOr() - oversee the operation of OR'ed query expressions. OR'ed expression evaluation runs each expression individually against the full collection, and finally does a set OR on each expression's results. Each evaluation can utilize a binary index to prevent multiple linear array scans.

      Returns

      this resultset for further chain ops.

      Parameters

      • expressionArray: any[]

        array of expressions

      Returns ResultSet<any>

branch: (() => ResultSet<any>) = ResultSet.prototype.copy

Type declaration

collection: Collection<RST>
disableFreeze: boolean
filterInitialized: boolean
filteredrows: number[]
options: Record<string, any>
rightData: RST[]

Methods

  • Allows sorting a resultset based on multiple columns.

    Example

    // to sort by age and then name (both ascending)
    rs.compoundsort(['age', 'name']);
    // to sort by age (ascending) and then by name (descending)
    rs.compoundsort(['age', ['name', true]]);

    Returns

    Reference to this resultset, sorted, for future chain operations.

    Memberof

    Resultset

    Parameters

    • properties: (string | [string, boolean])[]

      array of property names or subarray of [propertyname, isdesc] used evaluate sort order

    Returns ResultSet<RST>

  • count() - returns the number of documents in the resultset.

    Returns

    The number of documents in the resultset.

    Example

    var over30Count = users.chain().find({ age: { $gte: 30 } }).count();
    

    Returns number

  • Terminates the chain and returns array of filtered documents

    Returns

    Array of documents in the resultset

    Memberof

    Resultset

    Example

    var resutls = users.chain().find({ age: 34 }).data();
    

    Parameters

    • Optional options: Partial<ResultSetDataOptions>

      allows specifying 'forceClones' and 'forceCloneMethod' options.

    Returns RST[]

  • eqJoin() - Left joining two sets of data. Join keys can be defined or calculated properties eqJoin expects the right join key values to be unique. Otherwise left data will be joined on the last joinData object with that key

    Returns

    A resultset with data in the format [{left: leftObj, right: rightObj}]

    Memberof

    Resultset

    Example

    var db = new loki('sandbox.db');

    var products = db.addCollection('products');
    var orders = db.addCollection('orders');

    products.insert({ productId: "100234", name: "flywheel energy storage", unitCost: 19999.99 });
    products.insert({ productId: "140491", name: "300F super capacitor", unitCost: 129.99 });
    products.insert({ productId: "271941", name: "fuel cell", unitCost: 3999.99 });
    products.insert({ productId: "174592", name: "390V 3AH lithium bank", unitCost: 4999.99 });

    orders.insert({ orderDate : new Date("12/1/2017").getTime(), prodId: "174592", qty: 2, customerId: 2 });
    orders.insert({ orderDate : new Date("4/15/2016").getTime(), prodId: "271941", qty: 1, customerId: 1 });
    orders.insert({ orderDate : new Date("3/12/2017").getTime(), prodId: "140491", qty: 4, customerId: 4 });
    orders.insert({ orderDate : new Date("7/31/2017").getTime(), prodId: "100234", qty: 7, customerId: 3 });
    orders.insert({ orderDate : new Date("8/3/2016").getTime(), prodId: "174592", qty: 3, customerId: 5 });

    var mapfun = function(left, right) {
    return {
    orderId: left.$loki,
    orderDate: new Date(left.orderDate) + '',
    customerId: left.customerId,
    qty: left.qty,
    productId: left.prodId,
    prodName: right.name,
    prodCost: right.unitCost,
    orderTotal: +((right.unitCost * left.qty).toFixed(2))
    };
    };

    // join orders with relevant product info via eqJoin
    var orderSummary = orders.chain().eqJoin(products, "prodId", "productId", mapfun).data();

    console.log(orderSummary);

    Parameters

    • joinData: ResultSet<RST> | RST[] | Collection<RST>

      Data array to join to.

    • leftJoinKey: string | ((...args: any[]) => string)

      Property name in this result set to join on or a function to produce a value to join on

    • rightJoinKey: string | ((...args: any[]) => string)

      Property name in the joinData to join on or a function to produce a value to join on

    • mapFun: ((...args: any[]) => any)

      (Optional) A function that receives each matching pair and maps them into output objects - function(left,right){return joinedObject}

        • (...args: any[]): any
        • Parameters

          • Rest ...args: any[]

          Returns any

    • dataOptions: {
          forceCloneMethod: CloneMethods;
          forceClones: boolean;
          removeMeta: boolean;
      }

      options to data() before input to your map function

      • forceCloneMethod: CloneMethods

        Allows overriding the default or collection specified cloning method.

      • forceClones: boolean

        forcing the return of cloned objects to your map object

      • removeMeta: boolean

        allows removing meta before calling mapFun

    Returns ResultSet<RST>

  • Used for querying via a mongo-style query object.

    Returns

    this resultset for further chain ops.

    Memberof

    Resultset

    Example

    var over30 = users.chain().find({ age: { $gte: 30 } }).data();
    

    Parameters

    • Optional query: Record<string, any>

      A mongo-style query object used for filtering current results.

    • Optional firstOnly: boolean

      (Optional) Used by collection.findOne()

    Returns ResultSet<RST>

  • findAnd() - oversee the operation of AND'ed query expressions. AND'ed expression evaluation runs each expression progressively against the full collection, internally utilizing existing chained resultset functionality. Only the first filter can utilize a binary index.

    Returns

    this resultset for further chain ops.

    Parameters

    • expressionArray: any[]

      array of expressions

    Returns ResultSet<RST>

  • findOr() - oversee the operation of OR'ed query expressions. OR'ed expression evaluation runs each expression individually against the full collection, and finally does a set OR on each expression's results. Each evaluation can utilize a binary index to prevent multiple linear array scans.

    Returns

    this resultset for further chain ops.

    Parameters

    • expressionArray: any[]

      array of expressions

    Returns ResultSet<RST>

  • Allows you to limit the number of documents passed to next chain operation. A resultset copy() is made to avoid altering original resultset.

    Returns

    Returns a copy of the resultset, limited by qty, for subsequent chain ops.

    Memberof

    Resultset // find the two oldest users var result = users.chain().simplesort("age", true).limit(2).data();

    Parameters

    • qty: number

      The number of documents to return.

    Returns ResultSet<RST>

  • Applies a map function into a new collection for further chaining.

    Memberof

    Resultset

    Example

    var orders.chain().find({ productId: 32 }).map(function(obj) {
    return {
    orderId: $loki,
    productId: productId,
    quantity: qty
    };
    });

    Type Parameters

    • U

    Parameters

    • mapFun: ((value: RST, index: number, array: RST[]) => U)

      javascript map function

        • (value: RST, index: number, array: RST[]): U
        • Parameters

          • value: RST
          • index: number
          • array: RST[]

          Returns U

    • Optional dataOptions: Partial<ResultSetDataOptions>

      options to data() before input to your map function

    Returns ResultSet<RST>

  • data transformation via user supplied functions

    Returns

    The output of your reduceFunction

    Example

    var db = new loki("order.db");
    var orders = db.addCollection("orders");
    orders.insert([{ qty: 4, unitCost: 100.00 }, { qty: 10, unitCost: 999.99 }, { qty: 2, unitCost: 49.99 }]);

    function mapfun (obj) { return obj.qty*obj.unitCost };
    function reducefun(array) {
    var grandTotal=0;
    array.forEach(function(orderTotal) { grandTotal += orderTotal; });
    return grandTotal;
    }
    var grandOrderTotal = orders.chain().mapReduce(mapfun, reducefun);
    console.log(grandOrderTotal);

    Type Parameters

    • U

    Parameters

    • mapFunction: ((value: RST, index: number, array: RST[]) => U)

      this function accepts a single document for you to transform and return

        • (value: RST, index: number, array: RST[]): U
        • Parameters

          • value: RST
          • index: number
          • array: RST[]

          Returns U

    • reduceFunction: ((_: U[]) => U)

      this function accepts many (array of map outputs) and returns single value

        • (_: U[]): U
        • Parameters

          • _: U[]

          Returns U

    Returns U

  • Used for skipping 'pos' number of documents in the resultset.

    Returns

    Returns a copy of the resultset, containing docs starting at 'pos' for subsequent chain ops.

    Memberof

    Resultset // find everyone but the two oldest users var result = users.chain().simplesort("age", true).offset(2).data();

    Parameters

    • pos: number

      Number of documents to skip; all preceding documents are filtered out.

    Returns ResultSet<RST>

  • Removes all document objects which are currently in resultset from collection (as well as resultset)

    Returns

    this (empty) resultset for further chain ops.

    Memberof

    Resultset

    Example

    // remove users inactive since 1/1/2001
    users.chain().find({ lastActive: { $lte: new Date("1/1/2001").getTime() } }).remove();

    Returns ResultSet<RST>

  • Simpler, loose evaluation for user to sort based on a property name. (chainable). Sorting based on the same lt/gt helper functions used for binary indices.

    Returns

    Reference to this resultset, sorted, for future chain operations.

    Memberof

    Resultset

    Example

    var results = users.chain().simplesort('age').data();
    

    Parameters

    • propname: string

      name of property to sort by.

    • Optional options: boolean | Partial<{
          desc: boolean;
          disableIndexIntersect: boolean;
          forceIndexIntersect: boolean;
          useJavascriptSorting: boolean;
      }>

      boolean to specify if isdescending, or options object

    Returns ResultSet<RST>

  • User supplied compare function is provided two documents to compare. (chainable)

    Example

    rslt.sort(function(obj1, obj2) {
    if (obj1.name === obj2.name) return 0;
    if (obj1.name > obj2.name) return 1;
    if (obj1.name < obj2.name) return -1;
    });

    Returns

    Reference to this resultset, sorted, for future chain operations.

    Memberof

    Resultset

    Parameters

    • comparefun: ((a: any, b: any) => number)

      A javascript compare function used for sorting.

        • (a: any, b: any): number
        • Parameters

          • a: any
          • b: any

          Returns number

    Returns ResultSet<RST>

  • transform() - executes a named collection transform or raw array of transform steps against the resultset.

    Returns

    either (this) resultset or a clone of of this resultset (depending on steps)

    Memberof

    Resultset

    Example

    users.addTransform('CountryFilter', [
    {
    type: 'find',
    value: {
    'country': { $eq: '[%lktxp]Country' }
    }
    },
    {
    type: 'simplesort',
    property: 'age',
    options: { desc: false}
    }
    ]);
    var results = users.chain().transform("CountryFilter", { Country: 'fr' }).data();

    Parameters

    • transform: ChainTransform

      {(string|array)} - name of collection transform or raw transform array

    • Optional parameters: Record<string, any>

      {object=} - (Optional) object property hash of parameters, if the transform requires them.

    Returns any

  • Used to run an update operation on all documents currently in the resultset.

    Returns

    this resultset for further chain ops.

    Memberof

    Resultset

    Example

    users.chain().find({ country: 'de' }).update(function(user) {
    user.phoneFormat = "+49 AAAA BBBBBB";
    });

    Parameters

    • updateFunction: ((_: RST) => void)

      User supplied updateFunction(obj) will be executed for each document object.

        • (_: RST): void
        • Parameters

          • _: RST

          Returns void

    Returns ResultSet<RST>

  • where() - Used for filtering via a javascript filter function.

    Returns

    this resultset for further chain ops.

    Memberof

    Resultset

    Example

    var over30 = users.chain().where(function(obj) { return obj.age >= 30; }.data();
    

    Parameters

    • fun: ((_: RST) => boolean)

      A javascript function used for filtering current results by.

        • (_: RST): boolean
        • Parameters

          • _: RST

          Returns boolean

    Returns ResultSet<RST>

Generated using TypeDoc