Jump To …

model.coffee

Domain Model for MongoDB (Node.JS).

Slides, introduction (with video).

Slides from MoscowJS 2012.

Features

Models are JavaScript Objects.

class global.Post extends Model
  @collection 'posts'

Post.create text: 'Zerg on Tarsonis!'

Simple and flexible Queries and Scopes.

Post.first status: 'published'
Post.find(status: 'published').sort(createdAt: -1).
  limit(25).all()

Embedded Models with validations, and callbacks.

post.comments = []

comment = new Comment text: "Can't believe it!"
post.comments.push comment

post.save()

Associations, 1 to 1, 1 to N, N to M.

post.comments().create text: "Can't believe it!"

Callbacks and Validations

class global.Post extends Model
  @after 'delete', (callback) ->
    @comments().delete callback

Same API for Driver and Model.

posts = db.collection 'posts'
posts.find(status: 'published').sort(createdAt: -1).
  limit(25).all()

Use it with plain JavaScript and Callbacks or in synchronous mode with Fibers.

Post.first {status: 'published'}, (err, post) ->
  console.log post

console.log Post.first(status: 'published')

Installation

npm install mongo-model

Examples

Basics, Embedded Models, Queries and Scopes, Validations, Callbacks, Associations, Attribute Assignment & Mass Assignment, Upsersts and Modifiers, Working with Connnections and Databases, Optional Synchronous Mode.

You may also consider using only part of the Model - the Driver. It's independent from the Model.

All examples written in optional synchronous mode (with Fibers), it's made for simplicity and to demonstrate how to use Model with Fibers & CoffeeScript.

Note: Model itself doesn't depends on Fibers or CoffeeScript, You can use it with plain old JavaScript and asynchronous callbacks.

But, You need to install Fibers, Underscore & CoffeeScript to run examples:

npm install coffee-script fibers underscore

Next clone project, go to docs/samples folder and run any example (I recommend You to start with basics):

git clone git://github.com/alexeypetrushin/mongo-model.git
cd mongo-model/docs/samples
coffee basics.coffee

Note: if there will be error like no coffee command - try install coffee-script as global package npm install coffee-script -g.

Project

The project is hosted on GitHub. You can report bugs and discuss features on the issues page.