ChatiumFor developersPlaygroundPricing
Sign in

HeapTableRepo.isMyRecord

Allows you to determine whether the provided heap object belongs to this heap table. It also acts as a "TypeScript type guard," meaning it casts the type of the passed "untyped" variable to the type of the heap object of the table with its fields.

Belonging is determined by comparing the system field heapType of the heap object with the type property of the table.

Usage/Signature

table.isMyRecord(object)
  • Arguments

    • object: any
      The object to be checked.
  • Return Value: boolean Returns true if the passed variable is an object with the heapType field, where the value matches the type property of this table. In all other cases, it returns false.

Examples


Determining the record type pointed to by the GenericLink field.

const Orders = Heap.Table('orders', {
  customer: Heap.GenericLink(),
})
const Orgs = Heap.Table('orgs', {
  name: Heap.String(),
})
const Persons = Heap.Table('persons', {
  firstName: Heap.String(),
  lastName: Heap.String(),
})

async function getCustomerDisplayName(ctx: app.Ctx, order: typeof Orders.T): Promise<string> {
  const customer = await orders.customer.get(ctx)
  if (Orgs.isMyRecord(customer)) {
    return customer.name
  } else if (Persons.isMyRecord(customer)) {
    return customer.firstName + ' ' + customer.lastName
  } else {
    throw 'unsupported customer type ' + customer.heapType  
  }
}