Skip to content

Collection

dfahlander edited this page Dec 9, 2014 · 28 revisions

Represents a collection of database objects. Note that it will not contain any objects by itself. Instead, it yields a preparation for how to execute a DB query. A query will be executed when calling methods that returns a Promise, such as toArray() or each().

Inheritance Hierarchy

Construction

Collection constructor is private. Instances are returned from the WhereClause methods and some of the Table methods.

Samples

// each()
var collection = db.friends.where('name').equalsIgnoreCase('david');

collection.each(function(friend) {
    console.log('Found: ' + friend.name + ' with phone number ' + friend.phone);
});
// toArray()
db.friends.where('name').startsWithIgnoreCase('d').toArray(function(friends) {
    console.log("Found " friends.length + " friends starting with d");
});
// offset/limit
db.friends
  .where('age').above(25)
  .offset(10)
  .limit(10)
  .toArray(function (friends) {
    console.log(friends.map(JSON.stringify).join('\n'));
  });
// count
db.friends.toCollection().count(function (count) {
    console.log(count + " friends in total");
});
// or
db.friends
  .where('age').above(25)
  .or('shoeSize').above(9)
  .each (function (friend) {
    console.log("Found big friend: " + friend.name);
  });

Methods

Add JS based criteria to collection

Get the number of items in the collection

Sort in descending order

Remove duplicates of items with same primary key

Execute query and call a function for each item

Execute query on the index or primary key being used and call a function for each key

Execute query on the index or primary key being used and call a function for each unique key

Get the first item in the collection

Retrieve an array containing all keys of the collection (index or primary key depending on where() clause)

Get the last item in the collection

Limit the result to given number of items

Ignore N items before given offset and return the rest

Logical OR operation

Reverse the order of items.

Execute query and get an array with the results sorted by given property

Execute query and get an array with the results sorted by the index used in the where() clause

Retrieve an array containing all unique keys of the collection (index or primary key depending on where() clause)

Ignores items occurring after given filter returns true.

Clone this wiki locally