@couchbase/lite-js
    Preparing search index...

    Class Collection<D>

    Represents a single collection (namespace) of documents in a Database.

    Type Parameters

    Index

    Properties

    The configuration with which the Collection was created.

    database: Database

    The owning Database.

    db: Dexie
    name: string

    The Collection's name.

    Methods

    • Adds a listener function that will be called after any document(s) change. If documents are changed while in a transaction, the listener is not called until the transaction successfully commits.

      Note: Purged and expired documents do not trigger notifications.

      Parameters

      Returns ListenerToken

      A ListenerToken whose ListenerToken.remove method you can call to remove the listener.

    • Adds a listener function that will be called after a specific document changes.

      Note: If the document is changed while in a transaction, the listener is not called until the transaction successfully commits.

      Note: Purged and expired documents do not trigger notifications.

      Parameters

      Returns ListenerToken

      A ListenerToken whose ListenerToken.remove method you can call to remove the listener.

    • By default, returns the number of documents in the collection. If what is "deleted", it returns the number of deleted docs ("tombstones".) If what is "includeDeleted", it returns the total number of live and deleted docs.

      Parameters

      • what: "deleted" | "docs" | "includeDeleted" = 'docs'

      Returns Promise<number>

    • Creates a new document instance tied to this collection. The document will not be persisted to the database until you save it.

      Parameters

      • id: DocID | null

        The document ID, which must be unique in the Collection. If you pass null, a random ID will be generated.

      • Optionalbody: D

        The initial contents of the document; if omitted, it will be empty.

      Returns CBLDocument<D>

    • Deletes a document from this collection.

      Deletion leaves behind an invisible "tombstone" revision, a placeholder that's used by the Replicator to sync the deletion back to a server. If you don't want the overhead, and this deletion does not need to be synced, consider using purge instead.

      Conflicts can occur when deleting, just as on a regular save. A ConflictHandler allows you to handle them.

      Performance note: If you are deleting multiple documents, use updateMultiple instead; it's much faster than saving one at a time.

      Parameters

      Returns Promise<boolean>

      true if saved, false if the ConflictHandler decided to 'revert'.

      ConflictError on conflict.

      EncryptionError if the database is encrypted and locked.

    • Returns the DocIDs of all deleted documents.

      Returns Promise<DocID[]>

    • Returns the DocIDs of all (undeleted) documents.

      Returns Promise<DocID[]>

    • Invokes the callback with each (undeleted) Document of the Collection, ordered by docID. The callback should return true to continue, or false to stop the iteration.

      Parameters

      Returns Promise<boolean>

      True if the iteration completed, false if it was stopped.

    • Loads an existing document, or returns undefined if it doesn't exist.

      Parameters

      Returns Promise<CBLDocument<D> | undefined>

    • Gets a document's expiration date.

      Parameters

      Returns Promise<Date | undefined>

      The expiration date, or undefined if none, or if the document doesn't exist.

    • Deletes all traces of a document, without leaving a "tombstone" revision behind. However, this means purges are not visible to the replicator, which has two side effects:

      • A push replication will not push the deletion to a server.
      • If the document is later updated on the server side, the next pull replication will download the new revision.

      Parameters

      Returns Promise<void>

    • Purges multiple documents at once.

      Parameters

      Returns Promise<void>

    • Saves a document to this collection.

      If the document in the database has been changed since this instance was read -- by your app or by a replicator pulling revisions from a server -- a conflict occurs. By default, a ConflictError will be thrown. If you pass a ConflictHandler callback, it will be invoked during the save and can decide how to resolve the conflict.

      Performance note: If you are saving multiple documents to a collection, use updateMultiple instead; it's much faster than saving one at a time.

      Parameters

      Returns Promise<boolean>

      true if saved, false if the ConflictHandler decided to 'revert'.

      ConflictError on conflict.

      EncryptionError if the database is encrypted and locked.

    • Sets or clears an expiration date for a document.

      Parameters

      • doc: DocID | CBLDocument<D>

        The document or DocID

      • expiration: number | Date | undefined

        Can be an absolute Date, or a number interpreted as milliseconds into the future, or undefined to disable expiration.

      Returns Promise<void>

      if the document doesn't exist.

    • Saves and/or deletes multiple documents at once in a single database transaction. This is much faster than saving each individually.

      The args object has the following properties, all optional:

      • save: Array of CBLDocuments to save
      • delete: Array of CBLDocuments to delete
      • onConflict: Conflict handler
      • bestEffort: If true, the transaction will commit even if some documents had errors. An error will still be thrown, though.

      Parameters

      Returns Promise<void>

      MultipleConflictsError if any documents failed to update. Its errors property tells which documents failed and why.