Implementation is based on using Meteor.EnvironmentVariable to store the MongoDB session and overrides in
MongoInternals.NpmModule.Collection
for each method that supports sessions.
// server-side
import {runInTransaction} from 'meteor/equium:mongo-transactions';
const Invoice = new Mongo.Collection('invoice');
const LineItems = new Mongo.Collection('lineItems');
const Payments = new Mongo.Collection('payments');
function (invoice, lineItems) {
runInTransaction(() => {
Invoice.insert(invoice);
LineItems.insert(lineItems);
const payment = createPaymentDoc();
Payments.insert(payment);
});
}
runInTransaction<R>(fn: () => R, options?: RunInTransactionOptions): R;
fn - method to be run in transaction
options - optional SessionOptions, https://mongodb.github.io/node-mongodb-native/3.6/api/global.html#SessionOptions
isInTransaction(): boolean;
Whether we already are in the transaction.
sessionVariable: Meteor.EnvironmentVariable
This is the actual Meteor.EnvironmentVariable
instance used by the package.
You could get MongoDB's ClientSession instance with sessionVariable.get()
.
It will return undefined
if there is no session.
cd test-app
meteor npm i
# using browser driver
TEST_BROWSER_DRIVER=puppeteer MONGO_URL= ROOT_URL= meteor test-packages --port=3000 --driver-package meteortesting:mocha ../