| 24 | * @typeParam TSchema - The merged Drizzle schema object. |
| 25 | */ |
| 26 | export class DataConnection< |
| 27 | TSchema extends Record<string, unknown> = Record<string, unknown>, |
| 28 | > { |
| 29 | private _sqlite: SQLiteDatabase; |
| 30 | private _db: DrizzleDB<TSchema>; |
| 31 | private _logger: Logger; |
| 32 | |
| 33 | constructor(schemas: TSchema) { |
| 34 | this._logger = createLogger("database"); |
| 35 | |
| 36 | const dbPath = config.paths.resolveDataFilePath("agentara.db"); |
| 37 | this._sqlite = new SQLiteDatabase(dbPath, { create: true }); |
| 38 | this._sqlite.run("PRAGMA journal_mode = WAL"); |
| 39 | this._sqlite.run("PRAGMA foreign_keys = ON"); |
| 40 | |
| 41 | this._db = drizzle(this._sqlite, { schema: schemas }); |
| 42 | |
| 43 | const migrationsFolder = join(import.meta.dir, "..", "..", "drizzle"); |
| 44 | migrate(this._db, { migrationsFolder }); |
| 45 | |
| 46 | this._logger.info(`Database "${dbPath}" opened`); |
| 47 | } |
| 48 | |
| 49 | /** The Drizzle database instance for executing typed queries. */ |
| 50 | get db(): DrizzleDB<TSchema> { |
| 51 | return this._db; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Close the database connection. |
| 56 | */ |
| 57 | close(): void { |
| 58 | this._sqlite.close(); |
| 59 | this._logger.info("database closed"); |
| 60 | } |
| 61 | } |
nothing calls this directly
no outgoing calls
no test coverage detected