Skip to content
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

Please add an example of how to use transaction #81

Closed
devashishsethia opened this issue Jan 31, 2020 · 4 comments
Closed

Please add an example of how to use transaction #81

devashishsethia opened this issue Jan 31, 2020 · 4 comments

Comments

@devashishsethia
Copy link

Hi,
Thanks for your awesome work!
This is not an issue per se, but I haven't been able to get my head around how to use transactions. Since transactions are required in any real life project, a simple example in the docs could help us a lot.

Thanks in advance.

@dekelev
Copy link
Member

dekelev commented Feb 1, 2020

Hi @devashishsethia, thanks for the feedback. I've added some more info and link on the subject here.

A full example is available here.

@dekelev dekelev closed this as completed Feb 1, 2020
@devashishsethia
Copy link
Author

Thank you so much! Got it working with your help. I've created a set of simple feathers hooks that can be added to the project or docs, if you would like (startTransaction() method mentioned in the objection docs didn't work for me). Couldn't create a pull request.

const {transaction} = require('objection');
// Use this hook to manipulate incoming or outgoing data.
// For more information on hooks see: http://docs.feathersjs.com/api/hooks.html

// eslint-disable-next-line no-unused-vars

const start = (options = {}) => {
  return async context => {
    const { service } = context;
    const Model = service.Model;
    const trx = await transaction.start(Model); // use Model if you have installed a knex instance globally using the Model.knex() method, otherwise use Model.knex()
    context.params.transaction = { trx };
    return context;
  };
};

const commit = (options = {}) => {
  return async context => {
    const { transaction } = context.params;
    await transaction.trx.commit();
    return context;
  };
};

const rollback = (options = {}) => {
  return async context => {
    const { transaction } = context.params;
    await transaction.trx.rollback();
  };
};

module.exports = {
  _transaction: {
    start,
    commit,
    rollback
  }
};

@devashishsethia
Copy link
Author

If you have configured knex using app.set('knex', knex) in objection.js (If you used feathers-cli, that should be the case), you can also start the transaction without using Model like so:

const start = (options = {}) => {
  return async context => {
    const { service, app } = context;
    const knex = app.get('knex');
    const trx = await transaction.start(knex);
    context.params = {
      ...context.params,
      transaction: {
        trx
      }
    }
    return context;
  };
};

@Artaud
Copy link

Artaud commented Dec 29, 2021

This should definitely be in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants