import { type Kysely } from 'kysely'; import { type PikkuSchema, type RequiredTypes, type SchemaRequirement } from './pikku-schema.types.js'; export type { PikkuSchema, RequiredTypes, SchemaContext, SchemaRequirement, SchemaStatement, SchemaStatementFactory, } from './pikku-schema.types.js'; export { rawStatement, requiredType, schemaContext, unqualifiedContext, } from './pikku-schema.types.js'; /** * Every table any part of the pikku runtime can need, in dependency order. * * Order is load-bearing, not cosmetic: `scope` has foreign keys onto Better * Auth's `user`, and the tables within each schema reference each other. Sort * this list alphabetically and it stops applying. * * `audit` and `virtualUser` are in the list now. They used to be left out and * created by their own services at boot, which was the only way they ever came * into existence — so with the runtime no longer creating anything, leaving * them out would mean a wired audit sink starting up against a table that * nothing writes a migration for. Both are gated by `ownedBy`, so a project * that wires neither still carries neither. */ export declare const pikkuSchemas: PikkuSchema[]; export { agentSchema } from './agent.schema.js'; export { analyticsSchema } from './analytics.schema.js'; export { auditSchema } from './audit.schema.js'; export { channelSchema } from './channel.schema.js'; export { credentialSchema } from './credential.schema.js'; export { deploymentSchema } from './deployment.schema.js'; export { flagSchema } from './flag.schema.js'; export { scopeSchema } from './scope.schema.js'; export { secretSchema } from './secret.schema.js'; export { sessionSchema } from './session.schema.js'; export { virtualUserSchema } from './virtual-user.schema.js'; export { virtualUserScheduleSchema } from './virtual-user-schedule.schema.js'; export { webhookSchema } from './webhook.schema.js'; export { workflowSchema } from './workflow.schema.js'; /** * The schemas a project needs, given the services its code actually reaches. * * `services` is the inspector's `requiredServices` — the same set that drives * service tree-shaking, so a schema is written for exactly the projects whose * functions, middleware or wirings reach the service that owns its tables. * * The gate is one-sided: a schema declaring no `ownedBy` is always in, because * nothing a project writes implies it and leaving it out would be a guess. */ export declare const requiredPikkuSchemas: (services: ReadonlySet) => PikkuSchema[]; export interface UnmetRequirement { schema: PikkuSchema; requirement: SchemaRequirement; } /** * Look up every prerequisite in `db`, reporting the gaps and the types found. * * Both halves come from one introspection because they are one question. The * gaps say whether a schema can be applied at all; the types say how — a * foreign key onto a `uuid` primary key has to be declared `uuid`, and guessing * `text` produces DDL the database refuses. * * Introspection returns physical names, so `requires` is written in physical * names too — this is the one place in the declaration that is not camelCase. */ export declare const resolveRequirements: (db: Kysely, schemas?: PikkuSchema[]) => Promise<{ types: RequiredTypes; unmet: UnmetRequirement[]; }>; /** * The prerequisites `db` does not satisfy, as a list rather than a throw. * * Separate from `applyPikkuSchemas` because the two callers want opposite * things from the same answer: a service booting cannot proceed without its * tables, while a tool asking what a project's schema *would* be needs to * describe the gap rather than die on it. */ export declare const unsatisfiedRequirements: (db: Kysely, schemas?: PikkuSchema[]) => Promise; /** * Create the tables, in order, on a database that has whatever they require. * * Prerequisites are checked before anything is created, so a project missing * one is told which schema wanted what — rather than being handed a foreign key * error, or a half-applied database to clean up. * * The statements then run in one transaction, so the same promise holds for a * failure the check cannot foresee: a statement that dies mid-list takes the * ones before it with it. On an engine without transactional DDL the rollback * is the engine's to give, not ours. */ export declare const applyPikkuSchemas: (db: Kysely, schemas?: PikkuSchema[]) => Promise; /** A table a schema creates, as the DDL addresses it. */ export interface DeclaredTable { /** Present when the connection is `withSchema(...)`-bound. */ schema?: string; name: string; } /** * The tables a schema creates, read back out of its own compiled SQL. * * Derived rather than declared alongside the statements, because a hand-kept * list of names is a second source of truth: rename a table in a statement and * the list keeps answering for the old one. * * Compiled against `db` rather than in the abstract, so the answer carries the * schema the DDL will actually target — the same binding the lookup has to use. */ export declare const declaredTables: (schema: PikkuSchema, db: Kysely) => DeclaredTable[]; /** * Refuse to start unless one schema's tables are already there. * * What a service calls at boot, and it never issues DDL. The runtime is not an * author of the schema: `pikku db generate` writes the declaration down as a * migration and `pikku db migrate` applies it, and those two are the only way * these tables come into existence. A service that finds them missing says so * and stops. * * This is the whole point of gating what `db generate` writes. While boot could * still create, a schema that generation left out was created silently at * startup instead — two authorities over one set of tables, which is the * condition all of this exists to end. With creation gone, a schema generation * missed is a sentence at startup naming the command that fixes it. * * Half-present is not a distinct case any more. Every missing table is reported * the same way, because the remedy is the same one either way. */ export declare const requirePikkuSchema: (db: Kysely, schema: PikkuSchema) => Promise; /** * Render the declaration as SQL for the dialect `db` was built with. * * `types` comes from `resolveRequirements` against the database the migration * is being written for. Omit it and foreign keys onto another source's columns * compile as `text`, which is a shape, not a migration. * * `schema` qualifies every table the migration creates, for a project that * keeps the runtime tables somewhere other than the default search path. It is * applied here and not to `db` itself because the caller's connection is a * throwaway the declaration was just *applied* to, in whatever schema that * database actually has — qualifying that would create tables in a schema the * scratch database has never heard of. Only the rendered SQL is bound. */ export declare const compilePikkuSchemas: (db: Kysely, schemas?: PikkuSchema[], types?: RequiredTypes, schemaName?: string) => string;