Files
stash/packages/runtime/src/services/database/database.ts
Morten Olsen 25f614a730
Some checks failed
Build and release / Build (push) Failing after 2m28s
Build and release / update-release-draft (push) Has been skipped
Build and release / Release (push) Has been skipped
ci: add server build
2025-12-10 21:52:20 +01:00

55 lines
1.3 KiB
TypeScript

import knex, { type Knex } from 'knex';
import ClientPgLite from 'knex-pglite';
import { PGlite } from '@electric-sql/pglite';
import { vector } from '@electric-sql/pglite/vector';
import { destroy, type Services } from '../../utils/utils.services.js';
import { migrationSource } from './migrations/migrations.js';
class DatabaseService {
#services: Services;
#instance?: Promise<Knex>;
constructor(services: Services) {
this.#services = services;
}
#setup = async () => {
const pglite = new PGlite({
extensions: { vector },
});
const instance = knex({
client: ClientPgLite,
dialect: 'postgres',
connection: () => ({ pglite }) as object,
});
await instance.raw(`CREATE EXTENSION IF NOT EXISTS vector`);
await instance.migrate.latest({
migrationSource: migrationSource({ services: this.#services }),
});
return instance;
};
public getInstance = () => {
if (!this.#instance) {
this.#instance = this.#setup();
}
return this.#instance;
};
[destroy] = async () => {
if (!this.#instance) {
return;
}
const instance = await this.#instance;
await instance.destroy();
};
}
export { type TableRows, tableNames } from './migrations/migrations.js';
export { DatabaseService };