keyv-file icon indicating copy to clipboard operation
keyv-file copied to clipboard

How to await for a complete write to disk?

Open folex opened this issue 3 years ago • 2 comments

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.

folex avatar Apr 27 '22 17:04 folex

a

BasToTheMax avatar May 20 '22 07:05 BasToTheMax

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?

Sidnioulz avatar Sep 15 '24 20:09 Sidnioulz

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.

zaaack avatar Sep 24 '24 22:09 zaaack

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

Sidnioulz avatar Sep 25 '24 06:09 Sidnioulz