SDK でトリガーを作成して使用する
Microsoft.Azure.Cosmos.Container クラスの Scripts プロパティには、コードから新しい事前または事後トリガーを作成するために使用される CreateTriggerAsync メソッドが含まれています。
Note
次の一連の例では、コンテナー変数が既に定義されていることを前提としています。
プリトリガーを作成する
まず、JavaScript で事前トリガー関数の定義を使用して文字列変数を作成します。
string preTrigger = @"function addLabel() {
var context = getContext();
var request = context.getRequest();
var pendingItem = request.getBody();
if (!('label' in pendingItem))
pendingItem['label'] = 'new';
request.setBody(pendingItem);
}";
ヒント
または、System.IO.File などのファイル API を使用して、*.js ファイルから関数を読み取ることもできます。
ここで、Id および Body プロパティをそれぞれトリガーの一意識別子とコンテンツに設定して、Microsoft.Azure.Cosmos.Scripts.TriggerProperties 型のオブジェクトを作成します。 この例では TriggerOperation プロパティを TriggerOperation.Create に設定し、TriggerType プロパティを TriggerType.Pre に設定します。
TriggerProperties properties = new()
{
Id = "addLabel",
Body = preTrigger,
TriggerOperation = TriggerOperation.Create,
TriggerType = TriggerType.Pre
};
コンテナー変数の CreateTriggerAsync メソッドを呼び出して、前に構成したプロパティを渡す新しい事前トリガーを作成します。
await container.Scripts.CreateTriggerAsync(properties);
ポストトリガーを作成する
事後トリガーの作成は、事前トリガーの作成とほぼ同じです。 まず、事後トリガー関数の JavaScript 定義を使用して文字列変数を作成します。
string postTrigger = @"function createView() {
var context = getContext();
var container = context.getCollection();
var response = context.getResponse();
var createdItem = response.getBody();
var viewItem = {
sourceId: createdItem.id,
categoryId: createdItem.categoryId,
displayName: `${createdItem.name} [${createdItem.categoryName}]`
};
var accepted = container.createDocument(
container.getSelfLink(),
viewItem,
(error, newItem) => {
if (error) throw error;
}
);
if (!accepted) return;
}";
ここで、事前トリガーで使用されたものとほぼ同じ構成で構成されている Microsoft.Azure.Cosmos.Scripts.TriggerProperties 型のオブジェクトを作成します。違いは 1 つのキーです。 TriggerType プロパティを TriggerType.Post に設定します。 次に、コンテナー変数の CreateTriggerAsync メソッドを呼び出して、前に構成したプロパティを渡す新しい事後トリガーを作成します。
TriggerProperties properties = new()
{
Id = "createView",
Body = postTrigger,
TriggerOperation = TriggerOperation.Create,
TriggerType = TriggerType.Post
};
await container.Scripts.CreateTriggerAsync(properties);
ヒント
これらの例では、create 操作に対してトリガーを排他的に作成しました。 コンテナー内の他の操作のトリガーを作成することもできます。
操作でトリガーを使用する
これで、トリガーが定義され、コンテナー内に作成されたので、それらを同じコンテナーの操作で使用できます。
匿名型を使用して C# で新しい項目を作成する例を使用してみましょう。
var newItem = new
{
id = "caab0e5e-c037-48a4-a760-140497d19452",
name = "Handlebar",
categoryId = "e89a34d2-47ee-4da8-bcf6-10f552604b79",
categoryName = "Accessories",
price = 50
};
操作を呼び出す前に、Microsoft.Azure.Cosmos.ItemRequestOptions 型のオブジェクトを作成します。 その options オブジェクト内で、この操作に対して有効にするトリガーを含めるように、PreTriggers および PostTriggers プロパティ一覧を構成します。
ItemRequestOptions options = new()
{
PreTriggers = new List<string> { "addLabel" },
PostTriggers = new List<string> { "createView" }
};
Note
トリガーは自動的には実行されないことに注意してください。これらは、実行する各データベース操作に対して指定する必要があります。
次に、作成する項目と options オブジェクトを渡すコンテナー オブジェクトの CreateItemAsync メソッドを呼び出します。
await container.CreateItemAsync(newItem, requestOptions: options);
最後に、コンテナーに対してクエリを実行すると、次の 2 つのことが起きたことがわかります。
事前トリガーによって、最初の項目に new という値を持つ label プロパティが追加された。
事後トリガーによって、データの具体化されたビューを持つ 2 つ目の項目が作成された。
[
{
"id": "caab0e5e-c037-48a4-a760-140497d19452",
"name": "Handlebar",
"categoryId": "e89a34d2-47ee-4da8-bcf6-10f552604b79",
"categoryName": "Accessories",
"price": 50,
"label": "new"
},
{
"id": "77875d1e-dac4-3b66-9f9c-ab6747d65952",
"sourceId": "caab0e5e-c037-48a4-a760-140497d19452",
"categoryId": "e89a34d2-47ee-4da8-bcf6-10f552604b79",
"displayName": "Handlebar [Accessories]"
}
]