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

111 lines
2.9 KiB
JavaScript
Raw Normal View History

2012-03-11 04:48:38 +00:00
var safeRequire = require('../utils').safeRequire;
2011-10-01 15:51:51 +00:00
/**
* Module dependencies
*/
2012-03-11 04:48:38 +00:00
var uuid = require('node-uuid');
var riak = safeRequire('riak-js');
2011-10-01 15:51:51 +00:00
exports.initialize = function initializeSchema(schema, callback) {
2012-03-11 04:48:38 +00:00
schema.client = riak.getClient({
host: schema.settings.host || '127.0.0.1',
port: schema.settings.port || 8091
});
schema.adapter = new Riak(schema.client);
2011-10-01 15:51:51 +00:00
};
2012-03-11 04:48:38 +00:00
function Riak(client) {
2011-10-01 15:51:51 +00:00
this._models = {};
this.client = client;
}
2012-03-11 04:48:38 +00:00
Riak.prototype.define = function (descr) {
2011-10-08 17:11:26 +00:00
this._models[descr.model.modelName] = descr;
2011-10-01 15:51:51 +00:00
};
2012-03-11 04:48:38 +00:00
Riak.prototype.save = function (model, data, callback) {
this.client.save(model, data.id, data, callback);
2011-10-01 15:51:51 +00:00
};
2012-03-11 04:48:38 +00:00
Riak.prototype.create = function (model, data, callback) {
data.id = uuid();
this.save(model, data, function (err) {
if (callback) {
callback(err, data.id);
}
});
2011-10-01 15:51:51 +00:00
};
2012-03-11 04:48:38 +00:00
Riak.prototype.exists = function (model, id, callback) {
this.client.exists(model, id, function (err, exists, meta) {
2011-10-01 15:51:51 +00:00
if (callback) {
callback(err, exists);
}
});
};
2012-03-11 04:48:38 +00:00
Riak.prototype.find = function find(model, id, callback) {
this.client.get(model, id, function (err, data, meta) {
2011-10-01 15:51:51 +00:00
if (data && data.id) {
data.id = id;
} else {
data = null;
}
2012-03-11 04:48:38 +00:00
if (typeof callback === 'function') callback(err, data);
2011-10-01 15:51:51 +00:00
});
};
2012-03-11 04:48:38 +00:00
Riak.prototype.destroy = function destroy(model, id, callback) {
this.client.remove(model, id, function (err) {
2011-10-01 15:51:51 +00:00
callback(err);
});
};
2012-03-11 04:48:38 +00:00
Riak.prototype.all = function all(model, filter, callback) {
var opts = {};
if (filter && filter.where) opts.where = filter.where;
this.client.getAll(model, function (err, result, meta) {
if (err) return callback(err, []);
/// return callback(err, result.map(function (x) { return {id: x}; }));
result = (result || []).map(function (row) {
var record = row.data;
record.id = row.meta.key;
console.log(record);
return record;
2011-10-01 15:51:51 +00:00
});
2012-03-11 04:48:38 +00:00
return callback(err, result);
2011-10-08 17:11:26 +00:00
}.bind(this));
2011-10-01 15:51:51 +00:00
};
2012-03-11 04:48:38 +00:00
Riak.prototype.destroyAll = function destroyAll(model, callback) {
var self = this;
this.all(model, {}, function (err, recs) {
if (err) callback(err);
2011-10-01 15:51:51 +00:00
2012-03-11 04:48:38 +00:00
removeOne();
2011-10-01 15:51:51 +00:00
2012-03-11 04:48:38 +00:00
function removeOne(error) {
err = err || error;
var rec = recs.pop();
if (!rec) return callback(err && err.statusCode != '404' ? err : null);
console.log(rec.id);
self.client.remove(model, rec.id, removeOne);
2011-10-01 15:51:51 +00:00
}
2012-03-11 04:48:38 +00:00
});
2011-10-01 15:51:51 +00:00
};
2012-03-11 04:48:38 +00:00
Riak.prototype.count = function count(model, callback) {
2011-10-01 15:51:51 +00:00
this.client.keys(model + ':*', function (err, keys) {
callback(err, err ? null : keys.length);
});
};
2012-03-11 04:48:38 +00:00
Riak.prototype.updateAttributes = function updateAttrs(model, id, data, cb) {
data.id = id;
this.save(model, data, cb);
2011-10-01 15:51:51 +00:00
};