You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/cosmos-db/how-to-write-stored-procedures-triggers-udfs.md
+61-14Lines changed: 61 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,11 @@
1
1
---
2
2
title: Write stored procedures, triggers, and UDFs in Azure Cosmos DB
3
3
description: Learn how to define stored procedures, triggers, and user-defined functions in Azure Cosmos DB
4
-
author: markjbrown
4
+
author: timsander1
5
5
ms.service: cosmos-db
6
6
ms.topic: conceptual
7
-
ms.date: 10/31/2019
8
-
ms.author: mjbrown
7
+
ms.date: 05/07/2020
8
+
ms.author: tisande
9
9
---
10
10
11
11
# 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
16
16
17
17
> [!NOTE]
18
18
> 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
-
20
19
> [!Tip]
21
20
> 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)
22
21
23
22
## <aid="stored-procedures"></a>How to write stored procedures
24
23
25
24
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.
26
25
27
-
**Example**
28
-
29
26
Here is a simple stored procedure that returns a "Hello World" response.
30
27
31
28
```javascript
@@ -46,7 +43,7 @@ Once written, the stored procedure must be registered with a collection. To lear
46
43
47
44
### <aid="create-an-item"></a>Create an item using stored procedure
48
45
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.
50
47
51
48
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.
52
49
@@ -68,7 +65,7 @@ function createToDoItem(itemToCreate) {
68
65
}
69
66
```
70
67
71
-
### Arrays as input parameters for stored procedures
68
+
### Arrays as input parameters for stored procedures
72
69
73
70
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:
74
71
@@ -97,23 +94,23 @@ function tradePlayers(playerId1, playerId2) {
97
94
var player1Document, player2Document;
98
95
99
96
// query for players
100
-
var filterQuery =
101
-
{
97
+
var filterQuery =
98
+
{
102
99
'query':'SELECT * FROM Players p where p.id = @playerId1',
var accept2 =container.queryDocuments(container.getSelfLink(), filterQuery2, {},
119
116
function (err2, items2, responseOptions2) {
@@ -206,6 +203,56 @@ function bulkImport(items) {
206
203
}
207
204
```
208
205
206
+
### <aid="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.
if (!isAccepted) reject(newError(ERROR_CODE.NotAccepted, "replaceDocument was not accepted."));
234
+
});
235
+
}
236
+
};
237
+
238
+
asyncfunctionmain() {
239
+
let continuation;
240
+
do {
241
+
let { feed, options } =awaitasyncHelper.queryDocuments("SELECT * from c", { continuation });
242
+
243
+
for (let doc of feed) {
244
+
doc.newProp=1;
245
+
awaitasyncHelper.replaceDocument(doc);
246
+
}
247
+
248
+
continuation =options.continuation;
249
+
} while (continuation);
250
+
}
251
+
252
+
main().catch(err=>getContext().abort(err));
253
+
}
254
+
```
255
+
209
256
## <aid="triggers"></a>How to write triggers
210
257
211
258
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.
Copy file name to clipboardExpand all lines: articles/cosmos-db/javascript-query-api.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,11 @@
1
1
---
2
2
title: Work with JavaScript integrated query API in Azure Cosmos DB Stored Procedures and Triggers
3
3
description: This article introduces the concepts for JavaScript language-integrated query API to create stored procedures and triggers in Azure Cosmos DB.
0 commit comments