Heap.Nullable
Makes a field "nullable" (i.e., allows it to accept the value null
) and simultaneously optional for specification when creating a record, with a default value of null
. It is syntactic sugar for Heap.NonRequired(Heap.Union(x, Heap.Null()), null)
.
The Heap.Nullable modifier is supported and makes sense only for Heap.Object fields and top-level tables.
Usage / Signature
import { Heap } from '@app/heap'
Heap.Nullable(fieldSchema)
Arguments
- <b id="fieldSchema">fieldSchema</b>: HeapSchema <b style="color:red">*</b>
A schema describing the "meaningful" type of the field that we are making nullable. This can be any supported field type in a heap table - simple or complex.
Return Value
A JSON schema anyOf
with alternatives
fieldSchema and null,
with the NonRequired modifier and a default value of null
.
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.Nullable(
Heap.Object({
ltv: Heap.Nullable(Heap.Money()),
purchasesCount: Heap.Nullable(Heap.Integer()),
}),
),
})
await customers.create(ctx, { name: 'John Smith' }) // stats = null
const mary = await customers.create(ctx, { name: 'Mary', {
stats: {
ltv: new Money(0, 'USD')
}, // stats = { ltv: [0, 'USD'], purchasesCount: null }
})
// when updating, the NonRequired modifier does not apply
await customers.update(ctx, {
id: mary.id,
stats: { purchasesCount: 1 },
}) // error! field ltv is required