Skip to content

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.

typescript
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 ​

PropertyDescription
nameThe name queries use.
fieldsThe fields (FieldDef[]).
countEstimated rows. Drives cost estimates.
bytes, changesEstimated bytes per row, and how often the data changes.
indexesComposite indexes (see below).
identityThe primary key: a field name or a list of names.
semantic, searchMake the whole type eligible for similarity or full-text search.
insertable, updatable, deletableWhich writes are allowed (each default true).
label, descriptionShown to the model. Generated from the definition when omitted.

FieldDef ​

PropertyDescription
name, typeThe field name and its field type.
nullableNullability lives on the field, not the field type.
insertable, updatableWhich writes may set it (default true).
exprs{ only: [...] } or { not: [...] }: narrows which expression kinds may use it.
label, description, bytes, changesAs on the type.

Field types ​

kindOptions
numbermin, max, whole, minPlaces, maxPlaces, values
textminLength, maxLength, pattern, casing, semantic, search, values
moneycurrency, number (number options)
bool
date, timestamptimezone
jsonschema
arrayitem (a field type, so arrays nest), minItems, maxItems
relationto, 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: 1 is belongs-to: the foreign key lives on this type.
  • count > 1 is has-many.
  • inverseRelation on 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').

casingMeaningSQL for a = b
'fold'Case-insensitive, folded by the queryLOWER(a) = LOWER(b)
'collated'Case-insensitive, folded by the column's collationa = b
'exact'Case-sensitivea = 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 ​

typescript
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.

Released under the GPL-3.0 License.