Skip to content

Instantly share code, notes, and snippets.

@drenther
Last active March 13, 2019 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drenther/ca26695d4f7b12941980e6b746066318 to your computer and use it in GitHub Desktop.
Save drenther/ca26695d4f7b12941980e6b746066318 to your computer and use it in GitHub Desktop.
An example of $session getter/setter example
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