- Azure SQL bindings for Azure Functions - JavaScript
These instructions will guide you through creating your Function Project and adding the SQL binding extension. This only needs to be done once for every function project you create. If you have one created already you can skip this step.
-
Install Azure Functions Core Tools
-
Create a Function Project for JavaScript:
mkdir MyApp cd MyApp func init --worker-runtime node --language javascript
-
Enable SQL bindings on the function project. More information can be found in the Azure SQL bindings for Azure Functions docs.
Update the
host.json
file with the extension bundle."extensionBundle": { "id": "Microsoft.Azure.Functions.ExtensionBundle", "version": "[4.*, 5.0.0)" }
See Input Binding Overview for general information about the Azure SQL Input binding.
The following table explains the binding configuration properties that you set in the function.json file.
function.json property | Description |
---|---|
type | Required. Must be set to sql . |
direction | Required. Must be set to in . |
name | Required. The name of the variable that represents the query results in function code. |
commandText | Required. The Transact-SQL query command or name of the stored procedure executed by the binding. |
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. |
commandType | Required. A CommandType value, which is Text for a query and StoredProcedure for a stored procedure. |
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 (= ). |
Note: This tutorial requires that a SQL database is setup as shown in Create a SQL Server.
-
Open your project that you created in Create a Function Project in VS Code
-
Press 'F1' and search for 'Azure Functions: Create Function'
-
Choose HttpTrigger -> (Provide a function name) -> anonymous
-
In the file that opens (
index.js
), replace themodule.exports = async function (context, req)
block with the below code.module.exports = async function (context, req, employee) { return { status: 200, body: employee }; }
-
We also need to add the SQL input binding for the
employee
parameter. Open the function.json file. -
Paste the below in the file as an additional entry to the "bindings": [] array.
{ "name": "employee", "type": "sql", "direction": "in", "commandText": "select * from Employees", "commandType": "Text", "connectionStringSetting": "SqlConnectionString" }
In the above,
select * from Employees
is the SQL script run by the input binding. The CommandType on the line below specifies whether the first line is a query or a stored procedure. On the next line, the ConnectionStringSetting specifies that the app setting that contains the SQL connection string used to connect to the database is "SqlConnectionString." For more information on this, see the function.json Properties for Input Bindings section -
Open the local.settings.json file, and in the brackets for "Values," verify there is a 'SqlConnectionString.' If not, add it.
-
Hit 'F5' to run your code. This will start up the Functions Host with a local HTTP Trigger and SQL Input Binding.
-
Click the link that appears in your terminal.
-
You should see your database output in the browser window.
-
Congratulations! You have successfully created your first SQL input binding!
The database scripts used for the following samples can be found here.
See the GetProducts sample
See the GetProductsNameEmpty sample
See the GetProductsNameNull sample
See the GetProductsStoredProcedure sample
See Output Binding Overview for general information about the Azure SQL Output binding.
The following table explains the binding configuration properties that you set in the function.json file.
function.json property | Description |
---|---|
type | Required. Must be set to sql . |
direction | Required. Must be set to out . |
name | Required. The name of the variable that represents the entity in function code. |
commandText | Required. The name of the table being written to by the binding. |
connectionStringSetting | Required. The name of an app setting that contains the connection string for the database to which data is being written. This isn't the actual connection string and must instead resolve to an environment variable. Optional keywords in the connection string value are available to refine SQL bindings connectivity. |
Note: This tutorial requires that a SQL database is setup as shown in Create a SQL Server.
-
Open your app in VS Code
-
Press 'F1' and search for 'Azure Functions: Create Function'
-
Choose HttpTrigger -> (Provide a function name) -> anonymous
-
In the file that opens (
index.js
), replace themodule.exports = async function (context, req)
block with the below code. Note that the casing of the Object field names and the table column names must match.module.exports = async function (context, req) { const employees = [ { EmployeeId : 1, FirstName : "Hello", LastName : "World", Company : "Microsoft", Team : "Functions" }, { EmployeeId : 2, FirstName : "Hi", LastName : "SQLupdate", Company : "Microsoft", Team : "Functions" } ]; context.bindings.employee = employees; return { status: 201, body: employees }; }
-
We also need to add the SQL output binding for the
context.bindings.employee
property. Open the function.json file. -
Paste the below in the file as an additional entry to the "bindings": [] array.
{ "name": "employee", "type": "sql", "direction": "out", "commandText": "dbo.Employees", "connectionStringSetting": "SqlConnectionString" }
In the above, "dbo.Employees" is the name of the table our output binding is upserting into. The line below is similar to the input binding and specifies where our SqlConnectionString is. For more information on this, see the function.json Properties for Output Bindings section
-
Hit 'F5' to run your code. Click the link to upsert the output array values in your SQL table. Your upserted values should launch in the browser.
-
Congratulations! You have successfully created your first SQL output binding!
See the AddProductsArray sample
See the AddProduct sample
See Trigger Binding Overview for general information about the Azure SQL Trigger binding.
The following table explains the binding configuration properties that you set in the function.json file.
function.json property | Description |
---|---|
name | Required. The name of the parameter that the trigger binds to. |
type | Required. Must be set to sqlTrigger . |
direction | Required. Must be set to in . |
tableName | Required. The name of the table being monitored by the trigger. |
connectionStringSetting | Required. The name of an app setting that contains the connection string for the database containing the table monitored for changes. This isn't the actual connection string and must instead resolve to an environment variable. Optional keywords in the connection string value are available to refine SQL bindings connectivity. |
leasesTableName | Optional. The name of the table used to store leases. If not specified, the leases table name will be Leases_{FunctionId}_{TableId}. More information on how this is generated can be found here. |
Note: This tutorial requires that a SQL database is setup as shown in Create a SQL Server.
-
Create a new folder
EmployeeTrigger
-
Inside
EmployeeTrigger
create a new filefunction.json
{ "bindings": [ { "name": "changes", "type": "sqlTrigger", "direction": "in", "tableName": "dbo.Employees", "connectionStringSetting": "SqlConnectionString" } ], "disabled": false }
-
Inside
EmployeeTrigger
create a new fileindex.js
module.exports = async function (context, changes) { context.log(`SQL Changes: ${JSON.stringify(changes)}`) }
-
Skip these steps if you have not completed the output binding tutorial.
- Open your output binding file and modify some of the values. For example, change the value of Team column from 'Functions' to 'Azure SQL'.
- Hit 'F5' to run your code. Click the link of the HTTP trigger from the output binding tutorial.
-
Update, insert, or delete rows in your SQL table while the function app is running and observe the function logs.
-
You should see the new log messages in the Visual Studio Code terminal containing the values of row-columns after the update operation.
-
Congratulations! You have successfully created your first SQL trigger binding!
See the Node V4 Model samples here. More information about the Node V4 Model can be found here.