An Azure NoSQL database service for app development.
Hi @Md. Ruhul Amin Khan , welcome to Microsoft Q&A forum.
As I understand your query, you want to create a stored procedure in Azure Cosmos DB with commit and rollback and call it from node.js project.
As per the document, "Transactions are natively integrated into the Azure Cosmos DB JavaScript programming model. Within a JavaScript function, all the operations are automatically wrapped under a single transaction. If the JavaScript logic in a stored procedure completes without any exceptions, all the operations within the transaction are committed to the database."
We just need to throw the error in our stored procedure and that will be rollback statement by itself. If there is no error encountered then it is auto-commit. No need to write commit or transaction keywords in stored procedure. Below is one example of it:
function createMyDocument(documentToCreate) {
var context = getContext();
var collection = context.getCollection();
var accepted = collection.createDocument(collection.getSelfLink(),
documentToCreate,
function (err, documentCreated) {
if (err) throw new Error('Error' + err.message);
context.getResponse().setBody(documentCreated.id)
});
if (!accepted) return;
}
If you notice we are throwing error and that is rollback here.
For more details you can refer to : Build multi-item transactions with the Azure Cosmos DB SQL API
Now to call this stored procedure from node.js we can refer to below article:
Node.js examples to manage data in Azure Cosmos DB
This talks about how to call stored procedure by providing the code snippet:
https://github.com/Azure/azure-cosmos-js/blob/master/samples/ServerSideScripts/index.ts
Please let me know if this helps or else we can discuss further on the same.
----------
If answer is helpful please click on
as it could help other members of the Microsoft Q&A community who have similar questions and are looking for solutions. Thank you for helping to improve Microsoft Q&A!