Skip to content

Commit f5aafb2

Browse files
committed
Stable Version 2.0.1
1 parent 971cf70 commit f5aafb2

File tree

6 files changed

+126
-27
lines changed

6 files changed

+126
-27
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
##### 2.0.1 - 03 July 2015
2+
3+
###### Backwards compatible bug fixes
4+
- #8 - update/updateAll fail if there are no changes
5+
16
##### 2.0.0 - 02 July 2015
27

38
Stable Version 2.0.0

dist/js-data-rethinkdb.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ module.exports =
4545
/* 0 */
4646
/***/ function(module, exports, __webpack_require__) {
4747

48-
Object.defineProperty(exports, '__esModule', {
49-
value: true
50-
});
51-
5248
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
5349

5450
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
@@ -449,7 +445,11 @@ module.exports =
449445
return this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(function () {
450446
return _this6.r.db(options.db || _this6.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).get(id).update(attrs, { returnChanges: true }).run();
451447
}).then(function (cursor) {
452-
return cursor.changes[0].new_val;
448+
if (cursor.changes && cursor.changes.length && cursor.changes[0].new_val) {
449+
return cursor.changes[0].new_val;
450+
} else {
451+
return _this6.selectTable(resourceConfig, options).get(id).run();
452+
}
453453
});
454454
}
455455
}, {
@@ -463,11 +463,21 @@ module.exports =
463463
return this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(function () {
464464
return _this7.filterSequence(_this7.selectTable(resourceConfig, options), params).update(attrs, { returnChanges: true }).run();
465465
}).then(function (cursor) {
466-
var items = [];
467-
cursor.changes.forEach(function (change) {
468-
return items.push(change.new_val);
469-
});
470-
return items;
466+
if (cursor && cursor.changes && cursor.changes.length) {
467+
var _ret = (function () {
468+
var items = [];
469+
cursor.changes.forEach(function (change) {
470+
return items.push(change.new_val);
471+
});
472+
return {
473+
v: items
474+
};
475+
})();
476+
477+
if (typeof _ret === 'object') return _ret.v;
478+
} else {
479+
return _this7.filterSequence(_this7.selectTable(resourceConfig, options), params).run();
480+
}
471481
});
472482
}
473483
}, {
@@ -500,8 +510,7 @@ module.exports =
500510
return DSRethinkDBAdapter;
501511
})();
502512

503-
exports['default'] = DSRethinkDBAdapter;
504-
module.exports = exports['default'];
513+
module.exports = DSRethinkDBAdapter;
505514

