backbone-mongo
Advanced tools
Comparing version 0.5.9 to 0.6.0
// Generated by CoffeeScript 1.7.1 | ||
/* | ||
backbone-mongo.js 0.5.9 | ||
backbone-mongo.js 0.6.0 | ||
Copyright (c) 2013 Vidigami - https://github.com/vidigami/backbone-mongo | ||
@@ -10,14 +10,8 @@ License: MIT (http://www.opensource.org/licenses/mit-license.php) | ||
(function() { | ||
var ARRAY_QUERIES, MemoryCursor, MongoCursor, Queue, util, _, _adaptIds, _sortArgsToMongo, | ||
var ARRAY_QUERIES, MongoCursor, sync, _, _adaptIds, _ref, _sortArgsToMongo, | ||
__hasProp = {}.hasOwnProperty, | ||
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; | ||
util = require('util'); | ||
_ref = require('backbone-orm'), _ = _ref._, sync = _ref.sync; | ||
_ = require('underscore'); | ||
Queue = require('backbone-orm/lib/queue'); | ||
MemoryCursor = require('backbone-orm/lib/memory/cursor'); | ||
ARRAY_QUERIES = ['$or', '$nor', '$and']; | ||
@@ -180,4 +174,4 @@ | ||
})(MemoryCursor); | ||
})(sync.Cursor); | ||
}).call(this); |
@@ -10,9 +10,7 @@ // Generated by CoffeeScript 1.7.1 | ||
(function() { | ||
var DatabaseTools, Queue, _, | ||
var DatabaseTools, Queue, _, _ref, | ||
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; | ||
_ = require('underscore'); | ||
_ref = require('backbone-orm'), _ = _ref._, Queue = _ref.Queue; | ||
Queue = require('backbone-orm/lib/queue'); | ||
module.exports = DatabaseTools = (function() { | ||
@@ -32,5 +30,5 @@ function DatabaseTools(sync, options) { | ||
DatabaseTools.prototype.resetSchema = function(options, callback) { | ||
var queue, _ref; | ||
var queue, _ref1; | ||
if (arguments.length === 1) { | ||
_ref = [options, {}], callback = _ref[0], options = _ref[1]; | ||
_ref1 = [options, {}], callback = _ref1[0], options = _ref1[1]; | ||
} | ||
@@ -49,5 +47,5 @@ if (this.resetting) { | ||
return function(callback) { | ||
var join_queue, join_table, _fn, _i, _len, _ref1; | ||
var join_queue, join_table, _fn, _i, _len, _ref2; | ||
join_queue = new Queue(1); | ||
_ref1 = _this.schema.joinTables(); | ||
_ref2 = _this.schema.joinTables(); | ||
_fn = function(join_table) { | ||
@@ -58,4 +56,4 @@ return join_queue.defer(function(callback) { | ||
}; | ||
for (_i = 0, _len = _ref1.length; _i < _len; _i++) { | ||
join_table = _ref1[_i]; | ||
for (_i = 0, _len = _ref2.length; _i < _len; _i++) { | ||
join_table = _ref2[_i]; | ||
_fn(join_table); | ||
@@ -78,5 +76,5 @@ } | ||
DatabaseTools.prototype.ensureSchema = function(options, callback) { | ||
var queue, _ref; | ||
var queue, _ref1; | ||
if (arguments.length === 1) { | ||
_ref = [options, {}], callback = _ref[0], options = _ref[1]; | ||
_ref1 = [options, {}], callback = _ref1[0], options = _ref1[1]; | ||
} | ||
@@ -95,5 +93,5 @@ if (this.ensuring) { | ||
return function(callback) { | ||
var join_queue, join_table, _fn, _i, _len, _ref1; | ||
var join_queue, join_table, _fn, _i, _len, _ref2; | ||
join_queue = new Queue(1); | ||
_ref1 = _this.schema.joinTables(); | ||
_ref2 = _this.schema.joinTables(); | ||
_fn = function(join_table) { | ||
@@ -104,4 +102,4 @@ return join_queue.defer(function(callback) { | ||
}; | ||
for (_i = 0, _len = _ref1.length; _i < _len; _i++) { | ||
join_table = _ref1[_i]; | ||
for (_i = 0, _len = _ref2.length; _i < _len; _i++) { | ||
join_table = _ref2[_i]; | ||
_fn(join_table); | ||
@@ -123,3 +121,3 @@ } | ||
return function(err, collection) { | ||
var field, key, queue, relation, _ref, _ref1; | ||
var field, key, queue, relation, _ref1, _ref2; | ||
if (err) { | ||
@@ -135,5 +133,5 @@ return callback(err); | ||
}); | ||
_ref = _this.schema.fields; | ||
for (key in _ref) { | ||
field = _ref[key]; | ||
_ref1 = _this.schema.fields; | ||
for (key in _ref1) { | ||
field = _ref1[key]; | ||
if (field.indexed) { | ||
@@ -147,5 +145,5 @@ (function(key) { | ||
} | ||
_ref1 = _this.schema.relations; | ||
for (key in _ref1) { | ||
relation = _ref1[key]; | ||
_ref2 = _this.schema.relations; | ||
for (key in _ref2) { | ||
relation = _ref2[key]; | ||
if ((relation.type === 'belongsTo') && !relation.isVirtual() && !relation.isEmbedded()) { | ||
@@ -165,5 +163,5 @@ (function(key, relation) { | ||
DatabaseTools.prototype.dropTable = function(options, callback) { | ||
var _ref; | ||
var _ref1; | ||
if (arguments.length === 1) { | ||
_ref = [options, {}], callback = _ref[0], options = _ref[1]; | ||
_ref1 = [options, {}], callback = _ref1[0], options = _ref1[1]; | ||
} | ||
@@ -192,5 +190,5 @@ return this.sync.collection((function(_this) { | ||
DatabaseTools.prototype.ensureIndex = function(field_name, options, callback) { | ||
var index_info, _ref; | ||
var index_info, _ref1; | ||
if (arguments.length === 2) { | ||
_ref = [options, {}], callback = _ref[0], options = _ref[1]; | ||
_ref1 = [options, {}], callback = _ref1[0], options = _ref1[1]; | ||
} | ||
@@ -197,0 +195,0 @@ index_info = {}; |
// Generated by CoffeeScript 1.7.1 | ||
/* | ||
backbone-mongo.js 0.5.9 | ||
backbone-mongo.js 0.6.0 | ||
Copyright (c) 2013 Vidigami - https://github.com/vidigami/backbone-mongo | ||
@@ -10,7 +10,27 @@ License: MIT (http://www.opensource.org/licenses/mit-license.php) | ||
(function() { | ||
module.exports = { | ||
var Backbone, BackboneMongo, BackboneORM, key, publish, value, _, _ref, _ref1; | ||
_ref = BackboneORM = require('backbone-orm'), _ = _ref._, Backbone = _ref.Backbone; | ||
module.exports = BackboneMongo = require('./core'); | ||
publish = { | ||
configure: require('./lib/configure'), | ||
sync: require('./sync'), | ||
connection_options: require('./connection').options | ||
_: _, | ||
Backbone: Backbone | ||
}; | ||
publish._.extend(BackboneMongo, publish); | ||
BackboneMongo.modules = { | ||
'backbone-orm': BackboneORM | ||
}; | ||
_ref1 = BackboneORM.modules; | ||
for (key in _ref1) { | ||
value = _ref1[key]; | ||
BackboneMongo.modules[key] = value; | ||
} | ||
}).call(this); |
227
lib/sync.js
// Generated by CoffeeScript 1.7.1 | ||
/* | ||
backbone-mongo.js 0.5.9 | ||
backbone-mongo.js 0.6.0 | ||
Copyright (c) 2013 Vidigami - https://github.com/vidigami/backbone-mongo | ||
@@ -10,28 +10,12 @@ License: MIT (http://www.opensource.org/licenses/mit-license.php) | ||
(function() { | ||
var Backbone, Connection, DESTROY_BATCH_LIMIT, DatabaseTools, ModelCache, ModelTypeID, MongoCursor, MongoSync, QueryCache, Queue, Schema, Utils, moment, util, _, | ||
var Backbone, BackboneORM, CAPABILITIES, Connection, DESTROY_BATCH_LIMIT, DatabaseTools, JSONUtils, MongoCursor, MongoSync, ObjectID, Queue, Schema, Utils, _, _ref, | ||
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; | ||
util = require('util'); | ||
ObjectID = require('mongodb').ObjectID; | ||
_ = require('underscore'); | ||
_ref = BackboneORM = require('backbone-orm'), _ = _ref._, Backbone = _ref.Backbone, Queue = _ref.Queue, Schema = _ref.Schema, Utils = _ref.Utils, JSONUtils = _ref.JSONUtils; | ||
Backbone = require('backbone'); | ||
moment = require('moment'); | ||
Queue = require('backbone-orm/lib/queue'); | ||
Schema = require('backbone-orm/lib/schema'); | ||
Utils = require('backbone-orm/lib/utils'); | ||
QueryCache = require('backbone-orm/lib/cache/singletons').QueryCache; | ||
ModelCache = require('backbone-orm/lib/cache/singletons').ModelCache; | ||
ModelTypeID = require('backbone-orm/lib/cache/singletons').ModelTypeID; | ||
MongoCursor = require('./cursor'); | ||
Connection = require('./connection'); | ||
Connection = require('./lib/connection'); | ||
@@ -42,10 +26,19 @@ DatabaseTools = require('./database_tools'); | ||
CAPABILITIES = { | ||
embed: true, | ||
json: true, | ||
self_reference: true | ||
}; | ||
MongoSync = (function() { | ||
function MongoSync(model_type, sync_options) { | ||
this.model_type = model_type; | ||
this.sync_options = sync_options; | ||
this.sync_options = sync_options != null ? sync_options : {}; | ||
this.db = __bind(this.db, this); | ||
this.model_type.model_name = Utils.findOrGenerateModelName(this.model_type); | ||
this.model_type.model_id = ModelTypeID.generate(this.model_type); | ||
this.schema = new Schema(this.model_type); | ||
this.schema = new Schema(this.model_type, { | ||
id: { | ||
type: ObjectID | ||
} | ||
}); | ||
this.backbone_adapter = this.model_type.backbone_adapter = this._selectAdapter(); | ||
@@ -90,25 +83,20 @@ } | ||
if (this.manual_id && !model.id) { | ||
return options.error(new Error("Missing manual id for create: " + (util.inspect(model.attributes)))); | ||
return options.error(new Error("Missing manual id for create: " + (JSONUtils.stringify(model.attributes)))); | ||
} | ||
return QueryCache.reset(this.model_type, (function(_this) { | ||
return function(err) { | ||
return this.connection.collection((function(_this) { | ||
return function(err, collection) { | ||
var doc; | ||
if (err) { | ||
return options.error(err); | ||
} | ||
return _this.connection.collection(function(err, collection) { | ||
var doc; | ||
if (err) { | ||
return options.error(err); | ||
if (model.get('_rev')) { | ||
return options.error(new Error('New document has a non-empty revision')); | ||
} | ||
doc = _this.backbone_adapter.attributesToNative(model.toJSON()); | ||
doc._rev = 1; | ||
return collection.insert(doc, function(err, docs) { | ||
if (err || !docs || docs.length !== 1) { | ||
return options.error(new Error("Failed to create model. Error: " + (err || 'document not found'))); | ||
} | ||
if (model.get('_rev')) { | ||
return options.error(new Error('New document has a non-empty revision')); | ||
} | ||
doc = _this.backbone_adapter.attributesToNative(model.toJSON()); | ||
doc._rev = 1; | ||
return collection.insert(doc, function(err, docs) { | ||
if (err || !docs || docs.length !== 1) { | ||
return options.error(new Error("Failed to create model. Error: " + (err || 'document not found'))); | ||
} | ||
return options.success(_this.backbone_adapter.nativeToAttributes(docs[0])); | ||
}); | ||
return options.success(_this.backbone_adapter.nativeToAttributes(docs[0])); | ||
}); | ||
@@ -124,50 +112,45 @@ }; | ||
if (this.manual_id && !model.id) { | ||
return options.error(new Error("Missing manual id for create: " + (util.inspect(model.attributes)))); | ||
return options.error(new Error("Missing manual id for create: " + (JSONUtils.stringify(model.attributes)))); | ||
} | ||
return QueryCache.reset(this.model_type, (function(_this) { | ||
return function(err) { | ||
return this.connection.collection((function(_this) { | ||
return function(err, collection) { | ||
var find_query, json, key, modifications, unsets, _i, _len; | ||
if (err) { | ||
return options.error(err); | ||
} | ||
return _this.connection.collection(function(err, collection) { | ||
var find_query, json, key, modifications, unsets, _i, _len; | ||
json = _this.backbone_adapter.attributesToNative(model.toJSON()); | ||
if (_this.backbone_adapter.id_attribute === '_id') { | ||
delete json._id; | ||
} | ||
find_query = _this.backbone_adapter.modelFindQuery(model); | ||
find_query._rev = json._rev; | ||
json._rev++; | ||
modifications = { | ||
$set: json | ||
}; | ||
if (unsets = Utils.get(model, 'unsets')) { | ||
Utils.unset(model, 'unsets'); | ||
if (unsets.length) { | ||
modifications.$unset = {}; | ||
for (_i = 0, _len = unsets.length; _i < _len; _i++) { | ||
key = unsets[_i]; | ||
if (!model.attributes.hasOwnProperty(key)) { | ||
modifications.$unset[key] = ''; | ||
} | ||
} | ||
} | ||
} | ||
return collection.findAndModify(find_query, [[_this.backbone_adapter.id_attribute, 'asc']], modifications, { | ||
"new": true | ||
}, function(err, doc) { | ||
if (err) { | ||
return options.error(err); | ||
return options.error(new Error("Failed to update model (" + _this.model_type.model_name + "). Error: " + err)); | ||
} | ||
json = _this.backbone_adapter.attributesToNative(model.toJSON()); | ||
if (_this.backbone_adapter.id_attribute === '_id') { | ||
delete json._id; | ||
if (!doc) { | ||
return options.error(new Error("Failed to update model (" + _this.model_type.model_name + "). Either the document has been deleted or the revision (_rev) was stale.")); | ||
} | ||
find_query = _this.backbone_adapter.modelFindQuery(model); | ||
find_query._rev = json._rev; | ||
json._rev++; | ||
modifications = { | ||
$set: json | ||
}; | ||
if (unsets = Utils.get(model, 'unsets')) { | ||
Utils.unset(model, 'unsets'); | ||
if (unsets.length) { | ||
modifications.$unset = {}; | ||
for (_i = 0, _len = unsets.length; _i < _len; _i++) { | ||
key = unsets[_i]; | ||
if (!model.attributes.hasOwnProperty(key)) { | ||
modifications.$unset[key] = ''; | ||
} | ||
} | ||
} | ||
if (doc._rev !== json._rev) { | ||
return options.error(new Error("Failed to update revision (" + _this.model_type.model_name + "). Is: " + doc._rev + " expecting: " + json._rev)); | ||
} | ||
return collection.findAndModify(find_query, [[_this.backbone_adapter.id_attribute, 'asc']], modifications, { | ||
"new": true | ||
}, function(err, doc) { | ||
if (err) { | ||
return options.error(new Error("Failed to update model (" + _this.model_type.model_name + "). Error: " + err)); | ||
} | ||
if (!doc) { | ||
return options.error(new Error("Failed to update model (" + _this.model_type.model_name + "). Either the document has been deleted or the revision (_rev) was stale.")); | ||
} | ||
if (doc._rev !== json._rev) { | ||
return options.error(new Error("Failed to update revision (" + _this.model_type.model_name + "). Is: " + doc._rev + " expecting: " + json._rev)); | ||
} | ||
return options.success(_this.backbone_adapter.nativeToAttributes(doc)); | ||
}); | ||
return options.success(_this.backbone_adapter.nativeToAttributes(doc)); | ||
}); | ||
@@ -179,19 +162,14 @@ }; | ||
MongoSync.prototype["delete"] = function(model, options) { | ||
return QueryCache.reset(this.model_type, (function(_this) { | ||
return function(err) { | ||
return this.connection.collection((function(_this) { | ||
return function(err, collection) { | ||
if (err) { | ||
return options.error(err); | ||
} | ||
return _this.connection.collection(function(err, collection) { | ||
return collection.remove(_this.backbone_adapter.attributesToNative({ | ||
id: model.id | ||
}), function(err) { | ||
if (err) { | ||
return options.error(err); | ||
} | ||
return collection.remove(_this.backbone_adapter.attributesToNative({ | ||
id: model.id | ||
}), function(err) { | ||
if (err) { | ||
return options.error(err); | ||
} | ||
return options.success(); | ||
}); | ||
return options.success(); | ||
}); | ||
@@ -214,32 +192,31 @@ }; | ||
MongoSync.prototype.destroy = function(query, callback) { | ||
return QueryCache.reset(this.model_type, (function(_this) { | ||
return function(err) { | ||
var _ref1; | ||
if (arguments.length === 1) { | ||
_ref1 = [{}, query], query = _ref1[0], callback = _ref1[1]; | ||
} | ||
return this.connection.collection((function(_this) { | ||
return function(err, collection) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
return _this.connection.collection(function(err, collection) { | ||
if (err) { | ||
return callback(err); | ||
return _this.model_type.each(_.extend({ | ||
$each: { | ||
limit: DESTROY_BATCH_LIMIT, | ||
json: true | ||
} | ||
return _this.model_type.each(_.extend({ | ||
$each: { | ||
limit: DESTROY_BATCH_LIMIT, | ||
json: true | ||
}, query), (function(model_json, callback) { | ||
return Utils.patchRemoveByJSON(_this.model_type, model_json, function(err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
}, query), (function(model_json, callback) { | ||
return Utils.patchRemoveByJSON(_this.model_type, model_json, function(err) { | ||
return collection.remove(_this.backbone_adapter.attributesToNative({ | ||
id: model_json.id | ||
}), function(err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
return collection.remove(_this.backbone_adapter.attributesToNative({ | ||
id: model_json.id | ||
}), function(err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
return callback(); | ||
}); | ||
return callback(); | ||
}); | ||
}), callback); | ||
}); | ||
}); | ||
}), callback); | ||
}; | ||
@@ -256,3 +233,3 @@ })(this)); | ||
} | ||
return this.connection = new Connection(url, this.schema, this.sync_options.connection_options || {}); | ||
return this.connection = new Connection(url, this.schema, this.sync_options); | ||
}; | ||
@@ -269,6 +246,6 @@ | ||
MongoSync.prototype._selectAdapter = function() { | ||
var field_info, field_name, info, _i, _len, _ref; | ||
_ref = this.schema.raw; | ||
for (field_name in _ref) { | ||
field_info = _ref[field_name]; | ||
var field_info, field_name, info, _i, _len, _ref1; | ||
_ref1 = this.schema.raw; | ||
for (field_name in _ref1) { | ||
field_info = _ref1[field_name]; | ||
if ((field_name !== 'id') || !_.isArray(field_info)) { | ||
@@ -281,7 +258,7 @@ continue; | ||
this.manual_id = true; | ||
return require('./document_adapter_no_mongo_id'); | ||
return require('./lib/document_adapter_no_mongo_id'); | ||
} | ||
} | ||
} | ||
return require('./document_adapter_mongo_id'); | ||
return require('./lib/document_adapter_mongo_id'); | ||
}; | ||
@@ -326,6 +303,10 @@ | ||
}; | ||
require('backbone-orm/lib/extensions/model')(type); | ||
return ModelCache.configureSync(type, sync_fn); | ||
Utils.configureModelType(type); | ||
return BackboneORM.model_cache.configureSync(type, sync_fn); | ||
}; | ||
module.exports.capabilities = function(url) { | ||
return CAPABILITIES; | ||
}; | ||
}).call(this); |
{ | ||
"name": "backbone-mongo", | ||
"version": "0.5.9", | ||
"version": "0.6.0", | ||
"description": "MongoDB storage for BackboneORM", | ||
@@ -13,3 +13,6 @@ "main": "./lib/index.js", | ||
"scripts": { | ||
"test": "npm run build; NODE_ENV=test mocha test/suite.coffee --compilers coffee:coffee-script/register --reporter spec --timeout 10000", | ||
"test": "npm run build && ulimit -n 1024; NODE_ENV=test mocha --require backbone-rest/test/parameters_express4 '{node_modules/backbone-{orm,rest}/,}test/{issues,spec}/**/*.tests.coffee' --grep ''", | ||
"test-quick": "npm run build && ulimit -n 1024; NODE_ENV=test mocha --require backbone-rest/test/parameters_express4 'node_modules/backbone-orm/test/spec/**/*.tests.coffee' --grep '@no_cache @no_embed'", | ||
"test-self": "npm run build && ulimit -n 1024; NODE_ENV=test mocha '{node_modules/backbone-orm/,}test/{issues,spec}/**/*.tests.coffee' --grep ''", | ||
"test-rest": "npm run build && mocha --require backbone-rest/test/parameters_express4 'node_modules/backbone-rest/test/spec/**/*.tests.coffee' --grep ''", | ||
"build": "coffee -o lib -c src", | ||
@@ -25,5 +28,6 @@ "watch": "coffee -o lib -w src", | ||
"coffee-script": "*", | ||
"body-parser": "1.x", | ||
"mocha": "*", | ||
"powerset": "0.0.x", | ||
"backbone-rest": "0.5.x", | ||
"chai": "*", | ||
"backbone-rest": "0.6.x", | ||
"supertest": "*", | ||
@@ -33,9 +37,7 @@ "express": "3.x" | ||
"dependencies": { | ||
"underscore": "1.x", | ||
"backbone": "1.x", | ||
"backbone-orm": "0.5.x", | ||
"moment": "2.x", | ||
"inflection": "1.x", | ||
"mongodb": "1.x" | ||
"underscore": "1.6.x", | ||
"backbone": "1.1.x", | ||
"backbone-orm": "0.6.x", | ||
"mongodb": "1.4.x" | ||
} | ||
} |
@@ -0,1 +1,3 @@ | ||
[![Build Status](https://travis-ci.org/vidigami/backbone-mongo.svg?branch=develop)](https://travis-ci.org/vidigami/backbone-mongo#master) | ||
![logo](https://github.com/vidigami/backbone-mongo/raw/master/media/logo.png) | ||
@@ -73,2 +75,2 @@ | ||
$ npm test | ||
``` | ||
``` |
Please refer to the following release notes when upgrading your version of BackboneORM. | ||
### 0.6.0 | ||
* Upgraded to BackboneORM 0.6.x | ||
* See [upgrade notes](https://github.com/vidigami/backbone-mongo/blob/master/UPGRADING.md) for upgrading pointers from 0.5.x | ||
* BREAKING: moved connection_options to a configure function | ||
### 0.5.9 | ||
@@ -4,0 +9,0 @@ * Added database indexing through database tools (Model.db().ensureSchema(callback) or Model.db().resetSchema(callback)) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
104053
4
35
912
76
7
+ Addedbackbone@1.1.2(transitive)
+ Addedbackbone-orm@0.6.5(transitive)
+ Addedunderscore@1.6.0(transitive)
- Removedinflection@1.x
- Removedmoment@2.x
- Removedbackbone@1.6.0(transitive)
- Removedbackbone-orm@0.5.18(transitive)
- Removedinflection@1.13.4(transitive)
- Removedlru-cache@11.0.2(transitive)
- Removedmoment@2.30.1(transitive)
- Removedunderscore@1.13.7(transitive)
Updatedbackbone@1.1.x
Updatedbackbone-orm@0.6.x
Updatedmongodb@1.4.x
Updatedunderscore@1.6.x