a simple noSql JSON document database
a noSql simple JSON documents database for projects in developement.
I’m often working on projects which don’t store a big amount of data during dev. I wanted to have a small store easy to use and to implement.
This is NOT for production.
const Store = require('simple-document-store');
const store = new Store('tests/adresses.sds');
store.get({ fieldname: 'name', value: 'Place', operator: 'startsWith' }, function (err, records) {
if (err) {
console.log(err);
} else {
// need to proceed action to load objets in memory.
console.log('total in file: ' + store.documents.length);
console.log('total found: ' + records.length);
}
});
work in progress still testing the code. but you can give it a try and make me feedback :). Code is short and commented.
const store = new Store(path);
path
the path of the document base database.
If the file does not exist, it is created.
Get/search for documents.
store.get(parameters, function(err, documents));
parameters
it is an object with 3 properties
callback (err, documents)
the callback returns an array of documents corresponding to the search
Add a document in memory but does not save it to db file. Use it to add several documents in a loop before save all in one go.
for(let i=0; i< 10;i++){
store.add({
id:i,
label: 'label' + i
});
}
store.save(funtion(err){
console.log(err);
});
Add a document at end of database asynchronously. Care, using ‘append’ in a loop will probably cause a ‘file lock’ error.
store.append(object, function(err){
console.log(err);
});
object
the object to add.
callback(err)
err is not null if something went wrong.
Save whole documents (which are in memory) to disk asynchronouly.
store.save(object, function(err){
console.log(err);
});
It is NOT a method. you can directly update results of a search and modify documents returned, then save.
with a 480 documents database of postal adresses with geocoordonates (1 city), looking for a street took less than 1ms on a i5 8th generation Intel proc and SSD. File size is 55kb.
In comparison, the same request, with same data on a MySql database located in a datacenter took between 40 and 50ms.
I planned to do some test with a local MySQL server.
No network access, the security is associated with the security of the file system the db file is located on.