Skip to content

Latest commit

 

History

History
81 lines (58 loc) · 2.81 KB

.verb.md

File metadata and controls

81 lines (58 loc) · 2.81 KB

Usage example

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.

// create a config store ("foo.json") in the current working directory
const store = require('{%= name %}')({ path: process.cwd() + '/foo.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' } }

You may also access the Store class if you need to extend or modify the class:

const { Store } = require('data-store');

class MyClass extends Store {
  constructor(...args) {
    super(...args);
  }
}

API

{%= apidocs("index.js") %}

Options

Option Type Default Description
debounce number undefined Disabled by default. 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 The indent value to pass to JSON.stringify() when writing the file to the fs, or when .json() is called
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 relative sub-folder to join to home for data-store config files.
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.

Example: setting path options

You can set the store path using options.path:

const os = require('os');
const path = require('path');
const store = new Store({
  path: path.join(os.homedir(), '.config/my_app/settings.json')
});

console.log(store.path);
// '~/.config/my_app/settings.json'

Or you can set the path using a combination of path parts. The following is equivalent to the previous example:

const os = require('os');
const store = new Store({
  home: os.homedir(),
  base: '.config/my_app',
  name: 'settings'
});

console.log(store.path);
// '~/.config/my_app/settings.json'