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

    Class Database<Schema>

    A Couchbase Lite database. Contains one or more named collections, which contain documents.

    Call open to create a Database instance (the constructor is private.)

    Type Parameters

    • Schema extends SchemaLike<Schema> = NoSchema

      An optional interface type that improves type-safety of collection and document accessors. Its keys must be the names of the collections, and each value type is an interface that describes the properties of a document in that collection:

       interface MySchema {
         shoes: {style: string, foot: "left" | "right", size: number},
       }
      

      If you don't give a schema, the collections property will accept any string as a key, and collections will use CBLDictionary as their document type.

    Index

    Properties

    logger: Logger
    name: string

    The database's name.

    Accessors

    • get collectionNames(): readonly Extract<keyof Schema, string>[]

      The names of the Collections in this Database.

      Returns readonly Extract<keyof Schema, string>[]

    • get collections(): { readonly [K in string]: Collection<Schema[K]> }

      An object whose keys are collection names and values are Collections. In TypeScript, this object is typed accoring to the database's schema type, so accessing a nonexistent collection will result in a compile error, and Collection instances access from this will be typed according to their document schema.

      Returns { readonly [K in string]: Collection<Schema[K]> }

    • get defaultCollection(): Collection

      Returns the Collection named "_default".

      Returns Collection

      if there is no such collection.

    • get isOpen(): boolean

      True if the database is open.

      Returns boolean

    Methods

    • Encrypts or decrypts a database, or changes the encryption key.

      Parameters

      • password: string | undefined

        The new password for encryption, or undefined to decrypt.

      • OptionalexceptProperties: UnencryptedProperties

        Optional top-level properties to leave unencrypted, by collection (in addition to indexed properties, which cannot be encrypted.) Key is collection name, value is array or set of properties.

      Returns Promise<void>

      EncryptionError if it's already encrypted.

    • Closes the database.

      You MUST NOT call any instance methods after this except for reopen.

      You MUST NOT use or keep reference to the Database's Collections; they're invalidated.

      Returns void

    • Closes the database, then deletes its persistent storage.

      Note: You can call reopen, and have an empty database.

      Returns Promise<void>

    • Returns the number of blobs stored in the database.

      Returns Promise<number>

    • Creates a Query object from a N1QL/SQL++ SELECT statement.

      Parameters

      • select: string

      Returns Query<Schema>

    • Decrypts the database. (Same as encrypt(undefined).)

      Returns Promise<void>

    • Returns the Collection object for the named collection.

      Parameters

      • name: string

      Returns Collection

      if there is no such collection.

    • Deletes all blobs that are no longer referenced by any documents.

      Parameters

      • type: "compact"

      Returns Promise<number>

      The number of blobs deleted.

    • Reopens the database after a close or closeAndDelete call.

      Parameters

      • Optionalpassword: string

        If the database is encrypted, you must provide the password.

      Returns Promise<void>

    • Enable's Dexie's debug mode, which provides meaningful stack backtraces in exceptions.

      Parameters

      • debug: boolean

      Returns void

    • Static method that deletes a database by name. You MUST close any open Database instance using it first.

      Parameters

      • name: string

      Returns Promise<void>

    • Creates a Database instance and opens the database. If a local IndexedDB database with this name exists, it will be opened; otherwise a new one is created.

      Type Parameters

      • Schema extends SchemaLike<Schema> = NoSchema

        An optional interface type that improves type-safety of collection and document accessors. Its keys must be the names of the collections, and each value type is an interface that describes the properties of a document in that collection.

      Parameters

      Returns Promise<Database<Schema>>

    • Call this to use a non-default implementation of IndexedDB. This is required in non-browser environments like Node, Bun and Deno! It should be called only once, before creating any Database objects. Example:

          import { indexedDB, IDBKeyRange } from "fake-indexeddb";
      Database.useIndexedDB(indexedDB, IDBKeyRange);

      Parameters

      • indexedDB: IDBFactory
      • idbKeyRange: {
            prototype: IDBKeyRange;
            bound(
                lower: any,
                upper: any,
                lowerOpen?: boolean,
                upperOpen?: boolean,
            ): IDBKeyRange;
            lowerBound(lower: any, open?: boolean): IDBKeyRange;
            only(value: any): IDBKeyRange;
            upperBound(upper: any, open?: boolean): IDBKeyRange;
            new (): IDBKeyRange;
        }
          • new (): IDBKeyRange
          • Returns IDBKeyRange

        • prototype: IDBKeyRange
        • bound: function
          • The bound() static method of the IDBKeyRange interface creates a new key range with the specified upper and lower bounds.

            MDN Reference

            Parameters

            • lower: any
            • upper: any
            • OptionallowerOpen: boolean
            • OptionalupperOpen: boolean

            Returns IDBKeyRange

        • lowerBound: function
          • The lowerBound() static method of the By default, it includes the lower endpoint value and is closed.

            MDN Reference

            Parameters

            • lower: any
            • Optionalopen: boolean

            Returns IDBKeyRange

        • only: function
          • The only() static method of the IDBKeyRange interface creates a new key range containing a single value.

            MDN Reference

            Parameters

            • value: any

            Returns IDBKeyRange

        • upperBound: function
          • The upperBound() static method of the it includes the upper endpoint value and is closed.

            MDN Reference

            Parameters

            • upper: any
            • Optionalopen: boolean

            Returns IDBKeyRange

      Returns void