Optional
options: Record<string, 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.
this resultset for further chain ops.
array of expressions
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.
this resultset for further chain ops.
array of expressions
Allows sorting a resultset based on multiple columns.
// 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]]);
Reference to this resultset, sorted, for future chain operations.
Resultset
array of property names or subarray of [propertyname, isdesc] used evaluate sort order
Terminates the chain and returns array of filtered documents
Array of documents in the resultset
Resultset
var resutls = users.chain().find({ age: 34 }).data();
Optional
options: Partial<ResultSetDataOptions>allows specifying 'forceClones' and 'forceCloneMethod' options.
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
A resultset with data in the format [{left: leftObj, right: rightObj}]
Resultset
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);
Data array to join to.
Property name in this result set to join on or a function to produce a value to join on
Property name in the joinData to join on or a function to produce a value to join on
(Optional) A function that receives each matching pair and maps them into output objects - function(left,right){return joinedObject}
Rest
...args: any[]options to data() before input to your map function
Allows overriding the default or collection specified cloning method.
forcing the return of cloned objects to your map object
allows removing meta before calling mapFun
Used for querying via a mongo-style query object.
this resultset for further chain ops.
Resultset
var over30 = users.chain().find({ age: { $gte: 30 } }).data();
Optional
query: Record<string, any>A mongo-style query object used for filtering current results.
Optional
firstOnly: boolean(Optional) Used by collection.findOne()
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.
this resultset for further chain ops.
array of expressions
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.
this resultset for further chain ops.
array of expressions
Allows you to limit the number of documents passed to next chain operation. A resultset copy() is made to avoid altering original resultset.
Returns a copy of the resultset, limited by qty, for subsequent chain ops.
Resultset // find the two oldest users var result = users.chain().simplesort("age", true).limit(2).data();
The number of documents to return.
Applies a map function into a new collection for further chaining.
Resultset
var orders.chain().find({ productId: 32 }).map(function(obj) {
return {
orderId: $loki,
productId: productId,
quantity: qty
};
});
javascript map function
Optional
dataOptions: Partial<ResultSetDataOptions>options to data() before input to your map function
data transformation via user supplied functions
The output of your reduceFunction
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);
this function accepts a single document for you to transform and return
this function accepts many (array of map outputs) and returns single value
Used for skipping 'pos' number of documents in the resultset.
Returns a copy of the resultset, containing docs starting at 'pos' for subsequent chain ops.
Resultset // find everyone but the two oldest users var result = users.chain().simplesort("age", true).offset(2).data();
Number of documents to skip; all preceding documents are filtered out.
Removes all document objects which are currently in resultset from collection (as well as resultset)
this (empty) resultset for further chain ops.
Resultset
// remove users inactive since 1/1/2001
users.chain().find({ lastActive: { $lte: new Date("1/1/2001").getTime() } }).remove();
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.
Reference to this resultset, sorted, for future chain operations.
Resultset
var results = users.chain().simplesort('age').data();
name of property to sort by.
Optional
options: boolean | Partial<{ boolean to specify if isdescending, or options object
User supplied compare function is provided two documents to compare. (chainable)
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;
});
Reference to this resultset, sorted, for future chain operations.
Resultset
A javascript compare function used for sorting.
transform() - executes a named collection transform or raw array of transform steps against the resultset.
either (this) resultset or a clone of of this resultset (depending on steps)
Resultset
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();
{(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.
Used to run an update operation on all documents currently in the resultset.
this resultset for further chain ops.
Resultset
users.chain().find({ country: 'de' }).update(function(user) {
user.phoneFormat = "+49 AAAA BBBBBB";
});
User supplied updateFunction(obj) will be executed for each document object.
where() - Used for filtering via a javascript filter function.
this resultset for further chain ops.
Resultset
var over30 = users.chain().where(function(obj) { return obj.age >= 30; }.data();
A javascript function used for filtering current results by.
Generated using TypeDoc
Resultset class allowing chainable queries. Intended to be instanced internally. Collection.find(), Collection.where(), and Collection.chain() instantiate this.
Example
Param
The collection which this Resultset will query against.