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

Is there a way to run repo insert or update queries in a transaction? #200

Closed
grrenier opened this issue Jun 9, 2023 · 5 comments
Closed

Comments

@grrenier
Copy link

grrenier commented Jun 9, 2023

I there a way to run multiple insert/update queries inside a MongoDB transaction, for example when we do a deposit into an account we need to insert the deposit amount in the AccountTransaction and then update the Account balance but that should be a transaction to be able to do a rollback when there is any error.

@noam-honig
Copy link
Collaborator

If you run these multiple operations in a BackendMethod they will run in a transaction.

Test it and let me know

@grrenier
Copy link
Author

I did something like this and the first employee was saved to the DB. Should I do something different?

export class EmployeeController {
  @BackendMethod({ allowed: Allow.authenticated })
  static async testDbTransaction(employee1: Employee, employee2: Employee): Promise<Employee> {
    const e1 = await remult.repo(Employee).insert(employee1);
    if (true) {
      throw Error('Abort DB transaction');
    }
    const e2 = await remult.repo(Employee).insert(employee2);
    return e1;
}

@noam-honig
Copy link
Collaborator

I've reproduced the issue and am working on it now

noam-honig added a commit that referenced this issue Jun 11, 2023
@noam-honig
Copy link
Collaborator

Hi @grrenier, I've fixed the issue in this release:
https://github.com/remult/remult/releases/tag/v0.20.5

Can you check it out and let me know?

@grrenier
Copy link
Author

Hi @noam-honig, I tested the version 0.20.5 and it works as expected. You are doing an awesome work in this framework.

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

2 participants