@cardog/corgi
Version:
Fast, offline VIN decoding for Node.js, browsers, and Cloudflare Workers. Powered by the NHTSA VPIC database.
114 lines (111 loc) • 2.89 kB
TypeScript
/**
* Common interface for database operations across different environments
*/
interface DatabaseAdapter {
/**
* Execute a SQL query with parameters and return the results
*
* @param query - SQL query to execute
* @param params - Optional array of parameters to bind to the query
* @returns Array of query results
*/
exec(query: string, params?: any[]): Promise<QueryResult[]>;
/**
* Close the database connection
*/
close(): Promise<void>;
}
/**
* Result from a database query
*/
interface QueryResult {
/**
* Array of column names in the result set
*/
columns: string[];
/**
* Two-dimensional array of values:
* - First dimension: rows
* - Second dimension: column values for each row
*/
values: any[][];
}
/**
* Factory function to create the appropriate database adapter
*/
interface DatabaseAdapterFactory {
/**
* Create a database adapter for the given path or URL
*
* @param pathOrUrl - Path to SQLite file or URL for remote database
* @returns Initialized database adapter
*/
createAdapter(pathOrUrl: string): Promise<DatabaseAdapter>;
}
/**
* Interface for SQL.js static methods
*/
interface SQLJsStatic {
Database: new (data: Uint8Array) => SQLJsDatabase;
}
/**
* Interface for SQL.js database instance
*/
interface SQLJsDatabase {
exec(sql: string, params?: any[]): SQLJsResult[];
close(): void;
}
/**
* Interface for SQL.js query result
*/
interface SQLJsResult {
columns: string[];
values: any[][];
}
/**
* Global window declarations for SQL.js
*/
declare global {
interface Window {
initSqlJs: () => Promise<SQLJsStatic>;
SQL: SQLJsStatic;
}
}
/**
* Browser implementation of the DatabaseAdapter using SQL.js
*/
declare class BrowserDatabaseAdapter implements DatabaseAdapter {
private db;
private queryCount;
/**
* Create a new database adapter for browser environment
*
* @param db - SQL.js database instance
*/
constructor(db: SQLJsDatabase);
/**
* Execute a SQL query with parameters
*
* @param query - SQL query to execute
* @param params - Parameters to bind to the query
* @returns Query results
*/
exec(query: string, params?: any[]): Promise<QueryResult[]>;
/**
* Close the database connection
*/
close(): Promise<void>;
}
/**
* Factory for creating browser database adapters
*/
declare class BrowserDatabaseAdapterFactory implements DatabaseAdapterFactory {
/**
* Create a new database adapter for the given URL
*
* @param pathOrUrl - URL to the SQLite database file
* @returns Initialized database adapter
*/
createAdapter(pathOrUrl: string): Promise<DatabaseAdapter>;
}
export { BrowserDatabaseAdapter, BrowserDatabaseAdapterFactory };