Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
data-store
Advanced tools
Easily persist and load config data. No dependencies.
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.
(TOC generated by verb using markdown-toc)
Install with npm:
$ npm install --save data-store
By default a JSON file is created with the name of the store in the ~/.config/data-store/
directory. This is completely customizable via options.
const Store = require('data-store');
const store = new Store({ path: 'config.json' });
store.set('one', 'two');
console.log(store.data); //=> { one: 'two' }
store.set('x.y.z', 'boom!');
store.set({ c: 'd' });
console.log(store.get('e.f'));
//=> 'g'
console.log(store.get());
//=> { name: 'app', data: { a: 'b', c: 'd', e: { f: 'g' } } }
console.log(store.data);
//=> { a: 'b', c: 'd', e: { f: 'g' } }
Initialize a new Store
with the given name
, options
and default
data.
Params
name
{string}: Store name to use for the basename of the .json
file.options
{object}: See all available options.defaults
{object}: An object to initialize the store with.Example
const store = require('data-store')('abc');
//=> '~/data-store/a.json'
const store = require('data-store')('abc', { cwd: 'test/fixtures' });
//=> './test/fixtures/abc.json'
Assign value
to key
and save to the file system. Can be a key-value pair, array of objects, or an object.
Params
key
{string}val
{any}: The value to save to key
. Must be a valid JSON type: String, Number, Array or Object.returns
{object} Store
: for chainingExample
// key, value
store.set('a', 'b');
//=> {a: 'b'}
// extend the store with an object
store.set({a: 'b'});
//=> {a: 'b'}
Add the given value
to the array at key
. Creates a new array if one doesn't exist, and only adds unique values to the array.
Params
key
{string}val
{any}: The value to union to key
. Must be a valid JSON type: String, Number, Array or Object.returns
{object} Store
: for chainingExample
store.union('a', 'b');
store.union('a', 'c');
store.union('a', 'd');
store.union('a', 'c');
console.log(store.get('a'));
//=> ['b', 'c', 'd']
Get the stored value
of key
.
Params
key
{string}returns
{any}: The value to store for key
.Example
store.set('a', {b: 'c'});
store.get('a');
//=> {b: 'c'}
store.get();
//=> {a: {b: 'c'}}
Returns true
if the specified key
has a value.
Params
key
{string}returns
{boolean}: Returns true if key
hasExample
store.set('a', 42);
store.set('c', null);
store.has('a'); //=> true
store.has('c'); //=> true
store.has('d'); //=> false
Returns true
if the specified key
exists.
Params
key
{string}returns
{boolean}: Returns true if key
existsExample
store.set('a', 'b');
store.set('b', false);
store.set('c', null);
store.set('d', true);
store.set('e', undefined);
store.hasOwn('a'); //=> true
store.hasOwn('b'); //=> true
store.hasOwn('c'); //=> true
store.hasOwn('d'); //=> true
store.hasOwn('e'); //=> true
store.hasOwn('foo'); //=> false
Delete one or more properties from the store.
Params
keys
{string|Array}: One or more properties to delete.Example
store.set('foo.bar', 'baz');
console.log(store.data); //=> { foo: { bar: 'baz' } }
store.del('foo.bar');
console.log(store.data); //=> { foo: {} }
store.del('foo');
console.log(store.data); //=> {}
Return a clone of the store.data
object.
returns
{object}Example
console.log(store.clone());
Reset store.data
to an empty object.
returns
{undefined}Example
store.clear();
Stringify the store. Takes the same arguments as JSON.stringify
.
returns
{string}Example
console.log(store.json(null, 2));
Calls .writeFile() to persist the store to the file system, after an optional debounce period. This method should probably not be called directly as it's used internally by other methods.
returns
{undefined}Example
store.save();
Delete the store from the file system.
returns
{undefined}Example
store.unlink();
Option | Type | Default | Description |
---|---|---|---|
debounce | number | undefined | Milliseconds to delay writing the JSON file to the file system. This can make the store more performant by preventing multiple subsequent writes after calling .set or setting/getting store.data , but comes with the potential side effect that the config file will be outdated during the timeout. To get around this, use data-store's API to (re-)load the file instead of directly reading the file (using fs.readFile for example). |
indent | `number | null` | 2 |
name | string | undefined | The name to use for the store file stem (name + '.json' is the store's file name) |
home | string | process.env.XDG_CONFIG_HOME or path.join(os.homedir(), '.config') | The root home directory to use |
base | string | path.join(home, 'data-store') | The directory to use for data-store config files. This value is joined to home |
path | string | ... | Absolute file path for the data-store JSON file. This is created by joining base to name + '.json' . Setting this value directly will override the name , home and base values. |
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
$ npm install && npm test
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
$ npm install -g verbose/verb#dev verb-generate-readme && verb
You might also be interested in these projects:
'a.b.c'
) paths. | homepageCommits | Contributor |
---|---|
151 | jonschlinkert |
4 | doowb |
2 | charlike-old |
1 | jamen |
Jon Schlinkert
Copyright © 2018, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on June 10, 2018.
FAQs
Easily persist and load config data. No dependencies.
The npm package data-store receives a total of 23,360 weekly downloads. As such, data-store popularity was classified as popular.
We found that data-store demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.