dbfree-node

0.5.0 • Public • Published

dbfree-node

Just a simple key value store, local file system, json object
No muss no fuss

 
// Example plugin
// A plugin extends the capability of dbfree
 
var logstuff = function(settings){
    return {
        // runs when the db is started, return a value to set the in-memory object
        init: function(){
                console.log('Plugin loaded');
        },
        // the following run before and after each data change
        // the same params for get and set are passed in
        before_get: function(o, t, k){
                console.log('** Plugin ** Before getting table, key::', t, k);
        },
        after_get: function(o, t, k, v){
                console.log('** Plugin ** After getting table, key::', t, k);
        },
        // plugins don't modify the data being saved
        before_set: function(o, t, k, v){
                console.log('** Plugin ** Before setting table, key::', t, k, v);
        },
        after_set: function(o, t, k, v){
                console.log('** Plugin ** After setting table, key::', t, k, v);
        }
     }
}
 
 
// Another plugin, to persist all data in a file.  See /dbfree-node-plugins for plugins.
// A different plugin could be made to put each table in a different file, save files as CSV, whatever..
 
var filebacked = function(options){
        options = options || {};
        var fs = require('fs');
        var path = ('path' in options) ? options.path : ".";
        if(!fs.existsSync(path)) fs.mkdir(path);
        var dbfn = path + '/db.json';
 
        return {
 
                init: function(){
                    // init file if not exists
                    if(!fs.existsSync(dbfn)) fs.writeFileSync(dbfn, "{}");
                    // load data from file into memory
                    return JSON.parse(fs.readFileSync(dbfn))
                },
 
                before_get: function(o, t, k){
                    // no need
                },
 
                after_get: function(o, t, k, v){
                    // no need
                },
 
                before_set: function(o, t, k, v){
                    // no need
                },
 
                after_set: function(o, t, k, v){
                    // dump entire object to disk
                    fs.writeFileSync(dbfn, JSON.stringify(o));
                },
        }
}
 
// Run a test - with both plugins
 
var  db = require('./dbfree-node.js')({ plugins:[ logstuff(), filebacked() ] } );
db.log = console.log;  // log stuff
 
        console.log( db.set('table1', 'key1', { name: 'chris' }), db.get('table1', 'key1')  );
        console.log( db.set('table1', 'key2', { name: 'brandon' }), db.get('table1', 'key2')  );
        console.log( db.set('table2', 'key1', { name: 'john' }), db.get('table2', 'key1')  );
        
        console.log('Clear table 2', db.set('table2'), db.get('table2', 'key1') );
        
        console.log('All of table1::', db.get('table1'));
        console.log('All DB::', db.get());
 
 
 

Readme

Keywords

none

Package Sidebar

Install

npm i dbfree-node

Weekly Downloads

13

Version

0.5.0

License

none

Last publish

Collaborators

  • dpweb