A query builder for realm.js inspired of Realm Java's query engine.
https://realm.io/docs/java/latest/#queries
$ npm install --save realm-query
# or
$ yarn add realm-query
const RealmQuery = require('realm-query');
const realm = new Realm({...});
// Way 1
let query = RealmQuery
.create()
.contains('name', 'phu', true)
.in('id', [1001, 1002]);
// get objects
// query.toString() = name CONTAINS[c] "phu" AND (id == 1001 OR id == 1002)
let results = realm.objects('Person').filtered(query.toString());
// Way 2. use lib to get objects
let results = RealmQuery
.where(realm.objects('Person'))
.contains('name', 'phu', true)
.in('id', [1001, 1002])
.findAll()
// Complex query
let results = RealmQuery
.where(realm.objects('Person'))
.contains('name', 'phu', true)
.beginGroup()
.in('id', [1001, 1002])
.or()
.between('age', 20, 45)
.endGroup()
.sort('id', 'DESC')
.findAll()
// It will query like this
// name CONTAINS[c] "phu" AND ((id == 1001 OR id == 1002) OR (age >= 20 AND age <= 45))
// and sort by id desc
-
Returns the average of a given field
-
Begin grouping of conditions ("left parenthesis")
-
Condition that the value of field begins with the specified string
-
Between condition
-
Condition that value of field contains the specified substring
-
Counts the number of objects that fulfill the query conditions
-
Returns a distinct set of objects of a specific class.
-
End grouping of conditions ("right parenthesis") which was opened by a call to beginGroup()
-
Condition that the value of field ends with the specified string
-
Equal-to comparison
-
Finds all objects that fulfill the query conditions
-
Finds the first object that fulfills the query conditions
-
Greater-than comparison
-
greater-than-or-equal-to comparison
-
In comparison
-
Less-than comparison
-
Less-than-or-equal-to comparison
-
Finds the maximum value of a field
-
Finds the miniimum value of a field
-
Negate condition
-
Not-equal-to comparison
-
AND logic operator. Use in group
-
OR logic operator. Use in group
let results = RealmQuery .where(realm.objects('Person')) .contains('name', 'phu', true) .beginGroup() .in('id', [1001, 1002]) .or() .between('age', 20, 45) .endGroup() .findAll()
-
Calculates the sum of a given field
-
Set sorted into realm.objects
-
Join queries
let query1 = RealmQuery .where(realm.objects('Person')) .in('id', [1001, 1002]) let query2 = RealmQuery .where(realm.objects('Person')) .greaterThan('age', 25); query1.join(query2); query1.toString = (id == 1001 OR id == 1002) AND age > 25
-
Create new query
-
Create new query. Alias of where