node-persistent-json-cache
Transparently sync JSON-compatible Object to file. Useful as a cache for smallish objects.
I've used this for small cli scripts, so the code is very simple. The whole object is kept in memory and data is periodically written to disk (1000ms by default, only if modified). It is also written to disk on SIGINT, SIGTERM & exit.
Note: the cache does not do a deep clone when assigning objects/arrays.
Install
npm install persistent-json-cache
Example
{ const persistent = // Load our cache (the file will be created if it doesn't exist). let cache = await // Initially it is empty. assert // Use cache as a normal object. cacheobj = a: 1 cachearr = 1 2 3 cachearr cachearr // Functions cannot be serialized to JSON, so they are not persisted. // cache.fun = function() {} // Sets and Maps cannot be serialized to JSON, but the stringifier & parser // could be extended to transform them: https://stackoverflow.com/a/56150320 // cache.set = new Set([1, 2, 3]) // cache.map = new Map([[1, "one"]]) // Your program won't exit by itself while there are caches loaded because // there is a setTimeout() timer running for periodic saving. You will need // to call process.exit() or close the cache explicitely (both ways are // fine). await persistent // Load our cache again, this time with a different save period. cache = await // Check that everything was saved. assert process}