Module: rgjs/firebase/db

RGJS6 Firebase Database module.

Methods


<static> off(path, eventType [, args])

Firebase database off().

Parameters:
Name Type Argument Description
path string

The path of the database ref.

eventType string

Event type, for example: value, child_changed, child_added, etc.

args args <optional>

An object containing arguments.

Example
// Results in `ref('/path/to/data').off('child_added')`
rgjs.firebase.db.off('/path/to/data', 'child_added');

<static> on(path, eventType [, args] [, cb] [, cberr])

Firebase database on().
Can be aborted using args.opts.abortOn*.

Parameters:
Name Type Argument Description
path string

The path of the database ref.

eventType string

Event type, for example: value, child_changed, child_added, etc.

args args <optional>

An object containing arguments.

cb function <optional>

The callback.

cberr function <optional>

The error callback.

Example
// Results in `ref('/path/to/data').on('child_added', cb)`
rgjs.firebase.db.on('/path/to/data', 'child_added', null, cb);

<static> once(path, eventType [, args])

Firebase database once().
Can be aborted using args.opts.abortOn*.

Parameters:
Name Type Argument Description
path string

The path of the database ref.

eventType string

Event type, for example: value, child_changed, child_added, etc.

args args <optional>

An object containing arguments.

Returns:

A promise with the snapshot.

Type
promise
Example
// Results in `ref('/path/to/data').once('value').then( ... )`
rgjs.firebase.db.once('/path/to/data', 'value');

<static> ref(path [, args] [, eventType])

Firebase database ref(). Can abort once() and on() using args.opts.abortOn*.

Parameters:
Name Type Argument Description
path string

The path of the database ref.

args args <optional>

An object containing arguments.

eventType string <optional>

Event type, for example: value, child_changed, child_added, etc.

Returns:

The reference.

Type
object
Example
// Results in `ref('/path/to/data')`
rgjs.firebase.db.on('/path/to/data')

<static> setDefaultDatabase(db)

Set default database.

Parameters:
Name Type Description
db object

A firebase database object.

Example
const db = firebase_app.database();
rgjs.firebase.db.setDefaultDatabase(db);

Type Definitions


args

Generic arguments for rgjs.firebase.db functions.

Type:
  • object
Properties:
Name Type Argument Default Description
id string

Optional. Used to keep track of references. Defaults to a unique value. If a reference exists with given id and opts.abortOnId is true, ref[opts.id].off(eventType) is called, which effectively aborts once(), on(), etc calls.

queries array <optional>

Optional. An array containing queries. Syntax for query: ['FUNCTION', ...arguments]. See example below.

cb function <optional>

Optional. Callback, called on success with argument firebase.database.DataSnapshot.

cberr function <optional>

Optional. Error callback, called when an error occurs with argument Error.

db database <optional>

Optional if setDefaultDatabase() is used. The Firebase database object to use.

debug boolean <optional>

Optional. Turns on debug console logging.

opts object <optional>
{}

Options.

Properties
Name Type Argument Default Description
abortOnAll boolean <optional>
false

Call ref(path).off(|eventType) if the id or path match.

abortOnId boolean <optional>
true

Call ref(path).off() if the id matches. Note that the id option DOES NOT pass eventName!

abortOnAll boolean <optional>
false

Call ref(path).off(eventType) if the path matches. Note that the path option passes eventName!

Examples
// Example: Abort on id
ref.on('/data', 'child_added', {id: 'get-/data'});
// Does NOT call `off()` on `ref('/data')` because `opts.abortOnId = false`:
ref.on('/data', 'child_changed', {id: 'get-/data', opts: {abortOnId: false}});
// Does call `off()` on all ref('/data') that match the id 'get-/data':
ref.once('/data', 'value', {id: 'get-/data'});
// Example: Abort on path
ref.on('/data', 'child_added');
// Does NOT call `off()` on `ref('/data')` because `opts.abortOnPath = false`:
// You will have two `on()` listeners now for `ref('/data')`!
ref.on('/data', 'child_added');
// Does call `off('child_added')` on all ref('/data'):
ref.on('/data', 'child_added', {opts: {abortOnPath: true}});
// Example: Abort caveats #1 - Abort on path
ref.on('/data', 'child_added');
// Calls `off('value')` on all ref('/data'):
// This will NOT cancel 'child_added'!
ref.once('/data', 'value', {opts: {abortOnPath: true}});
// Example: Abort caveats #2 - Abort on path, id
ref.on('/data', 'child_added', {id: 'get-/data'});
// Calls `off('value')` on all ref('/data'), then calls `off()` on all ref('/data') that match the id 'get-/data':
// This WILL cancel 'child_added' because id is provided, matches and `abortOnId = true` by default!
ref.once('/data', 'value', {id: 'get-/data', opts: {abortOnPath: true}});
// Example: Queries
// Will apply `ref('/data').startAt('rgjs@rejh.nl', 'email').limitToFirst(1).once('value')`.
ref.once('/data', 'value', {queries: [['startAt', 'rgjs@rejh.nl', 'email'], ['limitToFirst', 1]]}).then( ... )