506515
/***/ },
507516
/* 1 */

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "js-data-rethinkdb",
33
"description": "RethinkDB adapter for js-data.",
4-
"version": "2.0.0",
4+
"version": "2.0.1",
55
"homepage": "http://www.js-data.io/docs/dsrethinkdbadapter",
66
"repository": {
77
"type": "git",
@@ -29,8 +29,8 @@
2929
],
3030
"devDependencies": {
3131
"babel-core": "5.6.15",
32-
"babel-loader": "5.2.2",
33-
"bluebird": "^2.9.30",
32+
"babel-loader": "5.3.0",
33+
"bluebird": "2.9.31",
3434
"chai": "3.0.0",
3535
"grunt": "0.4.5",
3636
"grunt-contrib-watch": "0.6.1",

src/index.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ class DSRethinkDBAdapter {
224224
relation: 'belongsTo'
225225
};
226226
} else if (def.type === 'hasMany') {
227-
merge[localField] = this.r.table(models[relationName].table || underscore(models[relationName].name)).getAll(id, { index: foreignKey }).coerceTo('ARRAY');
227+
merge[localField] = this.r.table(models[relationName].table || underscore(models[relationName].name)).getAll(id, {index: foreignKey}).coerceTo('ARRAY');
228228

229229
newModels[localField] = {
230230
modelName: relationName,
@@ -236,7 +236,7 @@ class DSRethinkDBAdapter {
236236
if (localKey) {
237237
merge[localField] = merge[localField].get(localKey);
238238
} else {
239-
merge[localField] = merge[localField].getAll(id, { index: foreignKey }).coerceTo('ARRAY');
239+
merge[localField] = merge[localField].getAll(id, {index: foreignKey}).coerceTo('ARRAY');
240240
}
241241

242242
newModels[localField] = {
@@ -318,7 +318,7 @@ class DSRethinkDBAdapter {
318318
relation: 'belongsTo'
319319
};
320320
} else if (def.type === 'hasMany') {
321-
merge[localField] = this.r.table(models[relationName].table || underscore(models[relationName].name)).getAll(id, { index: foreignKey }).coerceTo('ARRAY');
321+
merge[localField] = this.r.table(models[relationName].table || underscore(models[relationName].name)).getAll(id, {index: foreignKey}).coerceTo('ARRAY');
322322

323323
newModels[localField] = {
324324
modelName: relationName,
@@ -330,7 +330,7 @@ class DSRethinkDBAdapter {
330330
if (localKey) {
331331
merge[localField] = merge[localField].get(localKey);
332332
} else {
333-
merge[localField] = merge[localField].getAll(id, { index: foreignKey }).coerceTo('ARRAY');
333+
merge[localField] = merge[localField].getAll(id, {index: foreignKey}).coerceTo('ARRAY');
334334
}
335335

336336
newModels[localField] = {
@@ -354,17 +354,21 @@ class DSRethinkDBAdapter {
354354
attrs = removeCircular(omit(attrs, resourceConfig.relationFields || []));
355355
options = options || {};
356356
return this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(() => {
357-
return this.r.db(options.db || this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).insert(attrs, { returnChanges: true }).run();
357+
return this.r.db(options.db || this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).insert(attrs, {returnChanges: true}).run();
358358
}).then(cursor => cursor.changes[0].new_val);
359359
}
360360

361361
update(resourceConfig, id, attrs, options) {
362362
attrs = removeCircular(omit(attrs, resourceConfig.relationFields || []));
363363
options = options || {};
364364
return this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(() => {
365-
return this.r.db(options.db || this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).get(id).update(attrs, { returnChanges: true }).run();
365+
return this.r.db(options.db || this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).get(id).update(attrs, {returnChanges: true}).run();
366366
}).then(cursor => {
367-
return cursor.changes[0].new_val;
367+
if (cursor.changes && cursor.changes.length && cursor.changes[0].new_val) {
368+
return cursor.changes[0].new_val;
369+
} else {
370+
return this.selectTable(resourceConfig, options).get(id).run();
371+
}
368372
});
369373
}
370374

@@ -373,11 +377,15 @@ class DSRethinkDBAdapter {
373377
options = options || {};
374378
params = params || {};
375379
return this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(() => {
376-
return this.filterSequence(this.selectTable(resourceConfig, options), params).update(attrs, { returnChanges: true }).run();
380+
return this.filterSequence(this.selectTable(resourceConfig, options), params).update(attrs, {returnChanges: true}).run();
377381
}).then(cursor => {
378-
let items = [];
379-
cursor.changes.forEach(change => items.push(change.new_val));
380-
return items;
382+
if (cursor && cursor.changes && cursor.changes.length) {
383+
let items = [];
384+
cursor.changes.forEach(change => items.push(change.new_val));
385+
return items;
386+
} else {
387+
return this.filterSequence(this.selectTable(resourceConfig, options), params).run();
388+
}
381389
});
382390
}
383391

@@ -397,4 +405,4 @@ class DSRethinkDBAdapter {
397405
}
398406
}
399407

400-
export default DSRethinkDBAdapter;
408+
module.exports = DSRethinkDBAdapter;

test/update.spec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,42 @@ describe('DSRethinkDBAdapter#update', function () {
3737
assert.equal(err.message, 'Not Found!');
3838
});
3939
});
40+
it('should still work if there are no changes', function () {
41+
var id;
42+
return adapter.create(User, { name: 'John' })
43+
.then(function (user) {
44+
id = user.id;
45+
assert.equal(user.name, 'John');
46+
assert.isString(user.id);
47+
return adapter.find(User, user.id);
48+
})
49+
.then(function (foundUser) {
50+
assert.equal(foundUser.name, 'John');
51+
assert.isString(foundUser.id);
52+
assert.deepEqual(foundUser, { id: id, name: 'John' });
53+
return adapter.update(User, foundUser.id, foundUser);
54+
})
55+
.then(function (updatedUser) {
56+
assert.equal(updatedUser.name, 'John');
57+
assert.isString(updatedUser.id);
58+
assert.deepEqual(updatedUser, { id: id, name: 'John' });
59+
return adapter.find(User, updatedUser.id);
60+
})
61+
.then(function (foundUser) {
62+
assert.equal(foundUser.name, 'John');
63+
assert.isString(foundUser.id);
64+
assert.deepEqual(foundUser, { id: id, name: 'John' });
65+
return adapter.destroy(User, foundUser.id);
66+
})
67+
.then(function (user) {
68+
assert.isFalse(!!user);
69+
return adapter.find(User, id);
70+
})
71+
.then(function () {
72+
throw new Error('Should not have reached here!');
73+
})
74+
.catch(function (err) {
75+
assert.equal(err.message, 'Not Found!');
76+
});
77+
});
4078
});

test/updateAll.spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,43 @@ describe('DSRethinkDBAdapter#updateAll', function () {
4444
assert.isFalse(!!destroyedUser);
4545
});
4646
});
47+
it('should still work if there are no changes', function () {
48+
var id, id2;
49+
return adapter.create(User, { name: 'John', age: 20 })
50+
.then(function (user) {
51+
id = user.id;
52+
return adapter.create(User, { name: 'John', age: 30 });
53+
}).then(function (user) {
54+
id2 = user.id;
55+
return adapter.findAll(User, {
56+
name: 'John'
57+
});
58+
}).then(function (users) {
59+
users.sort(function (a, b) {
60+
return a.age - b.age;
61+
});
62+
assert.deepEqual(users, [{ id: id, name: 'John', age: 20 }, { id: id2, name: 'John', age: 30 }]);
63+
return adapter.updateAll(User, {
64+
name: 'John'
65+
}, {
66+
name: 'John'
67+
});
68+
}).then(function (users) {
69+
users.sort(function (a, b) {
70+
return a.age - b.age;
71+
});
72+
assert.deepEqual(users, [{ id: id, name: 'John', age: 20 }, { id: id2, name: 'John', age: 30 }]);
73+
return adapter.findAll(User, {
74+
name: 'John'
75+
});
76+
}).then(function (users) {
77+
users.sort(function (a, b) {
78+
return a.age - b.age;
79+
});
80+
assert.deepEqual(users, [{ id: id, name: 'John', age: 20 }, { id: id2, name: 'John', age: 30 }]);
81+
return adapter.destroyAll(User);
82+
}).then(function (destroyedUser) {
83+
assert.isFalse(!!destroyedUser);
84+
});
85+
});
4786
});

0 commit comments

Comments
 (0)