tweekdb
The flat-file, in-memory, remote-sync or hybrid db that is as lightning as you configure it to be.
Live playground
Installation
npm
$ npm install tweekdb --save
git
$ git clone https://github.com/angeal185/tweekdb.git
setup
const tweek tweekdb = ; //minimal base setup exampleconst db = ; //minimal cache only setup exampleconst db = ; //complete custom base setup exampleconst db = ; //multiple db can be created with different configurations like so: const db1 = ; const db2 = ; //create config file in cwddb;
config
"settings": "verbose": true //log to console "dev": true //development mode "lodash_path": "lodash" //require path to lodash or custom lodash "noconflict": false // fixes lodash conflict issues at a cost "crypto_utils": true // enable extra crypto utils "backup": //db backup config "enabled": false "ext": "tmp" //db backup file extention "pre": "." //db backup file prefix "turbo": // turbo mode config "enabled": true "ms": 5 // debounce delay in milliseconds "cron": // cronjobs config "enabled": false "ms": 10000 // cron task interval in milliseconds "fetch": // remote db fetch config "enabled": true "config": // accepts all nodejs https config options "hostname": "" "port": 443 "path": "" "method": "GET" "headers":{} // optional key/cert/pfx as path relative to cwd() ~ ./demo.cert "sync": // remote db save config "enabled": false "config": // accepts all nodejs https config options "hostname": "" "port": 443 "path": "" "method": "POST" "headers":{} // optional key/cert/pfx as path relative to cwd() ~ ./demo.pfx "gzip": // db gzip config "enabled": false // gzip db "backup": true // gzip db backup "settings": "level": 9 "memLevel": 9 "strategy": 0 "hmac": "secret": "" // hmac secret (encoded with hmac.encode) "encode": "hex" // hmac/hash encoding "digest": "sha512" // hmac/hash digest sha256/sha384/sha512 //sha3-256/sha3-384/sha3-512 "encryption": "enabled": false "secret": "" // encryption secret (encoded with encryption.settings.encode) "secret_len": 32 // secret length 16/24/32 "iterations": 60000 // pbkdf2 iterations "settings": "cipher": "aes" // encryption cipher aes/camellia/aria "bit_len": "256" // 128/192/256 "iv_len": 32 // encryption iv length "tag_len": 16 // encryption tag length "encode": "hex" // encryption encoding "mode": "gcm" // gcm/cbc/ccm/ctr/cfb/cfb1/cfb8/ocb/ofb "digest": "sha512" // keygen digest sha256/sha384/sha512 //sha3-256/sha3-384/sha3-512 ... "static": //db static file generator options "enabled": true "dest": "./" //static file dest "schema": {} //default db schema
config file
the tweekdb config file can be generated by calling db.clone_config()
and will create a tweekdb.json
in your working directory. without this config file, settings will fall back to their defaults. this file is key to optimizing your db. should you have more than one db using the same config file, certain overrides can be set when calling new tweekdb('./db', overrides)
. by default the config file is set for speed optimization with most features disabled.
backup mode
the config.backup
setting will enable db backups on save() and load from the backup file in the unlikely event of data corruption to the db. this setting can be used in conjunction with config.gzip
to compress your backups. if you are using a large sized db config.turbo
is recommended. db backups can also be called manually. this method is non blocking.
turbo mode
the config.turbo
setting will debounce file writes in a non blocking way within the time-frame that you specify. for example, if you set config.turbo.ms
to 100, all calls to .save() will have .val() stored to cache but the file write will be debounced for 100ms. if another call to .save() is detected within 100ms, the timeframe is reset and write is debounced again. out of all the consecutive writes you receive within 100ms of each other, only the last write will be written. this process is non blocking and will return/callback to the user at the point of .val() being updated. the speed gains of this feature should theoretically grow with the size of your db.
encryption
tweekdb supports encryption out of the box and can be configured at config.encryption
. prior to enabling this feature an appropriately sized encryption key must be generated with the appropriate encoding. this can be created and returned by calling db.keygen()
. this method is non blocking when used in conjunction with config.turbo
.
gzip
the config.gzip
setting will enable db compression should you wish to compress your db and or backup. if you are using a large sized db config.turbo
is recommended. this method is non blocking when used in conjunction with config.turbo
.
cron
the config.cron
setting will enable you to set a recurring cron job to be carried out at the inteval in milliseconds you specify at config.cron.ms
. the cron function has one arg which the current state of the db. an example function can be found in the settings section.
serialize/deserialize
by default, tweekdb will serialize/deserialize valid json. this can be customized via the serialize/deserialize functions so that tweekdb will serialize/deserialize from and to any format and or encoding. for example:
// store db as json pretty const db = ; // store db as byte array const db = ; // store db hex encoded const db =
lodash
tweekdb is built using lodash. should you wish, you can create your own filtered lodash module and update the require() path at config.settings.lodash_path
.
tweekdb uses all the same chain-able methods as lodash. be mindful that many of these methods will mutate your items in place while others will return a new item.
for example:
"array": 12345 // remove value 2 from the array and mutates the array.// this action will update the db cachedbconsole // [1,3,4,5] // remove value 2 from the array but returns a new array// this action will not update the db cache but will return// a new array without 2let x = db console // [1,2,3,4,5]console // [1,3,4,5]dbconsole // [1,3,4,5]
examples
// db.load/db.fetch/db.cache will store your db to cache and should only be called once. // db load from file to cache syncdb; //db load from file to cache asyncdb; //load remode db to cache using the settings in your config filedb //manually set db cache from any sourcedb // calling .save() will update cache state as well as write state to file.// calling .val() will update only the cache state. //save cached db state to file syncdb; //save cached db state to asyncdb; //save a remode db using the settings in your config filedb // add defaults to base db schema and save state to cache.db // add defaults to base db schema and save state to db file.db // create a key val pair and save state to cache.db; // create an array named array and save state to cache.db; // create a collection named collectiondb; // create object and save state to cachedb // append an object to a collectiondb // append a value then prepend a value to an arraydb // prepend an object to a collectiondb // prepend a value and append a value to an arraydb // remove an object from a collection and add a new objectdb // remove a value from an array and append a new valuedb // find an object in a collectionconsole // {test:'working'} // find index of an object in a collectionconsole // 0 // return first item in a collection or arrayconsole // 1 // return first item in a collection or arrayconsole // 5 // return last item in a collection or arrayconsole // {test:'working'}
static method
tweekdb can also be used as a dev tool to generate json files by calling .static('filename')
from your db items.
db // create a json file for each post in the config.static.dest folderlet x = db;for let i = 0; i < xlength; i++ db
utils
config.settings.crypto_utils
will add the following utils to the build.
//create config file in cwddb; //create new cryptographically secure secretdb; //generate uuidv4db; /** * encrypt a string * @param * @param **/ db; /** * decrypt a string * @param * @param **/ db; /** * hmac a string * @param * @param **/ db /** * hash a string * @param **/ dbhashdata; /** * random bytes * @param * @param **/ db
mixins
you can create your own custom chain-able methods using db._.mixin()
;
// mixin to replace an object within a collection db_ // use mixin like so. db;