Heap - API for Table Declarations
The Heap object, imported from the @app/heap module, provides access to a DSL (domain-specific language) for declaring heap tables. It simultaneously generates a TypeScript type for the table record and a JSON schema used for validating the data stored in the table. Below are all the available functions of the Heap object:
Functions
Table Declaration Function
- Heap.Table — the main and only way to declare a heap table.
Basic Simple Type Declaration Functions
-
- Heap.Integer — a number with validation for integer values.
-
- Heap.NonEmptyString — a string that cannot be empty or contain only whitespace characters.
- Heap.RegEx — syntactic sugar for a string validated using a regular expression.
Basic Composite Type Declaration Functions
- Heap.Array — an array with a specified element type.
- Heap.Object — a nested object with a specified structure.
- Heap.Record — a nested "map" with specified key and value types.
- Heap.Tuple — a tuple, a fixed-length array with specified types for each position.
- Heap.Union — one of several specified alternative types, fully analogous to TypeScript union.
- Heap.Enum
Field Type Modifier Functions
- Heap.NonRequired — allows omitting a "required" field during the
create
operation, automatically filling it with a specified default value. - Heap.Nullable — syntactic sugar for
Heap.NonRequired(Heap.Union(x, Heap.Null()), null)
. - Heap.Optional — makes an object field optional (adds a "?" to the type, e.g.,
x Heap.String()
->x? Heap.String()
).
Additional "Smart" Type Declaration Functions
- Heap.DateTime — standard JavaScript Date.
- Heap.Money — representation of a monetary amount through a special Money class.
- Heap.Currency — a helper type, a string of three uppercase Latin letters (international currency code), used within the Heap.Money schema.
- Heap.File — representation of a file uploaded to storage through a special StorageFile class.
- Heap.ImageFile — an image file uploaded to storage, represented through a special StorageImageFile class with additional convenient methods for working with images.
- Heap.VideoFile — a video file uploaded to storage, represented through a special StorageVideoFile class with additional convenient methods for working with videos.
- Heap.AudioFile — an audio file uploaded to storage, represented through a special StorageAudioFile class with additional convenient methods for working with audio.
Functions for Declaring References to Other Table Records
- Heap.RefLink — a reference to a record in a specific heap table.
- Heap.GenericLink — a reference to a record in any heap table.
Helper Simple Type Declaration Functions
- Heap.Any — TypeScript
any
- allows storing any JSON-serializable data without validation. - Heap.Literal — TypeScript literal, typically used for declaring alternatives within Heap.Union.
- Heap.Null — literally
null
, usually used for declaring an alternative within Heap.Union. - Heap.Undefined — literally
undefined
, usage is not recommended as undefined cannot be saved in JSON. - Heap.Unknown — TypeScript
unknown
- similar to Heap.Any.
Functions for Transforming Existing Types (Schemas)
- Heap.Intersect — combines multiple existing
Heap.Object
,Heap.Record
schemas into one, similar to the intersect operation (A & B
) on types in TypeScript. - Heap.KeyOf — returns the schema of the set of keys of the incoming
Heap.Object
schema. - Heap.Omit — excludes a specified set of keys (fields) from the incoming
Heap.Object
schema, similar to the built-in TypeScript typeOmit
. - Heap.Partial — turns all fields of the incoming
Heap.Object
schema into optional fields, similar to the built-in TypeScript typePartial
. - Heap.Pick — returns a schema including only the specified fields from the incoming
Heap.Object
schema, similar to the built-in TypeScript typePick
. - Heap.Required — turns all optional fields of the incoming
Heap.Object
schema into required fields, similar to the built-in TypeScript typeRequired
.