Permalink
Browse files

Make sure saved Riak and Mongo instances have id and _saved props

  • Loading branch information...
1 parent 60ef129 commit 05dedd19ce7fdfe6190fc84fd617c18dde78056b mde committed Nov 10, 2012
Showing with 14 additions and 15 deletions.
  1. +5 −9 lib/adapters/mongo/index.js
  2. +8 −5 lib/adapters/riak/index.js
  3. +1 −1 lib/index.js
@@ -99,10 +99,6 @@ utils.mixin(Adapter.prototype, new (function () {
, re
, val = comp.value;
- //if (comp.datatype == 'date' || comp.datetime == 'datetime') {
- // val = JSON.stringify(val).replace(/"/g, '');
- //}
-
switch (true) {
case comp instanceof comparison.EqualToComparison:
// Case-insensitive equality via regex
@@ -278,12 +274,13 @@ utils.mixin(Adapter.prototype, new (function () {
, items = Array.isArray(data) ? data.slice() : [data]
, collectionName = _collectionizeModelName(items[0].type)
, collection = this.client.collection(collectionName)
- , insert;
+ , insert
+ , id;
insert = function () {
var item;
if ((item = items.shift())) {
- var id = utils.string.uuid()
+ id = utils.string.uuid();
item.id = id;
item = item.toData({whitelist: ['id', 'createdAt']});
@@ -293,15 +290,14 @@ utils.mixin(Adapter.prototype, new (function () {
callback(err, null);
}
else {
- item.id = id;
- item._id = res._id;
- item._saved = true;
insert();
}
});
}
else {
if (data instanceof model.ModelBase) {
+ data.id = id; // Single instance, can use last id generated above
+ data._saved = true;
callback(null, data);
}
else {
@@ -302,13 +302,16 @@ utils.mixin(Adapter.prototype, new (function () {
var self = this
, items = Array.isArray(data) ? data.slice() : [data]
, bucket = _bucketizeModelName(items[0].type)
- , insert;
+ , insert
+ , id;
insert = function () {
var item;
if ((item = items.shift())) {
- var id = utils.string.uuid()
- , url = '/riak/' + bucket + '/' + id
+ var url;
+
+ id = utils.string.uuid();
+ url = '/riak/' + bucket + '/' + id
, requestOpts;
item.id = id;
@@ -325,14 +328,14 @@ utils.mixin(Adapter.prototype, new (function () {
callback(err, null);
}
else {
- item.id = id;
- item._saved = true;
insert();
}
});
}
else {
if (data instanceof model.ModelBase) {
+ data.id = id; // Single instance, can use last id generated above
+ data._saved = true;
callback(null, data);
}
else {
View
@@ -252,7 +252,7 @@ utils.mixin(model, new (function () {
else {
if (!(this._saved && this.id)) {
throw new Error('Item cannot have a hasOne/hasMany association ' +
- 'if it is not yet saved..');
+ 'if it is not yet saved.');
}
data[selfKeyName + 'Id'] = this.id;
unsaved = this._unsavedAssociations || [];

0 comments on commit 05dedd1

Please sign in to comment.