Skip to content

Commit 941506c

Browse files
authored
Merge pull request #114424 from markjbrown/stored-procs
add async promises, change author
2 parents 90a6180 + ae0211e commit 941506c

File tree

4 files changed

+70
-23
lines changed

4 files changed

+70
-23
lines changed

articles/cosmos-db/how-to-use-stored-procedures-triggers-udfs.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
title: Register and use stored procedures, triggers, and user-defined functions in Azure Cosmos DB SDKs
33
description: Learn how to register and call stored procedures, triggers, and user-defined functions using the Azure Cosmos DB SDKs
4-
author: markjbrown
4+
author: timsander1
55
ms.service: cosmos-db
66
ms.topic: conceptual
7-
ms.date: 02/24/2020
8-
ms.author: mjbrown
7+
ms.date: 05/07/2020
8+
ms.author: tisande
99
---
1010

1111
# How to register and use stored procedures, triggers, and user-defined functions in Azure Cosmos DB

articles/cosmos-db/how-to-write-javascript-query-api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
title: Write stored procedures and triggers using the JavaScript query API in Azure Cosmos DB
33
description: Learn how to write stored procedures and triggers using the JavaScript Query API in Azure Cosmos DB
4-
author: markjbrown
4+
author: timsander1
55
ms.service: cosmos-db
66
ms.topic: conceptual
7-
ms.date: 05/23/2019
8-
ms.author: mjbrown
7+
ms.date: 05/07/2020
8+
ms.author: tisande
99
---
1010

1111
# How to write stored procedures and triggers in Azure Cosmos DB by using the JavaScript query API

articles/cosmos-db/how-to-write-stored-procedures-triggers-udfs.md

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
title: Write stored procedures, triggers, and UDFs in Azure Cosmos DB
33
description: Learn how to define stored procedures, triggers, and user-defined functions in Azure Cosmos DB
4-
author: markjbrown
4+
author: timsander1
55
ms.service: cosmos-db
66
ms.topic: conceptual
7-
ms.date: 10/31/2019
8-
ms.author: mjbrown
7+
ms.date: 05/07/2020
8+
ms.author: tisande
99
---
1010

1111
# How to write stored procedures, triggers, and user-defined functions in Azure Cosmos DB
@@ -16,16 +16,13 @@ To call a stored procedure, trigger, and user-defined function, you need to regi
1616

