JavaScript

Using JavaScript, implement robust validation logic, manipulate data structures, and perform efficient data transformations in Pipeline resolvers, Triggers and the Tailor DB.

Here are some examples of where you can utilize JavaScript:

Pipeline

You can use PostHook field in the Pipeline to return the object that can be used in the later steps.

Throwing a TailorErrorMessage in a pipeline step causes the pipeline execution to halt when the error is caught, and the API then returns the error message as the response.

Example:

<span><span style="color: var(--shiki-color-text)">PostHook</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">common</span><span style="color: var(--shiki-color-text)">.#Script </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)">    Expr</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string-expression)">&quot;&quot;&quot;</span></span>
<span><span style="color: var(--shiki-token-string-expression)">        (() =&gt; {</span></span>
<span><span style="color: var(--shiki-color-text)">            </span><span style="color: var(--shiki-token-comment)">// throws an error if no heroes found</span></span>
<span><span style="color: var(--shiki-color-text)">            </span><span style="color: var(--shiki-token-keyword)">if</span><span style="color: var(--shiki-color-text)"> (</span><span style="color: var(--shiki-token-constant)">args</span><span style="color: var(--shiki-color-text)">.</span><span style="color: var(--shiki-token-constant)">characters</span><span style="color: var(--shiki-color-text)">.</span><span style="color: var(--shiki-token-constant)">collection</span><span style="color: var(--shiki-color-text)">.</span><span style="color: var(--shiki-token-constant)">length</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-keyword)">&lt;</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">0</span><span style="color: var(--shiki-color-text)">) {</span></span>
<span><span style="color: var(--shiki-color-text)">                </span><span style="color: var(--shiki-token-keyword)">throw</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-keyword)">new</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-function)">TailorErrorMessage</span><span style="color: var(--shiki-color-text)">(</span><span style="color: var(--shiki-token-string-expression)">&quot;No heroes found&quot;</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-token-keyword)">return</span><span style="color: var(--shiki-color-text)"> {</span></span>
<span><span style="color: var(--shiki-color-text)">                </span><span style="color: var(--shiki-token-string-expression)">&quot;success&quot;</span><span style="color: var(--shiki-token-keyword)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">true</span><span style="color: var(--shiki-token-punctuation)">,</span></span>
<span><span style="color: var(--shiki-color-text)">                </span><span style="color: var(--shiki-token-string-expression)">&quot;heroes&quot;</span><span style="color: var(--shiki-token-keyword)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">args</span><span style="color: var(--shiki-token-function)">.</span><span style="color: var(--shiki-token-constant)">characters</span><span style="color: var(--shiki-token-function)">.</span><span style="color: var(--shiki-token-constant)">collection</span><span style="color: var(--shiki-token-function)">.map</span><span style="color: var(--shiki-color-text)">((c) </span><span style="color: var(--shiki-token-keyword)">=&gt;</span><span style="color: var(--shiki-color-text)"> ({</span></span>
<span><span style="color: var(--shiki-color-text)">                    </span><span style="color: var(--shiki-token-string-expression)">&quot;id&quot;</span><span style="color: var(--shiki-token-keyword)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">c</span><span style="color: var(--shiki-color-text)">.id</span><span style="color: var(--shiki-token-punctuation)">,</span></span>
<span><span style="color: var(--shiki-color-text)">                    </span><span style="color: var(--shiki-token-string-expression)">&quot;name&quot;</span><span style="color: var(--shiki-token-keyword)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">c</span><span style="color: var(--shiki-color-text)">.name</span></span>
<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>
<span><span style="color: var(--shiki-color-text)">    </span><span style="color: var(--shiki-token-string-expression)">&quot;&quot;&quot;</span></span>
<span><span style="color: var(--shiki-token-string-expression)">}</span></span>
<span></span>

Triggers and Targets

You can write Javascript to check the condition for event based triggers. The value for environment.#app.namespace is injected when we evaluate the cue manifest before applying the configuration.

<span><span style="color: var(--shiki-color-text)">Condition</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">common</span><span style="color: var(--shiki-color-text)">.#Script </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)">    Expr</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string-expression)">&quot;&quot;&quot;</span></span>
<span><span style="color: var(--shiki-token-string-expression)">        args.NamespaceName == &quot;</span><span style="color: var(--shiki-color-text)">\(</span><span style="color: var(--shiki-token-constant)">environment</span><span style="color: var(--shiki-color-text)">.</span><span style="color: var(--shiki-token-constant)">#app</span><span style="color: var(--shiki-color-text)">.namespace)</span><span style="color: var(--shiki-token-string-expression)">&quot; &amp;&amp; args.TypeName == &quot;</span><span style="color: var(--shiki-color-text)">PurchaseOrder</span><span style="color: var(--shiki-token-string-expression)">&quot;</span></span>
<span><span style="color: var(--shiki-token-string-expression)">    &quot;&quot;&quot;</span></span>
<span><span style="color: var(--shiki-color-text)">}</span></span>
<span></span>

In the target type #TargetTailorGraphql, you can pass the input variables to the Graphql query as shown below.

<span><span style="color: var(--shiki-color-text)">Variables</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">common</span><span style="color: var(--shiki-color-text)">.#Script </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)">    Expr</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string-expression)">&quot;&quot;&quot;</span></span>
<span><span style="color: var(--shiki-token-string-expression)">        ({</span></span>
<span><span style="color: var(--shiki-color-text)">            input</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> {</span></span>
<span><span style="color: var(--shiki-color-text)">                </span><span style="color: var(--shiki-token-string-expression)">&quot;quantity&quot;</span><span style="color: var(--shiki-color-text)">: </span><span style="color: var(--shiki-token-constant)">args</span><span style="color: var(--shiki-color-text)">.</span><span style="color: var(--shiki-token-constant)">data</span><span style="color: var(--shiki-color-text)">.</span><span style="color: var(--shiki-token-constant)">newRecord</span><span style="color: var(--shiki-color-text)">.quantity</span><span style="color: var(--shiki-token-punctuation)">,</span></span>
<span><span style="color: var(--shiki-color-text)">                </span><span style="color: var(--shiki-token-string-expression)">&quot;purchaseOrderID&quot;</span><span style="color: var(--shiki-color-text)">: </span><span style="color: var(--shiki-token-constant)">args</span><span style="color: var(--shiki-color-text)">.</span><span style="color: var(--shiki-token-constant)">data</span><span style="color: var(--shiki-color-text)">.</span><span style="color: var(--shiki-token-constant)">newRecord</span><span style="color: var(--shiki-color-text)">.id</span><span style="color: var(--shiki-token-punctuation)">,</span></span>
<span><span style="color: var(--shiki-color-text)">                </span><span style="color: var(--shiki-token-string-expression)">&quot;putAwayDate&quot;</span><span style="color: var(--shiki-color-text)">: </span><span style="color: var(--shiki-token-constant)">args</span><span style="color: var(--shiki-color-text)">.</span><span style="color: var(--shiki-token-constant)">data</span><span style="color: var(--shiki-color-text)">.</span><span style="color: var(--shiki-token-constant)">newRecord</span><span style="color: var(--shiki-color-text)">.purchaseOrderDate</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-token-string-expression)">&quot;&quot;&quot;</span></span>
<span><span style="color: var(--shiki-token-string-expression)">}</span></span>
<span></span>

You can write custom logic using JavaScript in the URL and Body fields in the target type #TargetWebhook.

<span><span style="color: var(--shiki-color-text)">URL</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">common</span><span style="color: var(--shiki-color-text)">.#Script </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)">    Expr</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string-expression)">&quot;\&quot;https://hooks.slack.com/services/{service-name}/\&quot;&quot;</span></span>
<span><span style="color: var(--shiki-color-text)">}</span></span>
<span></span>
<span><span style="color: var(--shiki-color-text)">Body</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">common</span><span style="color: var(--shiki-color-text)">.#Script </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)">    Expr</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string-expression)">&quot;&quot;&quot;</span></span>
<span><span style="color: var(--shiki-token-string-expression)">        ({</span></span>
<span><span style="color: var(--shiki-color-text)">            </span><span style="color: var(--shiki-token-string-expression)">&quot;text&quot;</span><span style="color: var(--shiki-color-text)">: </span><span style="color: var(--shiki-token-string-expression)">&quot;New Product Registration :tada:&quot;</span><span style="color: var(--shiki-token-punctuation)">,</span></span>
<span><span style="color: var(--shiki-color-text)">            </span><span style="color: var(--shiki-token-string-expression)">&quot;blocks&quot;</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-token-string-expression)">&quot;type&quot;</span><span style="color: var(--shiki-token-keyword)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string-expression)">&quot;section&quot;</span><span style="color: var(--shiki-token-punctuation)">,</span></span>
<span><span style="color: var(--shiki-color-text)">                    </span><span style="color: var(--shiki-token-string-expression)">&quot;text&quot;</span><span style="color: var(--shiki-token-keyword)">:</span><span style="color: var(--shiki-color-text)"> {</span></span>
<span><span style="color: var(--shiki-color-text)">                        </span><span style="color: var(--shiki-token-string-expression)">&quot;type&quot;</span><span style="color: var(--shiki-token-keyword)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string-expression)">&quot;mrkdwn&quot;</span><span style="color: var(--shiki-token-punctuation)">,</span></span>
<span><span style="color: var(--shiki-color-text)">                        </span><span style="color: var(--shiki-token-string-expression)">&quot;text&quot;</span><span style="color: var(--shiki-token-keyword)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string-expression)">&quot;*New Product Registration* :tada: &quot;</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-keyword)">+</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">args</span><span style="color: var(--shiki-color-text)">.</span><span style="color: var(--shiki-token-constant)">data</span><span style="color: var(--shiki-color-text)">.</span><span style="color: var(--shiki-token-constant)">newRecord</span><span style="color: var(--shiki-color-text)">.name</span></span>
<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>
<span><span style="color: var(--shiki-color-text)">        })</span></span>
<span><span style="color: var(--shiki-color-text)">    </span><span style="color: var(--shiki-token-string-expression)">&quot;&quot;&quot;</span></span>
<span><span style="color: var(--shiki-token-string-expression)">}</span></span>
<span></span>

