Skip to content

Latest commit

 

History

History
175 lines (127 loc) · 4.97 KB

README.md

File metadata and controls

175 lines (127 loc) · 4.97 KB

CircleCI

Test Coverage

Code Climate


Realm Query

A query builder for realm.js inspired of Realm Java's query engine.

https://realm.io/docs/java/latest/#queries

Installation

$ npm install --save realm-query
# or
$ yarn add realm-query

Usage

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

API

  • average(fieldName: string): number

    Returns the average of a given field

  • beginGroup(): RealmQuery

    Begin grouping of conditions ("left parenthesis")

  • beginsWith(fieldName: string, value: string, casing?: boolean): RealmQuery

    Condition that the value of field begins with the specified string

  • between(fieldName: string, from: number|date, to: number|date): RealmQuery

    Between condition

  • contains(fieldName: string, value: string, casing?: boolean): RealmQuery

    Condition that value of field contains the specified substring

  • count(): number

    Counts the number of objects that fulfill the query conditions

  • distinct(fieldName: string): Array

    Returns a distinct set of objects of a specific class.

  • endGroup(): RealmQuery

    End grouping of conditions ("right parenthesis") which was opened by a call to beginGroup()

  • endsWith(fieldName: string, value: string, casing?: boolean): RealmQuery

    Condition that the value of field ends with the specified string

  • equalTo(fieldName: string, value: string|number|boolean|date): RealmQuery

    Equal-to comparison

  • findAll(): ReamResults

    Finds all objects that fulfill the query conditions

  • findFirst(): Object

    Finds the first object that fulfills the query conditions

  • greaterThan(fieldName: string, value: number|date): RealmQuery

    Greater-than comparison

  • greaterThanOrEqualTo(fieldName: string, value: number|date): RealmQuery

    greater-than-or-equal-to comparison

  • in(fieldName: string, values: string|number[]): RealmQuery

    In comparison

  • lessThan(fieldName: string, value: number|date): RealmQuery

    Less-than comparison

  • lessThanOrEqualTo(fieldName: string, value: number|date): RealmQuery

    Less-than-or-equal-to comparison

  • max(fieldName: string): Object

    Finds the maximum value of a field

  • min(fieldName: string): Object

    Finds the miniimum value of a field

  • not(): RealmQuery

    Negate condition

  • notEqualTo(fieldName: string, value: string|number|boolean|date): RealmQuery

    Not-equal-to comparison

  • and(): RealmQuery

    AND logic operator. Use in group

  • or(): RealmQuery

    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()
  • sum(): number

    Calculates the sum of a given field

  • sort(fieldName: string, order?: 'ASC' | 'DESC'): RealmQuery

    Set sorted into realm.objects

  • join(query: RealmQuery): RealmQuery

    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
  • where(objects?: RealmResults): RealmQuery

    Create new query

  • create(objects?: RealmResults): RealmQuery

    Create new query. Alias of where