import type { RowType, table, UntypedTableDef } from './table.ts'; import type { ColumnMetadata, IndexTypes } from './type_builders.ts'; import type { CollapseTuple, Prettify } from './type_util.ts'; import type { Identity } from './identity.ts'; import { Range } from '../server/range.ts'; import type { ColumnIsUnique } from './constraints.ts'; /** * Index helper type used *inside* {@link table} to enforce that only * existing column names are referenced. */ export type IndexOpts = { accessor: string; name?: string | undefined; } & ( | { algorithm: 'btree'; columns: readonly AllowedCol[] } | { algorithm: 'hash'; columns: readonly AllowedCol[] } | { algorithm: 'direct'; column: AllowedCol } ); /** * An untyped representation of an index definition. */ export type UntypedIndex = { name: string; unique?: boolean; algorithm: 'btree' | 'direct' | 'hash'; columns: readonly AllowedCol[]; }; /** * A helper type to extract the column names from an index definition. */ export type IndexColumns> = I extends { columns: readonly string[]; } ? readonly [...I['columns']] : I extends { column: infer Name extends string } ? readonly [Name] : never; /** * A type representing the indexes defined on a table. */ export type Indexes< TableDef extends UntypedTableDef, I extends Record>, > = { [k in keyof I]: Index; }; /** * Check whether every column in an index is a primary key column. */ type AllColumnsPrimaryKey< TableDef extends UntypedTableDef, Columns extends readonly string[], > = Columns extends readonly [ infer Head extends keyof TableDef['columns'] & string, ...infer Tail extends readonly string[], ] ? TableDef['columns'][Head]['columnMetadata'] extends { isPrimaryKey: true } ? AllColumnsPrimaryKey : false : true; /** * A type representing a database index, * which can either be unique or filter for a single value or range of values. * Unique indexes on primary key columns additionally support `update`. */ export type Index< TableDef extends UntypedTableDef, I extends UntypedIndex, > = I['unique'] extends true ? AllColumnsPrimaryKey extends true ? UniqueIndex & { update(row: Prettify>): Prettify>; } : UniqueIndex : I['algorithm'] extends 'hash' ? PointIndex : RangedIndex; /** * A type representing a collection of read-only indexes defined on a table. */ export type ReadonlyIndexes< TableDef extends UntypedTableDef, I extends Record>, > = { [k in keyof I]: ReadonlyIndex; }; /** * A type representing a read-only database index, * which can be either unique, pointed, or ranged. * This type only exposes read-only operations. */ export type ReadonlyIndex< TableDef extends UntypedTableDef, I extends UntypedIndex, > = I['unique'] extends true ? ReadonlyUniqueIndex : I['algorithm'] extends 'hash' ? ReadonlyPointIndex : ReadonlyRangedIndex; /** * A type representing a read-only unique index on a database table. */ export type ReadonlyUniqueIndex< TableDef extends UntypedTableDef, I extends UntypedIndex, > = { find(colVal: IndexVal): RowType | null; }; /** * A type representing a unique index on a database table. * Unique indexes enforce that the indexed columns contain unique values. */ export interface UniqueIndex< TableDef extends UntypedTableDef, I extends UntypedIndex, > extends ReadonlyUniqueIndex { delete(colVal: IndexVal): boolean; } /** * A type representing a read-only point index on a database table. */ export interface ReadonlyPointIndex< TableDef extends UntypedTableDef, I extends UntypedIndex, > { filter( point: IndexVal ): IteratorObject>, undefined>; } /** * A type representing a point index on a database table. * Point indexes allow for exact match queries on the indexed columns. */ export interface PointIndex< TableDef extends UntypedTableDef, I extends UntypedIndex, > extends ReadonlyPointIndex { delete(point: IndexVal): number; } /** * A type representing a read-only ranged index on a database table. */ export interface ReadonlyRangedIndex< TableDef extends UntypedTableDef, I extends UntypedIndex, > { filter( range: IndexScanRangeBounds ): IteratorObject>, undefined>; } /** * A type representing a ranged index on a database table. * Ranged indexes allow for range queries on the indexed columns. */ export interface RangedIndex< TableDef extends UntypedTableDef, I extends UntypedIndex, > extends ReadonlyRangedIndex { delete(range: IndexScanRangeBounds): number; } /** * A helper type to extract the value type of an index based on the table definition and index definition. * This type constructs a tuple of the types of the columns that make up the index. */ export type IndexVal< TableDef extends UntypedTableDef, I extends UntypedIndex, > = CollapseTuple<_IndexVal>; /** * A helper type to extract the types of the columns that make up an index. */ type _IndexVal< TableDef extends UntypedTableDef, Columns extends readonly string[], > = Columns extends readonly [ infer Head extends string, ...infer Tail extends readonly string[], ] ? [ UnbrandedIdentity, ..._IndexVal, ] : []; /** * A column type with a receiver identity's module brand removed; other types * pass through. Index keys and query comparisons use it, so they take a plain * `Identity` where a column holds a receiver's typed identity. A lookup makes * no claim about which module an identity belongs to, so a reducer or view can * check `ctx.sender` against such a column; rows keep the type. */ export type UnbrandedIdentity = T extends Identity ? Identity : T; /** * A helper type to define the bounds for scanning an index. * This type allows for specifying exact values or ranges for each column in the index. * It supports omitting trailing columns if the index is multi-column. */ export type IndexScanRangeBounds< TableDef extends UntypedTableDef, I extends UntypedIndex, > = _IndexScanRangeBounds<_IndexVal>; /** * A helper type to define the bounds for scanning an index. * This type allows for specifying exact values or ranges for each column in the index. * It supports omitting trailing columns if the index is multi-column. * This version only allows omitting the array if the index is single-column to avoid ambiguity. */ type _IndexScanRangeBounds = Columns extends [ infer Term, ] ? Term | Range : _IndexScanRangeBoundsCase; /** * A helper type to define the bounds for scanning an index. * This type allows for specifying exact values or ranges for each column in the index. * It supports omitting trailing columns if the index is multi-column. */ type _IndexScanRangeBoundsCase = Columns extends [...infer Prefix, infer Term] ? readonly [...Prefix, Term | Range] | _IndexScanRangeBounds : never; /** * A helper type representing a column index definition. */ export type ColumnIndex< Name extends string, M extends ColumnMetadata, > = Prettify< { name: Name; unique: ColumnIsUnique; columns: readonly [Name]; algorithm: 'btree' | 'direct' | 'hash'; } & (M extends { indexType: infer I extends NonNullable; } ? { algorithm: I } : ColumnIsUnique extends true ? { algorithm: 'btree' } : never) >;