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

(DOCSP-42573) Update transactions in Functions info about read/write settings #860

Merged
merged 4 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions source/functions/mongodb/api/snippets/transactions.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
exports = function () {
const client = context.services.get("mongodb-atlas");

db = client.db("exampleDatabase");
const db = client.db("exampleDatabase");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well-spotted! Nice fixups - thank you! 🙏


accounts = db.collection("accounts");
browniePointsTrades = db.collection("browniePointsTrades");
const accounts = db.collection("accounts");
const browniePointsTrades = db.collection("browniePointsTrades");

// create user accounts with initial balances
accounts.insertOne({ name: "henry", browniePoints: 42 });
Expand Down Expand Up @@ -34,18 +34,11 @@ async function tradeBrowniePoints(
// Step 1: Start a Client Session
const session = client.startSession();

// Step 2: Optional. Define options to use for the transaction
const transactionOptions = {
readPreference: "primary",
readConcern: { level: "local" },
writeConcern: { w: "majority" },
};

// Step 3: Use withTransaction to start a transaction, execute the callback, and commit (or abort on error)
// Step 2: Use withTransaction to start a transaction, execute the callback, and commit (or abort on error)
// Note: The callback for withTransaction MUST be async and/or return a Promise.
try {
await session.withTransaction(async () => {
// Step 4: Execute the queries you would like to include in one atomic transaction
// Step 3: Execute the queries you would like to include in one atomic transaction
// Important:: You must pass the session to the operations
await accounts.updateOne(
{ name: userSubtractPoints },
Expand All @@ -65,12 +58,12 @@ async function tradeBrowniePoints(
},
{ session }
);
}, transactionOptions);
});
} catch (err) {
// Step 5: Handle errors with a transaction abort
// Step 4: Handle errors with a transaction abort
await session.abortTransaction();
} finally {
// Step 6: End the session when you complete the transaction
// Step 5: End the session when you complete the transaction
await session.endSession();
}
}
12 changes: 2 additions & 10 deletions source/functions/mongodb/write.txt
Original file line number Diff line number Diff line change
Expand Up @@ -383,20 +383,12 @@ To perform a transaction:
1. Obtain and start a client session with ``client.startSession()``.

#. Call ``session.withTransaction()`` to define the transaction. The
method takes an async callback function and, optionally, a
configuration object that defines custom :manual:`read and write
settings
</core/transactions/#read-concern-write-concern-read-preference>` for
the transaction.
method takes an async callback function.

.. code-block:: javascript

session.withTransaction(async () => {
// ... Run MongoDB operations in this callback
}, {
readPreference: "primary",
readConcern: { level: "local" },
writeConcern: { w: "majority" },
})

#. In the transaction callback function, run the MongoDB queries that
Expand Down Expand Up @@ -439,7 +431,7 @@ To perform a transaction:
}

The following example creates two users, "henry" and "michelle", and
a uses a transaction to move "browniePoints" between those users
uses a transaction to move "browniePoints" between those users
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooo, another good spot - nice work!

atomically:

.. literalinclude:: /functions/mongodb/api/snippets/transactions.js
Expand Down
Loading