Permalink
Browse files

Started using subdir for model-adapters.

  • Loading branch information...
1 parent eb95c15 commit e6ed1d1caaf1ba9f419f0d9b5d92d7635a9a7755 mde committed Jun 21, 2012
Showing with 121 additions and 0 deletions.
  1. +121 −0 lib/model/adapters/riak/index.js
@@ -0,0 +1,121 @@
+var path = require('path')
+ , riak = require(path.join(process.cwd(), 'node_modules/riak-js'));
+
+ //require('riak-js').getClient()
+
+var Riak = function (config) {
+ var _config = {
+ host: 'localhost'
+ , port: '8098'
+ , username: null
+ , password: null
+ };
+
+ if (!geddy.config.db.riak) {
+ throw "No Riak config defined.";
+ }
+
+ geddy.mixin(_config, config);
+
+ // Model string
+ this.model = config.model;
+
+ /*
+ this.dbName = geddy.config.db.riak.db;
+ this.port = geddy.config.db.riak.port || 27017;
+ this.host = geddy.config.db.riak.host || 'localhost';
+ this.username = geddy.config.db.riak.username || null;
+ this.password = geddy.config.db.riak.password || null;
+ */
+
+ // Connect to the DB
+ this.db = riak.getClient(_config);
+
+ geddy.mixin(this, _config);
+
+ // Calling signature can be:
+ // callback
+ // query, callback
+ // query, opts, callback
+ this.load = function () {
+ var self = this
+ , args = Array.prototype.slice.call(arguments)
+ , arg
+ , callback = args.pop()
+ , query = args.shift() || {}
+ , opts = args.shift() || {};
+
+ if (typeof query == 'object') {
+ opts.limit = 1;
+ return this.all(query, opts, callback);
+ }
+ else {
+ return this.all({id: query}, opts, callback);
+ }
+
+ };
+
+ this.all = function () {
+ var self = this
+ , args = Array.prototype.slice.call(arguments)
+ , arg
+ , callback = args.pop()
+ , query = args.shift() || {}
+ , opts = args.shift() || {}
+ , handler;
+
+ handler = function (err, res) {
+ var data;
+ if (res) {
+ if (Array.isArray(res)) {
+ data = [];
+ res.forEach(function (item) {
+ });
+ }
+ else {
+ data = geddy.model[self.model].create(res);
+ }
+ }
+ return callback(err, data);
+ };
+
+ if (query.id) {
+ this.db.get(this.model, query.id, opts, handler);
+ }
+ else {
+ this.db.getAll(this.model, {where: query}, opts, handler);
+ }
+
+ };
+
+ this.save = function (inst, options, cb) {
+ var self = this
+ , instance = inst.toObj() // Strip function-props
+ , callback = cb || function () {}
+ , handler;
+
+ handler = function (err, doc, meta) {
+ if (err) {
+ instance = null;
+ }
+ return callback(err, instance);
+ };
+
+ // Double check to see if this thing is valid
+ instance = geddy.model[this.model].create(instance);
+ if (!instance.isValid()) {
+ callback(instance.errors, null);
+ }
+ else {
+ instance.id = geddy.string.uuid();
+ instance.saved = true;
+ this.db.save(this.model, instance.id, instance, {}, handler);
+ }
+ };
+
+ this.remove = function () {};
+
+};
+
+exports.Riak = Riak;
+

0 comments on commit e6ed1d1

Please sign in to comment.