import type { SQL } from 'drizzle-orm/sql'; import * as pgSchema from './schema.pg'; import * as sqliteSchema from './schema.sqlite'; export type IdentityDatabase = any; export type IdentitySchema = typeof pgSchema | typeof sqliteSchema; /** * Get the appropriate schema for the given database connection. * This provides a unified abstraction layer over PG and SQLite schemas. * * @example * const schema = getSchema(db); * await db.select().from(schema.usage).where(eq(schema.usage.scopeId, id)); */ export declare function getSchema(db: IdentityDatabase): typeof pgSchema | typeof sqliteSchema; /** * Standardized query result format across databases. */ export interface QueryResult> { rows: T[]; } /** * Returns true if the connection string is a SQLite URL. */ export declare function isSqliteUrl(connectionString: string): boolean; /** * Get or create a Drizzle database connection with the appropriate schema. * Supports both PostgreSQL and SQLite. */ export declare function getIdentityDatabase(connectionString: string): IdentityDatabase; /** * Get the schema for a given connection string. */ export declare function getIdentitySchema(connectionString: string): IdentitySchema; /** * Safely get a Drizzle database connection, returning undefined on error. * Use this when the identity database is optional (e.g., for usage tracking). */ export declare function tryGetIdentityDatabase(connectionString: string): IdentityDatabase | undefined; export declare function closeAllIdentityConnections(): Promise; /** * Check if a database connection is SQLite. * SQLite drizzle has `all()` method but no `execute()` method. * PostgreSQL drizzle has `execute()` method but no `all()` method. */ export declare function isDatabaseSqlite(db: IdentityDatabase): boolean; /** * Execute a SQL query uniformly across PostgreSQL and SQLite. * Returns a standardized result with rows array. * * @example * const result = await executeQuery(db, sql`SELECT * FROM users WHERE id = ${userId}`); * if (result.rows.length > 0) { ... } */ export declare function executeQuery>(db: IdentityDatabase, query: SQL): Promise>; /** * Execute a SQL statement that doesn't return rows (INSERT, UPDATE, DELETE). * Works uniformly across PostgreSQL and SQLite. */ export declare function executeStatement(db: IdentityDatabase, query: SQL): Promise; export declare function executePostgresLockedStatements(db: IdentityDatabase, lockKey: number, statements: string[]): Promise; export declare function ensureCloudClusterTables(db: IdentityDatabase): Promise; /** * Convert a Date to a value suitable for the database. * SQLite uses Unix timestamps (seconds), PostgreSQL uses Date objects. */ export declare function toDbTimestamp(db: IdentityDatabase, date: Date): number | Date; /** * Parse a timestamp value from database result to Date. * Handles both Unix timestamps (SQLite) and Date objects (PostgreSQL). */ export declare function fromDbTimestamp(value: unknown): Date | undefined; /** * `payload` JSON 字段提取表达式,按方言生成(SQLite json_extract / PG ->>)。 * alias 用于 join 场景下限定来源表。 */ export declare function jsonFieldExtract(db: IdentityDatabase, field: string, alias?: string): SQL; /** * `payload` 字段等于标量值的方言条件。 */ export declare function jsonFieldEquals(db: IdentityDatabase, field: string, value: string, alias?: string): SQL; /** * `payload` 字段落在值集合内的方言条件。values 为空时调用方应跳过查询。 */ export declare function jsonFieldIn(db: IdentityDatabase, field: string, values: string[], alias?: string): SQL; /** * 反向前缀匹配:`value LIKE payload->field || '%'`。 * 用于"资源 URL 以存储的 baseUrl 为前缀"的下推预筛;存储值中的 LIKE 元字符只会多匹配, * 调用方需在 JS 侧用 startsWith 复核,不会漏匹配。 */ export declare function jsonFieldIsPrefixOf(db: IdentityDatabase, field: string, value: string, alias?: string): SQL;