Skip to content

Commit 0148360

Browse files
Merge pull request #252155 from ejizba/ej/moreBindings
Add Node.js model v4 samples for sql and table bindings
2 parents c1de18c + 6cc07ac commit 0148360

5 files changed

+369
-76
lines changed

articles/azure-functions/functions-bindings-azure-sql-input.md

Lines changed: 119 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ms.custom: event-tier1-build-2022, build-2023, devx-track-extended-java, devx-tr
77
ms.date: 4/17/2023
88
ms.author: drskwier
99
ms.reviewer: glenga
10-
zone_pivot_groups: programming-languages-set-functions-lang-workers
10+
zone_pivot_groups: programming-languages-set-functions
1111
---
1212

1313
# Azure SQL input binding for Azure Functions
@@ -16,6 +16,10 @@ When a function runs, the Azure SQL input binding retrieves data from a database
1616

1717
For information on setup and configuration details, see the [overview](./functions-bindings-azure-sql.md).
1818

19+
::: zone pivot="programming-language-javascript,programming-language-typescript"
20+
[!INCLUDE [functions-nodejs-model-tabs-description](../../includes/functions-nodejs-model-tabs-description.md)]
21+
::: zone-end
22+
1923
## Examples
2024
<a id="example"></a>
2125

@@ -510,7 +514,7 @@ public class DeleteToDo {
510514

511515
::: zone-end
512516

513-
::: zone pivot="programming-language-javascript"
517+
::: zone pivot="programming-language-javascript,programming-language-typescript"
514518

515519
More samples for the Azure SQL input binding are available in the [GitHub repository](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-js).
516520

@@ -527,7 +531,29 @@ The examples refer to a database table:
527531
<a id="http-trigger-get-multiple-items-javascript"></a>
528532
### HTTP trigger, get multiple rows
529533

530-
The following example shows a SQL input binding in a function.json file and a JavaScript function that reads from a query and returns the results in the HTTP response.
534+
The following example shows a SQL input binding that reads from a query and returns the results in the HTTP response.
535+
536+
::: zone-end
537+
::: zone pivot="programming-language-typescript"
538+
539+
# [Model v4](#tab/nodejs-v4)
540+
541+
:::code language="typescript" source="~/azure-functions-nodejs-v4/ts/src/functions/sqlInput1.ts" :::
542+
543+
# [Model v3](#tab/nodejs-v3)
544+
545+
TypeScript samples are not documented for model v3.
546+
547+
---
548+
549+
::: zone-end
550+
::: zone pivot="programming-language-javascript"
551+
552+
# [Model v4](#tab/nodejs-v4)
553+
554+
:::code language="javascript" source="~/azure-functions-nodejs-v4/js/src/functions/sqlInput1.js" :::
555+
556+
# [Model v3](#tab/nodejs-v3)
531557

532558
The following is binding data in the function.json file:
533559

@@ -560,7 +586,6 @@ The [configuration](#configuration) section explains these properties.
560586

561587
The following is sample JavaScript code:
562588

563-
564589
```javascript
565590
module.exports = async function (context, req, todoItems) {
566591
context.log('JavaScript HTTP trigger and SQL input binding function processed a request.');
@@ -573,10 +598,37 @@ module.exports = async function (context, req, todoItems) {
573598
}
574599
```
575600

601+
---
602+
603+
::: zone-end
604+
::: zone pivot="programming-language-javascript,programming-language-typescript"
605+
576606
<a id="http-trigger-look-up-id-from-query-string-javascript"></a>
577607
### HTTP trigger, get row by ID from query string
578608

579-
The following example shows a SQL input binding in a JavaScript function that reads from a query filtered by a parameter from the query string and returns the row in the HTTP response.
609+
The following example shows a SQL input binding that reads from a query filtered by a parameter from the query string and returns the row in the HTTP response.
610+
611+
::: zone-end
612+
::: zone pivot="programming-language-typescript"
613+
614+
# [Model v4](#tab/nodejs-v4)
615+
616+
:::code language="typescript" source="~/azure-functions-nodejs-v4/ts/src/functions/sqlInput2.ts" :::
617+
618+
# [Model v3](#tab/nodejs-v3)
619+
620+
TypeScript samples are not documented for model v3.
621+
622+
---
623+
624+
::: zone-end
625+
::: zone pivot="programming-language-javascript"
626+
627+
# [Model v4](#tab/nodejs-v4)
628+
629+
:::code language="javascript" source="~/azure-functions-nodejs-v4/js/src/functions/sqlInput2.js" :::
630+
631+
# [Model v3](#tab/nodejs-v3)
580632

581633
The following is binding data in the function.json file:
582634

@@ -610,7 +662,6 @@ The [configuration](#configuration) section explains these properties.
610662

611663
The following is sample JavaScript code:
612664

613-
614665
```javascript
615666
module.exports = async function (context, req, todoItem) {
616667
context.log('JavaScript HTTP trigger and SQL input binding function processed a request.');
@@ -623,15 +674,41 @@ module.exports = async function (context, req, todoItem) {
623674
}
624675
```
625676

677+
---
678+
679+
::: zone-end
680+
::: zone pivot="programming-language-javascript,programming-language-typescript"
681+
626682
<a id="http-trigger-delete-one-or-multiple-rows-javascript"></a>
627683
### HTTP trigger, delete rows
628684

629-
The following example shows a SQL input binding in a function.json file and a JavaScript function that executes a stored procedure with input from the HTTP request query parameter.
685+
The following example shows a SQL input binding that executes a stored procedure with input from the HTTP request query parameter.
630686

631687
The stored procedure `dbo.DeleteToDo` must be created on the database. In this example, the stored procedure deletes a single record or all records depending on the value of the parameter.
632688

633689
:::code language="sql" source="~/functions-sql-todo-sample/sql/create.sql" range="11-25":::
634690

691+
::: zone-end
692+
::: zone pivot="programming-language-typescript"
693+
694+
# [Model v4](#tab/nodejs-v4)
695+
696+
:::code language="typescript" source="~/azure-functions-nodejs-v4/ts/src/functions/sqlInput3.ts" :::
697+
698+
# [Model v3](#tab/nodejs-v3)
699+
700+
TypeScript samples are not documented for model v3.
701+
702+
---
703+
704+
::: zone-end
705+
::: zone pivot="programming-language-javascript"
706+
707+
# [Model v4](#tab/nodejs-v4)
708+
709+
:::code language="javascript" source="~/azure-functions-nodejs-v4/js/src/functions/sqlInput3.js" :::
710+
711+
# [Model v3](#tab/nodejs-v3)
635712

636713
```json
637714
{
@@ -676,9 +753,9 @@ module.exports = async function (context, req, todoItems) {
676753
}
677754
```
678755

679-
::: zone-end
680-
756+
---
681757

758+
::: zone-end
682759
::: zone pivot="programming-language-powershell"
683760

684761
More samples for the Azure SQL input binding are available in the [GitHub repository](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-powershell).
@@ -1053,8 +1130,39 @@ In the [Java functions runtime library](/java/api/overview/azure/functions/runti
10531130
| **parameters** | Optional. Zero or more parameter values passed to the command during execution as a single string. Must follow the format `@param1=param1,@param2=param2`. Neither the parameter name nor the parameter value can contain a comma (`,`) or an equals sign (`=`). |
10541131

10551132
::: zone-end
1056-
1057-
::: zone pivot="programming-language-javascript,programming-language-powershell,programming-language-python"
1133+
::: zone pivot="programming-language-javascript,programming-language-typescript"
1134+
1135+
## Configuration
1136+
1137+
# [Model v4](#tab/nodejs-v4)
1138+
1139+
The following table explains the properties that you can set on the `options` object passed to the `input.sql()` method.
1140+
1141+
| Property | Description |
1142+
|---------|----------------------|
1143+
| **commandText** | Required. The Transact-SQL query command or name of the stored procedure executed by the binding. |
1144+
| **connectionStringSetting** | Required. The name of an app setting that contains the connection string for the database against which the query or stored procedure is being executed. This value isn't the actual connection string and must instead resolve to an environment variable name. Optional keywords in the connection string value are [available to refine SQL bindings connectivity](./functions-bindings-azure-sql.md#sql-connection-string). |
1145+
| **commandType** | Required. A [CommandType](/dotnet/api/system.data.commandtype) value, which is [Text](/dotnet/api/system.data.commandtype#fields) for a query and [StoredProcedure](/dotnet/api/system.data.commandtype#fields) for a stored procedure. |
1146+
| **parameters** | Optional. Zero or more parameter values passed to the command during execution as a single string. Must follow the format `@param1=param1,@param2=param2`. Neither the parameter name nor the parameter value can contain a comma (`,`) or an equals sign (`=`). |
1147+
1148+
# [Model v3](#tab/nodejs-v3)
1149+
1150+
The following table explains the binding configuration properties that you set in the function.json file.
1151+
1152+
| Property | Description |
1153+
|---------|----------------------|
1154+
|**type** | Required. Must be set to `sql`. |
1155+
|**direction** | Required. Must be set to `in`. |
1156+
|**name** | Required. The name of the variable that represents the query results in function code. |
1157+
| **commandText** | Required. The Transact-SQL query command or name of the stored procedure executed by the binding. |
1158+
| **connectionStringSetting** | Required. The name of an app setting that contains the connection string for the database against which the query or stored procedure is being executed. This value isn't the actual connection string and must instead resolve to an environment variable name. Optional keywords in the connection string value are [available to refine SQL bindings connectivity](./functions-bindings-azure-sql.md#sql-connection-string). |
1159+
| **commandType** | Required. A [CommandType](/dotnet/api/system.data.commandtype) value, which is [Text](/dotnet/api/system.data.commandtype#fields) for a query and [StoredProcedure](/dotnet/api/system.data.commandtype#fields) for a stored procedure. |
1160+
| **parameters** | Optional. Zero or more parameter values passed to the command during execution as a single string. Must follow the format `@param1=param1,@param2=param2`. Neither the parameter name nor the parameter value can contain a comma (`,`) or an equals sign (`=`). |
1161+
1162+
---
1163+
1164+
::: zone-end
1165+
::: zone pivot="programming-language-powershell,programming-language-python"
10581166
## Configuration
10591167

10601168
The following table explains the binding configuration properties that you set in the function.json file.
@@ -1075,17 +1183,12 @@ The following table explains the binding configuration properties that you set i
10751183

10761184
## Usage
10771185

1078-
::: zone pivot="programming-language-csharp,programming-language-javascript,programming-language-powershell,programming-language-python,programming-language-java"
1079-
10801186
The attribute's constructor takes the SQL command text, the command type, parameters, and the connection string setting name. The command can be a Transact-SQL (T-SQL) query with the command type `System.Data.CommandType.Text` or stored procedure name with the command type `System.Data.CommandType.StoredProcedure`. The connection string setting name corresponds to the application setting (in `local.settings.json` for local development) that contains the [connection string](/dotnet/api/microsoft.data.sqlclient.sqlconnection.connectionstring?view=sqlclient-dotnet-core-5.0&preserve-view=true#Microsoft_Data_SqlClient_SqlConnection_ConnectionString) to the Azure SQL or SQL Server instance.
10811187

10821188
Queries executed by the input binding are [parameterized](/dotnet/api/microsoft.data.sqlclient.sqlparameter) in Microsoft.Data.SqlClient to reduce the risk of [SQL injection](/sql/relational-databases/security/sql-injection) from the parameter values passed into the binding.
10831189

10841190
If an exception occurs when a SQL input binding is executed then the function code will not execute. This may result in an error code being returned, such as an HTTP trigger returning a 500 error code.
10851191

1086-
1087-
::: zone-end
1088-
10891192
## Next steps
10901193

10911194
- [Save data to a database (Output binding)](./functions-bindings-azure-sql-output.md)

0 commit comments

Comments
 (0)