diff --git a/source/app.ts b/source/index.ts similarity index 96% rename from source/app.ts rename to source/index.ts index 2b2d1df..decaeaf 100644 --- a/source/app.ts +++ b/source/index.ts @@ -39,7 +39,9 @@ export class App { private readonly router: ApplicationRouter, ) {} - async start(): Promise {} + async start(): Promise { + await this.database.start(); + } /** Stop transports before releasing the database they may still use. */ async stop(): Promise { diff --git a/source/services/storage/database.ts b/source/services/storage/database.ts index c3ef093..1cf194e 100644 --- a/source/services/storage/database.ts +++ b/source/services/storage/database.ts @@ -6,6 +6,7 @@ import type { Logger } from '../../utils/logger.ts'; /** Options required to open a SQLite database connection. */ export type DatabaseOptions = { + /** Filesystem path to the SQLite database file. */ path: string; @@ -39,9 +40,6 @@ export class Database { this.kysely = new Kysely({ dialect: this.dialect, }); - - // Configure the SQLite pragmas. - this.configurePragmas(); } /** @@ -53,6 +51,13 @@ export class Database { return this.kysely; } + async start(): Promise { + this.debug('starting database connection'); + + // Configure the SQLite pragmas. + await this.configurePragmas(); + } + /** * Destroys the database connection. */ @@ -66,10 +71,10 @@ export class Database { * * WAL improves write concurrency; foreign keys enforce referential integrity. */ - private configurePragmas(): void { + private async configurePragmas(): Promise { this.debug('configuring SQLite pragmas'); - this.kysely.executeQuery(CompiledQuery.raw('PRAGMA journal_mode = WAL')); - this.kysely.executeQuery(CompiledQuery.raw('PRAGMA foreign_keys = ON')); + await this.kysely.executeQuery(CompiledQuery.raw('PRAGMA journal_mode = WAL')); + await this.kysely.executeQuery(CompiledQuery.raw('PRAGMA foreign_keys = ON')); } } diff --git a/source/services/storage/migrations/001-resources.ts b/source/services/storage/migrations/001-resources.ts index 0e548c9..27dcf73 100644 --- a/source/services/storage/migrations/001-resources.ts +++ b/source/services/storage/migrations/001-resources.ts @@ -23,7 +23,7 @@ export const up = async (db: Kysely): Promise => { .addColumn('public_key', 'text', (col) => col.notNull()) .addColumn('blob', 'blob', (col) => col.notNull()) .addColumn('timestamp', 'integer', (col) => col.notNull().defaultTo(millisecondTime)) - .addPrimaryKeyConstraint('pk_resource_data', ['resource_id', 'public_key']) + .addPrimaryKeyConstraint('pk_resource_data', [ 'resource_id', 'public_key' ]) .execute(); }; @@ -33,5 +33,6 @@ export const up = async (db: Kysely): Promise => { * @param db - Kysely database to apply the rollback against. */ export const down = async (db: Kysely): Promise => { - await db.schema.dropTable('resource_data').ifExists().execute(); + await db.schema.dropTable('resource_data').ifExists() +.execute(); }; diff --git a/source/services/storage/tables.ts b/source/services/storage/tables.ts index cedf815..d937c28 100644 --- a/source/services/storage/tables.ts +++ b/source/services/storage/tables.ts @@ -10,6 +10,7 @@ export type BlobColumn = ColumnType; * One row per (resource_id, public_key). Each publicKey owns a slot within a shared resource. */ export interface ResourceDataTable { + /** Shared resource identifier grouping related instances. */ resource_id: string; diff --git a/tsconfig.json b/tsconfig.json index bae3d55..6e15aa7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,5 +15,5 @@ "declarationMap": true, "types": ["node"] }, - "exclude": ["node_modules/**/*", "dist/**/*", "test"] + "exclude": ["node_modules/**/*", "dist/**/*"] }