diff --git a/deno.json b/deno.json index f015e0d3..32b29b41 100644 --- a/deno.json +++ b/deno.json @@ -11,7 +11,13 @@ "nsec": "deno run scripts/nsec.ts" }, "exclude": ["./public"], - "imports": { "@/": "./src/", "@soapbox/nspec": "jsr:@soapbox/nspec@^0.8.1", "~/fixtures/": "./fixtures/" }, + "imports": { + "@/": "./src/", + "@nostrify/nostrify": "jsr:@nostrify/nostrify@^0.9.7", + "@soapbox/nspec": "jsr:@soapbox/nspec@^0.8.1", + "~/fixtures/": "./fixtures/", + "kysely": "npm:kysely@^0.26.3" + }, "lint": { "include": ["src/", "scripts/"], "rules": { diff --git a/src/db/DittoDB.ts b/src/db/DittoDB.ts index 794a2284..e27f92fa 100644 --- a/src/db/DittoDB.ts +++ b/src/db/DittoDB.ts @@ -1,4 +1,5 @@ import { Conf } from '@/config.ts'; +import { DittoPostgres } from '@/db/adapters/DittoPostgres.ts'; import { DittoSQLite } from '@/db/adapters/DittoSQLite.ts'; import { DittoTables } from '@/db/DittoTables.ts'; import { Kysely } from '@/deps.ts'; @@ -10,6 +11,8 @@ export class DittoDB { switch (databaseUrl.protocol) { case 'sqlite:': return DittoSQLite.getInstance(); + case 'postgres:': + return DittoPostgres.getInstance(); default: throw new Error('Unsupported database URL.'); } diff --git a/src/db/adapters/DittoPostgres.ts b/src/db/adapters/DittoPostgres.ts new file mode 100644 index 00000000..ea3120a8 --- /dev/null +++ b/src/db/adapters/DittoPostgres.ts @@ -0,0 +1,35 @@ +import { Kysely, PostgresAdapter, PostgresIntrospector, PostgresQueryCompiler } from 'kysely'; + +import { DittoTables } from '@/db/DittoTables.ts'; +import { PostgreSQLDriver } from 'https://deno.land/x/kysely_deno_postgres@v0.4.0/mod.ts'; + +export class DittoPostgres { + static db: Kysely | undefined; + + // deno-lint-ignore require-await + static async getInstance(): Promise> { + if (!this.db) { + this.db = new Kysely({ + dialect: { + createAdapter() { + return new PostgresAdapter(); + }, + // @ts-ignore mismatched kysely versions probably + createDriver() { + return new PostgreSQLDriver({ + connectionString: Deno.env.get('DATABASE_URL'), + }); + }, + createIntrospector(db: Kysely) { + return new PostgresIntrospector(db); + }, + createQueryCompiler() { + return new PostgresQueryCompiler(); + }, + }, + }); + } + + return this.db; + } +}