mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 03:19:46 +00:00
DittoDB.getInstance() -> Storages.kysely()
This commit is contained in:
parent
d2fb3fd253
commit
ebc0250d81
12 changed files with 35 additions and 37 deletions
|
|
@ -1,7 +1,7 @@
|
|||
import { DittoDB } from '@/db/DittoDB.ts';
|
||||
import { Storages } from '@/storages.ts';
|
||||
|
||||
// This migrates kysely internally.
|
||||
const { kysely } = await DittoDB.getInstance();
|
||||
const kysely = await Storages.kysely();
|
||||
|
||||
// Close the connection before exiting.
|
||||
await kysely.destroy();
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
import { nip19 } from 'nostr-tools';
|
||||
|
||||
import { DittoDB } from '@/db/DittoDB.ts';
|
||||
import { Storages } from '@/storages.ts';
|
||||
import { refreshAuthorStats } from '@/utils/stats.ts';
|
||||
|
||||
|
|
@ -18,6 +17,6 @@ try {
|
|||
}
|
||||
|
||||
const store = await Storages.db();
|
||||
const { kysely } = await DittoDB.getInstance();
|
||||
const kysely = await Storages.kysely();
|
||||
|
||||
await refreshAuthorStats({ pubkey, kysely, store });
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import { z } from 'zod';
|
|||
|
||||
import { AppController } from '@/app.ts';
|
||||
import { Conf } from '@/config.ts';
|
||||
import { DittoDB } from '@/db/DittoDB.ts';
|
||||
import { nostrNow } from '@/utils.ts';
|
||||
import { parseBody } from '@/utils/api.ts';
|
||||
import { Storages } from '@/storages.ts';
|
||||
|
|
@ -82,7 +81,7 @@ const createTokenController: AppController = async (c) => {
|
|||
async function getToken(
|
||||
{ pubkey, secret, relays = [] }: { pubkey: string; secret?: string; relays?: string[] },
|
||||
): Promise<`token1${string}`> {
|
||||
const { kysely } = await DittoDB.getInstance();
|
||||
const kysely = await Storages.kysely();
|
||||
const token = generateToken();
|
||||
|
||||
const serverSeckey = generateSecretKey();
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ import { z } from 'zod';
|
|||
|
||||
import { type AppController } from '@/app.ts';
|
||||
import { Conf } from '@/config.ts';
|
||||
import { DittoDB } from '@/db/DittoDB.ts';
|
||||
import { DittoUpload, dittoUploads } from '@/DittoUploads.ts';
|
||||
import { DittoEvent } from '@/interfaces/DittoEvent.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 params = c.get('listPagination');
|
||||
const store = await Storages.db();
|
||||
const { kysely } = await DittoDB.getInstance();
|
||||
const kysely = await Storages.kysely();
|
||||
|
||||
const zaps = await kysely.selectFrom('event_zaps')
|
||||
.selectAll()
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ import { z } from 'zod';
|
|||
|
||||
import { type AppController } from '@/app.ts';
|
||||
import { Conf } from '@/config.ts';
|
||||
import { DittoDB } from '@/db/DittoDB.ts';
|
||||
import { streamingConnectionsGauge } from '@/metrics.ts';
|
||||
import { MuteListPolicy } from '@/policies/MuteListPolicy.ts';
|
||||
import { getFeedPubkeys } from '@/queries.ts';
|
||||
|
|
@ -222,7 +221,7 @@ async function topicToFilter(
|
|||
|
||||
async function getTokenPubkey(token: string): Promise<string | undefined> {
|
||||
if (token.startsWith('token1')) {
|
||||
const { kysely } = await DittoDB.getInstance();
|
||||
const kysely = await Storages.kysely();
|
||||
|
||||
const { user_pubkey } = await kysely
|
||||
.selectFrom('nip46_tokens')
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
import { register } from 'prom-client';
|
||||
|
||||
import { AppController } from '@/app.ts';
|
||||
import { DittoDB } from '@/db/DittoDB.ts';
|
||||
import { dbAvailableConnectionsGauge, dbPoolSizeGauge } from '@/metrics.ts';
|
||||
import { Storages } from '@/storages.ts';
|
||||
|
||||
/** Prometheus/OpenMetrics controller. */
|
||||
export const metricsController: AppController = async (c) => {
|
||||
const db = await DittoDB.getInstance();
|
||||
const db = await Storages.database();
|
||||
|
||||
// Update some metrics at request time.
|
||||
dbPoolSizeGauge.set(db.poolSize);
|
||||
|
|
|
|||
|
|
@ -3,24 +3,12 @@ import path from 'node:path';
|
|||
|
||||
import { FileMigrationProvider, Kysely, Migrator } from 'kysely';
|
||||
|
||||
import { Conf } from '@/config.ts';
|
||||
import { DittoPglite } from '@/db/adapters/DittoPglite.ts';
|
||||
import { DittoPostgres } from '@/db/adapters/DittoPostgres.ts';
|
||||
import { DittoDatabase, DittoDatabaseOpts } from '@/db/DittoDatabase.ts';
|
||||
import { DittoTables } from '@/db/DittoTables.ts';
|
||||
|
||||
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. */
|
||||
static create(databaseUrl: string, opts?: DittoDatabaseOpts): DittoDatabase {
|
||||
const { protocol } = new URL(databaseUrl);
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import { nip19 } from 'nostr-tools';
|
|||
import { AppMiddleware } from '@/app.ts';
|
||||
import { ConnectSigner } from '@/signers/ConnectSigner.ts';
|
||||
import { ReadOnlySigner } from '@/signers/ReadOnlySigner.ts';
|
||||
import { DittoDB } from '@/db/DittoDB.ts';
|
||||
import { Storages } from '@/storages.ts';
|
||||
|
||||
/** We only accept "Bearer" type. */
|
||||
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')) {
|
||||
try {
|
||||
const { kysely } = await DittoDB.getInstance();
|
||||
const kysely = await Storages.kysely();
|
||||
|
||||
const { user_pubkey, server_seckey, relays } = await kysely
|
||||
.selectFrom('nip46_tokens')
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import { LRUCache } from 'lru-cache';
|
|||
import { z } from 'zod';
|
||||
|
||||
import { Conf } from '@/config.ts';
|
||||
import { DittoDB } from '@/db/DittoDB.ts';
|
||||
import { DittoEvent } from '@/interfaces/DittoEvent.ts';
|
||||
import { pipelineEventsCounter, policyEventsCounter } from '@/metrics.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');
|
||||
}
|
||||
|
||||
const { kysely } = await DittoDB.getInstance();
|
||||
const kysely = await Storages.kysely();
|
||||
|
||||
await Promise.all([
|
||||
storeEvent(event, signal),
|
||||
|
|
@ -104,7 +103,7 @@ async function existsInDB(event: DittoEvent): Promise<boolean> {
|
|||
async function hydrateEvent(event: DittoEvent, signal: AbortSignal): Promise<void> {
|
||||
await hydrateEvents({ events: [event], store: await Storages.db(), signal });
|
||||
|
||||
const { kysely } = await DittoDB.getInstance();
|
||||
const kysely = await Storages.kysely();
|
||||
const domain = await kysely
|
||||
.selectFrom('pubkey_domains')
|
||||
.select('domain')
|
||||
|
|
@ -118,7 +117,7 @@ async function hydrateEvent(event: DittoEvent, signal: AbortSignal): Promise<voi
|
|||
async function storeEvent(event: DittoEvent, signal?: AbortSignal): Promise<undefined> {
|
||||
if (NKinds.ephemeral(event.kind)) return;
|
||||
const store = await Storages.db();
|
||||
const { kysely } = await DittoDB.getInstance();
|
||||
const kysely = await Storages.kysely();
|
||||
|
||||
await updateStats({ event, store, kysely }).catch(debug);
|
||||
await store.event(event, { signal });
|
||||
|
|
@ -146,7 +145,7 @@ async function parseMetadata(event: NostrEvent, signal: AbortSignal): Promise<vo
|
|||
|
||||
// Track pubkey domain.
|
||||
try {
|
||||
const { kysely } = await DittoDB.getInstance();
|
||||
const kysely = await Storages.kysely();
|
||||
const { domain } = parseNip05(nip05);
|
||||
|
||||
await sql`
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
// deno-lint-ignore-file require-await
|
||||
import { Conf } from '@/config.ts';
|
||||
import { DittoDatabase } from '@/db/DittoDatabase.ts';
|
||||
import { DittoDB } from '@/db/DittoDB.ts';
|
||||
import { AdminStore } from '@/storages/AdminStore.ts';
|
||||
import { EventsDB } from '@/storages/EventsDB.ts';
|
||||
|
|
@ -11,16 +12,30 @@ import { seedZapSplits } from '@/utils/zap-split.ts';
|
|||
|
||||
export class Storages {
|
||||
private static _db: Promise<EventsDB> | undefined;
|
||||
private static _database: DittoDatabase | undefined;
|
||||
private static _admin: Promise<AdminStore> | undefined;
|
||||
private static _client: Promise<NPool> | undefined;
|
||||
private static _pubsub: Promise<InternalRelay> | 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. */
|
||||
public static async db(): Promise<EventsDB> {
|
||||
if (!this._db) {
|
||||
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 });
|
||||
await seedZapSplits(store);
|
||||
return store;
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
import { NStore } from '@nostrify/nostrify';
|
||||
import { Kysely } from 'kysely';
|
||||
import { matchFilter } from 'nostr-tools';
|
||||
|
||||
import { DittoDB } from '@/db/DittoDB.ts';
|
||||
import { DittoTables } from '@/db/DittoTables.ts';
|
||||
import { Conf } from '@/config.ts';
|
||||
import { type DittoEvent } from '@/interfaces/DittoEvent.ts';
|
||||
import { findQuoteTag } from '@/utils/tags.ts';
|
||||
import { findQuoteInContent } from '@/utils/note.ts';
|
||||
import { Kysely } from 'kysely';
|
||||
import { Storages } from '@/storages.ts';
|
||||
|
||||
interface HydrateOpts {
|
||||
events: DittoEvent[];
|
||||
|
|
@ -18,7 +18,7 @@ interface HydrateOpts {
|
|||
|
||||
/** Hydrate events using the provided storage. */
|
||||
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) {
|
||||
return events;
|
||||
|
|
|
|||
|
|
@ -3,10 +3,10 @@ import { Stickynotes } from '@soapbox/stickynotes';
|
|||
import { Kysely, sql } from 'kysely';
|
||||
|
||||
import { Conf } from '@/config.ts';
|
||||
import { DittoDB } from '@/db/DittoDB.ts';
|
||||
import { DittoTables } from '@/db/DittoTables.ts';
|
||||
import { handleEvent } from '@/pipeline.ts';
|
||||
import { AdminSigner } from '@/signers/AdminSigner.ts';
|
||||
import { Storages } from '@/storages.ts';
|
||||
import { Time } from '@/utils/time.ts';
|
||||
|
||||
const console = new Stickynotes('ditto:trends');
|
||||
|
|
@ -70,7 +70,7 @@ export async function updateTrendingTags(
|
|||
aliases?: string[],
|
||||
) {
|
||||
console.info(`Updating trending ${l}...`);
|
||||
const { kysely } = await DittoDB.getInstance();
|
||||
const kysely = await Storages.kysely();
|
||||
const signal = AbortSignal.timeout(1000);
|
||||
|
||||
const yesterday = Math.floor((Date.now() - Time.days(1)) / 1000);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue