WalletConnect / walletconnect-utils

Javascript Utilities for WalletConnect
MIT License
65 stars 56 forks source link

keyvaluestorage node-js constructor issue #48

Open Exzender opened 1 year ago

Exzender commented 1 year ago

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.

elshan-eth commented 1 year ago

I got the same error. image

same-id commented 1 year ago

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)