-
Notifications
You must be signed in to change notification settings - Fork 12
#10 - transaction support via knex #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#10 - transaction support via knex #32
Conversation
Sigh. I should have checked to be sure I was in sync with master before I submitted the PR. |
@techniq Can probably speak better than me on whether the |
@mechanicalpulse this looks great. I've only recently began using generators/yielded Promises so I'm not expert (and use them as a stop gap until Some feedback on your tests
describe('DSSqlAdapter#create + transaction', function () {
it('commit should persist created user in a sql db', function* () {
var id;
var co = require('co');
yield adapter.query.transaction(co.wrap(function * (trx) {
var createUser = yield adapter.create(User, {name: 'Jane'}, {transaction: trx});
id = createUser.id;
assert.equal(createUser.name, 'Jane');
assert.isDefined(createUser.id);
var findUser = yield adapter.find(User, createUser.id, {transaction: trx});
assert.equal(findUser.name, 'Jane');
assert.isDefined(findUser.id);
assert.equalObjects(findUser, {id: id, name: 'Jane', age: null, profileId: null});
return findUser;
})).then(
function (user) {
assert.isObject(user, 'transaction returned user object');
assert.equal(user.name, 'Jane');
assert.isDefined(user.id);
assert.equalObjects(user, {id: id, name: 'Jane', age: null, profileId: null});
},
function (err) {
throw new Error('transaction threw exception!');
}
);
try {
var findUser2 = yield adapter.find(User, id);
assert.isObject(findUser2, 'user committed to database');
} catch(err) {
throw new Error('transaction failed to commit!');
}
}); ... if it('commit should persist created user in a sql db', function* () {
var id;
var findUser = yield adapter.query.transaction(co.wrap(function * (trx) {
var createUser = yield adapter.create(User, {name: 'Jane'}, {transaction: trx});
id = createUser.id;
assert.equal(createUser.name, 'Jane');
assert.isDefined(createUser.id);
return adapter.find(User, createUser.id, {transaction: trx});
}));
assert.isObject(findUser, 'user committed to database');
assert.equal(findUser.name, 'Jane');
assert.isDefined(findUser.id);
assert.equalObjects(findUser, {id: id, name: 'Jane', age: null, profileId: null});
}); I believe I'm still testing everything that needs to be tested in this case (let me know if I'm wrong) but keeps things nice and concise. If you could make those changes to the tests, I think it looks good and I'll merge and cut a release. |
Regarding the assertions being swallowed up in the thenable -- I appended the fulfillment and rejection handlers in a single then() call instead of using a then().catch(), which should allow the assertions to fail as expected. Unless I'm misunderstanding. I will move the assertions outside, though, and avoid using then/catch. It does certainly make the tests more straightforward. It may also be best for the adapter.find() call to be done outside of the transaction callback, since it could potentially fetch stale data:
versus
|
If I changed say line 13 in
I'm not an expert, but I'm guessing the failed I think it makes since to move the find outside of the transaction. I wasn't sure if it was inside the transaction to test something but agree it should be moved out if not. |
Just saw your updates. Everything looks good to me. Thanks! I'll push a release out. We should update the docs with these changes if you get a chance. |
…upport #10 - transaction support via knex
@mechanicalpulse released 0.11.4 |
Ah, sorry -- I was looking at the Thanks for the feedback and quick turnaround. Looking forward to using it! |
Thanks @mechanicalpulse ! Just a note, future pull requests should be squashed into one commit. Thanks! |
Will do. |
Hi! I've added support for passing the knex transaction object through to knex as jmdobry described in this comment on issue #10. It appears to work pretty well.
I added co to the development dependencies because I couldn't figure out how to reconcile co-mocha's use of generators and knex's use of the transaction callback. I'm new to modern JavaScript -- particularly node.js and ES6 -- and I don't quite feel as though I know when best to use promises versus generators versus try/catch, so there may be a better way to implement my tests.
Thank you for js-data and everyone's assistance on Gitter. Fantastic community.