|
At the very least a session instance should expose CRUD operations on the database which is always ensured to run in the context of the session.
For context, I was trying to run a data-migration script and I was very surprised that the insertOne operation was not running in the context of the session / transaction and I instead had to pass it explicitly via the options object:
async function runMigration() {
|
const client = await MongoClient.connect(MONGO_CONNECTION_STRING, {
|
useUnifiedTopology: true,
|
});
|
const db = client.db(MONGO_DB_NAME);
|
await client.withSession((session) => {
|
return session.withTransaction(async (session) => {
|
const migrations = db.collection("Migrations");
|
await migrations.insertOne({ test: "123" }, { session }); // What if I forget this options object?
|
throw new Error("Abort!");
|
});
|
});
|
}
|
|