Home Manual Reference Source Test
public class | source

RxDatabase

Extends:

* → RxDatabase

The RxDatabase is the central database object of RxDB. You can instantiate as many databases as you'd like at the same time, and opening the same database path in multiple windows is fine - RxDB uses SQLite transactions and dispatches change events across windows via the Electron IPC module.

This class extends EventEmitter, and you can subscribe to all changes to the database by subscribing to the trigger event.

For more information about getting started with RxDB, see the Getting Started guide.

Constructor Summary

Public Constructor
public

constructor(objectPattern: {"primary": *, "databasePath": *, "databaseVersion": *, "logQueries": *, "logQueryPlans": *})

Member Summary

Public Members
public

models: *

Method Summary

Public Methods
public

addMutationHook(objectPattern: {"beforeDatabaseChange": *, "afterDatabaseChange": *})

Mutation hooks allow you to observe changes to the database and add functionality within the transaction, before and/or after the standard REPLACE / INSERT queries are made.

public

count(klass: Model, predicates: Matcher[]): Query

Creates a new Query that returns the number of models matching the predicates provided.

public

Typically, instances of RxDatabase are long-lasting and are created in renderer processes when they load.

public

find(klass: Model, id: String): Query

Creates a new Query for retrieving a single model specified by the class and id.

public

findAll(klass: Model, predicates: Matcher[]): Query

Creates a new Model Query for retrieving all models matching the predicates provided.

public

findBy(klass: Model, predicates: Matcher[]): Query

Creates a new Model Query for retrieving a single model matching the predicates provided.

public

findJSONBlob(id: *): *

public

Opens a new database transaction and executes the provided fn within the transaction.

public

listen(callback: Function, bindContext: Object): *

For compatibility with Reflux, Flux and other libraries, you can subscribe to the database using listen:

public

modelify(klass: Model, arr: Array): Promise

Modelify takes a mixed array of model IDs or model instances, and queries for items that are missing.

public
public

Removes a previously registered mutation hook.

Protected Methods
protected

_query(query: String, values: Array): Promise

Executes a SQL string on the database.

protected

createSearchIndex(klass: *): *

protected

dropSearchIndex(klass: *): *

protected

indexModel(model: *, indexData: *, isModelIndexed: *): *

protected

isIndexEmptyForAccount(accountId: *, modelKlass: *): *

protected

isModelIndexed(model: *, isIndexed: *): *

protected

run(modelQuery: Query, options: Object): Promise

Executes a model {Query} on the local database.

protected

searchIndexSize(klass: *): *

protected

transactionDidCommitChanges(changeRecords: *)

protected

trigger(record: *)

protected

unindexModel(model: *): *

protected

unindexModelsForAccount(accountId: *, modelKlass: *): *

protected

updateModelIndex(model: *, indexData: *, isModelIndexed: *)

Public Constructors

public constructor(objectPattern: {"primary": *, "databasePath": *, "databaseVersion": *, "logQueries": *, "logQueryPlans": *}) source

Params:

NameTypeAttributeDescription
objectPattern {"primary": *, "databasePath": *, "databaseVersion": *, "logQueries": *, "logQueryPlans": *}
  • optional
  • default: {}

Public Members

public models: * source

Public Methods

public addMutationHook(objectPattern: {"beforeDatabaseChange": *, "afterDatabaseChange": *}) source

Mutation hooks allow you to observe changes to the database and add functionality within the transaction, before and/or after the standard REPLACE / INSERT queries are made.

  • beforeDatabaseChange: Run queries, etc. and return a promise. The RxDatabase will proceed with changes once your promise has finished. You cannot call persistModel or unpersistModel from this hook. Instead, use low level calls like RxDatabase._query.

  • afterDatabaseChange: Run queries, etc. after the REPLACE / INSERT queries

Warning: this is very low level. If you just want to watch for changes, You should subscribe to the RxDatabase's trigger events.

Example: N1 uses these hooks to watch for changes to unread counts, which are maintained in a separate table to avoid frequent COUNT(*) queries.

Params:

NameTypeAttributeDescription
objectPattern {"beforeDatabaseChange": *, "afterDatabaseChange": *}
  • default: {"beforeDatabaseChange":null,"afterDatabaseChange":null}

public count(klass: Model, predicates: Matcher[]): Query source

Creates a new Query that returns the number of models matching the predicates provided.

Params:

NameTypeAttributeDescription
klass Model

The Model class you're trying to retrieve.

predicates Matcher[]

The set of predicates the returned model must match.

Return:

Query

public disconnect() source

Typically, instances of RxDatabase are long-lasting and are created in renderer processes when they load. If you need to manually tear down an instance of RxDatabase, call disconnect.

public find(klass: Model, id: String): Query source

Creates a new Query for retrieving a single model specified by the class and id.

Params:

NameTypeAttributeDescription
klass Model

The class of the {Model} you're trying to retrieve.

id String

The id of the {Model} you're trying to retrieve

Example:

db.find(Thread, 'id-123').then((thread) => {
// thread is a Thread object, or null if no match was found.
});

