Share via

Javascript programming workshop by azure - Link not working

Md. Ruhul Amin Khan 41 Reputation points
2022-04-07T03:49:28.163+00:00

I am struggling to solve an issue for my project, I am looking for complete guidance with code examples for stored procedure commit and rollback for a transaction.
While I was searching for a documentation this links from azure workshop doesn't work at all. Here is the LINK : To learn about programming using JavaScript in the SQL API: https://learn.microsoft.com/azure/cosmos-db/programming , Source : https://azurecosmosdb.github.io/labs/

I am asking for a complete code sample or guidance of how I can use commit and rollback in stored procedure transactions for a JavaScript, node js project.

Azure Cosmos DB
Azure Cosmos DB

An Azure NoSQL database service for app development.

0 comments No comments

Answer accepted by question author

Anurag Sharma 17,636 Reputation points
2022-04-07T06:42:19.787+00:00

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 190784-image.png 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!

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.