Common Dwarf Mongoose
Mongoose with some synthetic sugar
Common Dwarf Mongoose is a little wrapper around the excellent Mongoose ORM library. It provides some synthetic sugar like attributes and a more ExtJS/Backbone.js inspired way to define database models.
Installation
Installing is very easy, get it from the NPM registry:
npm install common_dwarf_mongoose
Usage
Mongoose.Base
is the superclass for all database related models.
It combines creation of a Mongo schema and a mongoose model. Additional it provides some synthetic sugar around this:
Let's say, we want to store a collection of authors in our MongoDB.
(Note: Connect to your MongoDB just like this says )
To use Mongoose.Base
require the module:
require 'common_dwarf_mongoose'
Define the model:
alias: 'author' fields: name : type : Stringindex: true blog : type : Stringdefault: "http://blogpost.com" alive : type : Booleandefault: false
The alias
property is used to name the collection. This is done by pluralizing
the alias which is inspired by Ruby On Rails in a very simple way:
author => authors
user => users
blog_post => blog_posts
Any options documented from Mongoose are possible with the following deviantions:
- To use ObjectId use Mongoose.ObjectId
- To use 'any' Type (Schema.Types.Mixed) use Mongoose.Mixed
The class takes care about reating a schema and a mongoose model as documented here.
With the addition of synthetic sugar it allows us:
author = authorset'name':'Jack Kerouac'authorset'blog':'www.ontheroad.com' authorsave throw err if err
Mass assignment is not supported at the moment.
Accessing saved authors is also easy. Maybe you know this from ActiveRecord or Backbone.js:
Authorfind for authorAsDocument in authorDocuments author = AuthorbecomesFromauthor consolelog authorgetName consolelog authorgetBlog
(Also supported are #findById
and findByOne
.)
Notice the last two lines before the parenthesis.
Any attribute you define in fields
will be accessible with a
get prefix after a model is instantiated and an attribute is assigned.
authorgetBlogauthorgetName
If you define a boolean attribute in fields
the getter will be
more context understandable:
authorisAlive
Another option to get an attribute is calling #get()
, which is also
well know from backbone.js or ExtJs:
authorget'name' #=> 'Jack Kerouac'
To get all attributes at once, use the #attributes
property:
authorattributes #=> {name: 'Jack Kerouac', blog: 'www.ontheroad.com'}
If you need a JSON representation of your model's data, you can achieve this with #toJSON()
method:
authortoJSON #=> {'name' : 'Jack Kerouac', 'blog' : 'www.ontheroad.com'}
Plugins
Common Dwarf Mongoose also supports plugins like you know them from mongoose. Adding them to your model is easy as defining fields:
Write a plugin or use an existing one:
= schemaadd lastMod: Date schemapre'save' this.lastMod = next schemapath'lastMod'indexoptionsindex if options && optionsindex
Add the plugin to the model:
alias: 'author' fields: name : type : Stringindex: true blog : type : Stringdefault: "http://blogpost.com" alive : type : Booleandefault: false plugins: plugin: lastModifiedconfig: index: true
That's it.
Multiple connections
If you have to handle documents scattered at different databases it would be nice to handle this within the model definition. Unfortunately, adding database names to a common_dwarf_mongoose model is not possible due the fact it is mongoose based. But a solution is to add a connection instance to your model.
personnel_db = mongocreateConnection"mongodb://0.0.0.0/personnel"tasks_db = mongocreateConnection"mongodb://0.0.0.0/tasks" alias: 'manager' fields: name : type: Stringindex: true salary : type: Number connection: personnel_db alias: 'task' fields: name : type: Stringindex: true due_to : type: Dateindex:true done : type: Boolean
Note: If you seperate your database connection handling from defining your models (say other files or modules),
connection
must be global or otherwise accessable.
Querying
To use #where()
ActorforWhere where'age'gte25 where'tags'in'movie''music''art' # select('name', 'age', 'tags') skip20 limit10 asc'age' slaveOk hint age: 1name: 1 runcallback
Aggregating
You can use #count
of course.
Mixin support
Common Dwarf Mongoose provides mixin support which is accessible if you require the library:
require 'common_dwarf_mongoose'
Any class which inherits from Mixin.Base
could include or extend functionality
with JavaScript objects:
Extensions.documentation = :-> "paper" Extensions.folder = :-> "extensions" @include Extensionsdocumentation @extend Extensionsfolder @include Extensionsdocumentation @extend Extensionsfolder a = b = consolelog akindconsolelog bkind consolelog AfolderTypeconsolelog BfolderType
Common Dwarf Mongoose provides a global namespace called Extensions which could be used to collect all your mixin modules within a single namespace.
Authors and Contributors
Written by Daniel Schmidt(@dsci), Datenspiel GmbH 2012
The picture above was taken by Quartl.
Support or Contact
Having trouble with Common Dwarf Mongoose? Do not hesitate to open an issue at the Issue Tracker :octocat: .