Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sinaps-odm

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sinaps-odm

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

  • 0.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

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);
	})

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

Keywords

FAQs

Package last updated on 24 Apr 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc