The entrypoint for all actions that relate to data stored by Ditto. Provides access to collections, a write transaction API, and a query hash API.

You don't create one directly but can access it from a particular Ditto instance via its store property.

Hierarchy

  • Store

Properties

ditto: Ditto

The Ditto instance this store belongs to.

observers: readonly StoreObserver[]

All currently active store observers.

Note: Manage store observers using registerObserver() to register a new store observer and StoreObserver.cancel() to remove an existing store observer.

Methods

  • Returns the collection for the given name. If the collection doesn't exist yet, it will be created automatically as soon as the first entry is inserted. A collection name is valid if:

    • its length is less than 100
    • it is not empty
    • it does not contain the char '\0'
    • it does not begin with "$TS_"

    Parameters

    • name: string

    Returns Collection

  • Returns the names of all available collections in the store of the related Ditto instance.

    Returns Promise<string[]>

  • Executes a DQL query and returns matching items as a query result.

    Parameters

    • query: string

      a string containing a valid query expressed in DQL.

    • Optional queryArguments: DQLQueryArguments

      an object of values keyed by the placeholder name without the leading :. Example: { "name": "John" } for a query like SELECT * FROM people WHERE name = :name.

    Returns Promise<QueryResult>

    a promise for a QueryResult containing a QueryResultItem for each match.

    Throws

    DittoError query/invalid: if query argument is not a string or not valid DQL.

    Throws

    DittoError query/arguments-invalid: if queryArguments argument is invalid (e.g. contains unsupported types).

    Throws

    DittoError may throw other errors.

  • Register a handler to be called whenever a query's results change in the local store.

    Convenience method, same as registerObserverWithSignalNext(), except that here, the next invocation of the observation handler is triggered automatically instead of having to call the passed in signalNext function.

    Parameters

    • query: string

      a string containing a valid query expressed in DQL.

    • observationHandler: StoreObservationHandler

      a function that is called whenever the query's results change. The function is passed a QueryResult containing a QueryResultItem for each match.

    • Optional queryArguments: DQLQueryArguments

      an object of values keyed by the placeholder name without the leading :. Example: { "name": "Joanna" } for a query like SELECT * FROM people WHERE name = :name.

    Returns StoreObserver

    a StoreObserver that can be used to cancel the observation.

    Throws

    DittoError query/invalid: if query argument is not a string or not valid DQL.

    Throws

    DittoError query/arguments-invalid: if queryArguments argument is invalid (e.g. contains unsupported types).

    Throws

    DittoError query/unsupported: if the query is not a SELECT query.

    Throws

    DittoError may throw other errors.

  • Registers and returns a store observer for a query, configuring Ditto to trigger the passed in observation handler whenever documents in the local store change such that the result of the matching query changes. The passed in query must be a SELECT query.

    Here, a function is passed as an additional argument to the observation handler. Call this function as soon as the observation handler is ready to process the the next change event. This allows the observation handler to control how frequently it is called. See registerObserver() for a convenience method that automatically signals the next invocation.

    The first invocation of observationHandler will always happen after this method has returned.

    Parameters

    • query: string

      a string containing a valid query expressed in DQL.

    • observationHandler: StoreObservationHandlerWithSignalNext

      an observation handler function that is called whenever the query's results change. The function is passed a QueryResult containing a QueryResultItem for each match.

    • Optional queryArguments: DQLQueryArguments

      an object of values keyed by the placeholder name without the leading :. Example: { "name": "Joanna" } for a query like SELECT * FROM people WHERE name = :name.

    Returns StoreObserver

    a StoreObserver that can be used to cancel the observation.

    Throws

    DittoError query/invalid: if query argument is not a string or not valid DQL.

    Throws

    DittoError query/arguments-invalid: if queryArguments argument is invalid (e.g. contains unsupported types).

    Throws

    DittoError query/unsupported: if the query is not a SELECT query.

    Throws

    DittoError may throw other errors.

  • Initiate a write transaction in a callback.

    Allows you to group multiple operations together that affect multiple documents, potentially across multiple collections.

    Parameters

    • callback: ((transaction) => Promise<void>)

      is given access to a write transaction object that can be used to perform operations on the store.

        • (transaction): Promise<void>
        • Parameters

          Returns Promise<void>

    Returns Promise<WriteTransactionResult[]>

    a list of WriteTransactionResults. There is a result for each operation performed as part of the write transaction.