Skip to content

Commit

Permalink
Added ModelBase base class, emit actual instance data for save/update…
Browse files Browse the repository at this point in the history
… on single instances
  • Loading branch information
mde committed Oct 20, 2012
1 parent 3bee8b0 commit 0804d40
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 29 deletions.
8 changes: 7 additions & 1 deletion lib/adapters/base_adapter.js
@@ -1,5 +1,6 @@
var BaseAdapter var BaseAdapter
, EventEmitter = require('events').EventEmitter , EventEmitter = require('events').EventEmitter
, model = require('../index')
, adapter = require('./index') , adapter = require('./index')
, utils = require('utilities'); , utils = require('utilities');


Expand All @@ -14,7 +15,12 @@ utils.mixin(BaseAdapter.prototype, new (function () {
}; };


this.update = function (data, query, callback) { this.update = function (data, query, callback) {
callback(null, true); if (data instanceof model.ModelBase) {
callback(null, data);
}
else {
callback(null, true);
}
}; };


this.remove = function (query, callback) { this.remove = function (query, callback) {
Expand Down
14 changes: 12 additions & 2 deletions lib/adapters/memory/index.js
Expand Up @@ -123,7 +123,12 @@ utils.mixin(Adapter.prototype, new (function () {
_data[key][id] = item; _data[key][id] = item;


}); });
callback(null, true); if (data instanceof model.ModelBase) {
callback(null, data);
}
else {
callback(null, true);
}
}; };


this.remove = function (query, callback) { this.remove = function (query, callback) {
Expand Down Expand Up @@ -165,7 +170,12 @@ utils.mixin(Adapter.prototype, new (function () {
item._saved = true; item._saved = true;
_data[key][id] = item; _data[key][id] = item;
}); });
callback(null, true); if (data instanceof model.ModelBase) {
callback(null, data);
}
else {
callback(null, true);
}
} }


