monadbjs
v0.1.6
Published
Functional database interface for JS
Downloads
2
Maintainers
Readme
MonadbJS
Fully functional database interface for JS.
Find full documentation here at MANUAL.md on Github
Installation
Via npm:
$ npm install monadbjs
Interface
Running sequential database operations more like a fluent interface sequence.
var M = require('monadbjs');
var db = new M('mongo', 'localhost', 27017, 'db1', 'collection1').start();
db.insert({name: 'Javi', title: 'MD' })
.insert({name: 'Craig', title: 'N/A'})
.set({}, {'$set': {'hometown': 'Wellington'}})
.loadAll()
.do((ns) => doSomething(ns))
.insert({name: 'Kevin', 'title': 'Architect', 'hometown': 'Sydney'})
.count({'hometown': 'Wellington'})
.do((n) => console.log(n, ' people live in Wellington'))
.deleteAll()
Release the connection after use
db.release()
Spawn multiple connections
Each time you call .start()
, the interface creates a new connection.
var db = new M('mongo', 'localhost', 27017, 'db1', 'collection1')
let conn1 = db.connect();
let conn2 = db.connect();
// And close all connections with
db.release();
Usage scenarios
Load the records given the query condition
db.load({id: 150001000})
.do((rec) => f(rec))
Insert records then trigger another Promise
var records = [
{a: 100, b: 200},
{a: 100, b: null},
{a: 200, b: null},
{a: 300, b: 200}
];
db.insertMany(records)
.countAll()
.do((num) => console.log(num))
.then(anotherPromise); // Trigger next promise as long as we finish
Update / Delete
db.update({g: 100}, {'$set': {g: 150}})
.count({g: 100})
.do((n) => console.log(`${n} records left`))
.delete({g: null})
.delete({g: 500})
.countAll()
Aggregation
let keys = ['team','player']
let by = {goals: {'$sum': 1}}
let sort = {'goals': -1}
let prefilter = {'team': {'$ne': '$against'}}
db.agg(keys, by, sort, prefilter)
.do((res) => console.log(res))
Iterate through records
db.forEach({foo: {'$gt': 0}}, (rec) => {
console.log(rec);
doSomething(rec);
})
Map records and wrap into a new Promise
db.insertMany(myRecords)
.map({}, (n) => n.name)
.then((ns) => ns.forEach((n) => console.log(n))) // New promise
Handle internal error
db.update({}, {'$set': {a: 100, b: 200}}) // Operation which may break
.onFailure((e) => {
console.error(`Exception raised ${e}`);
})
Licence
MIT