Skip to content

Commit 730a4e6

Browse files
authored
Merge pull request #249925 from ejizba/ej/v4bindings3
Add Node.js model v4 trigger/binding samples
2 parents f5527b8 + 1e1e33b commit 730a4e6

34 files changed

+1422
-196
lines changed

.openpublishing.publish.config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,12 @@
248248
"branch": "main",
249249
"branch_mapping": {}
250250
},
251+
{
252+
"path_to_root": "azure-functions-nodejs-v4",
253+
"url": "https://github.com/Azure/azure-functions-nodejs-samples",
254+
"branch": "main",
255+
"branch_mapping": {}
256+
},
251257
{
252258
"path_to_root": "azure-functions-durable-js",
253259
"url": "https://github.com/Azure/azure-functions-durable-js",

articles/azure-functions/functions-bindings-cosmosdb-v2-input.md

Lines changed: 144 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ms.topic: reference
55
ms.date: 03/02/2023
66
ms.devlang: csharp, java, javascript, powershell, python
77
ms.custom: devx-track-csharp, devx-track-python, ignite-2022, devx-track-extended-java, devx-track-js
8-
zone_pivot_groups: programming-languages-set-functions-lang-workers
8+
zone_pivot_groups: programming-languages-set-functions
99
---
1010

1111
# Azure Cosmos DB input binding for Azure Functions 2.x and higher
@@ -18,6 +18,9 @@ For information on setup and configuration details, see the [overview](./functio
1818
> When the collection is [partitioned](../cosmos-db/partitioning-overview.md#logical-partitions), lookup operations must also specify the partition key value.
1919
>
2020
21+
::: zone pivot="programming-language-javascript,programming-language-typescript"
22+
[!INCLUDE [functions-nodejs-model-tabs-description](../../includes/functions-nodejs-model-tabs-description.md)]
23+
::: zone-end
2124
::: zone pivot="programming-language-python"
2225
Azure Functions supports two programming models for Python. The way that you define your bindings depends on your chosen programming model.
2326

@@ -724,6 +727,82 @@ public class DocsFromRouteSqlQuery {
724727
}
725728
```
726729

730+
::: zone-end
731+
::: zone pivot="programming-language-typescript"
732+
733+
This section contains the following examples that read a single document by specifying an ID value from various sources:
734+
735+
* [Queue trigger, look up ID from JSON](#queue-trigger-look-up-id-from-json-typescript)
736+
* [HTTP trigger, look up ID from query string](#http-trigger-look-up-id-from-query-string-typescript)
737+
* [HTTP trigger, look up ID from route data](#http-trigger-look-up-id-from-route-data-typescript)
738+
* [Queue trigger, get multiple docs, using SqlQuery](#queue-trigger-get-multiple-docs-using-sqlquery-typescript)
739+
740+
<a id="queue-trigger-look-up-id-from-json-typescript"></a>
741+
742+
### Queue trigger, look up ID from JSON
743+
744+
The following example shows a [TypeScript function](functions-reference-node.md?tabs=typescript) that reads a single document and updates the document's text value.
745+
746+
# [Model v4](#tab/nodejs-v4)
747+
748+
:::code language="typescript" source="~/azure-functions-nodejs-v4/ts/src/functions/cosmosInput1.ts" :::
749+
750+
# [Model v3](#tab/nodejs-v3)
751+
752+
TypeScript samples are not documented for model v3.
753+
754+
---
755+
756+
<a id="http-trigger-look-up-id-from-query-string-typescript"></a>
757+
758+
### HTTP trigger, look up ID from query string
759+
760+
The following example shows a [TypeScript function](functions-reference-node.md?tabs=typescript) that retrieves a single document. The function is triggered by an HTTP request that uses a query string to specify the ID and partition key value to look up. That ID and partition key value are used to retrieve a `ToDoItem` document from the specified database and collection.
761+
762+
# [Model v4](#tab/nodejs-v4)
763+
764+
:::code language="typescript" source="~/azure-functions-nodejs-v4/ts/src/functions/cosmosInput2.ts" :::
765+
766+
# [Model v3](#tab/nodejs-v3)
767+
768+
TypeScript samples are not documented for model v3.
769+
770+
---
771+
772+
<a id="http-trigger-look-up-id-from-route-data-typescript"></a>
773+
774+
### HTTP trigger, look up ID from route data
775+
776+
The following example shows a [TypeScript function](functions-reference-node.md?tabs=typescript) that retrieves a single document. The function is triggered by an HTTP request that uses route data to specify the ID and partition key value to look up. That ID and partition key value are used to retrieve a `ToDoItem` document from the specified database and collection.
777+
778+
# [Model v4](#tab/nodejs-v4)
779+
780+
:::code language="typescript" source="~/azure-functions-nodejs-v4/ts/src/functions/cosmosInput3.ts" :::
781+
782+
# [Model v3](#tab/nodejs-v3)
783+
784+
TypeScript samples are not documented for model v3.
785+
786+
---
787+
788+
<a id="queue-trigger-get-multiple-docs-using-sqlquery-typescript"></a>
789+
790+
### Queue trigger, get multiple docs, using SqlQuery
791+
792+
The following example shows a [TypeScript function](functions-reference-node.md?tabs=typescript) that retrieves multiple documents specified by a SQL query, using a queue trigger to customize the query parameters.
793+
794+
The queue trigger provides a parameter `departmentId`. A queue message of `{ "departmentId" : "Finance" }` would return all records for the finance department.
795+
796+
# [Model v4](#tab/nodejs-v4)
797+
798+
:::code language="typescript" source="~/azure-functions-nodejs-v4/ts/src/functions/cosmosInput4.ts" :::
799+
800+
# [Model v3](#tab/nodejs-v3)
801+
802+
TypeScript samples are not documented for model v3.
803+
804+
---
805+
727806
::: zone-end
728807
::: zone pivot="programming-language-javascript"
729808

@@ -738,7 +817,13 @@ This section contains the following examples that read a single document by spec
738817

739818
### Queue trigger, look up ID from JSON
740819

741-
The following example shows an Azure Cosmos DB input binding in a *function.json* file and a [JavaScript function](functions-reference-node.md) that uses the binding. The function reads a single document and updates the document's text value.
820+
The following example shows a [JavaScript function](functions-reference-node.md) that reads a single document and updates the document's text value.
821+
822+
# [Model v4](#tab/nodejs-v4)
823+
824+
:::code language="javascript" source="~/azure-functions-nodejs-v4/js/src/functions/cosmosInput1.js" :::
825+
826+
# [Model v3](#tab/nodejs-v3)
742827

743828
Here's the binding data in the *function.json* file:
744829

@@ -777,12 +862,20 @@ Here's the JavaScript code:
777862
};
778863
```
779864

865+
---
866+
780867
<a id="http-trigger-look-up-id-from-query-string-javascript"></a>
781868

782869
### HTTP trigger, look up ID from query string
783870

784871
The following example shows a [JavaScript function](functions-reference-node.md) that retrieves a single document. The function is triggered by an HTTP request that uses a query string to specify the ID and partition key value to look up. That ID and partition key value are used to retrieve a `ToDoItem` document from the specified database and collection.
785872

873+
# [Model v4](#tab/nodejs-v4)
874+
875+
:::code language="javascript" source="~/azure-functions-nodejs-v4/js/src/functions/cosmosInput2.js" :::
876+
877+
# [Model v3](#tab/nodejs-v3)
878+
786879
Here's the *function.json* file:
787880

788881
```json
@@ -833,13 +926,20 @@ module.exports = async function (context, req, toDoItem) {
833926
}
834927
};
835928
```
929+
---
836930

837931
<a id="http-trigger-look-up-id-from-route-data-javascript"></a>
838932

839933
### HTTP trigger, look up ID from route data
840934

841935
The following example shows a [JavaScript function](functions-reference-node.md) that retrieves a single document. The function is triggered by an HTTP request that uses route data to specify the ID and partition key value to look up. That ID and partition key value are used to retrieve a `ToDoItem` document from the specified database and collection.
842936

937+
# [Model v4](#tab/nodejs-v4)
938+
939+
:::code language="javascript" source="~/azure-functions-nodejs-v4/js/src/functions/cosmosInput3.js" :::
940+
941+
# [Model v3](#tab/nodejs-v3)
942+
843943
Here's the *function.json* file:
844944

845945
```json
@@ -892,14 +992,22 @@ module.exports = async function (context, req, toDoItem) {
892992
};
893993
```
894994

995+
---
996+
895997
<a id="queue-trigger-get-multiple-docs-using-sqlquery-javascript"></a>
896998

897999
### Queue trigger, get multiple docs, using SqlQuery
8981000

899-
The following example shows an Azure Cosmos DB input binding in a *function.json* file and a [JavaScript function](functions-reference-node.md) that uses the binding. The function retrieves multiple documents specified by a SQL query, using a queue trigger to customize the query parameters.
1001+
The following example shows a [JavaScript function](functions-reference-node.md) that retrieves multiple documents specified by a SQL query, using a queue trigger to customize the query parameters.
9001002

9011003
The queue trigger provides a parameter `departmentId`. A queue message of `{ "departmentId" : "Finance" }` would return all records for the finance department.
9021004

1005+
# [Model v4](#tab/nodejs-v4)
1006+
1007+
:::code language="javascript" source="~/azure-functions-nodejs-v4/js/src/functions/cosmosInput4.js" :::
1008+
1009+
# [Model v3](#tab/nodejs-v3)
1010+
9031011
Here's the binding data in the *function.json* file:
9041012

9051013
```json
@@ -921,13 +1029,14 @@ Here's the JavaScript code:
9211029
```javascript
9221030
module.exports = async function (context, input) {
9231031
var documents = context.bindings.documents;
924-
for (var i = 0; i < documents.length; i++) {
925-
var document = documents[i];
1032+
for (const document of documents) {
9261033
// operate on each document
9271034
}
9281035
};
9291036
```
9301037

1038+
---
1039+
9311040
::: zone-end
9321041
::: zone pivot="programming-language-powershell"
9331042

@@ -1459,18 +1568,34 @@ From the [Java functions runtime library](/java/api/overview/azure/functions/run
14591568
+ [sqlQuery](/java/api/com.microsoft.azure.functions.annotation.cosmosdbinput.sqlquery)
14601569

14611570
::: zone-end
1462-
::: zone pivot="programming-language-javascript,programming-language-powershell,programming-language-python"
1571+
::: zone pivot="programming-language-javascript,programming-language-typescript,programming-language-powershell,programming-language-python"
14631572
## Configuration
14641573
::: zone-end
14651574

14661575
::: zone pivot="programming-language-python"
14671576
_Applies only to the Python v1 programming model._
14681577

14691578
::: zone-end
1470-
::: zone pivot="programming-language-javascript,programming-language-powershell,programming-language-python"
1579+
::: zone pivot="programming-language-javascript,programming-language-typescript"
1580+
1581+
# [Model v4](#tab/nodejs-v4)
1582+
1583+
The following table explains the properties that you can set on the `options` object passed to the `input.cosmosDB()` method. The `type`, `direction`, and `name` properties don't apply to the v4 model.
1584+
1585+
# [Model v3](#tab/nodejs-v3)
14711586

14721587
The following table explains the binding configuration properties that you set in the *function.json* file, where properties differ by extension version:
14731588

1589+
---
1590+
1591+
::: zone-end
1592+
::: zone pivot="programming-language-powershell,programming-language-python"
1593+
1594+
The following table explains the binding configuration properties that you set in the *function.json* file, where properties differ by extension version:
1595+
1596+
::: zone-end
1597+
::: zone pivot="programming-language-javascript,programming-language-typescript,programming-language-powershell,programming-language-python"
1598+
14741599
# [Extension 4.x+](#tab/extensionv4)
14751600

14761601
[!INCLUDE [functions-cosmosdb-settings-v4](../../includes/functions-cosmosdb-input-settings-v4.md)]
@@ -1531,8 +1656,18 @@ See [Binding types](./functions-bindings-cosmosdb-v2.md?tabs=isolated-process%2C
15311656
::: zone pivot="programming-language-java"
15321657
From the [Java functions runtime library](/java/api/overview/azure/functions/runtime), the [@CosmosDBInput](/java/api/com.microsoft.azure.functions.annotation.cosmosdbinput) annotation exposes Azure Cosmos DB data to the function. This annotation can be used with native Java types, POJOs, or nullable values using `Optional<T>`.
15331658
::: zone-end
1534-
::: zone pivot="programming-language-javascript,programming-language-powershell"
1535-
Updates are not made automatically upon function exit. Instead, use `context.bindings.<documentName>In` and `context.bindings.<documentName>Out` to make updates. See the [JavaScript example](#example) for more detail.
1659+
::: zone pivot="programming-language-javascript,programming-language-typescript"
1660+
1661+
# [Model v4](#tab/nodejs-v4)
1662+
1663+
Access the document by using `context.extraInputs.get()`.
1664+
1665+
# [Model v3](#tab/nodejs-v3)
1666+
1667+
Access the document by using `context.bindings.<name>` where `<name>` is the value specified in the `name` property of *function.json*.
1668+
1669+
---
1670+
15361671
::: zone-end
15371672
::: zone pivot="programming-language-powershell"
15381673
Updates to documents are not made automatically upon function exit. To update documents in a function use an [output binding](./functions-bindings-cosmosdb-v2-input.md). See the [PowerShell example](#example) for more detail.

0 commit comments

Comments
 (0)