From 2648cfb004ef8f152630b1b3d08116a8b06007fe Mon Sep 17 00:00:00 2001 From: Eric Jizba Date: Thu, 28 Mar 2024 16:06:03 -0700 Subject: [PATCH] Add support for sql trigger (#240) --- src/app.ts | 5 +++++ src/index.ts | 6 ++++++ src/trigger.ts | 9 +++++++++ types/app.d.ts | 8 ++++++++ types/sql.d.ts | 35 ++++++++++++++++++++++++++++++++++- types/trigger.d.ts | 6 ++++++ 6 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/app.ts b/src/app.ts index b9d974a..d370e10 100644 --- a/src/app.ts +++ b/src/app.ts @@ -14,6 +14,7 @@ import { HttpMethodFunctionOptions, ServiceBusQueueFunctionOptions, ServiceBusTopicFunctionOptions, + SqlFunctionOptions, StorageBlobFunctionOptions, StorageQueueFunctionOptions, TimerFunctionOptions, @@ -128,6 +129,10 @@ export function warmup(name: string, options: WarmupFunctionOptions): void { generic(name, convertToGenericOptions(options, trigger.warmup)); } +export function sql(name: string, options: SqlFunctionOptions): void { + generic(name, convertToGenericOptions(options, trigger.sql)); +} + export function generic(name: string, options: GenericFunctionOptions): void { if (!hasSetModel) { setProgrammingModel(); diff --git a/src/index.ts b/src/index.ts index 761f676..a2f0a18 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,3 +15,9 @@ export { InvocationContext } from './InvocationContext'; export * as output from './output'; export * as trigger from './trigger'; export { Disposable } from './utils/Disposable'; + +export enum SqlChangeOperation { + Insert = 0, + Update = 1, + Delete = 2, +} diff --git a/src/trigger.ts b/src/trigger.ts index d7749fd..a7fc629 100644 --- a/src/trigger.ts +++ b/src/trigger.ts @@ -16,6 +16,8 @@ import { ServiceBusQueueTriggerOptions, ServiceBusTopicTrigger, ServiceBusTopicTriggerOptions, + SqlTrigger, + SqlTriggerOptions, StorageBlobTrigger, StorageBlobTriggerOptions, StorageQueueTrigger, @@ -99,6 +101,13 @@ export function warmup(options: WarmupTriggerOptions): WarmupTrigger { }); } +export function sql(options: SqlTriggerOptions): SqlTrigger { + return addTriggerBindingName({ + ...options, + type: 'sqlTrigger', + }); +} + export function generic(options: GenericTriggerOptions): FunctionTrigger { return addTriggerBindingName(options); } diff --git a/types/app.d.ts b/types/app.d.ts index 8a6a75c..43e4dea 100644 --- a/types/app.d.ts +++ b/types/app.d.ts @@ -8,6 +8,7 @@ import { GenericFunctionOptions } from './generic'; import { HttpFunctionOptions, HttpHandler, HttpMethodFunctionOptions } from './http'; import { ServiceBusQueueFunctionOptions, ServiceBusTopicFunctionOptions } from './serviceBus'; import { SetupOptions } from './setup'; +import { SqlFunctionOptions } from './sql'; import { StorageBlobFunctionOptions, StorageQueueFunctionOptions } from './storage'; import { TimerFunctionOptions } from './timer'; import { WarmupFunctionOptions } from './warmup'; @@ -163,6 +164,13 @@ export function cosmosDB(name: string, options: CosmosDBFunctionOptions): void; */ export function warmup(name: string, options: WarmupFunctionOptions): void; +/** + * Registers a SQL function in your app that will be triggered when a row is created, updated, or deleted + * @param name The name of the function. The name must be unique within your app and will mostly be used for your own tracking purposes + * @param options Configuration options describing the inputs, outputs, and handler for this function + */ +export function sql(name: string, options: SqlFunctionOptions): void; + /** * Registers a generic function in your app that will be triggered based on the type specified in `options.trigger.type` * Use this method if your desired trigger type does not already have its own method diff --git a/types/sql.d.ts b/types/sql.d.ts index 482d4f4..5614d02 100644 --- a/types/sql.d.ts +++ b/types/sql.d.ts @@ -1,7 +1,40 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. -import { FunctionInput, FunctionOutput } from './index'; +import { FunctionInput, FunctionOptions, FunctionOutput, FunctionResult, FunctionTrigger } from './index'; +import { InvocationContext } from './InvocationContext'; + +export type SqlHandler = (changes: SqlChange[], context: InvocationContext) => FunctionResult; + +export interface SqlFunctionOptions extends SqlTriggerOptions, Partial { + handler: SqlHandler; + + trigger?: SqlTrigger; +} + +export interface SqlTriggerOptions { + /** + * The name of the table monitored by the trigger. + */ + tableName: string; + + /** + * An app setting (or environment variable) with the connection string for the database containing the table monitored for changes + */ + connectionStringSetting: string; +} +export type SqlTrigger = FunctionTrigger & SqlTriggerOptions; + +export interface SqlChange { + Item: unknown; + Operation: SqlChangeOperation; +} + +export enum SqlChangeOperation { + Insert = 0, + Update = 1, + Delete = 2, +} export interface SqlInputOptions { /** diff --git a/types/trigger.d.ts b/types/trigger.d.ts index bfae17c..864479a 100644 --- a/types/trigger.d.ts +++ b/types/trigger.d.ts @@ -13,6 +13,7 @@ import { ServiceBusTopicTrigger, ServiceBusTopicTriggerOptions, } from './serviceBus'; +import { SqlTrigger, SqlTriggerOptions } from './sql'; import { StorageBlobTrigger, StorageBlobTriggerOptions, @@ -72,6 +73,11 @@ export function cosmosDB(options: CosmosDBTriggerOptions): CosmosDBTrigger; */ export function warmup(options: WarmupTriggerOptions): WarmupTrigger; +/** + * [Link to docs and examples](https://docs.microsoft.com/azure/azure-functions/functions-bindings-azure-sql-trigger?pivots=programming-language-javascript) + */ +export function sql(options: SqlTriggerOptions): SqlTrigger; + /** * A generic option that can be used for any trigger type * Use this method if your desired trigger type does not already have its own method