model.coffee | |
---|---|
Domain Model for MongoDB (Node.JS). Slides, introduction (with video). | |
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
ExamplesBasics, 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:
Next clone project, go to
Note: if there will be error like ProjectThe project is hosted on GitHub. You can report bugs and discuss features on the issues page. | |