Replies: 1 comment 1 reply
-
Perhaps what you are looking for is a feature like Snapshots in unstorage. // polyfill for Array.fromAsync
import arrayFromAsync from 'array-from-async';
import { kvsEnvStorage } from '@kvs/env';
import * as fs from 'node:fs/promises';
// open database and initialize it
const storage = await kvsEnvStorage({
name: 'database-name',
version: 1,
});
// set
{
await storage.set('a1', 'string');
await storage.set('b2', 42);
await storage.set('c3', false);
console.log('Current', await arrayFromAsync(storage));
}
// SAVE snapshot
{
// Note: Node.js does not Array.fromAsync yet
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fromAsync
const snapshot = await arrayFromAsync(storage);
// save snapshot
await fs.writeFile('./snapshot.json', JSON.stringify(snapshot), 'utf-8');
}
// =====
// clean
{
await storage.clear();
console.log('Cleared', await arrayFromAsync(storage));
}
// =====
// RESTORE snapshot
{
const savedSnapshot = JSON.parse(
await fs.readFile('./snapshot.json', 'utf-8')
);
for (const entry of savedSnapshot) {
await storage.set(entry[0], entry[1]);
}
console.log('Restored', await arrayFromAsync(storage));
} 📝 Array.fromAsync() is Stage 3 proposal. Node.js does not support it yet, but it will be implemented in near future. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Is it possible to save files? localforage handle files and serialization of objects out of the box.
Beta Was this translation helpful? Give feedback.
All reactions