dokyu
A nice little orm for mongodb
Last updated 2 years ago by jldailey .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install dokyu 
SYNC missed versions from official npm registry.

dokyu

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" )
doc.author = "Anonymous"
doc.save().wait ->
  MyDocument.getOrCreate( title: "Manifesto" ).wait (err, doc) ->
    doc.author # "Anonymous", from the database
    doc.jazz() # "hands!", from the prototype

API

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.

  • MyDocument.save( 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.

Current Tags

  • 0.2.2                                ...           latest (2 years ago)

7 Versions

  • 0.2.2                                ...           2 years ago
  • 0.2.1                                ...           5 years ago
  • 0.2.0                                ...           5 years ago
  • 0.1.1                                ...           5 years ago
  • 0.1.0                                ...           5 years ago
  • 0.0.2                                ...           7 years ago
  • 0.0.1                                ...           7 years ago
Maintainers (1)
Downloads
Today 0
This Week 1
This Month 1
Last Day 0
Last Week 3
Last Month 4
Dependencies (2)
Dev Dependencies (2)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |