22
33const { MongoError, MongoClient } = require ( 'mongodb' ) ;
44
5- // Drop the "customers", "inventory", and "orders" collections from the "testdb" database
5+ /* Drop the "customers", "inventory", and "orders" collections from the
6+ "testdb" database */
67async function cleanUp ( client ) {
78 await Promise . all ( [ 'customers' , 'inventory' , 'orders' ] . map ( async c => {
89 try {
@@ -22,8 +23,8 @@ async function setup(client) {
2223
2324 // Insert inventory data for "sunblock" and "beach towel"
2425 await inventoryColl . insertMany ( [
25- { name : 'sunblock' , sku : 5432 , qty : 85 } ,
26- { name : 'beach towel' , sku : 7865 , qty : 41 } ,
26+ { item : 'sunblock' , item_id : 5432 , qty : 85 } ,
27+ { item : 'beach towel' , item_id : 7865 , qty : 41 } ,
2728 ] ) ;
2829 } catch ( e ) {
2930 // Print the exception if one was thrown
@@ -42,14 +43,13 @@ async function queryData() {
4243 } , client ) ) ;
4344
4445 } finally {
45- // Close the database connection
46+ // Close your connection
4647 client . close ( ) ;
4748 }
4849}
4950
5051// start placeOrder
5152async function placeOrder ( client , cart , payment ) {
52- // Specify readConcern, writeConcern, and readPreference transaction options
5353 const transactionOptions = {
5454 readConcern : { level : 'snapshot' } ,
5555 writeConcern : { w : 'majority' } ,
@@ -63,8 +63,8 @@ async function placeOrder(client, cart, payment) {
6363 session . startTransaction ( transactionOptions ) ;
6464
6565 const ordersCollection = client . db ( 'testdb' ) . collection ( 'orders' ) ;
66- // Within the session, insert an order that contains information about the
67- // customer, items purchased, and the total payment.
66+ /* Within the session, insert an order that contains information about the
67+ customer, items purchased, and the total payment */
6868 const orderResult = await ordersCollection . insertOne (
6969 {
7070 customer : payment . customer ,
@@ -75,36 +75,27 @@ async function placeOrder(client, cart, payment) {
7575 ) ;
7676
7777 const inventoryCollection = client . db ( 'testdb' ) . collection ( 'inventory' ) ;
78-
79- // Within the session, for each item purchased, decrement the purchased quantity in the "inventory" collection.
80- // Cancel the transaction when you have insufficient inventory or if the item SKU does not exist.
81- for ( let i = 0 ; i < cart . length ; i ++ ) {
82- const item = cart [ i ] ;
83-
84- // Retrieve the inventory information for the item
85- const checkInventory = await inventoryCollection . findOne (
78+
79+ for ( const item of order ) {
80+ /* Update the inventory for the purchased items. End the
81+ transaction if the quantity of an item in the inventory is
82+ insufficient to complete the purchase. */
83+ const inStock = await inventoryCollection . findOneAndUpdate (
8684 {
87- sku : item . sku ,
88- qty : { $gte : item . qty }
85+ item_id : item . item_id ,
86+ item_id : { $gte : item . qty }
8987 } ,
88+ { $inc : { 'qty' : - item . qty } } ,
9089 { session }
9190 )
92- // Throw an exception if the item lacks sufficient quantity or SKU does not exist.
93- if ( checkInventory === null ) {
94- throw new Error ( 'Insufficient quantity or SKU not found.' ) ;
91+ if ( inStock === null ) {
92+ throw new Error ( 'Insufficient quantity or item ID not found.' ) ;
9593 }
96-
97- // Decrement the inventory of the item by the amount specified in the order.
98- await inventoryCollection . updateOne (
99- { sku : item . sku } ,
100- { $inc : { 'qty' : - item . qty } } ,
101- { session }
102- ) ;
10394 }
10495
10596 const customerCollection = client . db ( 'testdb' ) . collection ( 'customers' ) ;
10697
107- // Within the session, add the order details to the "orders" array of the customer document.
98+ // Within the session, add the order details to the "orders" array of the customer document
10899 await customerCollection . updateOne (
109100 { _id : payment . customer } ,
110101 { $push : { orders : orderResult . insertedId } } ,
@@ -121,16 +112,16 @@ async function placeOrder(client, cart, payment) {
121112 transaction. Roll back all the updates performed in the transaction.
122113 */
123114 if ( error instanceof MongoError && error . hasErrorLabel ( 'UnknownTransactionCommitResult' ) ) {
124- // add your logic to retry or handle the error
115+ // Add your logic to retry or handle the error
125116 }
126117 else if ( error instanceof MongoError && error . hasErrorLabel ( 'TransientTransactionError' ) ) {
127- // add your logic to retry or handle the error
118+ // Add your logic to retry or handle the error
128119 } else {
129120 console . log ( 'An error occured in the transaction, performing a data rollback:' + error ) ;
130121 }
131122 await session . abortTransaction ( ) ;
132123 } finally {
133- // End the session so that no further calls can be made on it
124+ // End the session
134125 await session . endSession ( ) ;
135126 }
136127}
@@ -148,13 +139,14 @@ async function run() {
148139 // Call a method that creates sample inventory data for this example
149140 await setup ( client ) ;
150141
151- // Create sample data for a customer's shopping cart that includes "sunblock" and "beach towel" items
142+ // Create sample data for a customer's shopping cart
152143 const cart = [
153- { name : 'sunblock' , sku : 5432 , qty : 1 , price : 5.19 } ,
154- { name : 'beach towel' , sku : 7865 , qty : 2 , price : 15.99 }
144+ { item : 'sunblock' , item_id : 5432 , qty : 1 , price : 5.19 } ,
145+ { item : 'beach towel' , item_id : 7865 , qty : 2 , price : 15.99 }
155146 ] ;
156147
157- // Create sample data for a customer's payment, calculated from the contents of their cart
148+ /* Create sample data for a customer's payment based on the contents
149+ of their cart */
158150 const payment = { customer : 98765 , total : 37.17 } ;
159151
160152 try {
@@ -164,7 +156,7 @@ async function run() {
164156 // Call a method that removes data from prior runs of this example
165157 await cleanUp ( client ) ;
166158
167- // Close the database connection
159+ // Close your connection
168160 await client . close ( ) ;
169161 }
170162}
0 commit comments