トリガー
データベースの変更に応じて関数を実行します。
実行可能な関数を作成するには、基本の SquidService クラスを拡張したクラス内で、@trigger デコレーターを使用して関数を装飾します。
以下の例は、組み込みデータベース内の users コレクションに変更があった場合に実行されるトリガーを示しています:
Backend code
import { SquidService, trigger, TriggerRequest } from '@squidcloud/backend';
export class ExampleService extends SquidService {
@trigger('userChange', 'users')
async handleUserChange(request: TriggerRequest): Promise<void> {
// TODO - add your business logic here
}
}
もし関数が組み込みデータベース以外のデータベースによってトリガーされた場合、@trigger デコレーターの第三引数として統合IDを指定する必要があります。例えば:
Backend code
@trigger('userChange', 'users', 'HrDatabase') // id, collection, integration
トリガーは以下のパラメータを受け取ります:
| Name | Type | Description |
|---|---|---|
id | string | トリガーのID。ユニークである必要があります。 |
collectionName | string | トリガー対象のコレクションの名前。 |
integrationId? | string | トリガー対象のコネクタのID。指定されない場合は built_in_db が使用されます。 |
トリガーリクエストは、変更に関する以下の情報を提供します:
{
squidDocId: SquidDocId; // the primary key for the document
mutationType: MutationType; // the type of operation performed
docBefore?: T; // the document's previous state
docAfter?: T; // the document's updated state
}
詳細については、トリガーデコレーターの API documentation をご確認ください.