import type {DeepReadonly} from '../../../shared/src/json.ts'; import * as v from '../../../shared/src/valita.ts'; import type {PrimaryKey} from '../../../zero-protocol/src/primary-key.ts'; import type {SchemaValue} from '../../../zero-schema/src/table-schema.ts'; import * as PostgresReplicaIdentity from './postgres-replica-identity-enum.ts'; import * as PostgresTypeClass from './postgres-type-class-enum.ts'; export const pgTypeClassSchema = v.literalUnion( PostgresTypeClass.Base, PostgresTypeClass.Composite, PostgresTypeClass.Domain, PostgresTypeClass.Enum, PostgresTypeClass.Pseudo, PostgresTypeClass.Range, PostgresTypeClass.Multirange, ); const pgReplicaIdentitySchema = v.literalUnion( PostgresReplicaIdentity.Default, PostgresReplicaIdentity.Nothing, PostgresReplicaIdentity.Full, PostgresReplicaIdentity.Index, ); export const columnSpec = v.object({ pos: v.number(), dataType: v.string(), pgTypeClass: pgTypeClassSchema.optional(), // If the column is an array, this will be the type of the // elements in the array. If the column is not an array, // this will be null. elemPgTypeClass: pgTypeClassSchema.nullable().optional(), characterMaximumLength: v.number().nullable().optional(), notNull: v.boolean().nullable().optional(), dflt: v.string().nullable().optional(), }); export type ColumnSpec = Readonly>; export const publishedColumnSpec = columnSpec.extend({ typeOID: v.number(), }); const liteTableSpec = v.object({ name: v.string(), columns: v.record(columnSpec), primaryKey: v.array(v.string()).optional(), }); export const tableSpec = liteTableSpec.extend({ schema: v.string(), }); export const publishedTableSpec = tableSpec.extend({ oid: v.number(), // Always present for new instances (e.g. from DDL triggers), but // may from `initialSchema` object stored in the `replicas` table. schemaOID: v.number().optional(), columns: v.record(publishedColumnSpec), replicaIdentity: pgReplicaIdentitySchema.optional(), publications: v.record(v.object({rowFilter: v.string().nullable()})), }); export type MutableLiteTableSpec = v.Infer; export type LiteTableSpec = Readonly; export type LiteTableSpecWithKeysAndVersion = Omit< LiteTableSpec, 'primaryKey' > & { /** * All keys associated with a unique index. Includes indexes with * nullable columns. */ uniqueKeys: PrimaryKey[]; /** * The key selected to act as the "primary key". Primary keys * are not explicitly set on the replica, but an appropriate * unique index is required. */ primaryKey: PrimaryKey; // note: required /** * All keys associated with a unique index over non-null * columns, i.e. suitable as a primary key. */ allPotentialPrimaryKeys: PrimaryKey[]; /** * The minimum `_0_version` value for every row in the table. If this is * present, `_0_version` value in the row itself should only be used if * it is greater (i.e. later) than the `minRowVersion`. */ minRowVersion: string | null; }; export type LiteAndZqlSpec = { tableSpec: LiteTableSpecWithKeysAndVersion; zqlSpec: Record; }; export type TableSpec = Readonly>; export type PublishedTableSpec = Readonly>; const directionSchema = v.literalUnion('ASC', 'DESC'); export type IndexPredicateValue = | {readonly type: 'boolean'; readonly value: boolean} | {readonly type: 'integer'; readonly value: string} | {readonly type: 'string'; readonly value: string}; export type IndexPredicate = | { readonly type: 'and' | 'or'; readonly conditions: readonly IndexPredicate[]; } | {readonly type: 'not'; readonly condition: IndexPredicate} | { readonly type: 'null-test'; readonly column: string; readonly op: 'IS NULL' | 'IS NOT NULL'; } | { readonly type: 'comparison'; readonly column: string; readonly op: '=' | '<>' | '<' | '<=' | '>' | '>='; readonly value: IndexPredicateValue; }; const indexPredicateValueSchema: v.Type = v.union( v.readonlyObject({type: v.literal('boolean'), value: v.boolean()}), v.readonlyObject({type: v.literal('integer'), value: v.string()}), v.readonlyObject({type: v.literal('string'), value: v.string()}), ); const indexPredicateSchema: v.Type = v.lazy(() => v.union( v.readonlyObject({ type: v.literalUnion('and', 'or'), conditions: v.readonlyArray(indexPredicateSchema), }), v.readonlyObject({ type: v.literal('not'), condition: indexPredicateSchema, }), v.readonlyObject({ type: v.literal('null-test'), column: v.string(), op: v.literalUnion('IS NULL', 'IS NOT NULL'), }), v.readonlyObject({ type: v.literal('comparison'), column: v.string(), op: v.literalUnion('=', '<>', '<', '<=', '>', '>='), value: indexPredicateValueSchema, }), ), ); const liteIndexSpec = v.object({ name: v.string(), tableName: v.string(), unique: v.boolean(), columns: v.record(directionSchema), predicate: indexPredicateSchema.optional(), }); export type MutableLiteIndexSpec = v.Infer; export type LiteIndexSpec = Readonly; export const indexSpec = liteIndexSpec.extend({ schema: v.string(), }); export type IndexSpec = DeepReadonly>; export const publishedIndexSpec = indexSpec.extend({ isReplicaIdentity: v.boolean().optional(), isPrimaryKey: v.boolean().optional(), isImmediate: v.boolean().optional(), }); export type PublishedIndexSpec = DeepReadonly< v.Infer >;