db/events: support "search" filter

This commit is contained in:
Alex Gleason 2023-08-30 12:04:45 -05:00
parent 0c83e759f3
commit 02e1a4ce58
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
4 changed files with 30 additions and 2 deletions

View File

@ -6,6 +6,7 @@ import { Conf } from '@/config.ts';
interface DittoDB {
events: EventRow;
events_fts: EventFTSRow;
tags: TagRow;
users: UserRow;
relays: RelayRow;
@ -21,6 +22,11 @@ interface EventRow {
sig: string;
}
interface EventFTSRow {
id: string;
content: string;
}
interface TagRow {
tag: string;
value_1: string | null;
@ -43,7 +49,7 @@ interface RelayRow {
const db = new Kysely<DittoDB>({
dialect: new DenoSqliteDialect({
database: new Sqlite(Conf.dbPath),
database: new Sqlite(Conf.dbPath) as any,
}),
});

View File

@ -25,6 +25,10 @@ function insertEvent(event: Event): Promise<void> {
})
.execute();
await trx.insertInto('events_fts')
.values({ id: event.id, content: event.content })
.execute();
const tagCounts: Record<string, number> = {};
const tags = event.tags.reduce<Insertable<TagRow>[]>((results, tag) => {
const tagName = tag[0];
@ -111,6 +115,12 @@ function getFilterQuery(filter: DittoFilter) {
query = query.innerJoin('users', 'users.pubkey', 'events.pubkey');
}
if (filter.search) {
query = query
.innerJoin('events_fts', 'events_fts.id', 'events.id')
.where('events_fts.content', 'match', filter.search);
}
return query;
}

View File

@ -0,0 +1,9 @@
import { Kysely, sql } from '@/deps.ts';
export async function up(db: Kysely<any>): Promise<void> {
await sql`CREATE VIRTUAL TABLE events_fts USING fts5(id, content)`.execute(db);
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema.dropTable('events_fts').execute();
}

View File

@ -50,7 +50,10 @@ export {
export { generateSeededRsa } from 'https://gitlab.com/soapbox-pub/seeded-rsa/-/raw/v1.0.0/mod.ts';
export * as secp from 'npm:@noble/secp256k1@^2.0.0';
export { LRUCache } from 'npm:lru-cache@^10.0.0';
export { DB as Sqlite, SqliteError } from 'https://deno.land/x/sqlite@v3.7.3/mod.ts';
export {
DB as Sqlite,
SqliteError,
} from 'https://raw.githubusercontent.com/teleclimber/deno-sqlite/5283320fce74fbfd90b62d379e8703d386ed0b27/mod.ts';
export * as dotenv from 'https://deno.land/std@0.198.0/dotenv/mod.ts';
export {
FileMigrationProvider,