npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sinaps-odm

v0.1.2

Published

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

Downloads

9

Readme

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: '[email protected]'
});

// 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