Return:

Query

public findAll(klass: Model, predicates: Matcher[]): Query source

Creates a new Model Query for retrieving all models matching the predicates provided.

Params:

NameTypeAttributeDescription
klass Model

The class you're trying to retrieve.

predicates Matcher[]

An array of matcher objects. The set of predicates the returned model must match.

Return:

Query

public findBy(klass: Model, predicates: Matcher[]): Query source

Creates a new Model Query for retrieving a single model matching the predicates provided.

Params:

NameTypeAttributeDescription
klass Model

The class of the {Model} you're trying to retrieve.

predicates Matcher[]

the set of predicates the returned model must match.

Return:

Query

public findJSONBlob(id: *): * source

Params:

NameTypeAttributeDescription
id *

Return:

*

public inTransaction(fn: Function): Promise source

Opens a new database transaction and executes the provided fn within the transaction. After the transaction function resolves, the transaction is closed and changes are relayed to live queries and other subscribers.

RxDB makes the following guaruntees:

  • Serial Execution: Once started, no other calls to inTransaction will excute until the promise returned by fn has finished.

  • Single Process Writing: No other process will be able to write to the database while the provided function is running. RxDB uses SQLite's BEGIN IMMEDIATE TRANSACTION, with the following semantics:

  • No other connection will be able to write any changes.
  • Other connections can read from the database, but they will not see pending changes.

Params:

NameTypeAttributeDescription
fn Function

A callback that will be executed inside a database transaction

Return:

Promise

A promise that resolves when the transaction has successfully completed.

Emit:

*

RxDatabase#trigger

public listen(callback: Function, bindContext: Object): * source

For compatibility with Reflux, Flux and other libraries, you can subscribe to the database using listen:

componentDidMount() {
this._unsubscribe = db.listen(this._onDataChanged, this);
}

Params:

NameTypeAttributeDescription
callback Function

The function to execute when the database triggers.

bindContext Object
  • optional

Optional binding for callback.

Return:

*

public modelify(klass: Model, arr: Array): Promise source

Modelify takes a mixed array of model IDs or model instances, and queries for items that are missing. The returned array contains just model instances, or null if the model could not be found.

This function is useful if your code may receive an item or it's ID.

Modelify is efficient and uses a single database query. It resolves Immediately if no query is necessary. It does not change the order of items in the array.

Params:

NameTypeAttributeDescription
klass Model

The model class desired

arr Array

An {Array} with a mix of string model IDs and/or models.

Return:

Promise

A promise that resolves with the models.

public mutationHooks(): * source

Return:

*

currently registered mutation hooks

public removeMutationHook(hook: *) source

Removes a previously registered mutation hook. You must pass the exact same object that was provided to {RxDatabase.addMutationHook}.

Params:

NameTypeAttributeDescription
hook *

Protected Methods

protected _query(query: String, values: Array): Promise source

Executes a SQL string on the database. If a query is made before the database has been opened, the query will be held in a queue and run / resolved when the database is ready.

Params:

NameTypeAttributeDescription
query String

A SQLite SQL string

values Array

An array of values, corresponding to ? in the SQL string.

Return:

Promise

Resolves when the query has been completed and rejects when the query has failed.

protected createSearchIndex(klass: *): * source

Params:

NameTypeAttributeDescription
klass *

Return:

*

protected dropSearchIndex(klass: *): * source

Params:

NameTypeAttributeDescription
klass *

Return:

*

protected indexModel(model: *, indexData: *, isModelIndexed: *): * source

Params:

NameTypeAttributeDescription
model *
indexData *
isModelIndexed *

Return:

*

protected isIndexEmptyForAccount(accountId: *, modelKlass: *): * source

Params:

NameTypeAttributeDescription
accountId *
modelKlass *

Return:

*

protected isModelIndexed(model: *, isIndexed: *): * source

Params:

NameTypeAttributeDescription
model *
isIndexed *

Return:

*

protected run(modelQuery: Query, options: Object): Promise source

Executes a model {Query} on the local database. Typically, this method is called transparently and you do not need to invoke it directly.

Params:

NameTypeAttributeDescription
modelQuery Query

The query to execute.

options Object
options.format Boolean

Pass format: true to transform the result into a set of models. Defaults to true.

Return:

Promise

A promise that resolves with the result of the database query.

protected searchIndexSize(klass: *): * source

Params:

NameTypeAttributeDescription
klass *

Return:

*

protected transactionDidCommitChanges(changeRecords: *) source

Params:

NameTypeAttributeDescription
changeRecords *

protected trigger(record: *) source

Params:

NameTypeAttributeDescription
record *

protected unindexModel(model: *): * source

Params:

NameTypeAttributeDescription
model *

Return:

*

protected unindexModelsForAccount(accountId: *, modelKlass: *): * source

Params:

NameTypeAttributeDescription
accountId *
modelKlass *

Return:

*

protected updateModelIndex(model: *, indexData: *, isModelIndexed: *) source

Params:

NameTypeAttributeDescription
model *
indexData *
isModelIndexed *