Skip to content
A Functional ORM-like Library For MongoDB and NodeJS, based on MapJS
JavaScript CoffeeScript
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib first working draft Aug 19, 2012
test
.gitignore
Makefile
README.md
package.json

README.md

A functional data-binding library for MongoDB and NodeJS, based on MapJS. It's written in JavaScript (only tests are CoffeeScript).

Status: Alpha

Install

npm install map-mongo

Usage Example

Configuration

CoffeeScript:

mapmongo = require 'map-mongo'

mapmongo.config {
  name   : 'map-mongo-dev'
  host   : '127.0.0.1'
  port   : 27017
  user   : ''
  passwd : ''
}

JavaScript:

var mapmongo = require('map-mongo');

mapmongo.config({
  name   : 'map-mongo-dev'
  host   : '127.0.0.1'
  port   : 27017
  user   : ''
  passwd : ''
});

Schemas & Documents

Defining

CoffeeScript:

people = mapmongo 'people', {
  name : String # or { type: String, min: 2, max: 64, get: (name) -> "#{name}@localhost" }
  age  : Number # or { type: Number, set: Number  }
}

joe = people {
  name: 'Joe'
  age: 21
}

console.log joe.id() # undefined
console.log joe.name() # 'Joe'
console.log joe.age() # 21

JavaScript:

var people = mapmongo('people', {
  name : String,
  age  : Number
});

var joe = people({
  name: 'Joe'
  age: 21
});

console.log(joe.id()); // undefined
console.log(joe.name()); // 'Joe'
console.log(joe.age()); // 21

Available Methods

See tests for more examples.

Find

CoffeeScript:

people.find { age: 21 }, (error, a21) ->
  throw error if error

  console.log a21.length # 1
  console.log a21[0].name() # 'Joe'

JavaScript:

people.find({ age: 21 }, function(error, a21){
  if(error) throw error;

  console.log(a21.length); // 1
  console.log(a21[0].name()); // 'Joe'

});

Get

Remove

Save

CoffeeScript:

people.save joe, (error) ->
  throw error if error

  console.log joe.id() # 5030b575e004f24c65000004

  joe.name 'Fast Joe';

  people.save joe, (error) ->
    throw error if error

    people.get joe.id(), (error, copy) ->
      throw error if error

      console.log joe.name() # Fast Joe

JavaScript:

people.save(joe, function(error){
  if(error) throw error;

  console.log(joe.id()); // 5030b575e004f24c65000004

  joe.name('Fast Joe');

  people.save(joe, function(error){
    if(error) throw error;

    people.get(joe.id(), function(error){
      if(error) throw error;

      console.log( joe.name() ); // 'Fast Joe'

    })

  })

});
Something went wrong with that request. Please try again.