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

Support for transactions #315

Open
grzegorzj opened this issue Feb 3, 2021 · 1 comment
Open

Support for transactions #315

grzegorzj opened this issue Feb 3, 2021 · 1 comment

Comments

@grzegorzj
Copy link

grzegorzj commented Feb 3, 2021

Apologies in advance if I got something wrong, but as far as I can see, it's impossible to use PassportLocalModel's methods within a transaction. For example, PassportLocalModel's register() method doesn't take arguments/doesn't return an object that would take a session object.

Code example:

const session = await mongoose.startSession();
session.startTransaction();
const user: Document<User> = await UserModel.register({ email, active: false } as User & Document, password); // making this go into the session isn't possible as of now

How I think it should work

const session = await mongoose.startSession();
session.startTransaction();
const user: Document<User> = await UserModel.register({ email, active: false } as User & Document, password).session(session);
@hensansi
Copy link
Contributor

As an alternative solution, even if the register function doesn't support transactions you can pass a user document created with a session. If something fails, it will be rolled back.

  const session = await mongoose.startSession();
  session.startTransaction();
  const [user] = await this.create([userData], { session });
  if (password) {
    await this.register(user, password);
  }
  await session.commitTransaction();
  session.endSession();

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