sinaps-odm

0.1.2 • Public • Published

Sinaps Object Document Model

Sinaps ODM is a object document model for MongoDB-like databases.

Build Status

Why not using Mongoose or Camo?

After a few month of building up frustrations with Mongoose, I tried Camo ODM. Camo is really a great step in the right direction but was missing a few key feature I needed. That's why Sinaps ODM was born.

Key features

  • Built around ES6 Promise and Classes.
  • Feature a strong Schema model that support nested Schema and array of Schema. All field in schema are typed, provide default value, custom getter and setter.
  • All data are observed and can be listened for changes. Nested data change event bubble up to the root document.
  • Provide built-in memory cache for reusing existing document.
  • Trimed down, nothing-more-nothing-less than what you'd expect from an ODM.

Install

npm install --save sinaps-odm

Getting started

Initialize connection to database

Import Client from sinaps-odm package and initialize new connection using Client.fromUri(uri). It returns a Promise and when it succeed, your ready to go.

var Client = require('sinaps-odm').Client;
Client.fromUri('mongodb://localhost:27017/test')
	.then(client => {

		// From now on you can create & use Document using client

	})
	.catch(err => {
		console.error('Error', err.stack);
	});

Create your first document

With the resulting client, you can define new Document using client.document(collection, schema, options).

// Create new Document for users
var User = client.document('user', {
	name: String,
	email: String
});

// Create new object for John Doe
var john = new User({
	name: 'John Doe',
	email: 'john.doe@example.com'
});

// Save user
john.save()
	.then(() => {
		console.log('John saved');
	})
	.catch(err => {
		console.error('Could not save', err.stack);
	})

Link two Document together

Let's say you have a Post document with a field author that links to a User document.

var Post = client.document('post', {
	title: String,
	author: {
        type: Client.ObjectId,
        ref: 'user'
    }
});

var postA = new Post({
	title: 'Awesome new post by John',
	author: '56aec5a51e4e6e1022d7e301' // ID of previously inserted John Doe
});
console.log(postA.author.name); // undefined, name is not a property of String('56aec5a51e4e6e1022d7e301')

Well that wasn't what we were expecting eh? That's because the process of populating the linked document is asynchronious.

var postA = new Post({
	title: 'Awesome new post by John',
	author: '56aec5a51e4e6e1022d7e301' // ID of previously inserted John Doe
});

// You could wait a bit for the database to respond
setTimeout(function () {
	console.log(postA.author.name); // John Doe
}, 1000);

// or you could use populate factory
Post.populate({
	title: 'Awesome new post by John',
	author: '56aec5a51e4e6e1022d7e301'
}).then(postB => {
	console.log(postB.author.name); // John Doe
});

// or populate your document in multiple step
var postC = new Post({title: 'Awesome new post by John'});
postC.populate({author: '56aec5a51e4e6e1022d7e301'}).then(postC => {
	console.log(postC.author.name); // John Doe
});
console.log(postC.author.name); // Uncaught TypeError: Cannot read property 'name' of undefined

Readme

Keywords

Package Sidebar

Install

npm i sinaps-odm

Weekly Downloads

1

Version

0.1.2

License

MIT

Last publish

Collaborators

  • mgrenier