Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keyvaluestorage node-js constructor issue #48

Open
Exzender opened this issue Jan 12, 2023 · 2 comments
Open

keyvaluestorage node-js constructor issue #48

Exzender opened this issue Jan 12, 2023 · 2 comments
Assignees

Comments

@Exzender
Copy link

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
Copy link

I got the same error.
image

@same-id
Copy link

same-id commented Jul 30, 2023

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants