From e10f3e4deaed3a8d07d179830c1bcf039c4b9630 Mon Sep 17 00:00:00 2001 From: Sytone Date: Wed, 27 Dec 2023 14:02:03 -0800 Subject: [PATCH] feat: add database and table types for TypeScript This change extends the types already present to allow referencing of the database and tables via TypeScript without type errors being shown. It also makes the validations during compilation and IDE based development better. --- types/alasql.d.ts | 93 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/types/alasql.d.ts b/types/alasql.d.ts index a48e944ad4..6a38e20573 100644 --- a/types/alasql.d.ts +++ b/types/alasql.d.ts @@ -82,6 +82,62 @@ declare module 'alasql' { [x: string]: userFromFunction; } + /** + * AlaSQL database object. This is a lightweight implimentation + * + * @interface database + */ + interface database { + /** + * The database ID. + * + * @type {string} + * @memberof database + */ + databaseid: string; + + /** + * The collection of tables in the database. + * + * @type {tableLookUp} + * @memberof database + */ + tables: tableLookUp; + } + + /** + * AlaSQL table object. This is a lightweight implimentation + * + * @interface table + */ + interface table { + /** + * The array of data stored in the table which can be queried + * + * @type {any[]} + * @memberof table + */ + data: any[]; + } + + /** + * AlaSQL database dictionary + * + * @interface databaseLookUp + */ + interface databaseLookUp { + [databaseName: string]: database; + } + + /** + * AlaSQL table dictionary + * + * @interface tableLookUp + */ + interface tableLookUp { + [tableName: string]: table; + } + interface AlaSQL { options: AlaSQLOptions; error: Error; @@ -94,6 +150,43 @@ declare module 'alasql' { autoval(tablename: string, colname: string, getNext?: boolean): number; yy: {}; setXLSX(xlsxlib: typeof xlsx): void; + + /** + * Array of databases in the AlaSQL object. + * + * @type {databaseLookUp} + * @memberof AlaSQL + */ + databases: databaseLookUp; + + /** + * Equivalent to alasql('USE '+databaseid). This will change the current + * database to the one specified. This will update the useid property and + * the tables property. + * + * @param {string} databaseid + * @memberof AlaSQL + */ + use (databaseid: string): void; + + /** + * The current database ID. If no database is selected, this is the + * default database ID (called alasql). + * + * @type {string} + * @memberof AlaSQL + */ + useid: string; + + /** + * Array of the tables in the default database (called alasql). If + * the database is changes via a USE statement or the use method, this + * becomes the tables in the new database. + * + * @type {tableLookUp} + * @memberof AlaSQL + */ + tables: tableLookUp; } const alasql: AlaSQL;