ChatiumFor developersPlaygroundPricing
Sign in

Heap.GenericLink

Declares a reference field to a record in any heap table without being tied to a specific table.

The "younger sibling" of Heap.RefLink.

This is a non-standard type in the JSON schema. It is stored in the database as a tuple (an array with two elements) containing the type and the identifier of the referenced record (identifiers are globally unique), and at runtime, it is represented by an instance of a special class GenericLink.

You can read more about working with links in the relevant section of the guide.

Usage / Signature

import { Heap } from '@app/heap'
Heap.GenericLink(options)

Arguments

  • <b id="options">options</b>: {onDelete}
    An object with additional optional parameters for the field.

    • <b id="onDelete">onDelete</b>: 'restrict' | 'none' (default - 'restrict')
      Defines the integrity control behavior for this field, answering the question: what to do when the record that this field references is deleted.
      • 'restrict' - prohibit deletion. The delete operation will fail with an error.
      • 'none' - (not recommended) ignore. The record will be deleted, and this field will retain a reference to a non-existent record.

Return Value

A JSON schema for an array field with two elements, the first being a string with the table type, and the second being a string with the unique identifier of the record.

Examples

Storing references to different tables in a single GenericLink field and filtering by the type of the object being referenced
import { deals } from '@someAccount/deals'

const products = Heap.Table('products', {})
const issues = Heap.Table('issues', {
  source: Heap.GenericLink(),
})

const product1 = await products.create(ctx, {})

await projects.create(ctx, { source: product1 })
await projects.create(ctx, {
  source: await deals.findOneBy(ctx, { name: 'test' }),
})
const productIssues = await issues.findBy(ctx, {
  source: { type: products.type }
})