knex-settings
v2.0.1
Published
Simple key-value settings database for Knex
Downloads
6
Maintainers
Readme
knex-settings
knex-settings
encapsulates a simple key-value table, using the knex
query builder.
Installation
npm install knex-settings
Usage
- Create your
knex
instance.
var db = require('knex')({ client: 'sqlite3', connection:{ filename: 'test.sqlite' } });
- Create a
knex-settings
object, passing theknex
instance and the name of the settings table
var settings = require('knex-settings')(db, 'settings');
- Use
migrate_up
andmigrate_down
methods in a migrations file (or otherwise) to create the database table. Bothmigrate_up
andmigrate_down
return promises.
exports.up = function()
{
return settings.migrate_up();
}
exports.down = function()
{
return settings.migrate_down();
}
- Use the
set(key)
method to set values for a key.set
returns a promise.
settings.set('k', 'v');
- Use the
get(key, [default_value])
method to get a value. A promise is returned with the value.
settings.get('k')
.then(function(value) {
console.log(value); // 'v'
});
settings.get('non_existent_key')
.then(function(value) {
console.log(value); // value undefined
});
settings.get('non_existent_key', 'default_value')
.then(function(value)
{
console.log(value); // 'default_value'
});
- Use the
del(key)
method to delete a key. A promise is returned.
settings.del('k')
.then(function() {
return settings.get('k');
})
.then(function(value) {
console.log(value); // value undefined
});