db/events: don't throw on duplicate events

This commit is contained in:
Alex Gleason 2023-08-29 13:20:21 -05:00
parent ebd933126a
commit 77b09baa8c
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 10 additions and 3 deletions

View File

@ -1,5 +1,5 @@
import { db, type TagRow } from '@/db.ts'; import { db, type TagRow } from '@/db.ts';
import { type Event, type Insertable } from '@/deps.ts'; import { type Event, type Insertable, SqliteError } from '@/deps.ts';
import type { DittoFilter, GetFiltersOpts } from '@/filter.ts'; import type { DittoFilter, GetFiltersOpts } from '@/filter.ts';
@ -23,7 +23,7 @@ function insertEvent(event: Event): Promise<void> {
...event, ...event,
tags: JSON.stringify(event.tags), tags: JSON.stringify(event.tags),
}) })
.executeTakeFirst(); .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) => {
@ -48,6 +48,13 @@ function insertEvent(event: Event): Promise<void> {
.values(tags) .values(tags)
.execute(); .execute();
} }
}).catch((error) => {
// Don't throw for duplicate events.
if (error instanceof SqliteError && error.code === 19) {
return;
} else {
throw error;
}
}); });
} }

View File

@ -51,7 +51,7 @@ 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 } from 'https://deno.land/x/sqlite@v3.7.3/mod.ts'; export { DB as Sqlite, SqliteError } from 'https://deno.land/x/sqlite@v3.7.3/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,