db/events: support "search" filter
This commit is contained in:
parent
0c83e759f3
commit
02e1a4ce58
|
@ -6,6 +6,7 @@ import { Conf } from '@/config.ts';
|
||||||
|
|
||||||
interface DittoDB {
|
interface DittoDB {
|
||||||
events: EventRow;
|
events: EventRow;
|
||||||
|
events_fts: EventFTSRow;
|
||||||
tags: TagRow;
|
tags: TagRow;
|
||||||
users: UserRow;
|
users: UserRow;
|
||||||
relays: RelayRow;
|
relays: RelayRow;
|
||||||
|
@ -21,6 +22,11 @@ interface EventRow {
|
||||||
sig: string;
|
sig: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface EventFTSRow {
|
||||||
|
id: string;
|
||||||
|
content: string;
|
||||||
|
}
|
||||||
|
|
||||||
interface TagRow {
|
interface TagRow {
|
||||||
tag: string;
|
tag: string;
|
||||||
value_1: string | null;
|
value_1: string | null;
|
||||||
|
@ -43,7 +49,7 @@ interface RelayRow {
|
||||||
|
|
||||||
const db = new Kysely<DittoDB>({
|
const db = new Kysely<DittoDB>({
|
||||||
dialect: new DenoSqliteDialect({
|
dialect: new DenoSqliteDialect({
|
||||||
database: new Sqlite(Conf.dbPath),
|
database: new Sqlite(Conf.dbPath) as any,
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,10 @@ function insertEvent(event: Event): Promise<void> {
|
||||||
})
|
})
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
|
await trx.insertInto('events_fts')
|
||||||
|
.values({ id: event.id, content: event.content })
|
||||||
|
.execute();
|
||||||
|
|
||||||
const tagCounts: Record<string, number> = {};
|
const tagCounts: Record<string, number> = {};
|
||||||
const tags = event.tags.reduce<Insertable<TagRow>[]>((results, tag) => {
|
const tags = event.tags.reduce<Insertable<TagRow>[]>((results, tag) => {
|
||||||
const tagName = tag[0];
|
const tagName = tag[0];
|
||||||
|
@ -111,6 +115,12 @@ function getFilterQuery(filter: DittoFilter) {
|
||||||
query = query.innerJoin('users', 'users.pubkey', 'events.pubkey');
|
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;
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
|
@ -50,7 +50,10 @@ export {
|
||||||
export { generateSeededRsa } from 'https://gitlab.com/soapbox-pub/seeded-rsa/-/raw/v1.0.0/mod.ts';
|
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 * as secp from 'npm:@noble/secp256k1@^2.0.0';
|
||||||
export { LRUCache } from 'npm:lru-cache@^10.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 * as dotenv from 'https://deno.land/std@0.198.0/dotenv/mod.ts';
|
||||||
export {
|
export {
|
||||||
FileMigrationProvider,
|
FileMigrationProvider,
|
||||||
|
|
Loading…
Reference in New Issue