fix linting

This commit is contained in:
2025-05-25 20:48:33 -06:00
parent d6f9567777
commit 26fba66bba
14 changed files with 130 additions and 75 deletions

View File

@@ -1,23 +1,25 @@
import { DatabaseService } from '../interfaces/database';
import { DatabaseService } from "../interfaces/database";
declare module '@jlongster/sql.js' {
declare module "@jlongster/sql.js" {
interface SQL {
Database: any;
FS: any;
register_for_idb: (fs: any) => void;
}
function initSqlJs(config: { locateFile: (file: string) => string }): Promise<SQL>;
function initSqlJs(config: {
locateFile: (file: string) => string;
}): Promise<SQL>;
export default initSqlJs;
}
declare module 'absurd-sql' {
declare module "absurd-sql" {
export class SQLiteFS {
constructor(fs: any, backend: any);
}
}
declare module 'absurd-sql/dist/indexeddb-backend' {
declare module "absurd-sql/dist/indexeddb-backend" {
export default class IndexedDBBackend {
constructor();
}

View File

@@ -1,18 +1,21 @@
// Add type declarations for external modules
declare module '@jlongster/sql.js';
declare module 'absurd-sql';
declare module 'absurd-sql/dist/indexeddb-backend';
declare module "@jlongster/sql.js";
declare module "absurd-sql";
declare module "absurd-sql/dist/indexeddb-backend";
import initSqlJs from '@jlongster/sql.js';
import { SQLiteFS } from 'absurd-sql';
import IndexedDBBackend from 'absurd-sql/dist/indexeddb-backend';
import initSqlJs from "@jlongster/sql.js";
import { SQLiteFS } from "absurd-sql";
import IndexedDBBackend from "absurd-sql/dist/indexeddb-backend";
import { runMigrations } from '../db-sql/migration';
import type { QueryExecResult } from '../interfaces/database';
import { runMigrations } from "../db-sql/migration";
import type { QueryExecResult } from "../interfaces/database";
interface SQLDatabase {
exec: (sql: string, params?: any[]) => Promise<QueryExecResult[]>;
run: (sql: string, params?: any[]) => Promise<{ changes: number; lastId?: number }>;
run: (
sql: string,
params?: any[],
) => Promise<{ changes: number; lastId?: number }>;
}
class DatabaseService {
@@ -62,34 +65,39 @@ class DatabaseService {
const SQL = await initSqlJs({
locateFile: (file: string) => {
return new URL(`/node_modules/@jlongster/sql.js/dist/${file}`, import.meta.url).href;
}
return new URL(
`/node_modules/@jlongster/sql.js/dist/${file}`,
import.meta.url,
).href;
},
});
let sqlFS = new SQLiteFS(SQL.FS, new IndexedDBBackend());
const sqlFS = new SQLiteFS(SQL.FS, new IndexedDBBackend());
SQL.register_for_idb(sqlFS);
SQL.FS.mkdir('/sql');
SQL.FS.mount(sqlFS, {}, '/sql');
SQL.FS.mkdir("/sql");
SQL.FS.mount(sqlFS, {}, "/sql");
const path = '/sql/db.sqlite';
if (typeof SharedArrayBuffer === 'undefined') {
let stream = SQL.FS.open(path, 'a+');
const path = "/sql/db.sqlite";
if (typeof SharedArrayBuffer === "undefined") {
const stream = SQL.FS.open(path, "a+");
await stream.node.contents.readIfFallback();
SQL.FS.close(stream);
}
this.db = new SQL.Database(path, { filename: true });
if (!this.db) {
throw new Error('The database initialization failed. We recommend you restart or reinstall.');
throw new Error(
"The database initialization failed. We recommend you restart or reinstall.",
);
}
await this.db.exec(`PRAGMA journal_mode=MEMORY;`);
const sqlExec = this.db.exec.bind(this.db);
// Run migrations
await runMigrations(sqlExec);
this.initialized = true;
}
@@ -108,13 +116,20 @@ class DatabaseService {
// If initialized but no db, something went wrong
if (!this.db) {
console.error(`Database not properly initialized after await waitForInitialization() - initialized flag is true but db is null`);
throw new Error(`The database could not be initialized. We recommend you restart or reinstall.`);
console.error(
`Database not properly initialized after await waitForInitialization() - initialized flag is true but db is null`,
);
throw new Error(
`The database could not be initialized. We recommend you restart or reinstall.`,
);
}
}
// Used for inserts, updates, and deletes
async run(sql: string, params: any[] = []): Promise<{ changes: number; lastId?: number }> {
async run(
sql: string,
params: any[] = [],
): Promise<{ changes: number; lastId?: number }> {
await this.waitForInitialization();
return this.db!.run(sql, params);
}
@@ -141,4 +156,4 @@ class DatabaseService {
// Create a singleton instance
const databaseService = DatabaseService.getInstance();
export default databaseService;
export default databaseService;

View File

@@ -1,4 +1,4 @@
import { QueryExecResult } from '../interfaces/database';
import { QueryExecResult } from "../interfaces/database";
interface Migration {
name: string;
@@ -23,7 +23,7 @@ export class MigrationService {
}
async runMigrations(
sqlExec: (sql: string, params?: any[]) => Promise<Array<QueryExecResult>>
sqlExec: (sql: string, params?: any[]) => Promise<Array<QueryExecResult>>,
): Promise<void> {
// Create migrations table if it doesn't exist
await sqlExec(`
@@ -35,12 +35,16 @@ export class MigrationService {
`);
// Get list of executed migrations
const result: QueryExecResult[] = await sqlExec('SELECT name FROM migrations;');
const result: QueryExecResult[] = await sqlExec(
"SELECT name FROM migrations;",
);
let executedMigrations: Set<string> = new Set();
// Even with that query, the QueryExecResult may be [] (which doesn't make sense to me).
if (result.length > 0) {
const singleResult = result[0];
executedMigrations = new Set(singleResult.values.map((row: any[]) => row[0]));
executedMigrations = new Set(
singleResult.values.map((row: any[]) => row[0]),
);
}
// Run pending migrations in order
@@ -48,7 +52,9 @@ export class MigrationService {
if (!executedMigrations.has(migration.name)) {
try {
await sqlExec(migration.sql);
await sqlExec('INSERT INTO migrations (name) VALUES (?)', [migration.name]);
await sqlExec("INSERT INTO migrations (name) VALUES (?)", [
migration.name,
]);
console.log(`Migration ${migration.name} executed successfully`);
} catch (error) {
console.error(`Error executing migration ${migration.name}:`, error);
@@ -59,4 +65,4 @@ export class MigrationService {
}
}
export default MigrationService.getInstance();
export default MigrationService.getInstance();