-
Notifications
You must be signed in to change notification settings - Fork 52
DOCSP-32718: add CodeWhisperer comments to transactions code snippets #769
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
Changes from 5 commits
f81576e
160a2d7
026c6d4
90c3d6c
ab8d6fa
e6a25c7
f3ea0c0
9643564
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,12 +1,12 @@ | ||||||
const { MongoError, MongoClient } = require('mongodb'); | ||||||
|
||||||
// drop collections | ||||||
// Drop the "customers", "inventory", and "orders" collections from the "testdb" database. | ||||||
async function cleanUp(client) { | ||||||
await Promise.all( ['customers', 'inventory', 'orders'].map(async c => { | ||||||
try { | ||||||
const coll = client.db('testdb').collection(c); | ||||||
await coll.drop(); | ||||||
} catch(e) {} | ||||||
} catch(e) {} // Ignore any exceptions | ||||||
})); | ||||||
} | ||||||
|
||||||
|
@@ -15,17 +15,21 @@ async function setup(client) { | |||||
const customerColl = client.db('testdb').collection('customers'); | ||||||
const inventoryColl = client.db('testdb').collection('inventory'); | ||||||
|
||||||
// Insert order data for customer "98765" in the customers collection | ||||||
await customerColl.insertOne({ _id: 98765, orders: [] }); | ||||||
|
||||||
// Insert inventory data for "sunblock" and "beach towel" | ||||||
await inventoryColl.insertMany([ | ||||||
{ name: 'sunblock', sku: 5432, qty: 85 }, | ||||||
{ name: 'beach towel', sku: 7865, qty: 41 }, | ||||||
]); | ||||||
} catch (e) { | ||||||
// Print the exception if one was thrown | ||||||
console.log('Unable to insert test data: ' + e); | ||||||
} | ||||||
} | ||||||
|
||||||
// Print all documents in the "customers", "inventory", and "orders" collections. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
async function queryData() { | ||||||
const uri = process.env.MONGODB_URI; | ||||||
const client = new MongoClient(uri); | ||||||
|
@@ -36,23 +40,29 @@ async function queryData() { | |||||
}, client)); | ||||||
|
||||||
} finally { | ||||||
// Close the database connection | ||||||
client.close(); | ||||||
} | ||||||
} | ||||||
|
||||||
// start placeOrder | ||||||
async function placeOrder(client, cart, payment) { | ||||||
// Specify readConcern, writeConcern, and readPreference transaction options | ||||||
const transactionOptions = { | ||||||
readConcern: { level: 'snapshot' }, | ||||||
writeConcern: { w: 'majority' }, | ||||||
readPreference: 'primary' | ||||||
}; | ||||||
|
||||||
// Start the session | ||||||
const session = client.startSession(); | ||||||
try { | ||||||
// Start the transaction in the session, specifying the transaction options | ||||||
session.startTransaction(transactionOptions); | ||||||
|
||||||
const ordersCollection = client.db('testdb').collection('orders'); | ||||||
// Within the session, insert an order that contains information about the | ||||||
// customer, items purchased, and the total payment. | ||||||
const orderResult = await ordersCollection.insertOne( | ||||||
{ | ||||||
customer: payment.customer, | ||||||
|
@@ -63,21 +73,26 @@ async function placeOrder(client, cart, payment) { | |||||
); | ||||||
|
||||||
const inventoryCollection = client.db('testdb').collection('inventory'); | ||||||
|
||||||
// Within the session, for each item purchased, decrement the purchased quantity in the "inventory" collection. | ||||||
// Cancel the transaction when you have insufficient inventory or if the item SKU does not exist. | ||||||
for (let i=0; i<cart.length; i++) { | ||||||
const item = cart[i]; | ||||||
|
||||||
// Cancel the transaction when you have insufficient inventory | ||||||
// Retrieve the inventory information for the item | ||||||
const checkInventory = await inventoryCollection.findOne( | ||||||
{ | ||||||
sku: item.sku, | ||||||
qty: { $gte: item.qty } | ||||||
}, | ||||||
{ session } | ||||||
) | ||||||
// Throw an exception if the item lacks sufficient quantity or SKU does not exist. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
if (checkInventory === null) { | ||||||
throw new Error('Insufficient quantity or SKU not found.'); | ||||||
} | ||||||
|
||||||
// Decrement the inventory of the item by the amount specified in the order. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
await inventoryCollection.updateOne( | ||||||
{ sku: item.sku }, | ||||||
{ $inc: { 'qty': -item.qty }}, | ||||||
|
@@ -86,47 +101,68 @@ async function placeOrder(client, cart, payment) { | |||||
} | ||||||
|
||||||
const customerCollection = client.db('testdb').collection('customers'); | ||||||
|
||||||
// Within the session, add the order details to the "orders" array of the customer document. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
await customerCollection.updateOne( | ||||||
{ _id: payment.customer }, | ||||||
{ $push: { orders: orderResult.insertedId }}, | ||||||
{ session } | ||||||
); | ||||||
|
||||||
// Commit the transaction | ||||||
await session.commitTransaction(); | ||||||
console.log('Transaction successfully committed.'); | ||||||
|
||||||
} catch (error) { | ||||||
// Handle the UnknownTransactionCommitResult exception | ||||||
if (error instanceof MongoError && error.hasErrorLabel('UnknownTransactionCommitResult')) { | ||||||
// add your logic to retry or handle the error | ||||||
} | ||||||
// Handle the TransientTransactionError exception | ||||||
else if (error instanceof MongoError && error.hasErrorLabel('TransientTransactionError')) { | ||||||
// add your logic to retry or handle the error | ||||||
// Handle any other exception | ||||||
} else { | ||||||
console.log('An error occured in the transaction, performing a data rollback:' + error); | ||||||
} | ||||||
// End the transaction without making the updates performed in the session. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
await session.abortTransaction(); | ||||||
} finally { | ||||||
// End the transaction making all the changes performed in the session. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
await session.endSession(); | ||||||
} | ||||||
} | ||||||
// end placeOrder | ||||||
|
||||||
|
||||||
// Run the full transaction example. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
async function run() { | ||||||
const uri = process.env.MONGODB_URI; | ||||||
const client = new MongoClient(uri); | ||||||
|
||||||
// Call a method that removes data from prior runs of this example. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
await cleanUp(client); | ||||||
|
||||||
// Call a method that creates sample inventory data for this example. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
await setup(client); | ||||||
|
||||||
// Create sample data for a customer's shopping cart that includes "sunblock" and "beach towel" items. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
const cart = [ | ||||||
{ name: 'sunblock', sku: 5432, qty: 1, price: 5.19 }, | ||||||
{ name: 'beach towel', sku: 7865, qty: 2, price: 15.99 } | ||||||
]; | ||||||
|
||||||
// Create sample data for a customer's payment, calculated from the contents of their cart. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
const payment = { customer: 98765, total: 37.17 }; | ||||||
|
||||||
try { | ||||||
// Call the method that updates the customer and inventory in a transaction. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
await placeOrder(client, cart, payment); | ||||||
} finally { | ||||||
// Call a method that removes data from prior runs of this example. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
await cleanUp(client); | ||||||
|
||||||
// Close the database connection | ||||||
await client.close(); | ||||||
} | ||||||
} | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.