/** * Database Import — conversion layer * * Turns an `IntrospectedSchema` (read from a live Postgres/Supabase database) * into a `.apsorc`-shaped object. This is a pure function: same input always * yields the same output and report, with no I/O — which keeps it exhaustively * unit-testable. * * The reverse type map here is the inverse of `fieldTypeToColumnType` * (src/lib/utils/field.ts). The FK-to-relationship logic mirrors how the * platform derives FK column names in `getRelationshipIdField` * (src/lib/utils/relationships/parse.ts) so imported relationships round-trip. */ import { ImportReport, IntrospectedColumn, IntrospectedSchema } from "./types"; export interface ApsorcImportFieldOutput { name: string; type: string; nullable?: boolean; unique?: boolean; default?: unknown; index?: boolean; values?: string[]; length?: number; precision?: number; scale?: number; primary?: boolean; } export interface ApsorcImportEntityOutput { name: string; primaryKeyType?: "serial" | "uuid" | "text"; created_at?: boolean; updated_at?: boolean; fields?: ApsorcImportFieldOutput[]; uniques?: Array<{ fields: string[]; name?: string; }>; indexes?: Array<{ fields: string[]; unique?: boolean; }>; } export interface ApsorcImportRelationshipOutput { from: string; to: string; type: "OneToMany" | "ManyToOne" | "ManyToMany" | "OneToOne"; to_name?: string; nullable?: boolean; bi_directional?: boolean; cascadeDelete?: boolean; } export interface ApsorcImportOutput { version: number; rootFolder: string; apiType: string; entities: ApsorcImportEntityOutput[]; relationships: ApsorcImportRelationshipOutput[]; } interface TypeResolution { type: string; /** True when the mapping loses information (arrays, unknown types). */ lossy?: "array" | "defaulted"; } /** * Resolve a column's `.apsorc` field type from its Postgres type. Postgres * arrays (udt_name starting with "_") collapse to `array`; user enums become * `enum`; unknown types fall back to `text`. */ export declare function pgTypeToApsorcType(col: IntrospectedColumn): TypeResolution; /** * Translate a raw Postgres default expression into an `.apsorc` default value. * Returns `{ drop: true }` when the default cannot be represented (function * calls, sequence/uuid generators). Auto-timestamp and sequence defaults are * dropped silently by callers that handle them structurally. */ export declare function parseDefault(raw: string | null, apsorcType: string): { value?: unknown; drop?: boolean; }; /** * Derive the relationship `to_name` so the platform regenerates a FK column * matching the source column. Returns `undefined` when the default naming * (`${camelCase(target)}Id`) already produces the source column. Sets * `unmapped: true` when no name round-trips to the source column. */ export declare function deriveToName(sourceColumn: string, targetTable: string): { toName?: string; unmapped?: boolean; }; /** * Convert an introspected Postgres schema into a `.apsorc`-shaped object plus a * report of everything that could not be represented losslessly. */ export declare function pgToApsorc(schema: IntrospectedSchema): { apsorc: ApsorcImportOutput; report: ImportReport; }; /** * Invariant guard: every emitted field type must be a recognized `.apsorc` * column type, so code generation never silently falls back to varchar. * Returns the list of any offending types (empty when valid). */ export declare function findUnknownEmittedTypes(output: ApsorcImportOutput): string[]; export {};