Configuration
Performant and feature rich library for managing configurations/settings.
Features
This library has been modeled after VSCode's settings system, and it can be used for implementing a similarly powerful system in your app.
- Performant: it's designed to be extremely fast.
- Providers: a provider reads and writes the actual data, many are built-in (memory, json, local storage etc.) and others can be written easily.
- Universal: it works in the browser too, just use a suitable provider.
- Scopes: a single configuration instance can have multiple providers, so for example a
global + local
setup can be implemented easily. - Filtering: an arbitrary filter function can be provided, for example to make sure the settings are filtered by a schema.
- Path props: path props (e.g.
foo.bar
) are supported for retrieving/setting/deleting entries. - Flat objects: flat objects (e.g.
{ 'foo.bar': true, 'foo.baz': false }
) are supported transparently too.
Install
npm install --save configuration
Usage
The following providers are built-in:
import ProviderAbstract from 'configuration/abstract';
import ProviderAbstractFile from 'configuration/abstract-file';
import ProviderAbstractJSON from 'configuration/abstract-json';
import ProviderFile from 'configuration/file';
import ProviderJSON from 'configuration/json';
import ProviderMemory from 'configuration/memory';
import ProviderStorage from 'configuration/storage';
import ProviderLocalStorage from 'configuration/local-storage';
import ProviderSessionStorage from 'configuration/session-storage';
This is how you'd create a multi-tier settings sytem:
import Configuration from 'configuration';
import ProviderMemory from 'configuration/memory';
import ProviderJSON from 'configuration/json';
const Window = new ProviderMemory ({
scope: 'window',
defaults: {},
defaultsRaw: '{\n\t// Write window-level settings here\n}'
});
const Local = new ProviderJSON ({
scope: 'local',
path: './myapp/local.json',
watching: true,
defaults: {},
defaultsRaw: '{\n\t// Write local-level settings here\n}'
});
const Global = new ProviderJSON ({
scope: 'global',
path: '/Users/fabio/.config/myapp/global.json',
watching: true,
defaults: {},
defaultsRaw: '{\n\t// Write global-level settings here\n}'
});
const configuration = new Configuration ({
providers: [Window, Local, Global],
defaults: {
some: {
default: {
settings: true
}
}
},
filter: settings => {
return settings;
}
});
configuration.get ();
configuration.get ( '*' );
configuration.get ( '*', 'some.path' );
configuration.get ( 'local', 'some.path' );
configuration.get ( 'some.path' );
configuration.has ( '*', 'some.path' );
configuration.has ( 'local', 'some.path' );
configuration.has ( 'some.path' );
configuration.remove ( '*', 'some.path' );
configuration.remove ( 'local', 'some.path' );
configuration.remove ( 'some.path' );
configuration.reset ();
configuration.reset ( 'local' );
configuration.set ( '*', 'some.path', 123 );
configuration.set ( 'local', 'some.path', 123 );
configuration.set ( 'some.path', 123 );
configuration.update ( '*', { something: {} } );
configuration.update ( 'local', { something: {} } );
configuration.onChange ( () => {
});
configuration.onChange ( 'local', 'some.path', ( value, valuePrev ) => {
});
configuration.onChange ( 'some.path', ( value, valuePrev ) => {
});
configuration.dispose ();
License
MIT © Fabio Spampinato