Super-easy persistent data structures in Node.js
Super-easy persistent data structures in Node.js
Node-persist doesn't use a database. Instead, JSON documents are stored in the file system for persistence. Because there is no network overhead, node-persist is just about as fast as a database can get. Node-persist uses the HTML5 localStorage API, so it's easy to learn.
This is still a work in progress. Send pull requests please.
getItem/setItem does not.synchronous and asynchronous use [email protected]$ npm install node-persistconst storage = require('node-persist');
//you must first call storage.init or initSync
storage.initSync( /* options ... */ );
// or
// storage.init( /* options ... */ );
// then anywhere else in your code
await storage.setItem('name','yourname')
console.log(await storage.getItem('name')); // yourname$ cd examples/counter
$ node counter.js
$ open up localhost:8080Non-backward changes
backward changes
writeQueue* options, trying to resolve issue#108, see the API Documentation below.Non-backward changes
*Sync functions were removed, every operation is now asynchronouspersist* functions were removedasync/awaitcontinuous and interval options were removed, since we immediately persist to disk now, asynchronouslyforEach callback now accepts an object callback({key, value}) instead of 2 arguments callback(key, value)Non-backward changes
options.ttlDir, since the ttls are now stored in the same file as each valueexpiredInterval optionforgiveParseErrors optionMostly non-backward changes
storage.getItem() now returns a promisestorage.valuesWithKeyMatch() no longer accepts a callbackstorage.values() no longer accepts a callbackstorage.key() is gonedir is now process.cwd() + (dir || '.node-persist/storage'), unless you use an absolute pathstorage.get(), alias to getItem()storage.set(), alias to setItem()storage.del(), storage.rm(), as aliases to removeItem()/async init(options, [callback])if the storage dir is new, it will create it
You can pass init() an options object to customize the behavior of node-persist
These are the defaults
…async getItem(key)This function will get the value for that key stored on disk
let value = await storage.getItem('obj');async setItem(key, value, [options])This function sets 'key' in your database to 'value'
await storage.setItem('fibonacci',[0,1,1,2,3,5,8]);
await storage.setItem(42,'the answer to life, the universe, and everything.');
await storage.setItem(42,'the answer to life, the universe, and everything.', {ttl: 1000*60 /* 1 min */ });* The only option available when calling setItem(key, value, option) is {ttl: Number|Date}
async updateItem(key, value, [options])This function updates a 'key' in your database with a new 'value' without touching the ttl, however, if the key was not found or if it was expired a new item will get set
await storage.updateItem(42,'the answer to life, the universe, and everything.', {ttl: 1000*60*10 /* 10 minutes */ });
await storage.updateItem(42,'means nothing, do not trust wikipedia'); // ttl is still the same, will expired in 10 minutes since it was first set* The only option available when calling updateItem(key, value, option) is {ttl: Number|Date}
async removeItem(key)This function immediately deletes it from the file system asynchronously
await storage.removeItem('me');async clear()This function immediately deletes all files from the file system asynchronously.
await storage.clear();async values()This function returns all of the values
await storage.setItem("batman", {name: "Bruce Wayne"});
await storage.setItem("superman", {name: "Clark Kent"});
console.log(await storage.values()); //output: [{name: "Bruce Wayne"},{name: "Clark Kent"}]async valuesWithKeyMatch(match)This function returns all of the values matching a string or RegExp
await storage.setItem("batman", {name: "Bruce Wayne"});
await storage.setItem("superman", {name: "Clark Kent"});
await storage.setItem("hulk", {name: "Bruce Banner"});
console.log(await storage.valuesWithKeyMatch('man')); //output: [{name: "Bruce Wayne"},{name: "Clark Kent"}]
// also accepts a Regular Expression
console.log(await storage.valuesWithKeyMatch(/man/)); //output: [{name: "Bruce Wayne"},{name: "Clark Kent"}]async keys()this function returns an array of all the keys in the database.
console.log(await storage.keys()); // ['batman', 'superman']async length()This function returns the number of keys stored in the database.
console.log(await storage.length()); // 2async forEach(callback)This function iterates over each key/value pair and executes an asynchronous callback as well
storage.forEach(async function(datum) {
// use datum.key and datum.value
});create(options) - synchronous, static methodIf you choose to create multiple instances of storage, you can. Just avoid using the same dir for the storage location.
You still have to call init after create - you can pass your configs to either create or init
const storage = require('node-persist');
const myStorage = storage.create({dir: 'myDir', ttl: 3000});
await myStorage.init();npm install
npm testNo open issues yet, or sync has not completed.