digitaltwin-core
Version:
Minimalist framework to collect and handle data in a Digital Twin project
92 lines • 4.16 kB
TypeScript
import type { Knex } from 'knex';
import type { MetadataRow } from '../database_adapter.js';
import { DatabaseAdapter } from '../database_adapter.js';
import type { DataRecord } from '../../types/data_record.js';
import type { StorageService } from '../../storage/storage_service.js';
export interface PostgreSQLConfig {
host: string;
port?: number;
user: string;
password: string;
database: string;
ssl?: boolean;
}
export interface SQLiteConfig {
filename: string;
client?: 'sqlite3' | 'better-sqlite3';
enableForeignKeys?: boolean;
busyTimeout?: number;
}
/**
* Knex-based implementation with extended querying capabilities.
*/
export declare class KnexDatabaseAdapter extends DatabaseAdapter {
#private;
constructor(config: Knex.Config, storage: StorageService);
/**
* Create a KnexDatabaseAdapter for PostgreSQL with simplified configuration
*/
static forPostgreSQL(pgConfig: PostgreSQLConfig, storage: StorageService, _tableName?: string): KnexDatabaseAdapter;
/**
* Create a KnexDatabaseAdapter for SQLite with simplified configuration
*/
static forSQLite(sqliteConfig: SQLiteConfig, storage: StorageService, _tableName?: string): KnexDatabaseAdapter;
save(meta: MetadataRow): Promise<DataRecord>;
delete(id: string, name: string): Promise<void>;
getById(id: string, name: string): Promise<DataRecord | undefined>;
getLatestByName(name: string): Promise<DataRecord | undefined>;
doesTableExists(name: string): Promise<boolean>;
createTable(name: string): Promise<void>;
createTableWithColumns(name: string, columns: Record<string, string>): Promise<void>;
/**
* Migrate existing table schema to match expected schema.
*
* Automatically adds missing columns and indexes for asset tables.
* Only performs safe operations (adding columns with defaults or nullable).
*
* @param {string} name - Table name to migrate
* @returns {Promise<string[]>} Array of migration messages describing what was done
*/
migrateTableSchema(name: string): Promise<string[]>;
getFirstByName(name: string): Promise<DataRecord | undefined>;
getByDateRange(name: string, startDate: Date, endDate?: Date, limit?: number): Promise<DataRecord[]>;
getAfterDate(name: string, afterDate: Date, limit?: number): Promise<DataRecord[]>;
getLatestBefore(name: string, beforeDate: Date): Promise<DataRecord | undefined>;
getLatestRecordsBefore(name: string, beforeDate: Date, limit: number): Promise<DataRecord[]>;
hasRecordsAfterDate(name: string, afterDate: Date): Promise<boolean>;
countByDateRange(name: string, startDate: Date, endDate?: Date): Promise<number>;
saveBatch(metadataList: MetadataRow[]): Promise<DataRecord[]>;
deleteBatch(deleteRequests: Array<{
id: string;
name: string;
}>): Promise<void>;
getByIdsBatch(requests: Array<{
id: string;
name: string;
}>): Promise<DataRecord[]>;
getAllAssetsPaginated(name: string, offset?: number, limit?: number): Promise<{
records: DataRecord[];
total: number;
}>;
findByConditions(tableName: string, conditions: Record<string, any>): Promise<DataRecord[]>;
updateById(tableName: string, id: number, data: Record<string, any>): Promise<void>;
close(): Promise<void>;
/**
* Find records for custom tables (returns raw database rows, not DataRecords)
* This bypasses mapToDataRecord() which assumes standard table structure
*/
findCustomTableRecords(tableName: string, conditions?: Record<string, any>): Promise<any[]>;
/**
* Get a single custom table record by ID (returns raw database row, not DataRecord)
*/
getCustomTableRecordById(tableName: string, id: number): Promise<any | null>;
/**
* Insert a record into a custom table (returns the new record ID)
*/
insertCustomTableRecord(tableName: string, data: Record<string, any>): Promise<number>;
/**
* Get the underlying Knex instance for advanced operations
*/
getKnex(): Knex;
}
//# sourceMappingURL=knex_database_adapter.d.ts.map