feat(database): add index.ts with factory function
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import { SqliteAdapter } from './SqliteAdapter';
|
||||
import { migrationManager } from './migrations';
|
||||
|
||||
export async function createDatabase(dbPath: string): Promise<SqliteAdapter> {
|
||||
const db = new SqliteAdapter(dbPath);
|
||||
|
||||
// Apply migrations
|
||||
const currentVersion = await db.getMigrationVersion();
|
||||
const targetVersion = migrationManager.getLatestVersion();
|
||||
|
||||
if (currentVersion < targetVersion) {
|
||||
// Apply all migrations from currentVersion + 1 to targetVersion
|
||||
for (let version = currentVersion + 1; version <= targetVersion; version++) {
|
||||
const migration = migrationManager.getMigrationByVersion(version);
|
||||
if (migration) {
|
||||
await db.runMigration(migration.up);
|
||||
await db.setMigrationVersion(version);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return db;
|
||||
}
|
||||
|
||||
export * from './DatabaseInterface';
|
||||
export * from './SqliteAdapter';
|
||||
Reference in New Issue
Block a user