Context(optionsopt)

Context provides a context for interacting with Firestore. It will use a transaction if requested (e.g., we're making changes OR we need consistent reads across multiple docs).

new Context(optionsopt)

Parameters:
Name Type Attributes Description
options ContextOptions <optional>
Options for this context

Classes

Context

Members

defaultOptions

Returns the default options for a db context.

EVENTS

All events a context may emit. POST_COMMIT: When a transaction is committed. Do clean up, summery, post process here. TX_FAILED: When a transaction failed permanently (either by failing all retries, or getting a non-retryable error). Handler has the signature of (error) => {}.

Methods

async, static run(optionsopt, func)

Runs a function in transaction if needed, using specified parameters. If a non-retryable error is thrown while running the transaction, it will be re-raised.
Parameters:
Name Type Attributes Description
options ContextOptions <optional>
func function the closure to run.
Example
// Can be called in 2 ways:
Context.run(async (tx) => {
  // Do something
})

// Or
Context.run({ retryCount: 2 }, async (tx) => {
  // Do something
})

create(Cls, data)

Creates a model without accessing DB. Write will make sure the doc does not exist.
Parameters:
Name Type Description
Cls Model A Model class.
data CompositeID | Object A superset of CompositeID of the model, plus any data for Fields on the Model.

createOrOverwrite(Cls, data)

Sets a model's data. Will overwrite the existing data, if any.
Parameters:
Name Type Description
Cls Model A Model class.
data CompositeID | Object A superset of CompositeID of the model, plus any data for Fields on the Model.

async delete(…args)

Deletes model(s) from database. If a model is read from database, but it did not exist when deleting the doc, an exception is raised.
Parameters:
Name Type Attributes Description
args List.<(Key|Model)> <repeatable>
Keys and Models

enableModelCache()

Enables model cache

async get(Cls, key, paramsopt)

Fetches model(s) from database. This method supports 3 different signatures. get(Cls, keyOrDataValues, params) get(Key|Data, params) get([Key|Data], params) Must use a Key when createIfMissing is not true, and Data otherwise. When a list of docs is fetched: Firestore getAll API is called. Batched fetches are more efficient than calling get with 1 key many times, since there is less HTTP request overhead.
Parameters:
Name Type Attributes Description
Cls Class a Model class.
key String | CompositeID Key or keyValues
params GetParams <optional>
Returns:
- Model(s) associated with provided key

getModelDiffs()

Return before and after snapshots of all relevant models. Does not include deleted models.

makeReadOnly()

Marks a transaction as read-only.

async updateWithoutRead(key, data)

Updates an doc without reading from DB. Fails if doc is not in the db.
Parameters:
Name Type Description
key CompositeID The key to update
data Object Updated fields for the doc