Utility to work with the NEDB embedded database with ease
A Node helper to work with the embedded NEDB datastore
```
npm i --save nedb_node_util
`
// 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:
---
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);
});
`
---
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);
});
`
---
__getKeys()__
: _will return the _ids of the stored objects_
`
_db
.getKeys()
.then(results => {
console.log(results); // [{key: 'DATABASE_KEYS', value: '
})
.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);
});
``