Tailor DB

The example below demonstrates how to use JavaScript in Hooks to set a default value for the field. You can add custom logic to validate the input data and set default values.

<span><span style="color: var(--shiki-color-text)">Product: tailordb.#Type &amp; {</span></span>
<span><span style="color: var(--shiki-color-text)">	Name</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)">        </span><span style="color: var(--shiki-token-string-expression)">&quot;Product&quot;</span></span>
<span><span style="color: var(--shiki-color-text)">	Description: </span><span style="color: var(--shiki-token-string-expression)">&quot;Product model&quot;</span></span>
<span><span style="color: var(--shiki-color-text)">	Fields: {</span></span>
<span><span style="color: var(--shiki-color-text)">		...</span></span>
<span><span style="color: var(--shiki-color-text)">		inactive</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)">{</span></span>
<span><span style="color: var(--shiki-color-text)">			Type</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)">		tailordb.#TypeBool</span></span>
<span><span style="color: var(--shiki-color-text)">			Description: </span><span style="color: var(--shiki-token-string-expression)">&quot;Field used to delete the record. Instead of deleting the record, we set this field to true. This is to keep the data integrity.&quot;</span></span>
<span><span style="color: var(--shiki-color-text)">			Hooks: {</span></span>
<span><span style="color: var(--shiki-color-text)">				Create</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> common.#Script &amp; {</span></span>
<span><span style="color: var(--shiki-color-text)">					Expr</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string-expression)">&quot;false&quot;</span></span>
<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>
<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>
<span><span style="color: var(--shiki-color-text)">}</span></span>
<span></span>

