RxDatabase
Extends:
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 |
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 |
Creates a new Query for retrieving a single model specified by the class and id. |
|
public |
Creates a new Model Query for retrieving all models matching the predicates provided. |
|
public |
Creates a new Model Query for retrieving a single model matching the predicates provided. |
|
public |
findJSONBlob(id: *): * |
|
public |
inTransaction(fn: Function): Promise Opens a new database transaction and executes the provided |
|
public |
For compatibility with Reflux, Flux and other libraries, you can subscribe to
the database using |
|
public |
Modelify takes a mixed array of model IDs or model instances, and queries for items that are missing. |
|
public |
mutationHooks(): * |
|
public |
removeMutationHook(hook: *) Removes a previously registered mutation hook. |
Protected Methods | ||
protected |
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 |
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:
Name | Type | Attribute | Description |
objectPattern | {"primary": *, "databasePath": *, "databaseVersion": *, "logQueries": *, "logQueryPlans": *} |
|
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:
Name | Type | Attribute | Description |
objectPattern | {"beforeDatabaseChange": *, "afterDatabaseChange": *} |
|
public count(klass: Model, predicates: Matcher[]): Query source
Creates a new Query that returns the number of models matching the predicates provided.
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.
Return:
Query |
public findAll(klass: Model, predicates: Matcher[]): Query source
Creates a new Model Query for retrieving all models matching the predicates provided.
Return:
Query |
public findBy(klass: Model, predicates: Matcher[]): Query source
Creates a new Model Query for retrieving a single model matching the predicates provided.
Return:
Query |
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 byfn
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:
Name | Type | Attribute | Description |
fn | Function | A callback that will be executed inside a database transaction |
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);
}
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.
public removeMutationHook(hook: *) source
Removes a previously registered mutation hook. You must pass the exact same object that was provided to {RxDatabase.addMutationHook}.
Params:
Name | Type | Attribute | Description |
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.
protected createSearchIndex(klass: *): * source
Params:
Name | Type | Attribute | Description |
klass | * |
Return:
* |
protected dropSearchIndex(klass: *): * source
Params:
Name | Type | Attribute | Description |
klass | * |
Return:
* |
protected indexModel(model: *, indexData: *, isModelIndexed: *): * source
Params:
Name | Type | Attribute | Description |
model | * | ||
indexData | * | ||
isModelIndexed | * |
Return:
* |
protected isIndexEmptyForAccount(accountId: *, modelKlass: *): * source
Params:
Name | Type | Attribute | Description |
accountId | * | ||
modelKlass | * |
Return:
* |
protected isModelIndexed(model: *, isIndexed: *): * source
Params:
Name | Type | Attribute | Description |
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.
protected searchIndexSize(klass: *): * source
Params:
Name | Type | Attribute | Description |
klass | * |
Return:
* |
protected transactionDidCommitChanges(changeRecords: *) source
Params:
Name | Type | Attribute | Description |
changeRecords | * |
protected trigger(record: *) source
Params:
Name | Type | Attribute | Description |
record | * |
protected unindexModelsForAccount(accountId: *, modelKlass: *): * source
Params:
Name | Type | Attribute | Description |
accountId | * | ||
modelKlass | * |
Return:
* |
protected updateModelIndex(model: *, indexData: *, isModelIndexed: *) source
Params:
Name | Type | Attribute | Description |
model | * | ||
indexData | * | ||
isModelIndexed | * |