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/nosql/how-to-write-stored-procedures-triggers-udfs.md
+14-15Lines changed: 14 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -56,11 +56,11 @@ Once written, the stored procedure must be registered with a collection. To lear
56
56
57
57
### <aid="create-an-item"></a>Create items using stored procedures
58
58
59
-
When you create an item by using a stored procedure, the item is inserted into the Azure Cosmos DB 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. If a callback isn't provided and there's an error, the Azure Cosmos DB runtime throws an error.
59
+
When you create an item using a stored procedure, the item is inserted into the Azure Cosmos DB 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. If a callback isn't provided and there's an error, the Azure Cosmos DB runtime throws an error.
60
60
61
61
The stored procedure also includes a parameter to set the description as a boolean value. When the parameter is set to true and the description is missing, the stored procedure throws an exception. Otherwise, the rest of the stored procedure continues to run.
62
62
63
-
The following example of a stored procedure takes an array of new Azure Cosmos DB items as input, inserts it into the Azure Cosmos DB container and returns the count of the items inserted. In this example, we're using the ToDoList sample from the [Quickstart .NET API for NoSQL](quickstart-dotnet.md).
63
+
The following example of a stored procedure takes an array of new Azure Cosmos DB items as input, inserts it into the Azure Cosmos DB container, and returns the count of the items inserted. In this example, we're using the ToDoList sample from the [Quickstart .NET API for NoSQL](quickstart-dotnet.md).
64
64
65
65
```javascript
66
66
functioncreateToDoItems(items) {
@@ -101,7 +101,7 @@ function createToDoItems(items) {
101
101
102
102
### Arrays as input parameters for stored procedures
103
103
104
-
When you define 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 a 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:
104
+
When you define a stored procedure in the 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 a 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:
105
105
106
106
```javascript
107
107
functionsample(arr) {
@@ -119,13 +119,12 @@ function sample(arr) {
119
119
You can implement transactions on items within a container by using a stored procedure. The following example uses transactions within a fantasy football gaming app to trade players between two teams in a single operation. The stored procedure attempts to read the two Azure Cosmos DB items, each corresponding to the player IDs passed in as an argument. If both players are found, then the stored procedure updates the items by swapping their teams. If any errors are encountered along the way, the stored procedure throws a JavaScript exception that implicitly aborts the transaction.
120
120
121
121
```javascript
122
-
// JavaScript source code
123
122
functiontradePlayers(playerId1, playerId2) {
124
123
var context =getContext();
125
124
var container =context.getCollection();
126
125
var response =context.getResponse();
127
126
128
-
varplayer1Document, player2Document;
127
+
varplayer1Item, player2Item;
129
128
130
129
// query for players
131
130
var filterQuery =
@@ -138,7 +137,7 @@ function tradePlayers(playerId1, playerId2) {
138
137
function (err, items, responseOptions) {
139
138
if (err) thrownewError("Error"+err.message);
140
139
141
-
if (items.length!=1) throw"Unable to find both names";
140
+
if (items.length!=1) throw"Unable to find player 1";
142
141
player1Item = items[0];
143
142
144
143
var filterQuery2 =
@@ -148,8 +147,8 @@ function tradePlayers(playerId1, playerId2) {
148
147
};
149
148
var accept2 =container.queryDocuments(container.getSelfLink(), filterQuery2, {},
150
149
function (err2, items2, responseOptions2) {
151
-
if (err2) thrownewError("Error"+err2.message);
152
-
if (items2.length!=1) throw"Unable to find both names";
150
+
if (err2) thrownewError("Error"+err2.message);
151
+
if (items2.length!=1) throw"Unable to find player 2";
153
152
player2Item = items2[0];
154
153
swapTeams(player1Item, player2Item);
155
154
return;
@@ -191,7 +190,7 @@ function bulkImport(items) {
191
190
var container =getContext().getCollection();
192
191
var containerLink =container.getSelfLink();
193
192
194
-
// The count of imported items, also used as current item index.
193
+
// The count of imported items, also used as the current item index.
195
194
var count =0;
196
195
197
196
// Validate input.
@@ -213,9 +212,9 @@ function bulkImport(items) {
213
212
functiontryCreate(item, callback) {
214
213
var isAccepted =container.createDocument(containerLink, item, callback);
215
214
216
-
// If the request was accepted, callback will be called.
217
-
// Otherwise report current count back to the client,
218
-
// which will call the script again with remaining set of items.
215
+
// If the request was accepted, the callback will be called.
216
+
// Otherwise report the current count back to the client,
217
+
// which will call the script again with the remaining set of items.
219
218
if (!isAccepted) getContext().getResponse().setBody(count);
220
219
}
221
220
@@ -230,7 +229,7 @@ function bulkImport(items) {
230
229
// If we created all items, we are done. Just set the response.
231
230
getContext().getResponse().setBody(count);
232
231
} else {
233
-
// Create next document.
232
+
// Create the next document.
234
233
tryCreate(items[count], callback);
235
234
}
236
235
}
@@ -397,13 +396,13 @@ For examples of how to register and use a UDF, see [How to work with user-define
397
396
398
397
## Logging
399
398
400
-
When using stored procedure, triggers, or UDFs, you can log the steps by enabling script logging. A string for debugging is generated when `EnableScriptLogging` is set to *true*, as shown in the following examples:
399
+
When using stored procedures, triggers, or UDFs, you can log the steps by enabling script logging. A string for debugging is generated when `EnableScriptLogging` is set to *true*, as shown in the following examples:
401
400
402
401
# [JavaScript](#tab/javascript)
403
402
404
403
```javascript
405
404
let requestOptions = { enableScriptLogging:true };
0 commit comments