Fix DittoDB+EventsDB relationship

This commit is contained in:
Alex Gleason 2024-08-12 15:13:49 -05:00
parent 5e4a94457f
commit 617659c7fd
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
7 changed files with 35 additions and 30 deletions

View file

@ -9,8 +9,8 @@ import { nostrNow } from '@/utils.ts';
const signer = new AdminSigner();
const kysely = await DittoDB.getInstance();
const eventsDB = new EventsDB(kysely);
const db = await DittoDB.getInstance();
const eventsDB = new EventsDB(db);
const readable = Deno.stdin.readable
.pipeThrough(new TextDecoderStream())

View file

@ -6,8 +6,8 @@ import { AdminSigner } from '@/signers/AdminSigner.ts';
import { EventsDB } from '@/storages/EventsDB.ts';
import { nostrNow } from '@/utils.ts';
const kysely = await DittoDB.getInstance();
const eventsDB = new EventsDB(kysely);
const db = await DittoDB.getInstance();
const eventsDB = new EventsDB(db);
const [pubkeyOrNpub, role] = Deno.args;
const pubkey = pubkeyOrNpub.startsWith('npub1') ? nip19.decode(pubkeyOrNpub as `npub1${string}`).data : pubkeyOrNpub;

View file

@ -9,8 +9,8 @@ import { nip19 } from 'nostr-tools';
import { DittoDB } from '@/db/DittoDB.ts';
import { EventsDB } from '@/storages/EventsDB.ts';
const kysely = await DittoDB.getInstance();
const eventsDB = new EventsDB(kysely);
const db = await DittoDB.getInstance();
const eventsDB = new EventsDB(db);
interface ImportEventsOpts {
profilesOnly: boolean;

View file

@ -8,15 +8,12 @@ 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 { EventsDB } from '@/storages/EventsDB.ts';
export type DittoDatabase = {
dialect: 'sqlite';
store: EventsDB;
kysely: Kysely<DittoTables> & Kysely<NDatabaseSchema>;
} | {
dialect: 'postgres';
store: EventsDB;
kysely: Kysely<DittoTables> & Kysely<NPostgresSchema>;
};
@ -37,12 +34,10 @@ export class DittoDB {
case 'sqlite':
result.dialect = 'sqlite';
result.kysely = await DittoSQLite.getInstance();
result.store = new EventsDB(result.kysely as any);
break;
case 'postgres':
result.dialect = 'postgres';
result.kysely = await DittoPostgres.getInstance();
result.store = new EventsDB(result.kysely as any);
break;
default:
throw new Error('Unsupported database URL.');

View file

@ -20,7 +20,8 @@ export class Storages {
public static async db(): Promise<EventsDB> {
if (!this._db) {
this._db = (async () => {
const { store } = await DittoDB.getInstance();
const db = await DittoDB.getInstance();
const store = new EventsDB(db);
await seedZapSplits(store);
return store;
})();

View file

@ -1,6 +1,6 @@
// deno-lint-ignore-file require-await
import { NPostgres } from '@nostrify/db';
import { NDatabase, NPostgres } from '@nostrify/db';
import {
NIP50,
NKinds,
@ -13,11 +13,10 @@ import {
NStore,
} from '@nostrify/nostrify';
import { Stickynotes } from '@soapbox/stickynotes';
import { Kysely } from 'kysely';
import { nip27 } from 'nostr-tools';
import { Conf } from '@/config.ts';
import { DittoTables } from '@/db/DittoTables.ts';
import { DittoDatabase } from '@/db/DittoDB.ts';
import { dbEventCounter } from '@/metrics.ts';
import { RelayError } from '@/RelayError.ts';
import { purifyEvent } from '@/storages/hydrate.ts';
@ -33,7 +32,7 @@ type TagCondition = ({ event, count, value }: {
/** SQLite database storage adapter for Nostr events. */
class EventsDB implements NStore {
private store: NPostgres;
private store: NDatabase | NPostgres;
private console = new Stickynotes('ditto:db:events');
/** Conditions for when to index certain tags. */
@ -53,11 +52,21 @@ class EventsDB implements NStore {
't': ({ event, count, value }) => (event.kind === 1985 ? count < 20 : count < 5) && value.length < 50,
};
constructor(private kysely: Kysely<DittoTables>) {
this.store = new NPostgres(kysely, {
indexTags: EventsDB.indexTags,
indexSearch: EventsDB.searchText,
});
constructor(private database: DittoDatabase) {
const { dialect, kysely } = database;
if (dialect === 'postgres') {
this.store = new NPostgres(kysely, {
indexTags: EventsDB.indexTags,
indexSearch: EventsDB.searchText,
});
} else {
this.store = new NDatabase(kysely, {
fts: 'sqlite',
indexTags: EventsDB.indexTags,
searchText: EventsDB.searchText,
});
}
}
/** Insert an event (and its tags) into the database. */
@ -277,7 +286,7 @@ class EventsDB implements NStore {
) as { key: 'domain'; value: string } | undefined)?.value;
if (domain) {
const query = this.kysely
const query = this.database.kysely
.selectFrom('pubkey_domains')
.select('pubkey')
.where('domain', '=', domain);

View file

@ -99,7 +99,7 @@ export const createTestDB = async (databaseUrl?: string) => {
console.warn(`Using: ${dialect}`);
let kysely: DittoDatabase['kysely'];
const db: DittoDatabase = { dialect } as DittoDatabase;
if (dialect === 'sqlite') {
// migration 021_pgfts_index.ts calls 'Conf.db.dialect',
@ -107,13 +107,13 @@ export const createTestDB = async (databaseUrl?: string) => {
// The following line ensures to NOT use the DATABASE_URL that may exist in an .env file.
Deno.env.set('DATABASE_URL', 'sqlite://:memory:');
kysely = new Kysely({
db.kysely = new Kysely({
dialect: new DenoSqlite3Dialect({
database: new Sqlite(':memory:'),
}),
}) as Kysely<DittoTables> & Kysely<NDatabaseSchema>;
} else {
kysely = new Kysely({
db.kysely = new Kysely({
// @ts-ignore Kysely version mismatch.
dialect: new PostgresJSDialect({
postgres: postgres(Conf.databaseUrl, {
@ -123,14 +123,14 @@ export const createTestDB = async (databaseUrl?: string) => {
log: KyselyLogger,
}) as Kysely<DittoTables> & Kysely<NPostgresSchema>;
}
await DittoDB.migrate(kysely);
const store = new EventsDB(kysely);
await DittoDB.migrate(db.kysely);
const store = new EventsDB(db);
return {
dialect,
store,
kysely,
kysely: db.kysely,
[Symbol.asyncDispose]: async () => {
if (dialect === 'postgres') {
for (
@ -149,9 +149,9 @@ export const createTestDB = async (databaseUrl?: string) => {
'event_zaps',
]
) {
await kysely.schema.dropTable(table).ifExists().cascade().execute();
await db.kysely.schema.dropTable(table).ifExists().cascade().execute();
}
await kysely.destroy();
await db.kysely.destroy();
}
},
};