Event based trigger

Triggered based on the user defined event such as Tailor DB record creation EventType: "tailordb.type_record.created".

Tutorial steps

To create an event based trigger, you'll need to:

  1. Activate the Executor service
  2. Add the trigger in executor.cue manifest file
  3. Enable PublishRecordEvents settings
  4. Apply the change using tailorctl
  5. Verify the trigger

1. Activate the Executor service

To activate the Executor service, add executor service to the list of services in the workspace.cue file as the following.

<span><span style="color: var(--shiki-token-keyword)">package</span><span style="color: var(--shiki-color-text)"> v2</span></span>
<span></span>
<span><span style="color: var(--shiki-token-keyword)">import</span><span style="color: var(--shiki-color-text)"> (</span></span>
<span><span style="color: var(--shiki-color-text)">	...</span></span>
<span><span style="color: var(--shiki-color-text)">	</span><span style="color: var(--shiki-color-text)">&quot;</span><span style="color: var(--shiki-token-string-expression)">tailor.build/template/services/executor</span><span style="color: var(--shiki-color-text)">&quot;</span></span>
<span><span style="color: var(--shiki-color-text)">)</span></span>
<span></span>
<span><span style="color: var(--shiki-color-text)">v2.#Workspace </span><span style="color: var(--shiki-token-keyword)">&amp;</span><span style="color: var(--shiki-color-text)"> {</span></span>
<span><span style="color: var(--shiki-color-text)">	Apps: [</span></span>
<span><span style="color: var(--shiki-color-text)">		application</span></span>
<span><span style="color: var(--shiki-color-text)">	]</span></span>
<span><span style="color: var(--shiki-color-text)">	Services: [tailordb</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> pipeline</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> auth</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> executor]</span></span>
<span><span style="color: var(--shiki-color-text)">}</span></span>
<span></span>

2. Add the trigger in executor.cue manifest file

To add the triggerCreatePutAwayWithEvent trigger, define the trigger in the executor.cue manifest file.

package executor

import (
	"github.com/tailor-platform/tailorctl/schema/v2/executor"
)

executor.#Spec & {
	Executors: [
		#triggerCreatePutAwayWithEvent,
	]
}

#triggerCreatePutAwayWithEvent: executor.#Executor & {
	Name:        "eventbased-executor"
	Description: "Create a data based on the event"
	Trigger: executor.#TriggerEvent & {
		EventType: "tailordb.type_record.created"
		Condition: common.#Script & {
			Expr: """
				args.NamespaceName == "\(environment.#app.namespace)" && args.TypeName == "PurchaseOrder"
				"""
		}
	}
	Target: executor.#TargetTailorGraphql & {
		AppName: environment.#app.namespace
		Query: """
		mutation ($input: createPutAwayWithEventInput!) {
			createPutAwayWithEvent(input: $input)
		}"""
		Variables: common.#Script & {
			Expr: """
			({
				"input": {
					"quantity": args.data.newRecord.quantity,
					"purchaseOrderID": args.data.newRecord.id,
					"putAwayDate": args.data.newRecord.purchaseOrderDate
				}
			})"""
		}
	}
}

3. Enable PublishRecordEvents settings

To ensure that a PutAway record is created through the trigger for every PurchaseOrder type record created, you'll need to enable PublishRecordEvents in the settings.

package transaction

import (
	"github.com/tailor-platform/tailorctl/schema/v2/tailordb"
	"tailor.build/template/services/tailordb:permissions"
)

PurchaseOrder: tailordb.#Type & {
	Name:        "PurchaseOrder"
	Description: "Model for Purchase Order. Each record of the Purchase Order represents unique combination of a Product, a Location, and a Supplier. Customization is required to wrap multiple records (line items) into one purchase order."
	Settings: {
		Aggregation: true
		PublishRecordEvents: true
	}
	Fields: {
		...
	}

	TypePermission: permissions.editorAccess
}

4. Apply the change using tailorctl

Run the following command to apply the changes.

<span><span style="color: var(--shiki-token-comment)"># Apply Executor</span></span>
<span><span style="color: var(--shiki-token-function)">tailorctl</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">workspace</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">apply</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">-m</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">./workspace.cue</span></span>
<span></span>

5. Verify the trigger

Run the following query to create a new purchase order.

mutation {
  createPurchaseOrder(input: { productID: "<product-uuid>",  locationID: "<location-uuid>", purchaseOrderDate: "<date>", quantity: "<quantity>"}) {
    id
  }
}

Upon completion of the above query, triggerCreatePutAwayWithEvent will execute to create a new PutAway record.