Created
October 22, 2021 23:47
-
-
Save Pyrolistical/8ef172eb9c80c827d43b26f575d94cc3 to your computer and use it in GitHub Desktop.
ecommerce-escalation v2 repo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default (mongoClient, db) => { | |
return { | |
async checkoutTransaction(closure) { | |
const session = mongoClient.startSession({ | |
causalConsistency: true | |
}); | |
try { | |
const sessionRepository = { | |
async findInventoryByProuctSkus(productSkus) { | |
return db.collection('inventory').find({ | |
productSku: { | |
$in: productSkus | |
} | |
}, { | |
session | |
}) | |
.toArray(); | |
}, | |
async updateInventoryStock(updates) { | |
return db.collection('inventory').bulkWrite(updates.map(({productSku, stock}) => { | |
return { | |
updateOne: { | |
filter: { | |
productSku | |
}, | |
update: { | |
$set: stock | |
} | |
} | |
} | |
}), { | |
session | |
}); | |
}, | |
async createOrder(order) { | |
const {insertedId} = await db.collection('order').insertOne(order, { | |
session | |
}); | |
return insertedId; | |
} | |
}; | |
return session.withTransaction(closure(sessionRepository)); | |
} finally { | |
await session.endSession(); | |
} | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment