node package manager



A little ORM for mongodb: npm install dokyu

Basic Usage

Document = require 'dokyu'
class MyDocument extends Document("my_collection")
  @unique { title: 1 }
  jazz: -> "hands!"
Document.connect "mongodb://..."
doc = new MyDocument( title: "Manifesto" ) = "Anonymous" ->
  MyDocument.getOrCreate( title: "Manifesto" ).wait (err, doc) -> # "Anonymous", from the database 
    doc.jazz() # "hands!", from the prototype 


Notice: Some short-hand notation used in this document, a 'Promise(err, x)' means a Promise you use with .wait (err, x) ->

  • Document.connect( [name], url )

    The url should be something like: "mongodb://localhost:27017/db_name".

    TODO: Any connection options supported by mongo can be given as URL params, "?safe=true&replSet=rs0"

    TODO: The optional name is accepted, and creates a separate connection, but cannot yet be used.

  • Document(collection_name, [opts])

    This creates a new class, suitable for sub-classing.

    The generated class is referred to as the InnerDocument.

    For instance, given class Foo extends Document("foos"), all instances of Foo will read/write to the "foos" collection, using whatever database you are connected to (see Document.connect).

    The optional [opts] is a key/value object with two meaningful keys:

    • opts.timeout, in ms, for all operations on this collection.
    • opts.collection, over-rides the collection name in the first argument (a way to have a default and dynamic value)
  • MyDocument.count( query ) → Promise(err, count)

    The count value is the number of documents matching the query.

    MyDocument.count( query ).wait (err, count) ->
      assert typeof count is 'number'
  • MyDocument.findOne( query ) → Promise(err, doc)

    The doc value is the first matching instance of MyDocument.

    MyDocument.findOne( query ).wait (err, doc) ->
      doc.jazz() # "hands!" 
  • MyDocument.find( query, opts ) → Promise(err, cursor)

    The cursor given here is a special proxy cursor with a much simplified interface.

    • length: 0, the total number of records available to the cursor
    • position: 0, is incremented as you read from the cursor
    • nextObject(cb), calls cb(err, doc) where doc is an instance of MyDocument.
    • each(cb), calls cb(err, doc) for every result, each doc is an instance of MyDocument.
    • toArray(cb), calls cb(err, array), where array is full of the MyDocument instances found.
  • MyDocument.update( query, update, [ opts ] ) → Promise(err, updated)

    The updated value is the number of documents updated.

  • doc ) → Promise(err, saved)

    The saved value is the saved document (possibly with a new _id field).

  • MyDocument.remove( query ) → Promise(err, removed)

    The removed value is the number of document removed.

  • MyDocument.index( obj, [ opts ] ) → Promise(err)

    Calls ensureIndex on the underlying collection.

  • MyDocument.unique( obj ) → Promise(err)

    Calls ensureIndex( obj, { unique: true }) on the underlying collection.

  • MyDocument.timeout( ms ) → chainable

    Sets the timeout for operations on this collection.