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

Connection.prototype.transaction() return value #9919

Closed
kulakovAngel opened this issue Feb 11, 2021 · 3 comments
Closed

Connection.prototype.transaction() return value #9919

kulakovAngel opened this issue Feb 11, 2021 · 3 comments
Assignees
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@kulakovAngel
Copy link

Mongoose version: 5.10.11.
Node.js version: 12.16.3.
MongoDB vresion: 4.4.3 (cloud.mongodb Atlas)

Do you want to request a feature or report a bug?
I think is a bug with Connection.prototype.transaction() return value

What is the current behavior?
This method returns a "CommandResult" instance, but not the value returned by the "fn" callback

const t = await require('mongoose')
  .connection
  .transaction(async session => {

    const newServices = await Service
      .create(services, {
        session
      });

    if (!newServices || !newServices.length) throw new Error('It\'s impossible to create new services');
    return await newVisit.save({
      session
    });
  });

console.log('transaction: ', t); // CommandResult { result: { ok: 1, ...} ... }

This example can be simplified to primitive, but the result is the same:

const t = await require('mongoose')
  .connection
  .transaction(async session => {

    await SomeModel.save({ session });
    
    return 1;
  });

console.log('transaction: ', t); // CommandResult { result: { ok: 1, ...} ... }

What is the expected behavior?
As described in the documentation, the method Connection.prototype.transaction() must return a promise that resolves to the value returned by the fn callback. In my example, for simplicity, "1" the number.

Returns:

  • «Promise<Any>» promise that resolves to the returned value of fn

I expect something like the following:

const t = await require('mongoose')
  .connection
  .transaction(async session => {

    await SomeModel.save({ session });
    
    return 1;
  });

console.log('transaction: ', t); // based on the documentation it should be "1"
@vkarpov15 vkarpov15 added this to the 5.11.17 milestone Feb 15, 2021
@vkarpov15 vkarpov15 added the docs This issue is due to a mistake or omission in the mongoosejs.com documentation label Feb 16, 2021
@vkarpov15
Copy link
Collaborator

Unfortunately this is a mistake in our docs. The MongoDB driver's withTransaction() function doesn't return the resolved value of the promise that the transaction() callback returns. So Mongoose can't support this unless we redo the MongoDB driver's connection handling. Sorry for the confusion 👍

@kulakovAngel
Copy link
Author

I got it, thanks.

@meness
Copy link

meness commented Mar 17, 2022

@vkarpov15 It seems withTransaction() doesn't resolve the returned value. If so, please modify the return type of the helper.

withTransaction<T = void>(fn: WithTransactionCallback<T>, options?: TransactionOptions): ReturnType<typeof fn>;

@vkarpov15 vkarpov15 reopened this Mar 18, 2022
@vkarpov15 vkarpov15 modified the milestones: 5.11.17, 6.2.12 Mar 18, 2022
@vkarpov15 vkarpov15 added typescript Types or Types-test related issue / Pull Request and removed docs This issue is due to a mistake or omission in the mongoosejs.com documentation labels Mar 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

3 participants