Add Postgres adapter

This commit is contained in:
Alex Gleason 2024-04-19 15:17:09 -05:00
parent 3753648f99
commit 2e41254507
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 45 additions and 1 deletions

View File

@ -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": {

View File

@ -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.');
}

View File

@ -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<DittoTables> | undefined;
// deno-lint-ignore require-await
static async getInstance(): Promise<Kysely<DittoTables>> {
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<unknown>) {
return new PostgresIntrospector(db);
},
createQueryCompiler() {
return new PostgresQueryCompiler();
},
},
});
}
return this.db;
}
}