Types & Fields
A Type is a table-like entity: named Fields, indexes and cardinality estimates. Types are plain JSON (TypeDef), parsed and registered on a Registry.
import { createRegistry, type TypeDef } from '@aeye/query';
const userDef: TypeDef = {
name: 'user',
fields: [{ name: 'id', type: { kind: 'number', whole: true } }],
count: 1000,
};
const orderDef: TypeDef = {
name: 'order',
fields: [
{ name: 'id', type: { kind: 'number', whole: true } },
// belongs-to user; adds a has-many `user.orders` pointing back
{ name: 'userId', type: { kind: 'relation', to: 'user', count: 1, inverseRelation: 'orders' } },
{ name: 'status', type: { kind: 'text', values: [{ value: 'open' }, { value: 'paid' }] } },
{ name: 'total', type: { kind: 'money', currency: 'USD' } },
{ name: 'createdAt', type: { kind: 'timestamp', timezone: true } },
{ name: 'tags', type: { kind: 'array', item: { kind: 'text' }, maxItems: 8 }, nullable: true },
],
indexes: [{ exprs: [{ expr: { kind: 'field-ref', source: 'order', field: 'id' }, count: 1 }] }],
count: 5000,
bytes: 48,
};
const registry = createRegistry();
registry.registerType(registry.parseType(userDef));
registry.registerType(registry.parseType(orderDef));
registry.finalize(); // add the inverse relations (user.orders)TypeDef
| Property | Description |
|---|---|
name | The name queries use. |
fields | The fields (FieldDef[]). |
count | Estimated rows. Drives cost estimates. |
bytes, changes | Estimated bytes per row, and how often the data changes. |
indexes | Composite indexes (see below). |
identity | The primary key: a field name or a list of names. |
semantic, search | Make the whole type eligible for similarity or full-text search. |
insertable, updatable, deletable | Which writes are allowed (each default true). |
label, description | Shown to the model. Generated from the definition when omitted. |
FieldDef
| Property | Description |
|---|---|
name, type | The field name and its field type. |
nullable | Nullability lives on the field, not the field type. |
insertable, updatable | Which writes may set it (default true). |
exprs | { only: [...] } or { not: [...] }: narrows which expression kinds may use it. |
label, description, bytes, changes | As on the type. |
Field types
kind | Options |
|---|---|
number | min, max, whole, minPlaces, maxPlaces, values |
text | minLength, maxLength, pattern, casing, semantic, search, values |
money | currency, number (number options) |
bool | |
date, timestamp | timezone |
json | schema |
array | item (a field type, so arrays nest), minItems, maxItems |
relation | to, count, inverseRelation |
values is a closed set ([{ value, label? }]): an enum.
Any builtin can also carry as (a registered refinement) and with (the refinement's options for this column).
Relations
A relation field's name is the join key; there are no separate foreign-key fields.
count: 1is belongs-to: the foreign key lives on this type.count > 1is has-many.inverseRelationon a belongs-to adds the matching has-many to the target (registry.finalize()materializes it).
Each side joins through the target's identity: identity when declared, else the field of the first unique single-field index, else the field named id. To use different physical columns or a custom ON, see relation backing.
A relation can be compared to a value with =, <>, in and notIn. The value is keyed by the target's primary key ({ id: 5 }), or a bare scalar for a single-column key. Comparing a has-many tests membership. Relations can't be ordered or matched with like.
Indexes
An index is an ordered list of parts, each with the distinct-row count of the prefix up to that part. It is unique when the last part's count is 1. Indexes affect cost estimates and identity.
Text casing
A text field's casing decides how comparisons treat letter case. Fields that declare none use the engine's textCasing option (default 'fold').
casing | Meaning | SQL for a = b |
|---|---|---|
'fold' | Case-insensitive, folded by the query | LOWER(a) = LOWER(b) |
'collated' | Case-insensitive, folded by the column's collation | a = b |
'exact' | Case-sensitive | a = b |
LOWER(col) can't use an ordinary index, and on Postgres fails on a physical uuid column. For schemas of identifiers and codes, set new QueryEngine(registry, { textCasing: 'exact' }) and mark the columns that do want folding.
Inferring a type from rows
import { inferType } from '@aeye/query';
const def = inferType('user', [
{ id: 1, name: 'Ada', tags: ['admin'] },
{ id: 2, name: 'Cleo', tags: [] },
]);Field types and nullability come from the data. Options: sampleSize, label, description.
To go from a database table instead, see Tables & DDL.