ChatiumFor developersPlaygroundPricing
Sign in

Heap.NonRequired

Allows you to omit a non-optional field of an object when creating a record, automatically filling it with a specified default value.

It is important to understand that the NonRequired modifier allows you to omit the field only when creating a new record (create operation). When updating (update operation) the values of a nested object, the value of the NonRequired field must be explicitly specified (see examples below).

The Heap.NonRequired modifier is supported and makes sense only for Heap.Object fields and top-level table fields.

Usage / Signature

import { Heap } from '@app/heap'
Heap.NonRequired(fieldSchema, default)

Arguments

  • <b id="fieldSchema">fieldSchema</b>: HeapSchema <b style="color:red">*</b>
    A schema describing the type of the field that we are making optional when creating a record. This can be any supported field type of a heap table - simple or complex.

  • <b id="default">default</b>: fieldSchema serialized type <b style="color:red">*</b>
    The default value for this field. It should have a type corresponding to fieldSchema in serialized form. For more information on how default values work, see Guide / Heap / Default Values.

Return Value

The argument fieldSchema with the NonRequired modifier.

Examples

Demonstration of the modifier for both a top-level field and a nested object
const customers = Heap.Table('customers', {
  name: Heap.String(),
  stats: Heap.NonRequired(
    Heap.Object({
      ltv: Heap.NonRequired(Heap.Money(), [0, 'USD']),
      purchasesCount: Heap.NonRequired(Heap.Integer(), 0),
    }),
    { ltv: [0, 'USD'], purchasesCount: 0 },
  ),
})
await customers.create(ctx, { name: 'John Smith' })
// stats = { ltv: [0, 'USD'], purchasesCount: 0 }
const mary = await customers.create(ctx, { name: 'Mary', {
  stats: { ltv: new Money(0, 'EUR') },
})
// stats = { ltv: [0, 'EUR'], purchasesCount: 0 }
// when updating, the NonRequired modifier does not apply
await customers.update(ctx, {
  id: mary.id,
  stats: { purchasesCount: 1 },
})
// error! ltv field is required