Mylet Set Queries - GetMyle/Guides GitHub Wiki

Overview

What is a set query?

Mylet set queries are used to perform CRUD operations on a set.

There are three places where they can be used:

  • controller
  • hook
  • migrations

How to start with a set query in controller and hook?

In controller and hook set queries can be initiated through mylet-query module:

var query = require('mylet-query');
query('history')
    .then(docs => /* here docs is an array of documents in history set */...)

In example above a query to history set is performed. The result-set of the query is accessed in then() callback.

How to start with a set query in a migration script?

In migrations set queries are accessed through entry point's first argument:

  1. db.query('setname')
db.query('history')
    .then(docs => /* here docs is an array of documents in history set */...)

db.query() returns QueryBuilder interface.

  1. db.addSet('setname') or db.renameSet('setname')
db.addSet('history')
    .insert({ id: 1, val: 'moved to Calgary' })
    .then(() => /* here history set already contains inserted document */...)

db.addSet() and db.renameSet() return SetBuilder interface.

Set query builder interfaces

Overview

Each query is build using query builder interfaces described below. Here is example of querying first 2 history documents where id property is greater than 4:

query('history')
   .where({ id: { $gt: 4 } })
   .take(2)
   .then(docs => ...)

At any step query builder is represented as:

  1. builder itself, so it allows to call nested methods building desired query expression
  2. a promise, so once then() or catch() methods a called built query is evaluated

Let's do another example to see how it works:

// get a query builder of history set
var history = query('history');

// get history query builder that represents only documents with id greater than 4
var historyGreaterThan4 = history.where({ id: { $gt: 4 } });

// lets create another one that contain only documents with id greater than 4 ordered by id property
var historyGreaterThan4Ordered = historyGreaterThan4.orderBy('id');

// at this point no queries are evaluated - we just built them

// lets evaluate all three queries we have using power of promises:
Promise.all([history, historyGreaterThan4, historyGreaterThan4Ordered])
    .spread((all, greaterThan4, greaterThan4Ordered) => /* queries are evaluated now */...);

NOTE: builder methods do not mutate current builder state, but instead return new builder with new state.

Below described set query builder interfaces that are returned depending on what builder methods are called.

Each builder has the following promise methods:

Method Returned interface Description
then(onFulfilled, onRejected) Promise Evaluates built query and executes given callback once result-set is ready
catch(onRejected) Promise Evaluates built query and executes given callback when an error happened

SetBuilder interface

The interface is accessed by db.addSet() and db.renameSet() in migrations.

Method Returned interface Promise value Description
all(criteria) FinalQueryBuilder boolean Determines whether all the documents of a sequence satisfy criteria
any() FinalQueryBuilder boolean Determines whether a sequence contains any elements
any(criteria) FinalQueryBuilder boolean Determines whether any element of a sequence satisfies criteria
count() FinalQueryBuilder number Returns the number of elements in a sequence
count(criteria) FinalQueryBuilder number Returns the number of elements in the specified sequence that satisfies criteria
delete() UpdateQueryBuilder number Deletes all documents in a sequence
delete(criteria) UpdateQueryBuilder number Filters a sequence of document based on criteria and deletes all documents in resultset
first() FinalQueryBuilder object Returns the first element of a sequence
first(criteria) FinalQueryBuilder object Returns the first element of a sequence that satisfies specified criteria
insert(array) FinalQueryBuilder number Inserts array of documents into a set
insert(doc) FinalQueryBuilder number Inserts doc into a set
last() FinalQueryBuilder object Returns the last element in a sequence
last(criteria) FinalQueryBuilder object Returns the last element of a sequence that satisfies specified criteria
orderBy(prop1, ...) SelectQueryBuilder array Sorts a sequence of documents. See orderBy() parameters
select(prop1, ...) SelectQueryBuilder array Returns documents that contain only specified properties
single() FinalQueryBuilder object Returns the only element of a sequence, or a default value if the sequence is empty or contains more than one element
single(criteria) FinalQueryBuilder object Returns the only element of a sequence that satisfies specified criteria, or a default value if the sequence is empty or contains more than one element
skip(num) SelectSkipQueryBuilder array Bypasses a specified number of documents in a sequence and then returns the remaining documents
take(num) SelectTakeQueryBuilder array Returns a specified number of contiguous documents from the start of a sequence
update(doc) UpdateQueryBuilder number Replaces all documents in a sequence with doc
where(criteria) QueryBuilder array Filters a sequence of document based on criteria

QueryBuilder interface

The interface is returned by query(), where() and db.query().

