npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

realm-query-builder

v1.4.0

Published

Is a powerful query builder for Realm.js, designed to simplify and enhance the querying experience for your Realm databases.

Downloads

315

Readme

realm-query-builder

Is a powerful query builder for Realm.js, designed to simplify and enhance the querying experience for your Realm databases.

Immutable: All API operations that change the realm-query-builder object will return a new instance instead.

Installation

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

Useful Links

Some useful links for understanding and working with Realm queries:

These resources provide in-depth information on Realm query language and its usage, helping you make the most out of realm-query-builder.

Example

Using realmQueryBuilder directly

import realmQueryBuilder from 'realm-query-builder';

const users = realmQueryBuilder(realm.objects('User'))
  .equalTo('active', true)
  .greaterThanOrEqualTo('age', 18)
  .in('country', ['BR', 'AR', 'US'])
  .distinct('name')
  .result();

console.log(users);

Extending

import { RealmQueryBuilder } from 'realm-query-builder';

type User = {
  age: number,
  active: boolean,
}

class UserQuery extends RealmQueryBuilder<User & Realm.Object> {
  active() {
    return this.equalTo('active', true);
  }

  byAge(age: number) {
    return this.equalTo('age', age);
  }
}

const userQuery = () => new UserQuery(realm.objects<User>('User'));

const users = userQuery()
  .active()
  .byAge(18)
  .distinct('country')
  .result();

console.log(users);

API

where(field: string, condition: RealmConditionalOperator, value: any): this

Adds a filter to the query with the specified property, operator and value.

filtered(query: string, ...values: any[]): this

Concatenate the query as string, like realm Collection.filtered.

equalTo(field: string, value: any, caseInsensitive?: boolean): this

Filter the data by property that match a specified value.

notEqualTo(field: string, value: any, caseInsensitive?: boolean): this

Evaluates to true expression matches property value wildcard string expression. A wildcard string expression is a string that uses normal characters with two special wildcard characters:

  • The * wildcard matches zero or more of any character.
  • The ? wildcard matches any character.

For example, the wildcard string d?g matches dog, dig, and dug, but not ding, dg, or a dog.

like(field: string, value: string, caseInsensitive?: boolean): this

An alias for where(field, 'LIKE' | 'LIKE[c]', value). If caseInsensitive: true, the operator applied will be LIKE[c].

contains(field: string, value: string, caseInsensitive?: boolean): this

An alias for where(field, 'CONTAINS' | 'CONTAINS[c]', value). If caseInsensitive: true, the operator applied will be CONTAINS[c].

beginsWith(field: string, value: string, caseInsensitive?: boolean): this

An alias for where(field, 'BEGINSWITH' | BEGINSWITH[c], value). If caseInsensitive, true the operator applied will be BEGINSWITH[c].

endsWith(field: string, value: string, caseInsensitive?: boolean): this

An alias for where(field, 'ENDSWITH' | ENDSWITH[c], value). If caseInsensitive: true, the operator applied will be ENDSWITH[c].

greaterThan(field: string, value: RealmNumericValueType): this

An alias for where(field, '>', value).

greaterThanOrEqualTo(field: string, value: RealmNumericValueType): this

An alias for where(field, '>=', value).

lessThan(field: string, value: RealmNumericValueType): this

An alias for where(field, '<', value).

lessThanOrEqualTo(field: string, value: RealmNumericValueType): this

An alias for where(field, '<=', value).

between(field: string, start: RealmNumericValueType, end: RealmNumericValueType): this

A between condition, same as:

realmQueryBuilder(object)
  .beginGroup()
    .where(field, '>=', start)
    .where(field, '<=', end)
  .endGroup();

or(): this

Join previous and next conditions with the OR operator.

and(): this

Join previous and next conditions with the AND operator, the default operator is AND.

beginGroup(): this

Begins a group for combining filter conditions in the query. Use with endGroup() to close the group.

endGroup(): this

Ends a group for combining filter conditions in the query. Should be used after starting a group with beginGroup().

Example:

realmQueryBuilder(realm.objects('User'))
  .equalTo('active', true)
  .beginGroup()
    .equalTo('canWrite', true)
    .or()
    .equalTo('admin', true)
  .endGroup()
  .result();

in(field: string, values: ReadonlyArray<any>): this

Adds an IN filter condition to the query, filtering objects where the specified property's value matches any of the values in the provided array. Realm.js started supporting IN in version 10.20.0, it is applied with OR chains for more compatibility.

not(): this

Implements not operator for the next operation or group.

distinct(...fields: string[]): this

Add DISTINCT suffix, see: Realm Query Language

sorted(field: string, order?: RealmQuerySort): this

Call the sorted method, see: sorted

limit(limit: number): this

Add LIMIT suffix, see: Realm Query Language

clone(): this

Create a clone of the current object.

truepredicate(): this

A predicate that always evaluates to TRUE.

falsepredicate(): this

A predicate that always evaluates to FALSE.

merge(query: RealmQueryBuilder<T>)): this

Merges the current query with another RealmQueryBuilder instance.

The operators sorted and limit are not merged.

Example:

const queryA =  realmQueryBuilder(realm.objects('User'))
  .equalTo('active', true)
  .equalTo('admin', false)
  .limit(10);

const queryB =  realmQueryBuilder(realm.objects('User'))
  .greaterThanOrEqualTo('age', 18)
  .limit(20)
  .or()
  .beginGroup()
    .merge(queryA)
  .endGroup()
  .result();

min<V extends RealmNumericValueType>(property?: string): V | null

Call realm.js min

max<V extends RealmNumericValueType>(property?: string): V | null

Call realm.js max

sum(property?: string): number | null

Call realm.js sum

avg(property?: string): number

Call realm.js avg

first(): T | undefined

Get the first result.

last(): T | undefined

Get the last result.

findBy(field: string, value: any): T | undefined

Apply the where(field, '==', value) condition and return the first value.

size(): number

Query the data and get the result's size, alias to result().length

result(): Realm.Results<T>

Executes the query and returns the filtered results