Refactor Storages to get lazy-loaded only when they are used

This commit is contained in:
Alex Gleason
2024-05-01 14:56:47 -05:00
parent a6681a97d9
commit c190d2c8ce
27 changed files with 175 additions and 140 deletions

View File

@@ -9,7 +9,7 @@ import { Debug, parseFormData, type TypeFest } from '@/deps.ts';
import * as pipeline from '@/pipeline.ts';
import { AdminSigner } from '@/signers/AdminSigner.ts';
import { APISigner } from '@/signers/APISigner.ts';
import { client, eventsDB } from '@/storages.ts';
import { Storages } from '@/storages.ts';
import { nostrNow } from '@/utils.ts';
const debug = Debug('ditto:api');
@@ -43,7 +43,7 @@ async function updateEvent<E extends EventStub>(
fn: (prev: NostrEvent | undefined) => E,
c: AppContext,
): Promise<NostrEvent> {
const [prev] = await eventsDB.query([filter], { limit: 1, signal: c.req.raw.signal });
const [prev] = await Storages.db.query([filter], { limit: 1, signal: c.req.raw.signal });
return createEvent(fn(prev), c);
}
@@ -80,7 +80,7 @@ async function publishEvent(event: NostrEvent, c: AppContext): Promise<NostrEven
try {
await Promise.all([
pipeline.handleEvent(event, c.req.raw.signal),
client.event(event),
Storages.client.event(event),
]);
} catch (e) {
if (e instanceof pipeline.RelayError) {

View File

@@ -5,7 +5,7 @@ import { Conf } from '@/config.ts';
import { Debug } from '@/deps.ts';
import { SimpleLRU } from '@/utils/SimpleLRU.ts';
import { Time } from '@/utils/time.ts';
import { eventsDB } from '@/storages.ts';
import { Storages } from '@/storages.ts';
import { fetchWorker } from '@/workers/fetch.ts';
const debug = Debug('ditto:nip05');
@@ -37,7 +37,7 @@ const nip05Cache = new SimpleLRU<string, nip19.ProfilePointer>(
);
async function localNip05Lookup(name: string): Promise<nip19.ProfilePointer | undefined> {
const [label] = await eventsDB.query([{
const [label] = await Storages.db.query([{
kinds: [1985],
authors: [Conf.pubkey],
'#L': ['nip05'],

View File

@@ -1,10 +1,10 @@
import { Conf } from '@/config.ts';
import { eventsDB } from '@/storages.ts';
import { Storages } from '@/storages.ts';
export async function getRelays(pubkey: string): Promise<Set<string>> {
const relays = new Set<`wss://${string}`>();
const events = await eventsDB.query([
const events = await Storages.db.query([
{ kinds: [10002], authors: [pubkey, Conf.pubkey], limit: 2 },
]);