viewmodel.read(function(err, repository) {
if(err) {
console.log('ohhh :-(');
return;
}
});
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;
}
}
);
Be sure you have installed the db driver!
In this example run:
npm install mongodb
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();
Simply extend the repository with a collectionName.
var dummyRepo = repo.extend({
collectionName: 'dummy'
});
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) {
});
});
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'));
});
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'));
});
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'));
}
});
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'));
});
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) {
});
});
Simply call getNewId.
dummyRepo.getNewId(function(err, newId) {
if(err) {
console.log('ohhh :-(');
return;
}
console.log('the new id is: ' + newId);
});
Simply call clear. (only in write mode)
dummyRepo.clear(function(err) {
if(err) {
console.log('ohhh :-(');
return;
}
});
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: {} }
]
});
The find function does ignore the query argument and always fetches all items in the collection.