zaaack / keyv-file

File storage adapter for Keyv, using json to serialize data.
MIT License
69 stars 10 forks source link

How to await for a complete write to disk? #19

Closed folex closed 1 month ago

folex commented 2 years ago

I have a very short-lived CLI app. It starts, saves, dies.

In code, it looks like this.

Write:

const keyv = new Keyv({
  store: new KeyvFile({
    filename: "./state.json",
    writeDelay: 0,
  })
});

keyv.on('error', err => console.log('Keyv Storage Error', err));

let key = "key";
let value = "value";
let stored = await keyv.set(key, value);
console.log("stored", stored);

process.exit(0);

Then user tries to read value

const keyv = new Keyv({
  store: new KeyvFile({
    filename: "./state.json",
    writeDelay: 0,
  })
});

keyv.on('error', err => console.log('Keyv Storage Error', err));

let key = "key";
let value = await keyv.get(key);
console.log("loaded", value);

And value is undefined. I've tried to set writeDelay to zero, but it doesn't help. I don't see flush or something like that in Keyv.

What's a good way to make sure value is written to disk?

I guess it is possible to do something like

const store = new KeyvFile({
    filename: "./state.json",
    writeDelay: 0,
  });
const keyv = new Keyv({ store });

let key = "key";
let value = "value";
let stored = await keyv.set(key, value);

await store.writeToDisk(); // <<<<<<<======= this

console.log("stored", stored);

process.exit(0);

But it is quite tedious to do that every time.

BasToTheMax commented 2 years ago

a

Sidnioulz commented 1 month ago

Hi,

I'm concerned this issue hasn't been answered as it could cause data corruption, which is a pretty big deal for a file store package.

Do you need help addressing this issue? Do you accept external contributions?

zaaack commented 1 month ago

Hi,

I'm concerned this issue hasn't been answered as it could cause data corruption, which is a pretty big deal for a file store package.

Do you need help addressing this issue? Do you accept external contributions?

it should be fixed in newest version.

Sidnioulz commented 1 month ago

That's great news! Thanks for your work on this project :raised_hands: