Skip to content

Commit

Permalink
Stable Version 2.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed Jul 3, 2015
1 parent 971cf70 commit f5aafb2
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 27 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
##### 2.0.1 - 03 July 2015

###### Backwards compatible bug fixes
- #8 - update/updateAll fail if there are no changes

##### 2.0.0 - 02 July 2015

Stable Version 2.0.0
Expand Down
33 changes: 21 additions & 12 deletions dist/js-data-rethinkdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ module.exports =
/* 0 */
/***/ function(module, exports, __webpack_require__) {

Object.defineProperty(exports, '__esModule', {
value: true
});

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; }; })();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
Expand Down Expand Up @@ -449,7 +445,11 @@ module.exports =
return this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(function () {
return _this6.r.db(options.db || _this6.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).get(id).update(attrs, { returnChanges: true }).run();
}).then(function (cursor) {
return cursor.changes[0].new_val;
if (cursor.changes && cursor.changes.length && cursor.changes[0].new_val) {
return cursor.changes[0].new_val;
} else {
return _this6.selectTable(resourceConfig, options).get(id).run();
}
});
}
}, {
Expand All @@ -463,11 +463,21 @@ module.exports =
return this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(function () {
return _this7.filterSequence(_this7.selectTable(resourceConfig, options), params).update(attrs, { returnChanges: true }).run();
}).then(function (cursor) {
var items = [];
cursor.changes.forEach(function (change) {
return items.push(change.new_val);
});
return items;
if (cursor && cursor.changes && cursor.changes.length) {
var _ret = (function () {
var items = [];
cursor.changes.forEach(function (change) {
return items.push(change.new_val);
});
return {
v: items
};
})();

if (typeof _ret === 'object') return _ret.v;
} else {
return _this7.filterSequence(_this7.selectTable(resourceConfig, options), params).run();
}
});
}
}, {
Expand Down Expand Up @@ -500,8 +510,7 @@ module.exports =
return DSRethinkDBAdapter;
})();

exports['default'] = DSRethinkDBAdapter;
module.exports = exports['default'];
module.exports = DSRethinkDBAdapter;

/***/ },
/* 1 */
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "js-data-rethinkdb",
"description": "RethinkDB adapter for js-data.",
"version": "2.0.0",
"version": "2.0.1",
"homepage": "http://www.js-data.io/docs/dsrethinkdbadapter",
"repository": {
"type": "git",
Expand Down Expand Up @@ -29,8 +29,8 @@
],
"devDependencies": {
"babel-core": "5.6.15",
"babel-loader": "5.2.2",
"bluebird": "^2.9.30",
"babel-loader": "5.3.0",
"bluebird": "2.9.31",
"chai": "3.0.0",
"grunt": "0.4.5",
"grunt-contrib-watch": "0.6.1",
Expand Down
32 changes: 20 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class DSRethinkDBAdapter {
relation: 'belongsTo'
};
} else if (def.type === 'hasMany') {
merge[localField] = this.r.table(models[relationName].table || underscore(models[relationName].name)).getAll(id, { index: foreignKey }).coerceTo('ARRAY');
merge[localField] = this.r.table(models[relationName].table || underscore(models[relationName].name)).getAll(id, {index: foreignKey}).coerceTo('ARRAY');

newModels[localField] = {
modelName: relationName,
Expand All @@ -236,7 +236,7 @@ class DSRethinkDBAdapter {
if (localKey) {
merge[localField] = merge[localField].get(localKey);
} else {
merge[localField] = merge[localField].getAll(id, { index: foreignKey }).coerceTo('ARRAY');
merge[localField] = merge[localField].getAll(id, {index: foreignKey}).coerceTo('ARRAY');
}

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

newModels[localField] = {
modelName: relationName,
Expand All @@ -330,7 +330,7 @@ class DSRethinkDBAdapter {
if (localKey) {
merge[localField] = merge[localField].get(localKey);
} else {
merge[localField] = merge[localField].getAll(id, { index: foreignKey }).coerceTo('ARRAY');
merge[localField] = merge[localField].getAll(id, {index: foreignKey}).coerceTo('ARRAY');
}

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

update(resourceConfig, id, attrs, options) {
attrs = removeCircular(omit(attrs, resourceConfig.relationFields || []));
options = options || {};
return this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(() => {
return this.r.db(options.db || this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).get(id).update(attrs, { returnChanges: true }).run();
return this.r.db(options.db || this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).get(id).update(attrs, {returnChanges: true}).run();
}).then(cursor => {
return cursor.changes[0].new_val;
if (cursor.changes && cursor.changes.length && cursor.changes[0].new_val) {
return cursor.changes[0].new_val;
} else {
return this.selectTable(resourceConfig, options).get(id).run();
}
});
}

Expand All @@ -373,11 +377,15 @@ class DSRethinkDBAdapter {
options = options || {};
params = params || {};
return this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(() => {
return this.filterSequence(this.selectTable(resourceConfig, options), params).update(attrs, { returnChanges: true }).run();
return this.filterSequence(this.selectTable(resourceConfig, options), params).update(attrs, {returnChanges: true}).run();
}).then(cursor => {
let items = [];
cursor.changes.forEach(change => items.push(change.new_val));
return items;
if (cursor && cursor.changes && cursor.changes.length) {
let items = [];
cursor.changes.forEach(change => items.push(change.new_val));
return items;
} else {
return this.filterSequence(this.selectTable(resourceConfig, options), params).run();
}
});
}

Expand All @@ -397,4 +405,4 @@ class DSRethinkDBAdapter {
}
}

export default DSRethinkDBAdapter;
module.exports = DSRethinkDBAdapter;
38 changes: 38 additions & 0 deletions test/update.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,42 @@ describe('DSRethinkDBAdapter#update', function () {
assert.equal(err.message, 'Not Found!');
});
});
it('should still work if there are no changes', function () {
var id;
return adapter.create(User, { name: 'John' })
.then(function (user) {
id = user.id;
assert.equal(user.name, 'John');
assert.isString(user.id);
return adapter.find(User, user.id);
})
.then(function (foundUser) {
assert.equal(foundUser.name, 'John');
assert.isString(foundUser.id);
assert.deepEqual(foundUser, { id: id, name: 'John' });
return adapter.update(User, foundUser.id, foundUser);
})
.then(function (updatedUser) {
assert.equal(updatedUser.name, 'John');
assert.isString(updatedUser.id);
assert.deepEqual(updatedUser, { id: id, name: 'John' });
return adapter.find(User, updatedUser.id);
})
.then(function (foundUser) {
assert.equal(foundUser.name, 'John');
assert.isString(foundUser.id);
assert.deepEqual(foundUser, { id: id, name: 'John' });
return adapter.destroy(User, foundUser.id);
})
.then(function (user) {
assert.isFalse(!!user);
return adapter.find(User, id);
})
.then(function () {
throw new Error('Should not have reached here!');
})
.catch(function (err) {
assert.equal(err.message, 'Not Found!');
});
});
});
39 changes: 39 additions & 0 deletions test/updateAll.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,43 @@ describe('DSRethinkDBAdapter#updateAll', function () {
assert.isFalse(!!destroyedUser);
});
});
it('should still work if there are no changes', function () {
var id, id2;
return adapter.create(User, { name: 'John', age: 20 })
.then(function (user) {
id = user.id;
return adapter.create(User, { name: 'John', age: 30 });
}).then(function (user) {
id2 = user.id;
return adapter.findAll(User, {
name: 'John'
});
}).then(function (users) {
users.sort(function (a, b) {
return a.age - b.age;
});
assert.deepEqual(users, [{ id: id, name: 'John', age: 20 }, { id: id2, name: 'John', age: 30 }]);
return adapter.updateAll(User, {
name: 'John'
}, {
name: 'John'
});
}).then(function (users) {
users.sort(function (a, b) {
return a.age - b.age;
});
assert.deepEqual(users, [{ id: id, name: 'John', age: 20 }, { id: id2, name: 'John', age: 30 }]);
return adapter.findAll(User, {
name: 'John'
});
}).then(function (users) {
users.sort(function (a, b) {
return a.age - b.age;
});
assert.deepEqual(users, [{ id: id, name: 'John', age: 20 }, { id: id2, name: 'John', age: 30 }]);
return adapter.destroyAll(User);
}).then(function (destroyedUser) {
assert.isFalse(!!destroyedUser);
});
});
});

1 comment on commit f5aafb2

@jmdobry
Copy link
Member Author

@jmdobry jmdobry commented on f5aafb2 Jul 3, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes #8

Please sign in to comment.