MongoDB storage for BackboneORM

Install

npm install backbone-mongo

Introduction

BackboneMongo provides MongoDB storage for BackboneORM.

Because BackboneORM's query language is based on MongoDB's query language, many queries just work! With a twist...BackboneORM provides cross-collection relationships and embedded data for MongoDB.

In addition, BackboneMongo using CouchDB-style '_rev' versioning to ensure coherency of data.

class Change extends Backbone.Model
  model_name: 'Change'
  sync: require('backbone-orm').sync(Change)

class Task extends Backbone.Model
  urlRoot: 'mongodb://localhost:27017/tasks'
  schema:
    project: -> ['belongsTo', Project]
    changes: -> ['hasMany', Change, embed: true]
  sync: require('backbone-mongo').sync(Task)

class Project extends Backbone.Model
  urlRoot: 'mongodb://localhost:27017/projects'
  schema:
    tasks: -> ['hasMany', Task]
    changes: -> ['hasMany', Change, embed: true]
  sync: require('backbone-mongo').sync(Project)
var Change = Backbone.Model.extend({
  model_name: 'Change',
});
Task.prototype.sync = require('backbone-orm').sync(Change);

var Task = Backbone.Model.extend({
  urlRoot: 'mongodb://localhost:27017/tasks',
  schema: {
    project: function() { return ['belongsTo', Project]; }
    changes: function() { return ['hasMany', Change, {embed: true}]; }
  }
});
Task.prototype.sync = require('backbone-mongo').sync(Task);

var Project = Backbone.Model.extend({
  urlRoot: 'mongodb://localhost:27017/projects',
  schema: {
    tasks: function() { return ['hasMany', Task]; }
    changes: function() { return ['hasMany', Change, {embed: true}]; }
  }
});
Project.prototype.sync = require('backbone-mongo').sync(Project);

Indexing and Custom IDs

Specific attributes can be indexed using the indexed option.

You can use a custom id type (instead of the default ObjectID) in MongoDB by setting the manual_id option.

class Task extends Backbone.Model
  urlRoot: 'mongodb://localhost:27017/tasks?journal=true'
  schema:
    id: [indexed: true]
  sync: require('backbone-mongo').sync(Task)
var Task = Backbone.Model.extend({
  urlRoot: 'mongodb://localhost:27017/tasks',
  schema: {
    id: [indexed: true]
  }
});
Task.prototype.sync = require('backbone-mongo').sync(Task);

Configuring

You can globally configure BackboneMongo's connections using connection_options and can configure a specific collection using url query parameters.

# configure replicaset globally
require('backbone-mongo').configure {connection_options: {
  retryMiliSeconds: 1000
  numberOfRetries: 2*60
  autoReconnect: true
}}

# set journalling for a specific model
class Task extends Backbone.Model
  urlRoot: 'mongodb://localhost:27017/tasks?journal=true'
  sync: require('backbone-mongo').sync(Task)
// configure replicaset globally
_.extend(require('backbone-mongo').connection_options, {
  retryMiliSeconds: 1000,
  numberOfRetries: 2 * 60,
  autoReconnect: true,
  readPreference: require('mongodb').ReadPreference.NEAREST
});

// set journalling for a specific model
var Task = Backbone.Model.extend({
  urlRoot: 'mongodb://localhost:27017/tasks?journal=true',
});
Task.prototype.sync = require('backbone-mongo').sync(Task);