1.3.9 • Published 3 years ago

nedb_node_util v1.3.9

Weekly downloads
31
License
ISC
Repository
-
Last release
3 years ago

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);
    });
1.3.7

3 years ago

1.3.6

3 years ago

1.3.5

3 years ago

1.3.9

3 years ago

1.3.8

3 years ago

1.3.4

3 years ago

1.3.3

3 years ago

1.3.2

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.8

3 years ago

1.2.9

3 years ago

1.2.7

3 years ago

1.2.6

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago