Last active
March 13, 2019 11:41
-
-
Save drenther/ca26695d4f7b12941980e6b746066318 to your computer and use it in GitHub Desktop.
An example of $session getter/setter example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { runInTransaction } = require('mongoose-transact-utils'); | |
const { User } = require('./models/'); | |
async function someOperationsWithTransaction() { | |
let john; | |
await runInTransaction(async session => { | |
john = await User.findOne({ name: 'John' }).session(session); | |
// the same session | |
console.log(john.$session() === session); // returns true | |
}); | |
await runInTransaction(async anotherSession => { | |
// you can also change the attached session | |
// although be cautious with this approach | |
john.$session(anotherSession); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment