DittoDB.getInstance() -> Storages.kysely()

This commit is contained in:
Alex Gleason 2024-09-11 13:23:06 -05:00
parent d2fb3fd253
commit ebc0250d81
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
12 changed files with 35 additions and 37 deletions

View file

@ -1,7 +1,7 @@
import { DittoDB } from '@/db/DittoDB.ts'; import { Storages } from '@/storages.ts';
// This migrates kysely internally. // This migrates kysely internally.
const { kysely } = await DittoDB.getInstance(); const kysely = await Storages.kysely();
// Close the connection before exiting. // Close the connection before exiting.
await kysely.destroy(); await kysely.destroy();

View file

@ -1,6 +1,5 @@
import { nip19 } from 'nostr-tools'; import { nip19 } from 'nostr-tools';
import { DittoDB } from '@/db/DittoDB.ts';
import { Storages } from '@/storages.ts'; import { Storages } from '@/storages.ts';
import { refreshAuthorStats } from '@/utils/stats.ts'; import { refreshAuthorStats } from '@/utils/stats.ts';
@ -18,6 +17,6 @@ try {
} }
const store = await Storages.db(); const store = await Storages.db();
const { kysely } = await DittoDB.getInstance(); const kysely = await Storages.kysely();
await refreshAuthorStats({ pubkey, kysely, store }); await refreshAuthorStats({ pubkey, kysely, store });

View file

@ -6,7 +6,6 @@ import { z } from 'zod';
import { AppController } from '@/app.ts'; import { AppController } from '@/app.ts';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { DittoDB } from '@/db/DittoDB.ts';
import { nostrNow } from '@/utils.ts'; import { nostrNow } from '@/utils.ts';
import { parseBody } from '@/utils/api.ts'; import { parseBody } from '@/utils/api.ts';
import { Storages } from '@/storages.ts'; import { Storages } from '@/storages.ts';
@ -82,7 +81,7 @@ const createTokenController: AppController = async (c) => {
async function getToken( async function getToken(
{ pubkey, secret, relays = [] }: { pubkey: string; secret?: string; relays?: string[] }, { pubkey, secret, relays = [] }: { pubkey: string; secret?: string; relays?: string[] },
): Promise<`token1${string}`> { ): Promise<`token1${string}`> {
const { kysely } = await DittoDB.getInstance(); const kysely = await Storages.kysely();
const token = generateToken(); const token = generateToken();
const serverSeckey = generateSecretKey(); const serverSeckey = generateSecretKey();

View file

@ -8,7 +8,6 @@ import { z } from 'zod';
import { type AppController } from '@/app.ts'; import { type AppController } from '@/app.ts';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { DittoDB } from '@/db/DittoDB.ts';
import { DittoUpload, dittoUploads } from '@/DittoUploads.ts'; import { DittoUpload, dittoUploads } from '@/DittoUploads.ts';
import { DittoEvent } from '@/interfaces/DittoEvent.ts'; import { DittoEvent } from '@/interfaces/DittoEvent.ts';
import { getAncestors, getAuthor, getDescendants, getEvent } from '@/queries.ts'; import { getAncestors, getAuthor, getDescendants, getEvent } from '@/queries.ts';
@ -579,7 +578,7 @@ const zappedByController: AppController = async (c) => {
const id = c.req.param('id'); const id = c.req.param('id');
const params = c.get('listPagination'); const params = c.get('listPagination');
const store = await Storages.db(); const store = await Storages.db();
const { kysely } = await DittoDB.getInstance(); const kysely = await Storages.kysely();
const zaps = await kysely.selectFrom('event_zaps') const zaps = await kysely.selectFrom('event_zaps')
.selectAll() .selectAll()

View file

@ -4,7 +4,6 @@ import { z } from 'zod';
import { type AppController } from '@/app.ts'; import { type AppController } from '@/app.ts';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { DittoDB } from '@/db/DittoDB.ts';
import { streamingConnectionsGauge } from '@/metrics.ts'; import { streamingConnectionsGauge } from '@/metrics.ts';
import { MuteListPolicy } from '@/policies/MuteListPolicy.ts'; import { MuteListPolicy } from '@/policies/MuteListPolicy.ts';
import { getFeedPubkeys } from '@/queries.ts'; import { getFeedPubkeys } from '@/queries.ts';
@ -222,7 +221,7 @@ async function topicToFilter(
async function getTokenPubkey(token: string): Promise<string | undefined> { async function getTokenPubkey(token: string): Promise<string | undefined> {
if (token.startsWith('token1')) { if (token.startsWith('token1')) {
const { kysely } = await DittoDB.getInstance(); const kysely = await Storages.kysely();
const { user_pubkey } = await kysely const { user_pubkey } = await kysely
.selectFrom('nip46_tokens') .selectFrom('nip46_tokens')

View file

@ -1,12 +1,12 @@
import { register } from 'prom-client'; import { register } from 'prom-client';
import { AppController } from '@/app.ts'; import { AppController } from '@/app.ts';
import { DittoDB } from '@/db/DittoDB.ts';
import { dbAvailableConnectionsGauge, dbPoolSizeGauge } from '@/metrics.ts'; import { dbAvailableConnectionsGauge, dbPoolSizeGauge } from '@/metrics.ts';
import { Storages } from '@/storages.ts';
/** Prometheus/OpenMetrics controller. */ /** Prometheus/OpenMetrics controller. */
export const metricsController: AppController = async (c) => { export const metricsController: AppController = async (c) => {
const db = await DittoDB.getInstance(); const db = await Storages.database();
// Update some metrics at request time. // Update some metrics at request time.
dbPoolSizeGauge.set(db.poolSize); dbPoolSizeGauge.set(db.poolSize);

View file

@ -3,24 +3,12 @@ import path from 'node:path';
import { FileMigrationProvider, Kysely, Migrator } from 'kysely'; import { FileMigrationProvider, Kysely, Migrator } from 'kysely';
import { Conf } from '@/config.ts';
import { DittoPglite } from '@/db/adapters/DittoPglite.ts'; import { DittoPglite } from '@/db/adapters/DittoPglite.ts';
import { DittoPostgres } from '@/db/adapters/DittoPostgres.ts'; import { DittoPostgres } from '@/db/adapters/DittoPostgres.ts';
import { DittoDatabase, DittoDatabaseOpts } from '@/db/DittoDatabase.ts'; import { DittoDatabase, DittoDatabaseOpts } from '@/db/DittoDatabase.ts';
import { DittoTables } from '@/db/DittoTables.ts'; import { DittoTables } from '@/db/DittoTables.ts';
export class DittoDB { export class DittoDB {
private static db: DittoDatabase | undefined;
/** Create (and migrate) the database if it isn't been already, or return the existing connection. */
static async getInstance(): Promise<DittoDatabase> {
if (!this.db) {
this.db = this.create(Conf.databaseUrl, { poolSize: Conf.pg.poolSize });
await this.migrate(this.db.kysely);
}
return this.db;
}
/** Open a new database connection. */ /** Open a new database connection. */
static create(databaseUrl: string, opts?: DittoDatabaseOpts): DittoDatabase { static create(databaseUrl: string, opts?: DittoDatabaseOpts): DittoDatabase {
const { protocol } = new URL(databaseUrl); const { protocol } = new URL(databaseUrl);

View file

@ -5,7 +5,7 @@ import { nip19 } from 'nostr-tools';
import { AppMiddleware } from '@/app.ts'; import { AppMiddleware } from '@/app.ts';
import { ConnectSigner } from '@/signers/ConnectSigner.ts'; import { ConnectSigner } from '@/signers/ConnectSigner.ts';
import { ReadOnlySigner } from '@/signers/ReadOnlySigner.ts'; import { ReadOnlySigner } from '@/signers/ReadOnlySigner.ts';
import { DittoDB } from '@/db/DittoDB.ts'; import { Storages } from '@/storages.ts';
/** We only accept "Bearer" type. */ /** We only accept "Bearer" type. */
const BEARER_REGEX = new RegExp(`^Bearer (${nip19.BECH32_REGEX.source})$`); const BEARER_REGEX = new RegExp(`^Bearer (${nip19.BECH32_REGEX.source})$`);
@ -20,7 +20,7 @@ export const signerMiddleware: AppMiddleware = async (c, next) => {
if (bech32.startsWith('token1')) { if (bech32.startsWith('token1')) {
try { try {
const { kysely } = await DittoDB.getInstance(); const kysely = await Storages.kysely();
const { user_pubkey, server_seckey, relays } = await kysely const { user_pubkey, server_seckey, relays } = await kysely
.selectFrom('nip46_tokens') .selectFrom('nip46_tokens')

View file

@ -5,7 +5,6 @@ import { LRUCache } from 'lru-cache';
import { z } from 'zod'; import { z } from 'zod';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { DittoDB } from '@/db/DittoDB.ts';
import { DittoEvent } from '@/interfaces/DittoEvent.ts'; import { DittoEvent } from '@/interfaces/DittoEvent.ts';
import { pipelineEventsCounter, policyEventsCounter } from '@/metrics.ts'; import { pipelineEventsCounter, policyEventsCounter } from '@/metrics.ts';
import { RelayError } from '@/RelayError.ts'; import { RelayError } from '@/RelayError.ts';
@ -53,7 +52,7 @@ async function handleEvent(event: DittoEvent, signal: AbortSignal): Promise<void
throw new RelayError('blocked', 'user is disabled'); throw new RelayError('blocked', 'user is disabled');
} }
const { kysely } = await DittoDB.getInstance(); const kysely = await Storages.kysely();
await Promise.all([ await Promise.all([
storeEvent(event, signal), storeEvent(event, signal),
@ -104,7 +103,7 @@ async function existsInDB(event: DittoEvent): Promise<boolean> {
async function hydrateEvent(event: DittoEvent, signal: AbortSignal): Promise<void> { async function hydrateEvent(event: DittoEvent, signal: AbortSignal): Promise<void> {
await hydrateEvents({ events: [event], store: await Storages.db(), signal }); await hydrateEvents({ events: [event], store: await Storages.db(), signal });
const { kysely } = await DittoDB.getInstance(); const kysely = await Storages.kysely();
const domain = await kysely const domain = await kysely
.selectFrom('pubkey_domains') .selectFrom('pubkey_domains')
.select('domain') .select('domain')
@ -118,7 +117,7 @@ async function hydrateEvent(event: DittoEvent, signal: AbortSignal): Promise<voi
async function storeEvent(event: DittoEvent, signal?: AbortSignal): Promise<undefined> { async function storeEvent(event: DittoEvent, signal?: AbortSignal): Promise<undefined> {
if (NKinds.ephemeral(event.kind)) return; if (NKinds.ephemeral(event.kind)) return;
const store = await Storages.db(); const store = await Storages.db();
const { kysely } = await DittoDB.getInstance(); const kysely = await Storages.kysely();
await updateStats({ event, store, kysely }).catch(debug); await updateStats({ event, store, kysely }).catch(debug);
await store.event(event, { signal }); await store.event(event, { signal });
@ -146,7 +145,7 @@ async function parseMetadata(event: NostrEvent, signal: AbortSignal): Promise<vo
// Track pubkey domain. // Track pubkey domain.
try { try {
const { kysely } = await DittoDB.getInstance(); const kysely = await Storages.kysely();
const { domain } = parseNip05(nip05); const { domain } = parseNip05(nip05);
await sql` await sql`

View file

@ -1,5 +1,6 @@
// deno-lint-ignore-file require-await // deno-lint-ignore-file require-await
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { DittoDatabase } from '@/db/DittoDatabase.ts';
import { DittoDB } from '@/db/DittoDB.ts'; import { DittoDB } from '@/db/DittoDB.ts';
import { AdminStore } from '@/storages/AdminStore.ts'; import { AdminStore } from '@/storages/AdminStore.ts';
import { EventsDB } from '@/storages/EventsDB.ts'; import { EventsDB } from '@/storages/EventsDB.ts';
@ -11,16 +12,30 @@ import { seedZapSplits } from '@/utils/zap-split.ts';
export class Storages { export class Storages {
private static _db: Promise<EventsDB> | undefined; private static _db: Promise<EventsDB> | undefined;
private static _database: DittoDatabase | undefined;
private static _admin: Promise<AdminStore> | undefined; private static _admin: Promise<AdminStore> | undefined;
private static _client: Promise<NPool> | undefined; private static _client: Promise<NPool> | undefined;
private static _pubsub: Promise<InternalRelay> | undefined; private static _pubsub: Promise<InternalRelay> | undefined;
private static _search: Promise<SearchStore> | undefined; private static _search: Promise<SearchStore> | undefined;
public static async database(): Promise<DittoDatabase> {
if (!this._database) {
this._database = DittoDB.create(Conf.databaseUrl, { poolSize: Conf.pg.poolSize });
await DittoDB.migrate(this._database.kysely);
}
return this._database;
}
public static async kysely(): Promise<DittoDatabase['kysely']> {
const { kysely } = await this.database();
return kysely;
}
/** SQL database to store events this Ditto server cares about. */ /** SQL database to store events this Ditto server cares about. */
public static async db(): Promise<EventsDB> { public static async db(): Promise<EventsDB> {
if (!this._db) { if (!this._db) {
this._db = (async () => { this._db = (async () => {
const { kysely } = await DittoDB.getInstance(); const { kysely } = await this.database();
const store = new EventsDB({ kysely, pubkey: Conf.pubkey, timeout: Conf.db.timeouts.default }); const store = new EventsDB({ kysely, pubkey: Conf.pubkey, timeout: Conf.db.timeouts.default });
await seedZapSplits(store); await seedZapSplits(store);
return store; return store;

View file

@ -1,13 +1,13 @@
import { NStore } from '@nostrify/nostrify'; import { NStore } from '@nostrify/nostrify';
import { Kysely } from 'kysely';
import { matchFilter } from 'nostr-tools'; import { matchFilter } from 'nostr-tools';
import { DittoDB } from '@/db/DittoDB.ts';
import { DittoTables } from '@/db/DittoTables.ts'; import { DittoTables } from '@/db/DittoTables.ts';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { type DittoEvent } from '@/interfaces/DittoEvent.ts'; import { type DittoEvent } from '@/interfaces/DittoEvent.ts';
import { findQuoteTag } from '@/utils/tags.ts'; import { findQuoteTag } from '@/utils/tags.ts';
import { findQuoteInContent } from '@/utils/note.ts'; import { findQuoteInContent } from '@/utils/note.ts';
import { Kysely } from 'kysely'; import { Storages } from '@/storages.ts';
interface HydrateOpts { interface HydrateOpts {
events: DittoEvent[]; events: DittoEvent[];
@ -18,7 +18,7 @@ interface HydrateOpts {
/** Hydrate events using the provided storage. */ /** Hydrate events using the provided storage. */
async function hydrateEvents(opts: HydrateOpts): Promise<DittoEvent[]> { async function hydrateEvents(opts: HydrateOpts): Promise<DittoEvent[]> {
const { events, store, signal, kysely = (await DittoDB.getInstance()).kysely } = opts; const { events, store, signal, kysely = await Storages.kysely() } = opts;
if (!events.length) { if (!events.length) {
return events; return events;

View file

@ -3,10 +3,10 @@ import { Stickynotes } from '@soapbox/stickynotes';
import { Kysely, sql } from 'kysely'; import { Kysely, sql } from 'kysely';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { DittoDB } from '@/db/DittoDB.ts';
import { DittoTables } from '@/db/DittoTables.ts'; import { DittoTables } from '@/db/DittoTables.ts';
import { handleEvent } from '@/pipeline.ts'; import { handleEvent } from '@/pipeline.ts';
import { AdminSigner } from '@/signers/AdminSigner.ts'; import { AdminSigner } from '@/signers/AdminSigner.ts';
import { Storages } from '@/storages.ts';
import { Time } from '@/utils/time.ts'; import { Time } from '@/utils/time.ts';
const console = new Stickynotes('ditto:trends'); const console = new Stickynotes('ditto:trends');
@ -70,7 +70,7 @@ export async function updateTrendingTags(
aliases?: string[], aliases?: string[],
) { ) {
console.info(`Updating trending ${l}...`); console.info(`Updating trending ${l}...`);
const { kysely } = await DittoDB.getInstance(); const kysely = await Storages.kysely();
const signal = AbortSignal.timeout(1000); const signal = AbortSignal.timeout(1000);
const yesterday = Math.floor((Date.now() - Time.days(1)) / 1000); const yesterday = Math.floor((Date.now() - Time.days(1)) / 1000);