import { type Table as ArrowTable } from 'apache-arrow'; import type { Database as NativeDatabase, Transaction, ConditionSpec, Cell, PutResult, RowJs, TypedColumn, CacheStatsJs, TriggerConfigJs } from '@visorcraft/mongreldb/native.js'; import { IndexBuildPolicyJs, WriteBuffer } from '@visorcraft/mongreldb/native.js'; import { Schema } from './schema.js'; import type { TableSpec } from './types.js'; import { type ProcedureCallOptions, type ProcedureCallResult, type ProcedureSpec } from './procedure.js'; import { type TriggerSpec } from './trigger.js'; import { type ViewSpec, type VirtualTableSpec } from './external.js'; import { type Migration } from './migrate.js'; export interface SqlOptions { timeoutMs?: number; signal?: AbortSignal; queryId?: string; maxOutputRows?: number; maxOutputBytes?: number; } export type CancelOutcome = 'accepted' | 'already_cancelling' | 'too_late' | 'already_finished' | 'not_found' | 'pre_cancelled'; /** Structural HLC from server durable recovery (0.64+). */ export interface SqlCommitHlc { physicalMicros: number; logical: number; nodeTiebreaker: number; } export interface SqlDurableOutcome { committed: boolean | null; committedStatements: number | null; lastCommitEpoch?: bigint; /** Authoritative commit HLC when the server recorded one (0.64+). */ lastCommitHlc?: SqlCommitHlc; firstCommitStatementIndex?: number; lastCommitStatementIndex?: number; /** Serialization phase name (parity with native serialization_state). */ serializationState?: string; } export interface SqlQueryStatus { queryId: string; phase: string; terminalState?: string; serverState?: string; operation: string; committed: boolean | null; durableOutcome: SqlDurableOutcome; terminalErrorCode?: string; terminalErrorCategory?: string; completedStatements: number | null; statementIndex: number | null; cancellationReason: string; cancelOutcome?: CancelOutcome; retryable?: boolean; } export interface SqlQuery { readonly id: string; readonly result: Promise; cancel(): Promise; status(): Promise; } type NativeSqlOptions = { queryId?: string; timeoutMs?: number; maxOutputRows?: number; maxOutputBytes?: number; }; /** A reservoir-sampled approximate aggregate with a confidence interval. */ export interface ApproxAggregate { point: number; ci_low: number; ci_high: number; n_population: number; n_sample_live: number; n_passing: number; } type MongrelColumnSpec = { id: number; name: string; ty: number; primaryKey: boolean; nullable: boolean; autoIncrement?: boolean; embeddingDim?: number; defaultValue?: Cell; defaultExpr?: string; enumVariants?: string[]; encrypted?: boolean; encryptedIndexable?: boolean; embeddingSourceJson?: string; }; type MongrelDatabase = NativeDatabase & { transaction(fn: (txn: Transaction) => void | Promise, opts?: { maxRetries?: number; baseDelayMs?: number; }): Promise; alterColumn(table: string, columnName: string, column: MongrelColumnSpec): bigint; tableColumnSpecs(table: string): MongrelColumnSpec[]; createProcedure(spec: { json: string; }): bigint; createOrReplaceProcedure(spec: { json: string; }): bigint; dropProcedure(name: string): void; procedures(): { json: string; }[]; procedure(name: string): { json: string; } | null; callProcedure(name: string, opts?: { argsJson?: string; idempotencyKey?: string; }): { epoch?: bigint; resultJson: string; }; createTrigger(spec: { json: string; }): bigint; createOrReplaceTrigger(spec: { json: string; }): bigint; dropTrigger(name: string): void; triggers(): { json: string; }[]; trigger(name: string): { json: string; } | null; sql(sql: string): Promise; sqlWithOptions(sql: string, options?: NativeSqlOptions): Promise; createUser(username: string, password: string): void; dropUser(username: string): void; alterUserPassword(username: string, newPassword: string): void; verifyUser(username: string, password: string): boolean; setUserAdmin(username: string, isAdmin: boolean): void; users(): string[]; createRole(name: string): void; dropRole(name: string): void; roles(): string[]; grantRole(username: string, roleName: string): void; revokeRole(username: string, roleName: string): void; grantPermission(roleName: string, permission: string): void; revokePermission(roleName: string, permission: string): void; enableAuth(adminUsername: string, adminPassword: string): void; disableAuth(): void; requireAuthEnabled(): boolean; refreshPrincipal(): void; startCreateIndex(table: string, index: MongrelIndexSpec): bigint; startReplaceIndex(table: string, expectedOldName: string, index: MongrelIndexSpec): bigint; resumeIndexBuild(jobId: bigint): void; cancelJob(jobId: bigint): void; indexJob(jobId: bigint): IndexJobInfo; waitIndexJob(jobId: bigint, timeoutMs?: number): Promise; }; type MongrelIndexSpec = { name: string; columnId: number; kind: number; annQuantization?: number; annAlgorithm?: number; predicate?: string; annM?: number; annEfConstruction?: number; annEfSearch?: number; diskann?: { r?: number; l?: number; beamWidth?: number; alpha?: number; }; ivf?: { nlist?: number; nprobe?: number; trainingSamples?: number; }; product?: { numSubvectors: number; bits?: number; trainingSamples?: number; seed?: bigint; rerankFactor?: number; }; minhashPermutations?: number; minhashBands?: number; learnedRangeEpsilon?: number; }; export type IndexJobInfo = { jobId: bigint; state: string; progress: number; done: bigint; total: bigint; error?: string; }; /** * Run `fn` inside a fresh transaction, retrying bounded-exponentially on * retryable conflicts. Exported so query builders and `KitDatabase` methods * can share one implementation. */ export declare function runSyncTxn(kit: KitDatabase, fn: (txn: Transaction) => void, opts?: { maxRetries?: number; baseDelayMs?: number; }): void; /** * Async twin of {@link runSyncTxn}: run `fn` inside a fresh transaction, * committing via the native `Transaction.commitAsync()` (off the Node event * loop) and retrying bounded-exponentially on retryable conflicts. `fn` may * itself be async; the staged writes are committed atomically after it * resolves. Exported so query builders and `KitDatabase` methods can share one * implementation. */ export declare function runTxn(kit: KitDatabase, fn: (txn: Transaction) => Promise | void, opts?: { maxRetries?: number; baseDelayMs?: number; }): Promise; export declare class KitDatabase { private readonly db; readonly schema: Schema; private constructor(); static open(path: string, schema: Schema): Promise; static openSync(path: string, schema: Schema, options?: { encryption?: { passphrase: string; }; credentials?: { username: string; password: string; }; }): KitDatabase; static createEncryptedSync(path: string, schema: Schema, passphrase: string): KitDatabase; static openEncryptedSync(path: string, schema: Schema, passphrase: string): KitDatabase; /** Create a fresh database with `require_auth = true`, a single admin user, * and the given schema. The returned handle is already authenticated as * the admin. */ static createWithCredentialsSync(path: string, schema: Schema, adminUsername: string, adminPassword: string): KitDatabase; /** Create a fresh encrypted database with `require_auth = true` and a single * admin user. Composes encryption-at-rest with credential enforcement. */ static createEncryptedWithCredentialsSync(path: string, schema: Schema, passphrase: string, adminUsername: string, adminPassword: string): KitDatabase; private static initialize; private ensureInternalTables; private alignExistingTableColumnIds; private ensureAppTable; private writeSchemaCatalog; migrateSync(schema: Schema, migrations: Migration[]): void; close(): void; get nativeDb(): MongrelDatabase; /** Start an online single-column index build and return its durable job id. */ startCreateIndex(tableName: string, index: TableSpec['indexes'][number]): bigint; /** Replace an existing single-column index without exposing a partial index. */ startReplaceIndex(tableName: string, expectedOldName: string, replacement: TableSpec['indexes'][number]): bigint; resumeIndexBuild(jobId: bigint): void; cancelIndexBuild(jobId: bigint): void; indexBuild(jobId: bigint): IndexJobInfo; waitIndexBuild(jobId: bigint, timeoutMs?: number): Promise; tableNames(): string[]; createProcedureSync(spec: ProcedureSpec): bigint; createOrReplaceProcedureSync(spec: ProcedureSpec): bigint; dropProcedureSync(name: string): void; procedures(): ProcedureSpec[]; procedure(name: string): ProcedureSpec | null; callProcedureSync(name: string, opts?: ProcedureCallOptions): ProcedureCallResult; createTriggerSync(spec: TriggerSpec): bigint; createOrReplaceTriggerSync(spec: TriggerSpec): bigint; dropTriggerSync(name: string): void; triggers(): TriggerSpec[]; trigger(name: string): TriggerSpec | null; startSql(sql: string, options?: SqlOptions): SqlQuery; private startSqlWithResult; sql(sql: string, options?: SqlOptions): Promise; sqlRows(sql: string, options?: SqlOptions): Promise[]>; createVirtualTable(spec: VirtualTableSpec): Promise; dropVirtualTable(name: string): Promise; /** Create a SQL view (`CREATE VIEW AS