Skip to content

Commit 2648cfb

Browse files
authored
Add support for sql trigger (#240)
1 parent 7e61ae1 commit 2648cfb

File tree

6 files changed

+68
-1
lines changed

6 files changed

+68
-1
lines changed

src/app.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
HttpMethodFunctionOptions,
1515
ServiceBusQueueFunctionOptions,
1616
ServiceBusTopicFunctionOptions,
17+
SqlFunctionOptions,
1718
StorageBlobFunctionOptions,
1819
StorageQueueFunctionOptions,
1920
TimerFunctionOptions,
@@ -128,6 +129,10 @@ export function warmup(name: string, options: WarmupFunctionOptions): void {
128129
generic(name, convertToGenericOptions(options, trigger.warmup));
129130
}
130131

132+
export function sql(name: string, options: SqlFunctionOptions): void {
133+
generic(name, convertToGenericOptions(options, trigger.sql));
134+
}
135+
131136
export function generic(name: string, options: GenericFunctionOptions): void {
132137
if (!hasSetModel) {
133138
setProgrammingModel();

src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@ export { InvocationContext } from './InvocationContext';
1515
export * as output from './output';
1616
export * as trigger from './trigger';
1717
export { Disposable } from './utils/Disposable';
18+
19+
export enum SqlChangeOperation {
20+
Insert = 0,
21+
Update = 1,
22+
Delete = 2,
23+
}

src/trigger.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import {
1616
ServiceBusQueueTriggerOptions,
1717
ServiceBusTopicTrigger,
1818
ServiceBusTopicTriggerOptions,
19+
SqlTrigger,
20+
SqlTriggerOptions,
1921
StorageBlobTrigger,
2022
StorageBlobTriggerOptions,
2123
StorageQueueTrigger,
@@ -99,6 +101,13 @@ export function warmup(options: WarmupTriggerOptions): WarmupTrigger {
99101
});
100102
}
101103

104+
export function sql(options: SqlTriggerOptions): SqlTrigger {
105+
return addTriggerBindingName({
106+
...options,
107+
type: 'sqlTrigger',
108+
});
109+
}
110+
102111
export function generic(options: GenericTriggerOptions): FunctionTrigger {
103112
return addTriggerBindingName(options);
104113
}

types/app.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { GenericFunctionOptions } from './generic';
88
import { HttpFunctionOptions, HttpHandler, HttpMethodFunctionOptions } from './http';
99
import { ServiceBusQueueFunctionOptions, ServiceBusTopicFunctionOptions } from './serviceBus';
1010
import { SetupOptions } from './setup';
11+
import { SqlFunctionOptions } from './sql';
1112
import { StorageBlobFunctionOptions, StorageQueueFunctionOptions } from './storage';
1213
import { TimerFunctionOptions } from './timer';
1314
import { WarmupFunctionOptions } from './warmup';
@@ -163,6 +164,13 @@ export function cosmosDB(name: string, options: CosmosDBFunctionOptions): void;
163164
*/
164165
export function warmup(name: string, options: WarmupFunctionOptions): void;
165166

167+
/**
168+
* Registers a SQL function in your app that will be triggered when a row is created, updated, or deleted
169+
* @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
170+
* @param options Configuration options describing the inputs, outputs, and handler for this function
171+
*/
172+
export function sql(name: string, options: SqlFunctionOptions): void;
173+
166174
/**
167175
* Registers a generic function in your app that will be triggered based on the type specified in `options.trigger.type`
168176
* Use this method if your desired trigger type does not already have its own method

types/sql.d.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,40 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
import { FunctionInput, FunctionOutput } from './index';
4+
import { FunctionInput, FunctionOptions, FunctionOutput, FunctionResult, FunctionTrigger } from './index';
5+
import { InvocationContext } from './InvocationContext';
6+
7+
export type SqlHandler = (changes: SqlChange[], context: InvocationContext) => FunctionResult;
8+
9+
export interface SqlFunctionOptions extends SqlTriggerOptions, Partial<FunctionOptions> {
10+
handler: SqlHandler;
11+
12+
trigger?: SqlTrigger;
13+
}
14+
15+
export interface SqlTriggerOptions {
16+
/**
17+
* The name of the table monitored by the trigger.
18+
*/
19+
tableName: string;
20+
21+
/**
22+
* An app setting (or environment variable) with the connection string for the database containing the table monitored for changes
23+
*/
24+
connectionStringSetting: string;
25+
}
26+
export type SqlTrigger = FunctionTrigger & SqlTriggerOptions;
27+
28+
export interface SqlChange {
29+
Item: unknown;
30+
Operation: SqlChangeOperation;
31+
}
32+
33+
export enum SqlChangeOperation {
34+
Insert = 0,
35+
Update = 1,
36+
Delete = 2,
37+
}
538

639
export interface SqlInputOptions {
740
/**

types/trigger.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
ServiceBusTopicTrigger,
1414
ServiceBusTopicTriggerOptions,
1515
} from './serviceBus';
16+
import { SqlTrigger, SqlTriggerOptions } from './sql';
1617
import {
1718
StorageBlobTrigger,
1819
StorageBlobTriggerOptions,
@@ -72,6 +73,11 @@ export function cosmosDB(options: CosmosDBTriggerOptions): CosmosDBTrigger;
7273
*/
7374
export function warmup(options: WarmupTriggerOptions): WarmupTrigger;
7475

76+
/**
77+
* [Link to docs and examples](https://docs.microsoft.com/azure/azure-functions/functions-bindings-azure-sql-trigger?pivots=programming-language-javascript)
78+
*/
79+
export function sql(options: SqlTriggerOptions): SqlTrigger;
80+
7581
/**
7682
* A generic option that can be used for any trigger type
7783
* Use this method if your desired trigger type does not already have its own method

0 commit comments

Comments
 (0)