walletconnect-utils icon indicating copy to clipboard operation
walletconnect-utils copied to clipboard

keyvaluestorage node-js constructor issue

Open Exzender opened this issue 2 years ago • 2 comments

Last string this.databaseInitialize(this.db); of constructor in keyvaluestorage/src/node-js/index.ts forces the file storage to be recreated on each call.

Exzender avatar Jan 12 '23 12:01 Exzender

I got the same error. image

elshan-eth avatar Feb 27 '23 17:02 elshan-eth

Same for us. Removing this.databaseInitialize(this.db) solved this for us. Shouldn't callback: this.database.Initialize in Db.create be the caller?

edit:

I now see that ./db.ts ignores the callback being passed in case `:memory: was supplied:

  private constructor(opts: DbKeyValueStorageOptions) {
    if (!Lokijs) {
      Lokijs = importLokijs();
    }

    if (opts?.db === ":memory:") {
      this.database = new Lokijs(opts?.db, {});
    } else {
      this.database = new Lokijs(opts?.db, {
        autoload: true,
        autoloadCallback: opts.callback,
      });
    }
  }

And does not relay this to Lokijs which I'm not sure why it behaves like that.

So either in case :memory: is used also allow to supply autoloadCallback or edit the code to

  constructor(opts?: KeyValueStorageOptions) {
    // flag it so we don't manually save to file
    if (opts?.database === ":memory:") {
      this.inMemory = true;
    }
    const instance = Db.create({
      db: opts?.database || opts?.table || DB_NAME,
      callback: this.databaseInitialize,
    });
    this.db = instance.database;
    if (opts?.database === ":memory:") {
      this.databaseInitialize(this.db);
    }
  }

or something (probably code can be refactored to be more nice)

same-id avatar Jul 30 '23 16:07 same-id