From 02e1a4ce589b22ecee3b7aed20e5836e10423379 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 30 Aug 2023 12:04:45 -0500 Subject: [PATCH] db/events: support "search" filter --- src/db.ts | 8 +++++++- src/db/events.ts | 10 ++++++++++ src/db/migrations/002_events_fts.ts | 9 +++++++++ src/deps.ts | 5 ++++- 4 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 src/db/migrations/002_events_fts.ts diff --git a/src/db.ts b/src/db.ts index 474652c..9f497b2 100644 --- a/src/db.ts +++ b/src/db.ts @@ -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({ dialect: new DenoSqliteDialect({ - database: new Sqlite(Conf.dbPath), + database: new Sqlite(Conf.dbPath) as any, }), }); diff --git a/src/db/events.ts b/src/db/events.ts index fdd04c0..0bedccf 100644 --- a/src/db/events.ts +++ b/src/db/events.ts @@ -25,6 +25,10 @@ function insertEvent(event: Event): Promise { }) .execute(); + await trx.insertInto('events_fts') + .values({ id: event.id, content: event.content }) + .execute(); + const tagCounts: Record = {}; const tags = event.tags.reduce[]>((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; } diff --git a/src/db/migrations/002_events_fts.ts b/src/db/migrations/002_events_fts.ts new file mode 100644 index 0000000..c0341c1 --- /dev/null +++ b/src/db/migrations/002_events_fts.ts @@ -0,0 +1,9 @@ +import { Kysely, sql } from '@/deps.ts'; + +export async function up(db: Kysely): Promise { + await sql`CREATE VIRTUAL TABLE events_fts USING fts5(id, content)`.execute(db); +} + +export async function down(db: Kysely): Promise { + await db.schema.dropTable('events_fts').execute(); +} diff --git a/src/deps.ts b/src/deps.ts index dcef225..31104ac 100644 --- a/src/deps.ts +++ b/src/deps.ts @@ -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,