Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A JS ORM for both server and browser providing Rest server connect middleware
JavaScript
tag: v0.1.0

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
src/rest-mongo
vendor
.gitignore
.gitmodules
LICENSE
README.md

README.md

rest-mongo

A JS ORM for nodejs and mongo-db

Intro

rest-mongo is a wrapper around node-mongodb-native for easier use of MongoDB from nodejs. It provides high level functions to interact with your objects, which are generated from a JSON schema.

Examples

A Schema describing a person


var schema = {
  "Person": {
    schema: {
      id: "Person",
      description: "someone, blablabla",
      type: "object",
       
      properties: {
        firstname: {type: "string"},
        friends: {type: "array", items: {"$ref": "Person"}},
        mother: {"$ref": "Person"}
      }
    }
  }
};

The schema describes a document. It can have references or list of references to other documents.

Get a unit of work (we call it 'R'):


var rest_mongo = require("rest-mongo");
var RFactory = rest_mongo.getRFactory(schema, "db_name");
var R = RFactory();

Get a new R every time you need to get a new context to work in (at every client request for example).

Create and save an object into the DB


var lilly = new R.Person({firstname: "Lilly"});
lilly.save(function() {
  sys.puts("Lilly saved in DB with id " + lilly.id());
});

Get one or more objects from DB


R.Person.get({
 ids: lilly.id()
}, function(lilly2){
  // lilly and lilly2 are the same
});

Search in DB


R.Person.index({
 query: {firstname: "Lilly"}
}, function(data){
  var lilly = data[0];
});

Delete an object


lilly.delete_(function(){
  sys.puts('Lilly deleted from DB!');
}, function() {
  sys.puts('You can not delete Lily!');
});

Usage of references:


var harry = new R.Person({firstname: "Harry", mother: lilly});
harry.save(function() {
  sys.puts('Only the id of Lilly has been saved in harry.mother in DB.');
});

Update more than one field in once:


R.Person.update({
  ids: [lilly.id(), harry.id()], 
  data: {firstname: 'anonymous'}
}, function() {
  sys.puts("Voldemort cannot find them anymore...");
});

Save more than on field in once:


var p1 = new R.Person({firstname: 'Hermione'});
var p2 = new R.Person({firstname: 'Ron'});
R.save([p1, p2], function() {
  console.log('Now Harry has friends.')
}, function(error) {
  console.log('Harry has no friends, because of ', error);
});

Installation

This version has been tested on:

node-mongodb-native and nodetk are vendorized through git submodules: $ git submodule update --init

To run the tests: nodetests Please have a look at the nodetk README file to install nodetk.

License

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see http://www.fsf.org/licensing/licenses/agpl-3.0.html

Something went wrong with that request. Please try again.