import SimpleWebTable from "./SimpleWebTable.js"; import Simple from "./Simple.js"; /** * SimpleWebDB is a class that provides a simplified interface for working with DuckDB, a high-performance in-memory analytical database. This class is meant to be used in a web browser. For NodeJS and similar runtimes, use SimpleDB. * * Here's how to instantiate a SimpleWebDB instance and then a SimpleWebTable. * * @example * Basic usage * ```ts * // Instantiating the database. * const sdb = new SimpleWebDB() * * // Creating a new table. * const employees = sdb.newTable("employees") * * // You can now invoke methods on the table. * await employees.loadData("./employees.csv") * await employees.logTable() * * // Removing the table. * await employees.removeTable() * * // Removing the DB to free up memory. * await sdb.done() * ``` * * @example * Instanciating with options * ```ts * // Creating a database with options. Debug information will be logged each time a method is invoked. The first 20 rows of tables will be logged (default is 10). * const sdb = new SimpleWebDB({ debug: true, nbRowsToLog: 20 }) * ``` * * @param options - Configuration options for the SimpleWebDB instance. * @param options.logDuration - If true, logs the duration of operations. * @param options.debug - If true, enables debug logging. * @param options.nbRowsToLog - Number of rows to log when displaying table data. * @param options.nbCharactersToLog - Maximum number of characters to log for strings. Useful to avoid logging large text content. */ export default class SimpleWebDB extends Simple { /** An object keeping track of the data used in cache. @category Properties */ cacheSourcesUsed: string[]; /** A timestamp used to track the total duration logged in done(). @category Properties */ durationStart: number | undefined; constructor(options?: { logDuration?: boolean; debug?: boolean; nbRowsToLog?: number; nbCharactersToLog?: number; }); /** * Initializes DuckDB and establishes a connection to the database. For internal use only. * * @category Internal */ start(): Promise; /** Creates a table in the DB. * * @example * Basic usage * ```ts * // This returns a new SimpleWebTable * const employees = sdb.newTable() * ``` * * @example * With a specific name * ```ts * // By default, tables will be named table1, table2, etc. * // But you can also give them specific names. * const employees = sdb.newTable("employees") * ``` * * @param name - The name of the new table. * @param projections - The projections of the geospatial data, if any. * * @category DB methods */ newTable(name?: string, projections?: { [key: string]: string; }): SimpleWebTable; /** * Remove a table or multiple tables from the database. Invoking methods on the tables will throw and error. * * @example * Basic usage * ```ts * await table.removeTables(tableA) * ``` * * @example * Multiple tables * ```ts * await table.removeTables([tableA, tableB]) * ``` * * @param tables - The tables to be removed * * @category DB methods */ removeTables(tables: SimpleWebTable | SimpleWebTable[]): Promise; /** * Returns the list of tables. * * @example * Basic usage * ```ts * const tables = await sdb.getTables() * ``` * * @category DB methods */ getTables(): Promise; /** * Returns true if a specified table exists and false if not. * * @example * Basic usage * ```ts * // You can also pass a table instance. * const hasEmployees = await sdb.hasTable("employees") * ``` * * @param table - The name of the table to check for existence. * * @category DB methods */ hasTable(table: SimpleWebTable | string): Promise; /** * Returns the DuckDB extensions. * * @example * Basic usage * ```ts * const extensions = await sdb.getExtensions() * ``` * * @category DB methods */ getExtensions(): Promise<{ [key: string]: string | number | boolean | Date | null; }[]>; /** * Executes a custom SQL query, providing flexibility for advanced users. * * @example * Basic usage * ```ts * // You can use the returnDataFrom option to retrieve the data from the query, if needed. * const data = await sdb.customQuery("SELECT * FROM employees WHERE Job = 'Clerk'", { returnDataFrom: "query" }) * ``` * * @param query - The custom SQL query to be executed. * @param options - An optional object with configuration options: * @param options.returnDataFrom - Specifies whether to return data from the "query" or not. Defaults to "none". * @param options.table - The name of the table associated with the query (if applicable). Needed when debug is true. * * @category DB methods */ customQuery(query: string, options?: { returnDataFrom?: "query" | "none"; table?: string; }): Promise<{ [key: string]: string | number | boolean | Date | null; }[] | null>; /** * Frees up memory. Closes the connection to the database and terminates associated resources. * * @example * Basic usage * ```typescript * await sdb.done(); * ``` * * @category DB methods */ done(): Promise; }