graph-common
Version:
Open Graph API core js lib
102 lines (87 loc) • 3.18 kB
JavaScript
(function() {
var MongoDBStorage,
__slice = [].slice;
MongoDBStorage = (function() {
function MongoDBStorage(database_uri) {
this.mongoose = require('mongoose');
this.plugins = {
uniqueValidator: require('mongoose-unique-validator')
};
this.Schema = this.mongoose.Schema;
this.db = this.mongoose.connect(database_uri);
this.connection = this.db.connection;
}
MongoDBStorage.prototype.apply_plugins = function(schema) {
var plugin_name, _results;
_results = [];
for (plugin_name in this.plugins) {
_results.push(schema.plugin(this.plugins[plugin_name]));
}
return _results;
};
MongoDBStorage.prototype.create = function(query, callback) {
var model, object;
model = this.model(query.schema);
object = new model(query.data);
return object.save(callback);
};
MongoDBStorage.prototype.read = function(query, callback) {
var conditions, dbquery, model, properties, search_query, _ref;
model = this.model(query.schema);
search_query = query.search_query || {};
conditions = search_query.conditions || {};
dbquery = model.find(conditions);
properties = ((_ref = search_query.property) != null ? _ref.join(' ') : void 0) || void 0;
if (properties) {
dbquery = dbquery.select(properties);
}
MongoDBStorage.add_uniq_search_option(dbquery, search_query, 'limit');
MongoDBStorage.add_uniq_search_option(dbquery, search_query, 'skip');
MongoDBStorage.add_multiple_search_option(dbquery, search_query, 'sort');
MongoDBStorage.add_multiple_search_option(dbquery, search_query, 'populate');
return dbquery.exec(function(err, data) {
return callback(data);
});
};
MongoDBStorage.prototype.update = function(query, callback) {
var model;
return model = this.model(query.schema);
};
MongoDBStorage.prototype["delete"] = function(query, callback) {};
MongoDBStorage.prototype.model = function() {
var args, _ref;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return (_ref = this.mongoose).model.apply(_ref, args);
};
MongoDBStorage.prototype.disconnect = function() {
return this.connection.close();
};
MongoDBStorage.add_uniq_search_option = function(dbquery, search_query, option) {
var value;
value = search_query[option];
if (value && typeof value === 'array') {
value = value.pop();
}
if (value) {
dbquery = dbquery[option](value);
}
return dbquery;
};
MongoDBStorage.add_multiple_search_option = function(dbquery, search_query, option) {
var value, values, _i, _len;
values = search_query[option];
if (values && typeof values === 'string') {
values = [values];
}
if (values) {
for (_i = 0, _len = values.length; _i < _len; _i++) {
value = values[_i];
dbquery = dbquery[option](value);
}
}
return dbquery;
};
return MongoDBStorage;
})();
module.exports = MongoDBStorage;
}).call(this);