Here's an example on how you can leverage the built-in support for console messages in JavaScript to debug and troubleshoot your code.

<span><span style="color: var(--shiki-color-text)">Product: tailordb.#Type &amp; {</span></span>
<span><span style="color: var(--shiki-color-text)">	Name</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)">        </span><span style="color: var(--shiki-token-string-expression)">&quot;Product&quot;</span></span>
<span><span style="color: var(--shiki-color-text)">	Description: </span><span style="color: var(--shiki-token-string-expression)">&quot;Product model&quot;</span></span>
<span><span style="color: var(--shiki-color-text)">	Fields: {</span></span>
<span><span style="color: var(--shiki-color-text)">		description</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> {</span></span>
<span><span style="color: var(--shiki-color-text)">			Type</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)">        tailordb.#TypeString</span></span>
<span><span style="color: var(--shiki-color-text)">			Description: </span><span style="color: var(--shiki-token-string-expression)">&quot;Description of the product.&quot;</span></span>
<span><span style="color: var(--shiki-color-text)">			Validate: [{</span></span>
<span><span style="color: var(--shiki-color-text)">                Script</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> common.#Script &amp; {</span></span>
<span><span style="color: var(--shiki-color-text)">                    Expr</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string-expression)">&quot;&quot;&quot;</span></span>
<span><span style="color: var(--shiki-token-string-expression)">                        ((value, data) =&gt; {</span></span>
<span><span style="color: var(--shiki-token-string-expression)">                            console.log(value)</span></span>
<span><span style="color: var(--shiki-token-string-expression)">                            console.log(value.length)</span></span>
<span><span style="color: var(--shiki-token-string-expression)">                            console.log(data.name)</span></span>
<span><span style="color: var(--shiki-token-string-expression)">                            console.log(Object.keys(user))</span></span>
<span><span style="color: var(--shiki-token-string-expression)">                            Object.keys(user).forEach(function(key) {</span></span>
<span><span style="color: var(--shiki-token-string-expression)">								console.log(key, user[key], typeof user[key])</span></span>
<span><span style="color: var(--shiki-token-string-expression)">							})</span></span>
<span><span style="color: var(--shiki-token-string-expression)">                            return !(value.length &lt; 40)</span></span>
<span><span style="color: var(--shiki-token-string-expression)">                        })(_value, _data)</span></span>
<span><span style="color: var(--shiki-token-string-expression)">                    &quot;&quot;&quot;</span></span>
<span><span style="color: var(--shiki-color-text)">                }</span></span>
<span><span style="color: var(--shiki-color-text)">				Action: tailordb.#Permit.Deny</span></span>
<span><span style="color: var(--shiki-color-text)">				ErrorMessage: </span><span style="color: var(--shiki-token-string-expression)">&quot;Description length should be less than 40 characters.&quot;</span></span>
<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>
<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>
<span></span>

Run the Tailor platform in your local environment to debug the application and view console messages in the log. Contact us if you need more information about running the platform locally.

The Action will be evaluated when the Expr returns true.

The _value and _data arguments to the function represent the description field and the input data object for the new record.

Additionally, you can access user object which contains information about the user who created or updated the record.

The user object has the following fields:

  • id: ID of the user.
  • type: Type of the user Ex: machine_user.
  • workspace_id: Current workspace ID.
  • attributes: List of IDs ex: If the current user has admin rights then admin id is present in the list.
  • tenant_id: Tenant ID.