diff --git a/src/config.ts b/src/config.ts index cc14998..d8e322d 100644 --- a/src/config.ts +++ b/src/config.ts @@ -61,27 +61,6 @@ class Conf { static get externalDomain() { return Deno.env.get('NOSTR_EXTERNAL') || Conf.localDomain; } - /** Path to the main SQLite database which stores users, events, and more. */ - static get dbPath() { - if (Deno.env.get('DATABASE_URL') === 'sqlite://:memory:') { - return ':memory:'; - } - - const { host, pathname } = Conf.databaseUrl; - - if (!pathname) return ''; - - // Get relative path. - if (host === '') { - return pathname; - } else if (host === '.') { - return pathname; - } else if (host) { - return host + pathname; - } - - return ''; - } /** * Heroku-style database URL. This is used in production to connect to the * database. @@ -92,9 +71,24 @@ class Conf { * protocol://username:password@host:port/database_name * ``` */ - static get databaseUrl(): url.UrlWithStringQuery { - return url.parse(Deno.env.get('DATABASE_URL') ?? 'sqlite://data/db.sqlite3'); + static get databaseUrl(): string { + return Deno.env.get('DATABASE_URL') ?? 'sqlite://data/db.sqlite3'; } + static db = { + get url(): url.UrlWithStringQuery { + return url.parse(Deno.env.get('DATABASE_URL') ?? 'sqlite://data/db.sqlite3'); + }, + get dialect(): 'sqlite' | 'postgres' | undefined { + switch (Conf.db.url.protocol) { + case 'sqlite:': + return 'sqlite'; + case 'postgres:': + case 'postgresql:': + return 'postgres'; + } + return undefined; + }, + }; /** Character limit to enforce for posts made through Mastodon API. */ static get postCharLimit() { return Number(Deno.env.get('POST_CHAR_LIMIT') || 5000); diff --git a/src/db/DittoDB.ts b/src/db/DittoDB.ts index fbca18d..d06b331 100644 --- a/src/db/DittoDB.ts +++ b/src/db/DittoDB.ts @@ -19,16 +19,13 @@ export class DittoDB { } static async _getInstance(): Promise> { - const { databaseUrl } = Conf; - let kysely: Kysely; - switch (databaseUrl.protocol) { - case 'sqlite:': + switch (Conf.db.dialect) { + case 'sqlite': kysely = await DittoSQLite.getInstance(); break; - case 'postgres:': - case 'postgresql:': + case 'postgres': kysely = await DittoPostgres.getInstance(); break; default: diff --git a/src/db/adapters/DittoPostgres.ts b/src/db/adapters/DittoPostgres.ts index d0abbf9..bfecd92 100644 --- a/src/db/adapters/DittoPostgres.ts +++ b/src/db/adapters/DittoPostgres.ts @@ -19,7 +19,7 @@ export class DittoPostgres { // @ts-ignore mismatched kysely versions probably createDriver() { return new PostgreSQLDriver( - { connectionString: Deno.env.get('DATABASE_URL') }, + { connectionString: Conf.databaseUrl }, Conf.pg.poolSize, ); }, diff --git a/src/db/adapters/DittoSQLite.ts b/src/db/adapters/DittoSQLite.ts index fe225a2..d412ca3 100644 --- a/src/db/adapters/DittoSQLite.ts +++ b/src/db/adapters/DittoSQLite.ts @@ -36,11 +36,11 @@ export class DittoSQLite { /** Get the relative or absolute path based on the `DATABASE_URL`. */ static get path() { - if (Deno.env.get('DATABASE_URL') === 'sqlite://:memory:') { + if (Conf.databaseUrl === 'sqlite://:memory:') { return ':memory:'; } - const { host, pathname } = Conf.databaseUrl; + const { host, pathname } = Conf.db.url; if (!pathname) return ''; diff --git a/src/storages/EventsDB.ts b/src/storages/EventsDB.ts index a550f39..1c52f40 100644 --- a/src/storages/EventsDB.ts +++ b/src/storages/EventsDB.ts @@ -42,17 +42,8 @@ class EventsDB implements NStore { }; constructor(private kysely: Kysely) { - let fts: 'sqlite' | 'postgres' | undefined; - - if (Conf.databaseUrl.protocol === 'sqlite:') { - fts = 'sqlite'; - } - if (['postgres:', 'postgresql:'].includes(Conf.databaseUrl.protocol!)) { - fts = 'postgres'; - } - this.store = new NDatabase(kysely, { - fts, + fts: Conf.db.dialect, indexTags: EventsDB.indexTags, searchText: EventsDB.searchText, });