loopback-datasource-juggler/lib/adapters/mongoose.js

233 lines
6.3 KiB
JavaScript
Raw Normal View History

var safeRequire = require('../utils').safeRequire;
2011-10-03 17:18:44 +00:00
/**
* Module dependencies
*/
var mongoose = safeRequire('mongoose');
2011-10-03 17:18:44 +00:00
exports.initialize = function initializeSchema(schema, callback) {
if (!mongoose) return;
if (!schema.settings.url) {
var url = schema.settings.host || 'localhost';
if (schema.settings.port) url += ':' + schema.settings.port;
var auth = '';
if (schema.settings.username) {
auth = schema.settings.username;
if (schema.settings.password) {
auth += ':' + schema.settings.password;
}
}
if (auth) {
url = auth + '@' + url;
}
if (schema.settings.database) {
url += '/' + schema.settings.database;
} else {
url += '/';
}
url = 'mongodb://' + url;
schema.settings.url = url;
}
if (!schema.settings.rs) {
schema.client = mongoose.connect(schema.settings.url);
} else {
schema.client = mongoose.connectSet(schema.settings.url, {rs_name: schema.settings.rs});
}
2011-10-03 17:18:44 +00:00
schema.adapter = new MongooseAdapter(schema.client);
2011-11-13 08:05:50 +00:00
process.nextTick(callback);
2011-10-03 17:18:44 +00:00
};
function MongooseAdapter(client) {
this._models = {};
this.client = client;
this.cache = {};
}
MongooseAdapter.prototype.define = function (descr) {
var props = {};
Object.keys(descr.properties).forEach(function (key) {
props[key] = descr.properties[key].type;
2012-06-02 18:33:29 +00:00
if (props[key].name === 'Text' || props[key].name === 'JSON') props[key] = String;
2011-10-03 17:18:44 +00:00
});
var schema = new mongoose.Schema(props);
this._models[descr.model.modelName] = mongoose.model(descr.model.modelName, schema);
this.cache[descr.model.modelName] = {};
};
2011-10-05 20:33:07 +00:00
MongooseAdapter.prototype.defineForeignKey = function (model, key, cb) {
var piece = {};
piece[key] = {type: mongoose.Schema.ObjectId, index: true};
this._models[model].schema.add(piece);
cb(null, String);
};
2011-10-03 17:18:44 +00:00
MongooseAdapter.prototype.setCache = function (model, instance) {
this.cache[model][instance.id] = instance;
};
MongooseAdapter.prototype.getCached = function (model, id, cb) {
if (this.cache[model][id]) {
cb(null, this.cache[model][id]);
} else {
this._models[model].findById(id, function (err, instance) {
if (err) {
return cb(err);
}
2011-10-03 17:18:44 +00:00
this.cache[model][id] = instance;
cb(null, instance);
}.bind(this));
}
};
MongooseAdapter.prototype.create = function (model, data, callback) {
var m = new this._models[model](data);
m.save(function (err) {
callback(err, err ? null : m.id);
});
};
MongooseAdapter.prototype.save = function (model, data, callback) {
this.getCached(model, data.id, function (err, inst) {
if (err) {
return callback(err);
}
2011-10-03 17:18:44 +00:00
merge(inst, data);
inst.save(callback);
});
};
MongooseAdapter.prototype.exists = function (model, id, callback) {
delete this.cache[model][id];
this.getCached(model, id, function (err, data) {
if (err) {
return callback(err);
}
2011-10-03 17:18:44 +00:00
callback(err, !!data);
});
};
MongooseAdapter.prototype.find = function find(model, id, callback) {
delete this.cache[model][id];
this.getCached(model, id, function (err, data) {
if (err) {
return callback(err);
}
2012-04-11 19:39:11 +00:00
callback(err, data ? data.toObject() : null);
2011-10-03 17:18:44 +00:00
});
};
MongooseAdapter.prototype.destroy = function destroy(model, id, callback) {
this.getCached(model, id, function (err, data) {
if (err) {
return callback(err);
}
if (data) {
data.remove(callback);
} else {
callback(null);
}
2011-10-03 17:18:44 +00:00
});
};
MongooseAdapter.prototype.all = function all(model, filter, callback) {
if (!filter) {
filter = {};
}
var query = this._models[model].find({});
if (filter.where) {
Object.keys(filter.where).forEach(function (k) {
2012-02-01 17:33:08 +00:00
var cond = filter.where[k];
var spec = false;
if (cond && cond.constructor.name === 'Object') {
spec = Object.keys(cond)[0];
cond = cond[spec];
}
if (spec) {
if (spec === 'between') {
query.where(k).gte(cond[0]).lte(cond[1]);
} else {
query.where(k)[spec](cond);
}
} else {
query.where(k, cond);
}
});
}
if (filter.order) {
var m = filter.order.match(/\s+(A|DE)SC$/);
var key = filter.order;
var reverse = false;
if (m) {
key = key.replace(/\s+(A|DE)SC$/, '');
if (m[1] === 'DE') reverse = true;
}
if (reverse) {
2012-08-14 15:12:42 +00:00
query.sort('-' + key);
} else {
2012-08-14 15:12:42 +00:00
query.sort(key);
}
}
if (filter.limit) {
query.limit(filter.limit);
}
if (filter.skip) {
query.skip(filter.skip);
2012-02-02 20:15:01 +00:00
} else if (filter.offset) {
query.skip(filter.offset);
}
query.exec(function (err, data) {
2011-10-03 17:18:44 +00:00
if (err) return callback(err);
callback(null, data);
});
};
MongooseAdapter.prototype.destroyAll = function destroyAll(model, callback) {
var wait = 0;
this._models[model].find(function (err, data) {
if (err) return callback(err);
wait = data.length;
if (!data.length) return callback(null);
2011-10-03 17:18:44 +00:00
data.forEach(function (obj) {
obj.remove(done)
});
});
var error = null;
function done(err) {
error = error || err;
if (--wait === 0) {
callback(error);
}
}
};
MongooseAdapter.prototype.count = function count(model, callback, where) {
this._models[model].count(where || {}, callback);
2011-10-03 17:18:44 +00:00
};
MongooseAdapter.prototype.updateAttributes = function updateAttrs(model, id, data, cb) {
this.getCached(model, id, function (err, inst) {
if (err) {
return cb(err);
} else if (inst) {
merge(inst, data);
inst.save(cb);
} else cb();
});
};
MongooseAdapter.prototype.disconnect = function () {
this.client.connection.close();
};
2011-10-03 17:18:44 +00:00
function merge(base, update) {
Object.keys(update).forEach(function (key) {
base[key] = update[key];
});
return base;
}