Method Returned interface Promise value Description
all(criteria) FinalQueryBuilder boolean Determines whether all the documents of a sequence satisfy criteria
any() FinalQueryBuilder boolean Determines whether a sequence contains any elements
any(criteria) FinalQueryBuilder boolean Determines whether any element of a sequence satisfies criteria
count() FinalQueryBuilder number Returns the number of elements in a sequence
count(criteria) FinalQueryBuilder number Returns the number of elements in the specified sequence that satisfies criteria
delete() UpdateQueryBuilder number Deletes all documents in a sequence
delete(criteria) UpdateQueryBuilder number Filters a sequence of document based on criteria and deletes all documents in resultset
first() FinalQueryBuilder object Returns the first element of a sequence
first(criteria) FinalQueryBuilder object Returns the first element of a sequence that satisfies specified criteria
insert(array) FinalQueryBuilder number Inserts array of documents into a set
insert(doc) FinalQueryBuilder number Inserts doc into a set
last() FinalQueryBuilder object Returns the last element in a sequence
last(criteria) FinalQueryBuilder object Returns the last element of a sequence that satisfies specified criteria
orderBy(prop1, ...) SelectQueryBuilder array Sorts a sequence of documents. See orderBy() parameters for more details
select(prop1, ...) SelectQueryBuilder array Returns documents that contain only specified properties
single() FinalQueryBuilder object Returns the only element of a sequence, or a default value if the sequence is empty or contains more than one element
single(criteria) FinalQueryBuilder object Returns the only element of a sequence that satisfies specified criteria, or a default value if the sequence is empty or contains more than one element
skip(num) SelectSkipQueryBuilder array Bypasses a specified number of documents in a sequence and then returns the remaining documents
take(num) SelectTakeQueryBuilder array Returns a specified number of contiguous documents from the start of a sequence
update(doc) UpdateQueryBuilder number Replaces all documents in a sequence with doc
where(criteria) QueryBuilder array Filters a sequence of document based on criteria

SelectQueryBuilder interface

The interface is returned by orderBy() and select() methods.

Method Returned interface Promise value Description
first() FinalQueryBuilder object Returns the first element of a sequence
first(criteria) FinalQueryBuilder object Returns the first element of a sequence that satisfies specified criteria
last() FinalQueryBuilder object Returns the last element in a sequence
last(criteria) FinalQueryBuilder object Returns the last element of a sequence that satisfies specified criteria
orderBy(prop1, ...) SelectQueryBuilder array Sorts a sequence of documents. See orderBy() parameters for more details
select(prop1, ...) SelectQueryBuilder array Returns documents that contain only specified properties
single() FinalQueryBuilder object Returns the only element of a sequence, or a default value if the sequence is empty or contains more than one element
single(criteria) FinalQueryBuilder object Returns the only element of a sequence that satisfies specified criteria, or a default value if the sequence is empty or contains more than one element
skip(num) SelectSkipQueryBuilder array Bypasses a specified number of documents in a sequence and then returns the remaining documents
take(num) SelectTakeQueryBuilder array Returns a specified number of contiguous documents from the start of a sequence
where(criteria) QueryBuilder array Filters a sequence of document based on criteria

SelectTakeQueryBuilder interface

The interface is returned by take() method.

Method Returned interface Promise value Description
first() FinalQueryBuilder object Returns the first element of a sequence
first(criteria) FinalQueryBuilder object Returns the first element of a sequence that satisfies specified criteria
last() FinalQueryBuilder object Returns the last element in a sequence
last(criteria) FinalQueryBuilder object Returns the last element of a sequence that satisfies specified criteria
orderBy(prop1, ...) SelectTakeQueryBuilder array Sorts a sequence of documents. See orderBy() parameters for more details
select(prop1, ...) SelectTakeQueryBuilder array Returns documents that contain only specified properties
single() FinalQueryBuilder object Returns the only element of a sequence, or a default value if the sequence is empty or contains more than one element
single(criteria) FinalQueryBuilder object Returns the only element of a sequence that satisfies specified criteria, or a default value if the sequence is empty or contains more than one element
skip(num) SelectTakeSkipQueryBuilder array Bypasses a specified number of documents in a sequence and then returns the remaining documents
where(criteria) SelectTakeQueryBuilder array Filters a sequence of document based on criteria

SelectSkipQueryBuilder interface

The interface is returned by skip() method.

Method Returned interface Promise value Description
first() FinalQueryBuilder object Returns the first element of a sequence
first(criteria) FinalQueryBuilder object Returns the first element of a sequence that satisfies specified criteria
last() FinalQueryBuilder object Returns the last element in a sequence
last(criteria) FinalQueryBuilder object Returns the last element of a sequence that satisfies specified criteria
orderBy(prop1, ...) SelectSkipQueryBuilder array Sorts a sequence of documents. See orderBy() parameters for more details
select(prop1, ...) SelectSkipQueryBuilder array Returns documents that contain only specified properties
single() FinalQueryBuilder object Returns the only element of a sequence, or a default value if the sequence is empty or contains more than one element
single(criteria) FinalQueryBuilder object Returns the only element of a sequence that satisfies specified criteria, or a default value if the sequence is empty or contains more than one element
take(num) SelectTakeSkipQueryBuilder array Returns a specified number of contiguous documents from the start of a sequence
where(criteria) SelectSkipQueryBuilder array Filters a sequence of document based on criteria

