import { Statement } from 'better-sqlite3'; import { BetterSqlite3Helper } from '@beenotung/better-sqlite3-helper'; import { IntLike } from 'integer'; import { Cache } from './utils/cache'; import { Int } from './types'; export type DB = BetterSqlite3Helper.DBInstance; export type DBInstance = DB; export declare function delDBFile(file: string): void; export type CreateDBOptions = { file: string; mode?: 'incremental' | 'overwrite'; } & Omit; export declare function createDB(options: CreateDBOptions): BetterSqlite3Helper.DBInstance; export type TableSchema = { table: string; fields?: TableFields; autoAddField?: boolean; skipFields?: string[]; refFields?: Array; cacheFields?: string[]; whitelistFields?: string[] | boolean; idFieldSuffix?: string; inplaceUpdate?: boolean; deduplicateFields?: string[]; idField?: string; primaryKeys?: string[]; } & CacheOptions & AutoCreateOptions & CreateOptions; export type AutoCreateOptions = { autoCreateTable?: boolean; autoCreateIndex?: boolean; }; export type CreateOptions = { createTableSql?: string; createIndexSql?: string; }; export type RefFieldSchema = { field: string; idField: string; type?: string; } & CacheOptions & AutoCreateOptions; export type InsertRowFn = (row: any) => number; export declare function makeInsertRowFnFromSchema(db: DB, schema: TableSchema): InsertRowFn; export declare const defaultIdFieldSuffix = "_id"; export declare function toRefIdFieldNames(schema: Pick): string[]; export declare function toRefFieldNames(schema: TableSchema): string[]; export declare const defaultTableFields: { id: string; }; export type DeduplicatedTableSchema = { table: string; deduplicateFields: string[]; idField: string; } & Omit; export declare function makeDeduplicatedInsertRowFnFromSchema(db: DB, schema: DeduplicatedTableSchema, insertRowFn: InsertRowFn): InsertRowFn; type MapRowFn = (row: T) => T; export declare function toRefSchemas(schema: TableSchema): RefFieldSchema[]; export declare function toRefSchema(refField: string | RefFieldSchema, schema: TableSchema): RefFieldSchema; export type InsertRefSqls = { select: Statement; insert: Statement; idField: string; }; export declare function makeInsertRefSqls(db: DB, schema: RefFieldSchema): InsertRefSqls; export type Bool = 0 | 1; export type TableColumn = { cid: number; name: string; type: string; notnull: Bool; pk: Bool; }; export declare function getTableFields(db: DB, table: string): TableColumn[]; export type SqliteMasterRow = { type: 'table' | 'index'; name: string; tbl_name: string; sql: string; }; export declare function getTableIndices(db: DB, table: string): SqliteMasterRow[]; export declare function getAllTables(db: DB): SqliteMasterRow[]; export declare function getAllIndices(db: DB): SqliteMasterRow[]; export declare function removeTableIndices(db: DB, table: string): void; /** remove all indices, including those for primary key */ export declare function removeTableIndicesAndPrimaryKeys(db: DB, table: string): void; /** remove all indices, including those for primary key */ export declare function removeAllTableIndicesAndPrimaryKeys(db: DB): void; export declare function removeAllIndices(db: DB): void; export declare function removeAllTables(db: DB): void; export declare function vacuum(db: DB): void; export declare function setUnsafeMode(db: DB, enable: boolean): void; export type TableInfo = { table: string; fields: TableFields; }; export type TableFields = Record; export declare function makeTableInfo(db: DB, table: string): TableInfo; export declare function addField(db: DB, table: TableInfo, field: string, type: string): void; export declare function makeAutoAddFieldMapRowFn(db: DB, table: TableInfo): MapRowFn; export declare function toSqliteDataType(fieldData: any): string; export declare function isInt(number: any): boolean; export declare function makeSchemaScanner(): { fields: TableFields; addRowFn: (row: any) => void; }; export type Id = string | number; export type IdFields = Record; export type CacheOptions = { cache?: Cache; cacheSize?: number; }; /** * @remark the rows will be updated in-place * */ export declare function insertArrayField(rows: any[] | undefined, idFields: IdFields, insertRowFn: InsertRowFn): void; export declare function forEach(rows: any[] | undefined, fn: (row: any) => void): void; export type SelectRowFn = (offset: number) => any; export declare function makeSelectRowFnFromSchema(db: DB, schema: TableSchema): SelectRowFn; export declare function makeSelectRefFieldSql(db: DB, field: string, idField?: string): Statement; export declare function makeGetRefValueFnFromSchema(db: DB, schema: { field: string; idField?: string; } & CacheOptions): (fieldId: Id) => any; export declare function countRows(db: DB, table: string): number; export declare function makeSelectRefFieldArray(db: DB, schema: CacheOptions & { field: string; table: string; idField: string; } & ({ joinField: string; } | { idFieldSuffix?: string; })): (fieldId: string) => any[]; export declare function makeSelectJoin(db: DB, schema: { field: string; fromTable: string; joinTable: string; joinField: string; idField: string; } & CacheOptions): { sql: Statement; all: (fieldId: string) => any[]; get: (fieldId: string) => any; }; export declare function iterateRows(select: (offset: number) => T, count: number): Generator; export declare function cacheAllRefFields(schema: TableSchema): void; export declare function escapeField(field: string): string; export declare function makeCreateRefTableSql(schema: RefFieldSchema): string; export declare function makeUniqueIndexSql(table: string, fields: string[]): string; export declare function toExportMode(db: DB, cache_size?: number): void; export declare function toSafeMode(db: DB, cache_size?: number): void; export type RefCache = Record; export declare function loadAllRefCache(db: DB, field: string, idField?: string): RefCache; export declare function getRefValueFromCache(cache: RefCache, id: string | number, name?: string): T; /** * select from existing record or insert and return new id * @deprecated use makeCachedPreparedRefFns() instead * */ export declare function makeCachedPreparedGetRefIdFn(db: DB, field: string, idFields?: string): (value: string) => Int; export declare function makeCachedPreparedRefFns(db: DB, field: string, idFields?: string): { getRefValue: (id: IntLike) => any; getRefId: (value: string) => Int; populateCache: () => void; val_cache: any; id_cache: any; }; export declare function makePreparedRefFns(db: DB, field: string, idFields?: string): { getRefValue: (id: IntLike) => import("./types").SqliteDataType; getRefId: (value: string) => Int; }; export {};