this.createTable = function (names, callback) { this.createTable = function (names, callback) {
Expand Down
21 changes: 13 additions & 8 deletions lib/adapters/mongo/index.js
Expand Up @@ -232,15 +232,17 @@ utils.mixin(Adapter.prototype, new (function () {


item = item.toData({whitelist: ['_id', 'id', 'createdAt']}); item = item.toData({whitelist: ['_id', 'id', 'createdAt']});


collection.update({id: id}, item, function (err, data) { collection.update({id: id}, item, function (err, res) {
if (err) { if (err) {
callback(err, null); callback(err, null);
} }
else { else {
// FIXME: What is the right data to return here? Right now this if (data instanceof model.ModelBase) {
// is basically overwriting a doc, but we might be supporting callback(null, data);
// bulk-updates at some point }
callback(null, true); else {
callback(null, true);
}
} }
}); });
} }
Expand Down Expand Up @@ -281,7 +283,6 @@ utils.mixin(Adapter.prototype, new (function () {
, items = Array.isArray(data) ? data.slice() : [data] , items = Array.isArray(data) ? data.slice() : [data]
, collectionName = _collectionizeModelName(items[0].type) , collectionName = _collectionizeModelName(items[0].type)
, collection = this.client.collection(collectionName) , collection = this.client.collection(collectionName)
, ret = []
, insert; , insert;


insert = function () { insert = function () {
Expand All @@ -300,13 +301,17 @@ utils.mixin(Adapter.prototype, new (function () {
item.id = id; item.id = id;
item._id = res._id; item._id = res._id;
item._saved = true; item._saved = true;
ret.push(data);
insert(); insert();
} }
}); });
} }
else { else {
callback(null, ret); if (data instanceof model.ModelBase) {
callback(null, data);
}
else {
callback(null, true);
}
} }
}; };
insert(); insert();
Expand Down
21 changes: 13 additions & 8 deletions lib/adapters/riak/index.js
Expand Up @@ -211,15 +211,17 @@ utils.mixin(Adapter.prototype, new (function () {
, data: item , data: item
}; };


this.request(requestOpts, function (err, data) { this.request(requestOpts, function (err, res) {
if (err) { if (err) {
callback(err, null); callback(err, null);
} }
else { else {
// FIXME: What is the right data to return here? Right now this if (data instanceof model.ModelBase) {
// is basically overwriting a doc, but we might be supporting callback(null, data);
// bulk-updates at some point }
callback(null, true); else {
callback(null, true);
}
} }
}); });
} }
Expand Down Expand Up @@ -305,7 +307,6 @@ utils.mixin(Adapter.prototype, new (function () {
var self = this var self = this
, items = Array.isArray(data) ? data.slice() : [data] , items = Array.isArray(data) ? data.slice() : [data]
, bucket = _bucketizeModelName(items[0].type) , bucket = _bucketizeModelName(items[0].type)
, ret = []
, insert; , insert;


insert = function () { insert = function () {
Expand All @@ -331,13 +332,17 @@ utils.mixin(Adapter.prototype, new (function () {
else { else {
item.id = id; item.id = id;
item._saved = true; item._saved = true;
ret.push(data);
insert(); insert();
} }
}); });
} }
else { else {
callback(null, ret); if (data instanceof model.ModelBase) {
callback(null, data);
}
else {
callback(null, true);
}
} }
}; };
insert(); insert();
Expand Down
19 changes: 13 additions & 6 deletions lib/adapters/sql/postgres.js
Expand Up @@ -144,10 +144,12 @@ utils.mixin(Adapter.prototype, new (function () {
callback(err, null); callback(err, null);
} }
else { else {
// FIXME: What is the right data to return here? SQL updates if (data instanceof model.ModelBase) {
// can affect lots of rows, and I don't think we want to limit callback(null, data);
// it to single-item updates }
callback(null, true); else {
callback(null, true);
}
} }
}); });
}; };
Expand Down Expand Up @@ -192,10 +194,15 @@ utils.mixin(Adapter.prototype, new (function () {
rows = res.rows; rows = res.rows;
for (var i = 0, ii = items.length; i < ii; i++) { for (var i = 0, ii = items.length; i < ii; i++) {
item = items[i]; item = items[i];
item.id = rows[i].id; item.id = rows[i].id; // Set the id from the row
item._saved = true; item._saved = true;
} }
callback(null, items.length == 1 ? items[0] : items); if (data instanceof model.ModelBase) {
callback(null, data);
}
else {
callback(null, true);
}
} }
}); });
}; };
Expand Down
7 changes: 5 additions & 2 deletions lib/index.js
Expand Up @@ -49,6 +49,7 @@ var model = {}


utils.mixin(model, new (function () { utils.mixin(model, new (function () {


this.ModelBase = function () {};
this.adapters = {}; this.adapters = {};
this.loadedAdapters = {}; this.loadedAdapters = {};
this.descriptionRegistry = {}; this.descriptionRegistry = {};
Expand Down Expand Up @@ -451,7 +452,7 @@ utils.mixin(model, new (function () {
} }


// Data may by just a bag or params, or an actual instance // Data may by just a bag or params, or an actual instance
if (typeof data.emit == 'function') { if (data instanceof model.ModelBase) {
data.emit('beforeUpdate'); data.emit('beforeUpdate');
} }


Expand Down Expand Up @@ -546,9 +547,11 @@ utils.mixin(model, new (function () {
// actual constructor // actual constructor
utils.mixin(origProto, defined); utils.mixin(origProto, defined);


ModelCtor.prototype = origProto; ModelCtor.prototype = new model.ModelBase();
// Add eventing to instances // Add eventing to instances
utils.enhance(ModelCtor.prototype, new EventEmitter()); utils.enhance(ModelCtor.prototype, new EventEmitter());
// Preserve any inherited shit from the definition proto
utils.enhance(ModelCtor.prototype, origProto);


model[name] = ModelCtor; model[name] = ModelCtor;


Expand Down
4 changes: 2 additions & 2 deletions test/events.js
Expand Up @@ -161,9 +161,9 @@ tests = {
}); });
} }


, 'emit static update': function (next) { , 'emit static update for single instance': function (next) {
User.once('update', function (res) { User.once('update', function (res) {
assert.ok(res); assert.ok(res instanceof User);
next(); next();
}); });
var user = User.create(_params); var user = User.create(_params);
Expand Down

0 comments on commit 0804d40

Please sign in to comment.