1717
> [!NOTE]
1818
> For partitioned containers, when executing a stored procedure, a partition key value must be provided in the request options. Stored procedures are always scoped to a partition key. Items that have a different partition key value will not be visible to the stored procedure. This also applied to triggers as well.
19-
2019
> [!Tip]
2120
> Cosmos supports deploying containers with stored procedures, triggers and user-defined functions. For more information see [Create an Azure Cosmos DB container with server-side functionality.](manage-sql-with-resource-manager.md#create-sproc)
2221
2322
## <a id="stored-procedures"></a>How to write stored procedures
2423

2524
Stored procedures are written using JavaScript, they can create, update, read, query, and delete items inside an Azure Cosmos container. Stored procedures are registered per collection, and can operate on any document or an attachment present in that collection.
2625

27-
**Example**
28-
2926
Here is a simple stored procedure that returns a "Hello World" response.
3027

3128
```javascript
@@ -46,7 +43,7 @@ Once written, the stored procedure must be registered with a collection. To lear
4643

4744
### <a id="create-an-item"></a>Create an item using stored procedure
4845

49-
When you create an item by using stored procedure, the item is inserted into the Azure Cosmos container and an ID for the newly created item is returned. Creating an item is an asynchronous operation and depends on the JavaScript callback functions. The callback function has two parameters - one for the error object in case the operation fails and another for a return value; in this case, the created object. Inside the callback, you can either handle the exception or throw an error. In case a callback is not provided and there is an error, the Azure Cosmos DB runtime will throw an error.
46+
When you create an item by using stored procedure, the item is inserted into the Azure Cosmos container and an ID for the newly created item is returned. Creating an item is an asynchronous operation and depends on the JavaScript callback functions. The callback function has two parameters - one for the error object in case the operation fails and another for a return value; in this case, the created object. Inside the callback, you can either handle the exception or throw an error. In case a callback is not provided and there is an error, the Azure Cosmos DB runtime will throw an error.
5047

5148
The stored procedure also includes a parameter to set the description, it's a boolean value. When the parameter is set to true and the description is missing, the stored procedure will throw an exception. Otherwise, the rest of the stored procedure continues to run.
5249

@@ -68,7 +65,7 @@ function createToDoItem(itemToCreate) {
6865
}
6966
```
7067

71-
### Arrays as input parameters for stored procedures
68+
### Arrays as input parameters for stored procedures
7269

7370
When defining a stored procedure in Azure portal, input parameters are always sent as a string to the stored procedure. Even if you pass an array of strings as an input, the array is converted to string and sent to the stored procedure. To work around this, you can define a function within your stored procedure to parse the string as an array. The following code shows how to parse a string input parameter as an array:
7471

@@ -97,23 +94,23 @@ function tradePlayers(playerId1, playerId2) {
9794
var player1Document, player2Document;
9895

9996
// query for players
100-
var filterQuery =
101-
{
97+
var filterQuery =
98+
{
10299
'query' : 'SELECT * FROM Players p where p.id = @playerId1',
103100
'parameters' : [{'name':'@playerId1', 'value':playerId1}]
104101
};
105-
102+
106103
var accept = container.queryDocuments(container.getSelfLink(), filterQuery, {},
107104
function (err, items, responseOptions) {
108105
if (err) throw new Error("Error" + err.message);
109106

110107
if (items.length != 1) throw "Unable to find both names";
111108
player1Item = items[0];
112109

113-
var filterQuery2 =
114-
{
110+
var filterQuery2 =
111+
{
115112
'query' : 'SELECT * FROM Players p where p.id = @playerId2',
116-
'parameters' : [{'name':'@playerId2', 'value':playerId2}]
113+
'parameters' : [{'name':'@playerId2', 'value':playerId2}]
117114
};
118115
var accept2 = container.queryDocuments(container.getSelfLink(), filterQuery2, {},
119116
function (err2, items2, responseOptions2) {
@@ -206,6 +203,56 @@ function bulkImport(items) {
206203
}
207204
```
208205

206+
### <a id="async-promises"></a>Async await with stored procedures
207+
208+
The following is an example of a stored procedure that uses async-await with Promises using a helper function. The stored procedure queries for an item and replaces it.
209+
210+
```javascript
211+
function async_sample() {
212+
const ERROR_CODE = {
213+
NotAccepted: 429
214+
};
215+
216+
const asyncHelper = {
217+
queryDocuments(sqlQuery, options) {
218+
return new Promise((resolve, reject) => {
219+
const isAccepted = __.queryDocuments(__.getSelfLink(), sqlQuery, options, (err, feed, options) => {
220+
if (err) reject(err);
221+
resolve({ feed, options });
222+
});
223+
if (!isAccepted) reject(new Error(ERROR_CODE.NotAccepted, "replaceDocument was not accepted."));
224+
});
225+
},
226+
227+
replaceDocument(doc) {
228+
return new Promise((resolve, reject) => {
229+
const isAccepted = __.replaceDocument(doc._self, doc, (err, result, options) => {
230+
if (err) reject(err);
231+
resolve({ result, options });
232+
});
233+
if (!isAccepted) reject(new Error(ERROR_CODE.NotAccepted, "replaceDocument was not accepted."));
234+
});
235+
}
236+
};
237+
238+
async function main() {
239+
let continuation;
240+
do {
241+
let { feed, options } = await asyncHelper.queryDocuments("SELECT * from c", { continuation });
242+
243+
for (let doc of feed) {
244+
doc.newProp = 1;
245+
await asyncHelper.replaceDocument(doc);
246+
}
247+
248+
continuation = options.continuation;
249+
} while (continuation);
250+
}
251+
252+
main().catch(err => getContext().abort(err));
253+
}
254+
```
255+
209256
## <a id="triggers"></a>How to write triggers
210257

211258
Azure Cosmos DB supports pre-triggers and post-triggers. Pre-triggers are executed before modifying a database item and post-triggers are executed after modifying a database item.

articles/cosmos-db/javascript-query-api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
title: Work with JavaScript integrated query API in Azure Cosmos DB Stored Procedures and Triggers
33
description: This article introduces the concepts for JavaScript language-integrated query API to create stored procedures and triggers in Azure Cosmos DB.
4-
author: markjbrown
4+
author: timsander1
55
ms.service: cosmos-db
66
ms.topic: conceptual
7-
ms.date: 08/01/2019
8-
ms.author: mjbrown
7+
ms.date: 05/07/2020
8+
ms.author: tisande
99
ms.reviewer: sngun
1010

1111
---

0 commit comments

Comments
 (0)