micromodel
Version:
Model that can be used on both Client & Server
246 lines (231 loc) • 6.69 kB
JavaScript
// Generated by CoffeeScript 1.8.0
(function() {
var Collection, Cursor, Db, MongoClient, inspect, sync, syncCursor, _, _ref;
sync = require('synchronize');
_ = require('underscore');
_ref = module.exports = require('mongodb'), MongoClient = _ref.MongoClient, Db = _ref.Db, Collection = _ref.Collection, Cursor = _ref.Cursor;
Db.prototype.clear = function(callback) {
if (!callback) {
throw new Error("callback required!");
}
return this.collectionNames((function(_this) {
return function(err, names) {
var counter, dropNext;
if (err) {
return callback(err);
}
names = _(names).collect(function(obj) {
return obj.name.replace(/^[^\.]+\./, '');
});
names = _(names).select(function(name) {
return !/^system\./.test(name);
});
counter = 0;
dropNext = function() {
var name;
if (counter === names.length) {
return callback();
} else {
name = names[counter];
counter += 1;
return _this.collection(name, function(err, collection) {
if (err) {
return callback(err);
}
return collection.drop(function(err) {
if (err) {
return callback(err);
}
return dropNext();
});
});
}
};
return dropNext();
};
})(this));
};
sync(MongoClient, 'connect');
sync(Db.prototype, 'collection', 'clear', 'eval');
sync(Collection.prototype, 'insert', 'findOne', 'count', 'remove', 'update', 'ensureIndex', 'indexes', 'drop', 'aggregate', 'findAndModify');
syncCursor = function(obj) {
return sync(obj, 'toArray', 'count', 'nextObject');
};
syncCursor(Cursor.prototype);
Collection.prototype.findWithoutSynchronizedCursor = Collection.prototype.find;
Collection.prototype.find = function() {
var cursor;
cursor = this.findWithoutSynchronizedCursor.apply(this, arguments);
syncCursor(cursor);
return cursor;
};
module.exports.ModelPersistence = function(Model) {
var handleSomeErrors;
handleSomeErrors = function(model, fn) {
var err, _base, _ref1;
try {
fn();
return true;
} catch (_error) {
err = _error;
if ((_ref1 = err.code) === 11000 || _ref1 === 11001) {
((_base = model.errors).base || (_base.base = [])).push('not unique');
return false;
} else {
throw err;
}
}
};
Model.db = function(db) {
if (db) {
return this._db = db;
} else {
return this._collection || (function() {
throw new Error("db for '" + this + "' not specified!");
}).call(this);
}
};
Model.prototype.db = function() {
return this.constructor.db();
};
Model.collection = function(name) {
if (name) {
return this._collection = name;
} else {
return this.db().collection(this._collection || (function() {
throw new Error("collection name for '" + this + "' not specified!");
}).call(this));
}
};
Model.prototype.collection = function() {
return this.constructor.collection();
};
Model.first = function(selector, options) {
var data;
if (selector == null) {
selector = {};
}
if (options == null) {
options = {};
}
data = this.collection().findOne(selector, options);
if (data) {
return new this(data);
} else {
return null;
}
};
Model.firstRequired = function(selector, options) {
if (selector == null) {
selector = {};
}
if (options == null) {
options = {};
}
return this.first(selector, options) || ((function() {
throw new Error("no '" + this.name + "' for '" + selector + "' query!");
}).call(this));
};
Model.exist = function(selector) {
if (selector == null) {
selector = {};
}
return !!this.first(selector, {
_id: 1
});
};
Model.all = function(selector, options) {
if (selector == null) {
selector = {};
}
if (options == null) {
options = {};
}
return this.collection().find(selector, options).toArray().map((function(_this) {
return function(o) {
return new _this(o);
};
})(this));
};
Model.find = function(selector, options) {
if (selector == null) {
selector = {};
}
if (options == null) {
options = {};
}
return this.collection().find(selector, options);
};
Model.count = function(selector, options) {
if (selector == null) {
selector = {};
}
if (options == null) {
options = {};
}
return this.collection().count(selector, options);
};
Model.build = function(attrs) {
return new this(attrs);
};
Model.create = function(attrs, options) {
var model;
model = this.build(attrs);
model.create(options) || (function() {
throw new Error("can't save!");
})();
return model;
};
Model.prototype.create = function(options) {
if (options == null) {
options = {};
}
if (!this.isValid()) {
return false;
}
return handleSomeErrors(this, (function(_this) {
return function() {
return _this.collection().insert(_this.toJson(), options);
};
})(this));
};
Model.prototype.update = function(options) {
var originalId;
if (options == null) {
options = {};
}
options = _(options).clone();
originalId = options.originalId;
delete options.originalId;
if (!this.isValid()) {
return false;
}
return handleSomeErrors(this, (function(_this) {
return function() {
return _this.collection().update({
id: originalId || _this.id
}, _this.toJson(), options);
};
})(this));
};
Model.prototype["delete"] = function(options) {
if (options == null) {
options = {};
}
return this.collection().remove({
id: this.id
}, options);
};
return Model.prototype.refresh = function(options) {
if (options == null) {
options = {};
}
return this.set(this.collection().findOne({
id: this.id
}, options));
};
};
inspect = function(obj) {
return JSON.stringify(obj);
};
}).call(this);