Skip to content

Commit

Permalink
Add support for sql trigger (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
ejizba committed Mar 28, 2024
1 parent 7e61ae1 commit 2648cfb
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
HttpMethodFunctionOptions,
ServiceBusQueueFunctionOptions,
ServiceBusTopicFunctionOptions,
SqlFunctionOptions,
StorageBlobFunctionOptions,
StorageQueueFunctionOptions,
TimerFunctionOptions,
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
9 changes: 9 additions & 0 deletions src/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
ServiceBusQueueTriggerOptions,
ServiceBusTopicTrigger,
ServiceBusTopicTriggerOptions,
SqlTrigger,
SqlTriggerOptions,
StorageBlobTrigger,
StorageBlobTriggerOptions,
StorageQueueTrigger,
Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 8 additions & 0 deletions types/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down
35 changes: 34 additions & 1 deletion types/sql.d.ts
Original file line number Diff line number Diff line change
@@ -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<FunctionOptions> {
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 {
/**
Expand Down
6 changes: 6 additions & 0 deletions types/trigger.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
ServiceBusTopicTrigger,
ServiceBusTopicTriggerOptions,
} from './serviceBus';
import { SqlTrigger, SqlTriggerOptions } from './sql';
import {
StorageBlobTrigger,
StorageBlobTriggerOptions,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2648cfb

Please sign in to comment.