54 lines
1.2 KiB
TypeScript
54 lines
1.2 KiB
TypeScript
|
import { DuckDBConnection, DuckDBInstance } from "@duckdb/node-api";
|
||
|
import { Migrations } from "./migrations/migrations.ts";
|
||
|
|
||
|
export class Manager {
|
||
|
/**
|
||
|
* The internal database connection, available when the manager is initialized.
|
||
|
* @protected
|
||
|
*/
|
||
|
protected database?: DuckDBInstance;
|
||
|
|
||
|
/**
|
||
|
* Main initialization promise.
|
||
|
* @protected
|
||
|
*/
|
||
|
protected initialization: Promise<void>;
|
||
|
|
||
|
constructor(public readonly options: { databasePath?: string } = {}) {
|
||
|
this.initialization = this.initialize();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Initialize the infernod manager.
|
||
|
*/
|
||
|
async initialize(): Promise<void> {
|
||
|
this.database = await DuckDBInstance.create(this.databasePath);
|
||
|
|
||
|
// Setup database.
|
||
|
const migrations = new Migrations(this);
|
||
|
await migrations.execute();
|
||
|
await migrations.close();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Return the promise to wait for initialization.
|
||
|
*/
|
||
|
async waitForInitialized(): Promise<void> {
|
||
|
return this.initialization;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get database path option, using default value if undefined.
|
||
|
*/
|
||
|
get databasePath(): string {
|
||
|
return this.options?.databasePath ?? "infernod.db";
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get a new database connection.
|
||
|
*/
|
||
|
newDatabaseConnection(): Promise<DuckDBConnection> {
|
||
|
return this.database!.connect();
|
||
|
}
|
||
|
}
|