-
Notifications
You must be signed in to change notification settings - Fork 74
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
Transactions support #188
Comments
Some notes: 1. patch method https://github.com/feathersjs-ecosystem/feathers-sequelize/blob/master/lib/index.js#L180 getModel method accept params but update method accept constructed options which use params.sequelize So for right use transaction I need pass params like 2.events |
It also worth mentioning, that update method doesn't even support transactions, or any sequelize parameters. https://github.com/feathersjs-ecosystem/feathers-sequelize/blob/master/lib/index.js#L256 |
For the events i created these hooks:
Use something like this as a patch before hook:
You have to use the stackTransactionEvents as the last after hook on your services which are called in your transaction. With returning SKIP the automatic event dispatching is disabled. You have to use the unstackTransactionEvents as the last after hook on your service which creating the transaction. Please keep inn mind, that I'm not a feathers expert. Maybe the code has some flaws, or design misconceptions. It would be nice, if someone from the core team could take a look at it, and confirm this is a right approach. @daffl What do you think? |
Good catch on not wanting events to be emitted if the transaction is aborted. I don't think the team will say this is a good approach as it's a hack. I think it best if a hook.skipEvent prop is supported that the eventHook function, as a 'finally' hook called after 'after' hooks, checks whether to emit an event. https://github.com/feathersjs/feathers/blob/master/lib/events.js#L6
I did a quick and dirty PR: |
Are there any guides on how to use transactions? |
Feathers specific: Pass 'sequelize' as a param to your service calls, with a nested 'transaction'. Read up on transactions in Sequelize for everything else to do with transaction lifetime.
|
Thank you! Would you be interested in some documentation regarding transactions? |
Just noticed the comments about update() not passing the transaction. params.sequelize is not consistently passed to all sequelize calls, I'll do a PR. |
My intention is to make multiple service call on before create hook. myService.hooks.js:
|
* use transactions in `update`, related to #188 * revert to raw get query
Does anyone know how to do transactions for an entire service? Something like what feathers-knex has. LINK |
any update on this? |
Copy the feathers-knex hooks.js and change it to use unmanaged Sequelize transaction mechanism. |
@tunaung2048 Sequelize transaction object has references back to parent => circular reference when console.log is traversing it to output.
|
This is how I manage system-wide transaction support for ALL sequelize requests. Add the following in app.hooks.js // Application hooks that run for every service
module.exports = {
before: {
all: [function (context) {
const sequelize = context.app.get('sequelizeClient');
context.params.transaction = sequelize.transaction();
//console.log(context);
}],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},
after: {
all: [function (context) {
//context.params.transaction.commit();
return context.params.transaction.then(t => {
return t.commit();
});
}],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},
error: {
all: [function (context) {
//context.params.transaction.rollback();
return context.params.transaction.then(t => {
return t.rollback();
});
}],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
}
};
` |
this is a fantastic cookbook recipe.
Thank you,
Mark Edwards
…On Tue, Jan 21, 2020 at 9:53 AM Boniface Mbaria ***@***.***> wrote:
This is how I manage system-wide transaction support for ALL sequelize
requests. Add the following in app.hooks.js
// Application hooks that run for every servicemodule.exports = {
before: {
all: [function (context) {
const sequelize = context.app.get('sequelizeClient');
context.params.transaction = sequelize.transaction();
//console.log(context);
}],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},
after: {
all: [function (context) {
//context.params.transaction.commit();
return context.params.transaction.then(t => {
return t.commit();
});
}],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},
error: {
all: [function (context) {
//context.params.transaction.rollback();
return context.params.transaction.then(t => {
return t.rollback();
});
}],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
}
};`
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#188?email_source=notifications&email_token=AAWJ3YQQ7QWXOKQ6AHHQEQLQ64R7TA5CNFSM4EMX2TDKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEJQOE4Y#issuecomment-576774771>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAWJ3YWKA23KOIBVV5OEK3TQ64R7TANCNFSM4EMX2TDA>
.
|
How would you approach this problem if you need the reference to the same transaction in the service and in an after hook? If there is an error in the service or in an after hook (to create a related entity), could you do the roll back in the |
Don't know if this helps but all my tables have a
This adds the same uuid to To identify the paths/services in current request associated with the `last_request_id I also add the following to app.hooks.js
Now you will have access to the services that were processed in the current request where you need them
|
Thanks everyone! This is a mix of multiple solutions. It seems to work for me when I call a service from another hook using:
sequelize-transaction.ts
|
Tks @edwardsmarkf edwardsmarkf and @yohane55 yohane55 In my case I wanted to use the same transaction across multiple services. When I send a POST to /notification service I wanted to save all tags, using my /tag service and save all on the same request/transaction. So I did some changes on this hook and the behaviour was changed from transaction by service to transaction by request: You can see more details here: https://github.com/denisgmarques/feathersjs-sequelize-transaction-across-multiple-services |
Readme says that adapter supports transactions. but not found any other mentions even in sources.
Is it any guides how to use it?
The text was updated successfully, but these errors were encountered: