From 16cad04e5cc5e9522725b17342407b084a383aea Mon Sep 17 00:00:00 2001 From: Matthew Raymer Date: Wed, 28 May 2025 10:56:27 +0000 Subject: [PATCH] WIP: fix(AbsurdSqlDatabaseService) fixes to typing and other curious beasts --- src/main.web.ts | 1 - src/types/absurd-sql.d.ts | 45 ++++++++++++++++++++++++++ src/types/global.d.ts | 36 +++++++++++++++++++++ src/types/modules.d.ts | 67 +++++++++++++++++++++++++++++++++++++++ tsconfig.electron.json | 3 +- tsconfig.json | 3 +- 6 files changed, 152 insertions(+), 3 deletions(-) create mode 100644 src/types/absurd-sql.d.ts create mode 100644 src/types/global.d.ts create mode 100644 src/types/modules.d.ts diff --git a/src/main.web.ts b/src/main.web.ts index 668e793e..592d55fa 100644 --- a/src/main.web.ts +++ b/src/main.web.ts @@ -1,4 +1,3 @@ -// @ts-expect-error but not sure why it's not in there import { initBackend } from "absurd-sql/dist/indexeddb-main-thread"; import { initializeApp } from "./main.common"; diff --git a/src/types/absurd-sql.d.ts b/src/types/absurd-sql.d.ts new file mode 100644 index 00000000..942f7b45 --- /dev/null +++ b/src/types/absurd-sql.d.ts @@ -0,0 +1,45 @@ +declare module 'absurd-sql/dist/indexeddb-backend' { + export default class IndexedDBBackend { + constructor(options?: { + dbName?: string; + storeName?: string; + onReady?: () => void; + onError?: (error: Error) => void; + }); + init(): Promise; + exec(sql: string, params?: any[]): Promise; + close(): Promise; + } +} + +declare module 'absurd-sql/dist/indexeddb-main-thread' { + export function initBackend(worker: Worker): Promise; + + export default class IndexedDBMainThread { + constructor(options?: { + dbName?: string; + storeName?: string; + onReady?: () => void; + onError?: (error: Error) => void; + }); + init(): Promise; + exec(sql: string, params?: any[]): Promise; + close(): Promise; + } +} + +declare module 'absurd-sql' { + export class SQLiteFS { + constructor(fs: unknown, backend: IndexedDBBackend); + init(): Promise; + close(): Promise; + exec(sql: string, params?: any[]): Promise; + prepare(sql: string): Promise; + run(sql: string, params?: any[]): Promise; + get(sql: string, params?: any[]): Promise; + all(sql: string, params?: any[]): Promise; + } + + export * from 'absurd-sql/dist/indexeddb-backend'; + export * from 'absurd-sql/dist/indexeddb-main-thread'; +} \ No newline at end of file diff --git a/src/types/global.d.ts b/src/types/global.d.ts new file mode 100644 index 00000000..9cf4b740 --- /dev/null +++ b/src/types/global.d.ts @@ -0,0 +1,36 @@ +import type { QueryExecResult, SqlValue } from "./database"; + +declare module '@jlongster/sql.js' { + interface SQL { + Database: new (path: string, options?: { filename: boolean }) => Database; + FS: { + mkdir: (path: string) => void; + mount: (fs: any, options: any, path: string) => void; + open: (path: string, flags: string) => any; + close: (stream: any) => void; + }; + register_for_idb: (fs: any) => void; + } + + interface Database { + exec: (sql: string, params?: unknown[]) => Promise; + run: (sql: string, params?: unknown[]) => Promise<{ changes: number; lastId?: number }>; + get: (sql: string, params?: unknown[]) => Promise; + all: (sql: string, params?: unknown[]) => Promise; + prepare: (sql: string) => Promise; + close: () => void; + } + + interface Statement { + run: (params?: unknown[]) => Promise<{ changes: number; lastId?: number }>; + get: (params?: unknown[]) => Promise; + all: (params?: unknown[]) => Promise; + finalize: () => void; + } + + const initSqlJs: (options?: { + locateFile?: (file: string) => string; + }) => Promise; + + export default initSqlJs; +} \ No newline at end of file diff --git a/src/types/modules.d.ts b/src/types/modules.d.ts new file mode 100644 index 00000000..62eb3f8e --- /dev/null +++ b/src/types/modules.d.ts @@ -0,0 +1,67 @@ +import type { QueryExecResult, SqlValue } from "./database"; + +declare module '@jlongster/sql.js' { + interface SQL { + Database: new (path: string, options?: { filename: boolean }) => Database; + FS: { + mkdir: (path: string) => void; + mount: (fs: any, options: any, path: string) => void; + open: (path: string, flags: string) => any; + close: (stream: any) => void; + }; + register_for_idb: (fs: any) => void; + } + + interface Database { + exec: (sql: string, params?: unknown[]) => Promise; + run: (sql: string, params?: unknown[]) => Promise<{ changes: number; lastId?: number }>; + get: (sql: string, params?: unknown[]) => Promise; + all: (sql: string, params?: unknown[]) => Promise; + prepare: (sql: string) => Promise; + close: () => void; + } + + interface Statement { + run: (params?: unknown[]) => Promise<{ changes: number; lastId?: number }>; + get: (params?: unknown[]) => Promise; + all: (params?: unknown[]) => Promise; + finalize: () => void; + } + + const initSqlJs: (options?: { + locateFile?: (file: string) => string; + }) => Promise; + + export default initSqlJs; +} + +declare module 'absurd-sql' { + import type { SQL } from '@jlongster/sql.js'; + export class SQLiteFS { + constructor(fs: any, backend: any); + } +} + +declare module 'absurd-sql/dist/indexeddb-backend' { + export default class IndexedDBBackend { + constructor(); + } +} + +declare module 'absurd-sql/dist/indexeddb-main-thread' { + import type { QueryExecResult } from './database'; + export interface SQLiteOptions { + filename?: string; + autoLoad?: boolean; + debug?: boolean; + } + + export interface SQLiteDatabase { + exec: (sql: string, params?: unknown[]) => Promise; + close: () => Promise; + } + + export function initSqlJs(options?: any): Promise; + export function createDatabase(options?: SQLiteOptions): Promise; + export function openDatabase(options?: SQLiteOptions): Promise; +} \ No newline at end of file diff --git a/tsconfig.electron.json b/tsconfig.electron.json index 73aec38b..c667c18e 100644 --- a/tsconfig.electron.json +++ b/tsconfig.electron.json @@ -16,7 +16,8 @@ "types": ["vite/client"], "paths": { "@/*": ["./src/*"] - } + }, + "typeRoots": ["./node_modules/@types", "./src/types"] }, "include": [ "src/**/*.ts", diff --git a/tsconfig.json b/tsconfig.json index 1eeeed49..043201d8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -19,7 +19,8 @@ "experimentalDecorators": true, "paths": { "@/*": ["src/*"] - } + }, + "typeRoots": ["./node_modules/@types", "./src/types"] }, "include": [ "src/**/*.ts",