メインコンテンツまでスキップ

トリガー

データベースの変更に応じて関数を実行します。

実行可能な関数を作成するには、基本の 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 デコレーターの第三パラメーターとして integration ID を指定する必要があります。例えば:

Backend code
@trigger('userChange', 'users', 'HrDatabase') // id, collection, integration

トリガーは、以下のパラメーターを取ります:

名前説明
idstringトリガーの id。ユニークである必要があります。
collectionNamestringトリガー対象のコレクションの名前。
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
}

詳細については、trigger デコレーターの API documentation をご覧ください。