SelectTakeSkipQueryBuilder interface

The interface is returned by skip().take() or by take().skip() methods.

Method Returned interface Promise value Description
first() FinalQueryBuilder object Returns the first element of a sequence
first(criteria) FinalQueryBuilder object Returns the first element of a sequence that satisfies specified criteria
last() FinalQueryBuilder object Returns the last element in a sequence
last(criteria) FinalQueryBuilder object Returns the last element of a sequence that satisfies specified criteria
orderBy(prop1, ...) SelectTakeSkipQueryBuilder array Sorts a sequence of documents. See orderBy() parameters for more details
select(prop1, ...) SelectTakeSkipQueryBuilder array Returns documents that contain only specified properties
single() FinalQueryBuilder object Returns the only element of a sequence, or a default value if the sequence is empty or contains more than one element
single(criteria) FinalQueryBuilder object Returns the only element of a sequence that satisfies specified criteria, or a default value if the sequence is empty or contains more than one element
where(criteria) SelectTakeSkipQueryBuilder array Filters a sequence of document based on criteria

UpdateQueryBuilder interface

The interface is returned by update() method.

Method Returned interface Promise value Description
where(criteria) UpdateQueryBuilder array Filters a sequence of document based on criteria

FinalQueryBuilder interface

The interface is returned by insert(), all(), any(), count(), first(), last() and single() methods.

The interface doesn't have builder methods - just promise ones: then() and catch().

Special case: master set

Master set is a set that provides access to user records. This is read-only set, so insert(), update() and delete() methods throw corresponding error.

Master set has the following schema:

  • time - UTC time when a record was added
  • phrase - raw record phrase
  • latitude
  • longitude
  • altitude

Filter criteria

To specify equality condition, use the criteria { <field>: <value> } to select all documents that contain the with the specified .

A criteria can use the property operators to specify conditions. For example, { a: { $ne: 1 } } selects all documents where property a not equal to 1.

A compound criteria can specify conditions for more than one field in a document. Implicitly, a logical AND conjunction connects the clauses of a compound criteria so that the query selects the documents that match all the conditions. For example, { a: 1, b: 2 } selects all documents where a equals to 1 and b equals to 2.

Using the $and, $or and $not group operator, you can explicitly specify a compound criteria. For example, { $or: [{ a: 1 }, { a: 2 }] } selects all documents where property a equals to 1 or 2.

Supported group operators:

Operator Meaning Examples
$and AND { latitude: 4, longitude: 6 } is equal to { $and: { latitude: 4, longitude: 6 } } and is equal to { $and: [ { latitude: 4 }, { longitude: 6 } ] } and means latitude equals 4 and longitude equals 6
$or OR { $or: { latitude: 4, longitude: 6 } } is equal to { $or: [ { latitude: 4 }, { longitude: 6 } ] } and means latitude equal 4 or longitude equals 6
$not negation { $not: { latitude: 4 } } means latitude not equal to 4; { latitude: 4, { $not: { longitude: 6 } } } means latitude equals 4 and longitude not equals to 6

Supported property operators:

Operator Meaning Examples
$e equals { latitude: { $e: 4 } } means latitude equals 4 and is the same as { latitude: 4 }
$ne not equal to { latitude: { $ne: 4 } } means latitude not equal to 4
$gte greater than or equal to { latitude: { $gte: 4 } } means latitude is greater than or equal to 4
$gt greater than { latitude: { $gte: 4 } } means latitude is greater than 4
$lte less than or equal to { latitude: { $lte: 4 } } means latitude is less than or equal to 4
$lt less than { latitude: { $lt: 4 } } means latitude is less than 4
$contains string contains { phrase: { $contains: "test" } } means phrase contains "test" string. Applicable only to phrase field
$startsWith string starts with { phrase: { $startsWith: "a" } } means phrase starts with "a" string. Applicable only to phrase field
$endsWith string ends with { phrase: { $endsWith: "a" } } means phrase ends with "a" string. Applicable only to phrase field
$in is in array { altitude: { $in: [1, 2, 3, 4] } } means altitude has value 1, 2, 3 or 4

orderBy() parameters

prop1 parameter in orderBy(prop1, ...) has the following options:

  • a string - name of a property to order by ascendingly
  • an object - { $asc: 'propName' } or { $desc: 'propName' } object that specify property and order direction
⚠️ **GitHub.com Fallback** ⚠️