nedb_node_util
TypeScript icon, indicating that this package has built-in type declarations

1.3.9 • Public • Published

NEDB Node Utils

A Node helper to work with the embedded NEDB datastore

Install

  npm i --save nedb_node_util
  

Usage


    // require this package
    var _datastore = require("nedb_node_util");

    // calling this function returns an obj that we can 
    // use to work with our database
    const _db = _datastore();

    
    // we first need to initialise the db
    _db.init(); 
    
    // or we can create a datastore by setting db name and path 
    _db.init( "myDB", "/my/db/path" );
    

Then once we have the DB initalised, the helpers all return promises which we can implement in 2 different ways:


Simple Key / Value Store

The following methods are avaiable:

setValue(key, value) : any string key to store any data type

  // using promises then() and catch()
    _db
    .setValue(key, value)
    .then(savedValue => {
      console.log(savedValue);
    })
    .catch(er => {
      console.log(er.error);
    });


or


  // using async / await instead of promises
  let result = await _db.getQuery({}, skip, count); 
  if(result.hasOwnProperty('error')){
    // result will be the error object from mongo
    return result.error
  }
  return result
  

getValue( key ) : get a stored value by it's string key

    _db
    .getValue(key)
    .then(result => {
      console.log(result);
    })
    .catch(er => {
      console.log(er.error);
    });



Typical Mongo Document DB

The following methods are available:

setObject( object ) : here you can implement your own document structure

    _db
    .setObject(object)
    .then(savedValue => {
      console.log(savedValue);
    })
    .catch(er => {
      console.log(er.error);
    });


updateObject( query, object ) : here you can update your own document structure

    _db
    .updateObject(object)
    .then(updatedValue => {
      console.log(updatedValue);
    })
    .catch(er => {
      console.log(er.error);
    });


getQuery(query, skip = 0, count = 100) : here you can pass mongo query objects based on your custom object structures

    _db
    .getQuery(query, 0, 500) // skip 0 and limit to 100 docs
    .then(results => {
      console.log(results);
    })
    .catch(er => {
      console.log(er.error);
    });



Database Maintenance Methods

getKeys() : _will return the ids of the stored objects

    _db
    .getKeys()
    .then(results => {
      console.log(results); // [{key: 'DATABASE_KEYS', value: '<This is the _id of the stored object>', _id" 'Mongo _id of the keys document'},...]
    })
    .catch(er => {
      console.log(er.error);
    });


deleteObjectByKey( id ) : removes a single document using its id


  _db
    .deleteObjectByKey(_id)
    .then(results => {
      console.log(results); // this object will have the db changes
    })
    .catch(er => {
      console.log(er.error);
    });


getQueryCount( query ) : here you can pass mongo query object and get the document count that would be returned

    _db
    .getQueryCount(query) // skip 0 and limit to 100 docs
    .then(results => {
      console.log(results);
    })
    .catch(er => {
      console.log(er.error);
    });


deleteObjectByQuery( query ) : removes all object or values matching the mongo query object


  _db
    .deleteObjectByQuery(query)
    .then(results => {
      console.log(results); // this object will have the db changes
    })
    .catch(er => {
      console.log(er.error);
    });


clearDB() : removes all objects from the datastore


  _db
    .clearDB(query)
    .then(results => {
      console.log(results); // this object will have the db changes
    })
    .catch(er => {
      console.log(er.error);
    });


clearDBKEYS() - : removes all key references from the datastore


  _db
    .clearDBKEYS()
    .then(results => {
      console.log(results); // this object will have the db changes
    })
    .catch(er => {
      console.log(er.error);
    });


Readme

Keywords

none

Package Sidebar

Install

npm i nedb_node_util

Weekly Downloads

8

Version

1.3.9

License

ISC

Unpacked Size

12.7 kB

Total Files

4

Last publish

Collaborators

  • seeward