loopback-datasource-juggler/lib/connectors/cradle.js

344 lines
7.8 KiB
JavaScript
Raw Normal View History

2012-06-10 11:41:07 +00:00
var safeRequire = require('../utils').safeRequire;
/**
* Module dependencies
*/
var cradle = safeRequire('cradle');
/**
* Private functions for internal use
*/
function CradleAdapter(client) {
2014-01-24 17:09:53 +00:00
this._models = {};
this.client = client;
2012-06-10 11:41:07 +00:00
}
function createdbif(client, callback) {
2014-01-24 17:09:53 +00:00
client.exists(function (err, exists) {
if (err) callback(err);
if (!exists) {
client.create(function () {
callback();
});
}
else {
callback();
}
});
2012-06-10 11:41:07 +00:00
}
function naturalize(data, model) {
2014-01-24 17:09:53 +00:00
data.nature = model;
//TODO: maybe this is not a really good idea
if (data.date) data.date = data.date.toString();
return data;
2012-06-10 11:41:07 +00:00
}
function idealize(data) {
2014-01-24 17:09:53 +00:00
data.id = data._id;
return data;
2012-06-10 11:41:07 +00:00
}
function stringify(data) {
2014-01-24 17:09:53 +00:00
return data ? data.toString() : data
2012-06-10 11:41:07 +00:00
}
function errorHandler(callback, func) {
2014-01-24 17:09:53 +00:00
return function (err, res) {
if (err) {
console.log('cradle', err);
callback(err);
} else {
if (func) {
func(res, function (res) {
callback(null, res);
});
} else {
callback(null, res);
}
2012-09-26 05:18:43 +00:00
}
2014-01-24 17:09:53 +00:00
}
2012-06-10 11:41:07 +00:00
};
function synchronize(functions, args, callback) {
2014-01-24 17:09:53 +00:00
if (functions.length === 0) callback();
if (functions.length > 0 && args.length === functions.length) {
functions[0](args[0][0], args[0][1], function (err, res) {
if (err) callback(err);
functions.splice(0, 1);
args.splice(0, 1);
synchronize(functions, args, callback);
});
}
2012-06-10 11:41:07 +00:00
};
function applyFilter(filter) {
2014-01-24 17:09:53 +00:00
if (typeof filter.where === 'function') {
return filter.where;
}
var keys = Object.keys(filter.where);
return function (obj) {
var pass = true;
keys.forEach(function (key) {
if (!test(filter.where[key], obj[key])) {
pass = false;
}
});
return pass;
}
2012-06-10 11:41:07 +00:00
2014-01-24 17:09:53 +00:00
function test(example, value) {
if (typeof value === 'string' && example && example.constructor.name === 'RegExp') {
return value.match(example);
2012-06-10 11:41:07 +00:00
}
2014-01-24 17:09:53 +00:00
// not strict equality
return example == value;
}
2012-06-10 11:41:07 +00:00
}
function numerically(a, b) {
2014-01-24 17:09:53 +00:00
return a[this[0]] - b[this[0]];
2012-06-10 11:41:07 +00:00
}
function literally(a, b) {
2014-01-24 17:09:53 +00:00
return a[this[0]] > b[this[0]];
2012-06-10 11:41:07 +00:00
}
function filtering(res, model, filter, instance) {
2014-01-24 17:09:53 +00:00
if (model) {
if (filter == null) filter = {};
if (filter.where == null) filter.where = {};
filter.where.nature = model;
}
// do we need some filtration?
if (filter.where) {
res = res ? res.filter(applyFilter(filter)) : res;
}
// do we need some sorting?
if (filter.order) {
var props = instance[model].properties;
var allNumeric = true;
var orders = filter.order;
var reverse = false;
if (typeof filter.order === "string") {
orders = [filter.order];
}
2012-06-10 11:41:07 +00:00
2014-01-24 17:09:53 +00:00
orders.forEach(function (key, i) {
var m = key.match(/\s+(A|DE)SC$/i);
if (m) {
key = key.replace(/\s+(A|DE)SC/i, '');
if (m[1] === 'DE') reverse = true;
2012-06-10 11:41:07 +00:00
}
2014-01-24 17:09:53 +00:00
orders[i] = key;
if (props[key].type.name !== 'Number') {
allNumeric = false;
}
});
if (allNumeric) {
res = res.sort(numerically.bind(orders));
} else {
res = res.sort(literally.bind(orders));
}
if (reverse) res = res.reverse();
}
return res;
2012-06-10 11:41:07 +00:00
}
/**
* Connection/Disconnection
*/
2014-01-24 17:09:53 +00:00
exports.initialize = function (dataSource, callback) {
if (!cradle) return;
// when using cradle if we dont wait for the dataSource to be connected, the models fails to load correctly.
dataSource.waitForConnect = true;
if (!dataSource.settings.url) {
var host = dataSource.settings.host || 'localhost';
var port = dataSource.settings.port || '5984';
var options = dataSource.settings.options || {
cache: true,
raw: false
};
if (dataSource.settings.username) {
options.auth = {};
options.auth.username = dataSource.settings.username;
if (dataSource.settings.password) {
options.auth.password = dataSource.settings.password;
}
2012-06-10 11:41:07 +00:00
}
2014-01-24 17:09:53 +00:00
var database = dataSource.settings.database || 'loopback-datasource-juggler';
dataSource.settings.host = host;
dataSource.settings.port = port;
dataSource.settings.database = database;
dataSource.settings.options = options;
}
dataSource.client = new (cradle.Connection)(dataSource.settings.host, dataSource.settings.port, dataSource.settings.options).database(dataSource.settings.database);
createdbif(
dataSource.client,
errorHandler(callback, function () {
dataSource.connector = new CradleAdapter(dataSource.client);
process.nextTick(callback);
}));
2012-06-10 11:41:07 +00:00
};
2014-01-24 17:09:53 +00:00
CradleAdapter.prototype.disconnect = function () {
2012-06-10 11:41:07 +00:00
};
/**
* Write methods
*/
2014-01-24 17:09:53 +00:00
CradleAdapter.prototype.define = function (descr) {
this._models[descr.model.modelName] = descr;
2012-06-10 11:41:07 +00:00
};
2014-01-24 17:09:53 +00:00
CradleAdapter.prototype.create = function (model, data, callback) {
this.client.save(
stringify(data.id),
naturalize(data, model),
errorHandler(callback, function (res, cb) {
cb(res.id);
})
);
2012-06-10 11:41:07 +00:00
};
2014-01-24 17:09:53 +00:00
CradleAdapter.prototype.save = function (model, data, callback) {
this.client.save(
stringify(data.id),
naturalize(data, model),
errorHandler(callback)
)
2012-06-10 11:41:07 +00:00
};
2014-01-24 17:09:53 +00:00
CradleAdapter.prototype.updateAttributes = function (model, id, data, callback) {
this.client.merge(
stringify(id),
data,
errorHandler(callback, function (doc, cb) {
cb(idealize(doc));
})
);
2012-06-10 11:41:07 +00:00
};
2014-01-24 17:09:53 +00:00
CradleAdapter.prototype.updateOrCreate = function (model, data, callback) {
this.client.get(
stringify(data.id),
function (err, doc) {
if (err) {
this.create(model, data, callback);
} else {
this.updateAttributes(model, data.id, data, callback);
}
}.bind(this)
)
2012-06-10 11:41:07 +00:00
};
/**
* Read methods
*/
2014-01-24 17:09:53 +00:00
CradleAdapter.prototype.exists = function (model, id, callback) {
this.client.get(
stringify(id),
errorHandler(callback, function (doc, cb) {
cb(!!doc);
})
);
2012-06-10 11:41:07 +00:00
};
2014-01-24 17:09:53 +00:00
CradleAdapter.prototype.find = function (model, id, callback) {
this.client.get(
stringify(id),
errorHandler(callback, function (doc, cb) {
cb(idealize(doc));
})
);
2012-06-10 11:41:07 +00:00
};
2014-01-24 17:09:53 +00:00
CradleAdapter.prototype.count = function (model, callback, where) {
this.models(
model,
{where: where},
callback,
function (docs, cb) {
cb(docs.length);
}
);
2012-06-10 11:41:07 +00:00
};
2014-01-24 17:09:53 +00:00
CradleAdapter.prototype.models = function (model, filter, callback, func) {
var limit = 200;
var skip = 0;
if (filter != null) {
limit = filter.limit || limit;
skip = filter.skip || skip;
}
var self = this;
self.client.save('_design/' + model, {
views: {
all: {
map: 'function(doc) { if (doc.nature == "' + model + '") { emit(doc._id, doc); } }'
}
}
2014-01-24 17:09:53 +00:00
}, function () {
self.client.view(model + '/all', {include_docs: true, limit: limit, skip: skip},
errorHandler(callback, function (res, cb) {
var docs = res.map(function (doc) {
return idealize(doc);
});
var filtered = filtering(docs, model, filter, this._models)
2014-01-24 17:09:53 +00:00
func ? func(filtered, cb) : cb(filtered);
}.bind(self)));
});
2012-06-10 11:41:07 +00:00
};
2014-01-24 17:09:53 +00:00
CradleAdapter.prototype.all = function (model, filter, callback) {
this.models(
model,
filter,
callback
);
2012-06-10 11:41:07 +00:00
};
/**
* Detroy methods
*/
2014-01-24 17:09:53 +00:00
CradleAdapter.prototype.destroy = function (model, id, callback) {
this.client.remove(
stringify(id),
function (err, doc) {
callback(err);
}
);
2012-06-10 11:41:07 +00:00
};
2014-01-24 17:09:53 +00:00
CradleAdapter.prototype.destroyAll = function (model, callback) {
this.models(
model,
null,
callback,
function (docs, cb) {
var docIds = docs.map(function (doc) {
return doc.id;
});
this.client.get(docIds, function (err, res) {
if (err) cb(err);
var funcs = res.map(function (doc) {
return this.client.remove.bind(this.client);
}.bind(this));
var args = res.map(function (doc) {
return [doc._id, doc._rev];
});
synchronize(funcs, args, cb);
}.bind(this));
}.bind(this)
);
2012-06-10 11:41:07 +00:00
};