mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
Fix DittoDB+EventsDB relationship
This commit is contained in:
parent
5e4a94457f
commit
617659c7fd
7 changed files with 35 additions and 30 deletions
|
|
@ -9,8 +9,8 @@ import { nostrNow } from '@/utils.ts';
|
||||||
|
|
||||||
const signer = new AdminSigner();
|
const signer = new AdminSigner();
|
||||||
|
|
||||||
const kysely = await DittoDB.getInstance();
|
const db = await DittoDB.getInstance();
|
||||||
const eventsDB = new EventsDB(kysely);
|
const eventsDB = new EventsDB(db);
|
||||||
|
|
||||||
const readable = Deno.stdin.readable
|
const readable = Deno.stdin.readable
|
||||||
.pipeThrough(new TextDecoderStream())
|
.pipeThrough(new TextDecoderStream())
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,8 @@ import { AdminSigner } from '@/signers/AdminSigner.ts';
|
||||||
import { EventsDB } from '@/storages/EventsDB.ts';
|
import { EventsDB } from '@/storages/EventsDB.ts';
|
||||||
import { nostrNow } from '@/utils.ts';
|
import { nostrNow } from '@/utils.ts';
|
||||||
|
|
||||||
const kysely = await DittoDB.getInstance();
|
const db = await DittoDB.getInstance();
|
||||||
const eventsDB = new EventsDB(kysely);
|
const eventsDB = new EventsDB(db);
|
||||||
|
|
||||||
const [pubkeyOrNpub, role] = Deno.args;
|
const [pubkeyOrNpub, role] = Deno.args;
|
||||||
const pubkey = pubkeyOrNpub.startsWith('npub1') ? nip19.decode(pubkeyOrNpub as `npub1${string}`).data : pubkeyOrNpub;
|
const pubkey = pubkeyOrNpub.startsWith('npub1') ? nip19.decode(pubkeyOrNpub as `npub1${string}`).data : pubkeyOrNpub;
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,8 @@ import { nip19 } from 'nostr-tools';
|
||||||
import { DittoDB } from '@/db/DittoDB.ts';
|
import { DittoDB } from '@/db/DittoDB.ts';
|
||||||
import { EventsDB } from '@/storages/EventsDB.ts';
|
import { EventsDB } from '@/storages/EventsDB.ts';
|
||||||
|
|
||||||
const kysely = await DittoDB.getInstance();
|
const db = await DittoDB.getInstance();
|
||||||
const eventsDB = new EventsDB(kysely);
|
const eventsDB = new EventsDB(db);
|
||||||
|
|
||||||
interface ImportEventsOpts {
|
interface ImportEventsOpts {
|
||||||
profilesOnly: boolean;
|
profilesOnly: boolean;
|
||||||
|
|
|
||||||
|
|
@ -8,15 +8,12 @@ import { Conf } from '@/config.ts';
|
||||||
import { DittoPostgres } from '@/db/adapters/DittoPostgres.ts';
|
import { DittoPostgres } from '@/db/adapters/DittoPostgres.ts';
|
||||||
import { DittoSQLite } from '@/db/adapters/DittoSQLite.ts';
|
import { DittoSQLite } from '@/db/adapters/DittoSQLite.ts';
|
||||||
import { DittoTables } from '@/db/DittoTables.ts';
|
import { DittoTables } from '@/db/DittoTables.ts';
|
||||||
import { EventsDB } from '@/storages/EventsDB.ts';
|
|
||||||
|
|
||||||
export type DittoDatabase = {
|
export type DittoDatabase = {
|
||||||
dialect: 'sqlite';
|
dialect: 'sqlite';
|
||||||
store: EventsDB;
|
|
||||||
kysely: Kysely<DittoTables> & Kysely<NDatabaseSchema>;
|
kysely: Kysely<DittoTables> & Kysely<NDatabaseSchema>;
|
||||||
} | {
|
} | {
|
||||||
dialect: 'postgres';
|
dialect: 'postgres';
|
||||||
store: EventsDB;
|
|
||||||
kysely: Kysely<DittoTables> & Kysely<NPostgresSchema>;
|
kysely: Kysely<DittoTables> & Kysely<NPostgresSchema>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -37,12 +34,10 @@ export class DittoDB {
|
||||||
case 'sqlite':
|
case 'sqlite':
|
||||||
result.dialect = 'sqlite';
|
result.dialect = 'sqlite';
|
||||||
result.kysely = await DittoSQLite.getInstance();
|
result.kysely = await DittoSQLite.getInstance();
|
||||||
result.store = new EventsDB(result.kysely as any);
|
|
||||||
break;
|
break;
|
||||||
case 'postgres':
|
case 'postgres':
|
||||||
result.dialect = 'postgres';
|
result.dialect = 'postgres';
|
||||||
result.kysely = await DittoPostgres.getInstance();
|
result.kysely = await DittoPostgres.getInstance();
|
||||||
result.store = new EventsDB(result.kysely as any);
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error('Unsupported database URL.');
|
throw new Error('Unsupported database URL.');
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,8 @@ export class Storages {
|
||||||
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 { store } = await DittoDB.getInstance();
|
const db = await DittoDB.getInstance();
|
||||||
|
const store = new EventsDB(db);
|
||||||
await seedZapSplits(store);
|
await seedZapSplits(store);
|
||||||
return store;
|
return store;
|
||||||
})();
|
})();
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
// deno-lint-ignore-file require-await
|
// deno-lint-ignore-file require-await
|
||||||
|
|
||||||
import { NPostgres } from '@nostrify/db';
|
import { NDatabase, NPostgres } from '@nostrify/db';
|
||||||
import {
|
import {
|
||||||
NIP50,
|
NIP50,
|
||||||
NKinds,
|
NKinds,
|
||||||
|
|
@ -13,11 +13,10 @@ import {
|
||||||
NStore,
|
NStore,
|
||||||
} from '@nostrify/nostrify';
|
} from '@nostrify/nostrify';
|
||||||
import { Stickynotes } from '@soapbox/stickynotes';
|
import { Stickynotes } from '@soapbox/stickynotes';
|
||||||
import { Kysely } from 'kysely';
|
|
||||||
import { nip27 } from 'nostr-tools';
|
import { nip27 } from 'nostr-tools';
|
||||||
|
|
||||||
import { Conf } from '@/config.ts';
|
import { Conf } from '@/config.ts';
|
||||||
import { DittoTables } from '@/db/DittoTables.ts';
|
import { DittoDatabase } from '@/db/DittoDB.ts';
|
||||||
import { dbEventCounter } from '@/metrics.ts';
|
import { dbEventCounter } from '@/metrics.ts';
|
||||||
import { RelayError } from '@/RelayError.ts';
|
import { RelayError } from '@/RelayError.ts';
|
||||||
import { purifyEvent } from '@/storages/hydrate.ts';
|
import { purifyEvent } from '@/storages/hydrate.ts';
|
||||||
|
|
@ -33,7 +32,7 @@ type TagCondition = ({ event, count, value }: {
|
||||||
|
|
||||||
/** SQLite database storage adapter for Nostr events. */
|
/** SQLite database storage adapter for Nostr events. */
|
||||||
class EventsDB implements NStore {
|
class EventsDB implements NStore {
|
||||||
private store: NPostgres;
|
private store: NDatabase | NPostgres;
|
||||||
private console = new Stickynotes('ditto:db:events');
|
private console = new Stickynotes('ditto:db:events');
|
||||||
|
|
||||||
/** Conditions for when to index certain tags. */
|
/** 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,
|
't': ({ event, count, value }) => (event.kind === 1985 ? count < 20 : count < 5) && value.length < 50,
|
||||||
};
|
};
|
||||||
|
|
||||||
constructor(private kysely: Kysely<DittoTables>) {
|
constructor(private database: DittoDatabase) {
|
||||||
this.store = new NPostgres(kysely, {
|
const { dialect, kysely } = database;
|
||||||
indexTags: EventsDB.indexTags,
|
|
||||||
indexSearch: EventsDB.searchText,
|
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. */
|
/** Insert an event (and its tags) into the database. */
|
||||||
|
|
@ -277,7 +286,7 @@ class EventsDB implements NStore {
|
||||||
) as { key: 'domain'; value: string } | undefined)?.value;
|
) as { key: 'domain'; value: string } | undefined)?.value;
|
||||||
|
|
||||||
if (domain) {
|
if (domain) {
|
||||||
const query = this.kysely
|
const query = this.database.kysely
|
||||||
.selectFrom('pubkey_domains')
|
.selectFrom('pubkey_domains')
|
||||||
.select('pubkey')
|
.select('pubkey')
|
||||||
.where('domain', '=', domain);
|
.where('domain', '=', domain);
|
||||||
|
|
|
||||||
16
src/test.ts
16
src/test.ts
|
|
@ -99,7 +99,7 @@ export const createTestDB = async (databaseUrl?: string) => {
|
||||||
|
|
||||||
console.warn(`Using: ${dialect}`);
|
console.warn(`Using: ${dialect}`);
|
||||||
|
|
||||||
let kysely: DittoDatabase['kysely'];
|
const db: DittoDatabase = { dialect } as DittoDatabase;
|
||||||
|
|
||||||
if (dialect === 'sqlite') {
|
if (dialect === 'sqlite') {
|
||||||
// migration 021_pgfts_index.ts calls 'Conf.db.dialect',
|
// 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.
|
// The following line ensures to NOT use the DATABASE_URL that may exist in an .env file.
|
||||||
Deno.env.set('DATABASE_URL', 'sqlite://:memory:');
|
Deno.env.set('DATABASE_URL', 'sqlite://:memory:');
|
||||||
|
|
||||||
kysely = new Kysely({
|
db.kysely = new Kysely({
|
||||||
dialect: new DenoSqlite3Dialect({
|
dialect: new DenoSqlite3Dialect({
|
||||||
database: new Sqlite(':memory:'),
|
database: new Sqlite(':memory:'),
|
||||||
}),
|
}),
|
||||||
}) as Kysely<DittoTables> & Kysely<NDatabaseSchema>;
|
}) as Kysely<DittoTables> & Kysely<NDatabaseSchema>;
|
||||||
} else {
|
} else {
|
||||||
kysely = new Kysely({
|
db.kysely = new Kysely({
|
||||||
// @ts-ignore Kysely version mismatch.
|
// @ts-ignore Kysely version mismatch.
|
||||||
dialect: new PostgresJSDialect({
|
dialect: new PostgresJSDialect({
|
||||||
postgres: postgres(Conf.databaseUrl, {
|
postgres: postgres(Conf.databaseUrl, {
|
||||||
|
|
@ -123,14 +123,14 @@ export const createTestDB = async (databaseUrl?: string) => {
|
||||||
log: KyselyLogger,
|
log: KyselyLogger,
|
||||||
}) as Kysely<DittoTables> & Kysely<NPostgresSchema>;
|
}) 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 {
|
return {
|
||||||
dialect,
|
dialect,
|
||||||
store,
|
store,
|
||||||
kysely,
|
kysely: db.kysely,
|
||||||
[Symbol.asyncDispose]: async () => {
|
[Symbol.asyncDispose]: async () => {
|
||||||
if (dialect === 'postgres') {
|
if (dialect === 'postgres') {
|
||||||
for (
|
for (
|
||||||
|
|
@ -149,9 +149,9 @@ export const createTestDB = async (databaseUrl?: string) => {
|
||||||
'event_zaps',
|
'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();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue