viewmodel

viewmodel is a node.js module for multiple databases. It can be very useful if you work with (d)ddd, cqrs, eventdenormalizer, host, etc.

node.js:

npm install viewmodel

Build status:

Release:

Connecting

Connecting to an in-memory repository in read mode

Simply take the read function

var viewmodel = require('viewmodel');
 
viewmodel
.read(function(err, repository) {
     
if(err) {
          console
.log('ohhh :-(');
         
return;
     
}
 
});

Connecting to any repository (mongodb in the example / mode=write)

Simply take the write function
and call it with options.

var viewmodel = require('viewmodel');
 
viewmodel
.write(
   
{
        type
: 'mongodb',
        host
: 'localhost',      // optional
        port
: 27017,            // optional
        dbName
: 'viewmodel',    // optional
        authSource
: 'db',       // optional
        username
: 'user',       // optional
        password
: 'pwd'         // optional
       
// url: 'mongodb://user:pass@host:port/db?opts // optional
   
},
   
function(err, repository) {
       
if(err) {
            console
.log('ohhh :-(');
           
return;
       
}
   
}
);
Important hint:

Be sure you have installed the db driver!

In this example run:

npm install mongodb

Catch connect ad disconnect events

Simply listen to connect and disconnect events.

var repository = viewmodel.write({ type: 'mongodb' });
repository
.on('connect', function() {
    console
.log('hello from event');
   
// or here
});
repository
.on('disconnect', function() {
    console
.log('bye');
});
repository
.connect();

Work with a viewmodel

Define a collection

Simply extend the repository with a collectionName.

var dummyRepo = repo.extend({
    collectionName
: 'dummy'
});

Create a new viewmodel (only in write mode)

Calling get without an id will generate a new not persisted viewmodel with generated id.

dummyRepo.get(function(err, vm) {
   
if(err) {
        console
.log('ohhh :-(');
       
return;
   
}
 
    vm
.set('myProp', 'myValue');
    vm
.set('myProp.deep', 'myValueDeep');
 
    console
.log(vm.toJSON());
 
    console
.log(vm.has('myProp.deep'));
 
    dummyRepo
.commit(vm, function(err) {
   
});
   
// or you can call commit directly on vm...
    vm
.commit(function(err) {
   
});
});

Find...

The query object is like the mongodb query object.

dummyRepo.find({ color: 'green' }, function(err, vms) {
// or
//dummyRepo.find({ 'deep.prop': 'dark' }, function(err, vms) {
// or
//dummyRepo.find({ age: { $gte: 10, $lte: 20 } }, function(err, vms) {
// or
//dummyRepo.find({ $or: [{age: 18}, {special: true}] }, function(err, vms) {
// or
//dummyRepo.find({ age: { $in: [1, 2, 3, 6] } }, function(err, vms) {
   
if(err) {
        console
.log('ohhh :-(');
       
return;
   
}
 
   
// vms is an array
   
var firstItem = vms[0];
    console
.log('the id: ' + firstItem.id);
    console
.log('the saved value: ' + firstItem.get('color'));
});

Find with query options

The query options object is like the mongodb query object.

dummyRepo.find({ color: 'green' }, { limit: 2, skip: 1 }, function(err, vms) {
// or
//dummyRepo.find({ color: 'green' }, { limit: 2, skip: 1, sort: [['age', 'desc']] }, function(err, vms) {
   
if(err) {
        console
.log('ohhh :-(');
       
return;
   
}
 
   
// vms is an array
   
var firstItem = vms[0];
    console
.log('the id: ' + firstItem.id);
    console
.log('the saved value: ' + firstItem.get('color'));
});

FindOne...

The query object is like the mongodb query object.

dummyRepo.findOne({ color: 'green' }, function(err, vm) {
   
if(err) {
        console
.log('ohhh :-(');
       
return;
   
}
 
    console
.log('the id: ' + vm.id);
   
if (vm.has('color')) {
        console
.log('the saved value: ' + vm.get('color'));
   
}
});

Find/Get by id...

Calling get with an id will try to fetch the preferred viewmodel from the repository.

If an object with such an id does not exists the returning viewmodel is null.

dummyRepo.get('myId', function(err, vm) {
   
if(err) {
        console
.log('ohhh :-(');
       
return;
   
}
 
    console
.log('the id: ' + vm.id);
    console
.log('the saved value: ' + vm.get('color'));
});

Delete a viewmodel (only in write mode)

Simply call destroy on a viewmodel and commit it.

dummyRepo.get('myId', function(err, vm) {
   
if(err) {
        console
.log('ohhh :-(');
       
return;
   
}
 
    vm
.destroy();
 
    dummyRepo
.commit(vm, function(err) {
   
});
   
// or you can call commit directly on vm...
    vm
.commit(function(err) {
   
});
});

Other helping function

Obtain a new id

Simply call getNewId.

dummyRepo.getNewId(function(err, newId) {
   
if(err) {
        console
.log('ohhh :-(');
       
return;
   
}
 
    console
.log('the new id is: ' + newId);
});

Clear a "collection"

Simply call clear. (only in write mode)

dummyRepo.clear(function(err) {
   
if(err) {
        console
.log('ohhh :-(');
       
return;
   
}
});

Implementation differences

mongodb

For mongodb you can define indexes for performance boosts in find function.

var dummyRepo = repository.extend({
    collectionName
: 'dummy',
    indexes
: [
       
'profileId',
       
// or
       
{ profileId: 1 },
       
// or:
       
{ index: {profileId: 1}, options: {} }
   
]
});

redis

The find function does ignore the query argument and always fetches all items in the collection.

Database Support

Currently these databases are supported: