async open() {
    if (this._db) {
      return this;
    }

    // In previous versions, we used to have a database with name `${bid}/${cid}`.
    // Check if it exists, and migrate data once new schema is in place.
    // Note: the built-in migrations from IndexedDB can only be used if the
    // database name does not change.
    const dataToMigrate = this._options.migrateOldData
      ? await migrationRequired(this.cid)
      : null;

    this._db = await open(this.dbName, {
      version: 1,
      onupgradeneeded: event => {
        const db = event.target.result;
        // Records store
        const recordsStore = db.createObjectStore("records", {
          keyPath: ["_cid", "id"],
        });
        // An index to obtain all the records in a collection.
        recordsStore.createIndex("cid", "_cid");
        // Here we create indices for every known field in records by collection.
        // Record id (generated by IdSchema, UUID by default)
        recordsStore.createIndex("id", ["_cid", "id"]);
        // Local record status ("synced", "created", "updated", "deleted")
        recordsStore.createIndex("_status", ["_cid", "_status"]);
        // Last modified field
        recordsStore.createIndex("last_modified", ["_cid", "last_modified"]);
        // Timestamps store
        db.createObjectStore("timestamps", {
          keyPath: "cid",
        });
      },
    });

    if (dataToMigrate) {
      const { records, timestamp } = dataToMigrate;
      await this.loadDump(records);
      await this.saveLastModified(timestamp);
      console.log(`${this.cid}: data was migrated successfully.`);
      // Delete the old database.
      await deleteDatabase(this.cid);
      console.warn(`${this.cid}: old database was deleted.`);
    }

    return this;
  }
/**
   * Ensures a connection to the IndexedDB database has been opened.
   *
   * @override
   * @return {Promise}
   */
Ensures a connection to the IndexedDB database has been opened.
@override
@return {Promise}
Ensures a connection to the IndexedDB database has been opened . @override @return { Promise }
async open() {
    if (this._db) {
      return this;
    }
    const dataToMigrate = this._options.migrateOldData
      ? await migrationRequired(this.cid)
      : null;
    this._db = await open(this.dbName, {
      version: 1,
      onupgradeneeded: event => {
        const db = event.target.result;
        const recordsStore = db.createObjectStore("records", {
          keyPath: ["_cid", "id"],
        });
        recordsStore.createIndex("cid", "_cid");
        recordsStore.createIndex("id", ["_cid", "id"]);
        recordsStore.createIndex("_status", ["_cid", "_status"]);
        recordsStore.createIndex("last_modified", ["_cid", "last_modified"]);
        db.createObjectStore("timestamps", {
          keyPath: "cid",
        });
      },
    });
    if (dataToMigrate) {
      const { records, timestamp } = dataToMigrate;
      await this.loadDump(records);
      await this.saveLastModified(timestamp);
      console.log(`${this.cid}: data was migrated successfully.`);
      await deleteDatabase(this.cid);
      console.warn(`${this.cid}: old database was deleted.`);
    }
    return this;
  }
async open ( ) { if ( this . _db ) { return this ; } const dataToMigrate = this . _options . migrateOldData ? await migrationRequired ( this . cid ) : null ; this . _db = await open ( this . dbName , { version : 1 , onupgradeneeded : event => { const db = event . target . result ; const recordsStore = db . createObjectStore ( "records" , { keyPath : [ "_cid" , "id" ] , } ) ; recordsStore . createIndex ( "cid" , "_cid" ) ; recordsStore . createIndex ( "id" , [ "_cid" , "id" ] ) ; recordsStore . createIndex ( "_status" , [ "_cid" , "_status" ] ) ; recordsStore . createIndex ( "last_modified" , [ "_cid" , "last_modified" ] ) ; db . createObjectStore ( "timestamps" , { keyPath : "cid" , } ) ; } , } ) ; if ( dataToMigrate ) { const { records , timestamp } = dataToMigrate ; await this . loadDump ( records ) ; await this . saveLastModified ( timestamp ) ; console . log ( ` ${ this . cid } ` ) ; await deleteDatabase ( this . cid ) ; console . warn ( ` ${ this . cid } ` ) ; } return this ; }
Ensures a connection to the IndexedDB database has been opened.
Ensures a connection to the IndexedDB database has been opened .
// In previous versions, we used to have a database with name `${bid}/${cid}`. // Check if it exists, and migrate data once new schema is in place. // Note: the built-in migrations from IndexedDB can only be used if the // database name does not change. // Records store // An index to obtain all the records in a collection. // Here we create indices for every known field in records by collection. // Record id (generated by IdSchema, UUID by default) // Local record status ("synced", "created", "updated", "deleted") // Last modified field // Timestamps store // Delete the old database.