debug: pool, db/events

This commit is contained in:
Alex Gleason 2023-12-27 19:48:48 -06:00
parent 52d39c7a56
commit e121a8805e
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 12 additions and 2 deletions

View File

@ -1,5 +1,5 @@
import { db, type DittoDB } from '@/db.ts';
import { type Event, type SelectQueryBuilder } from '@/deps.ts';
import { Debug, type Event, type SelectQueryBuilder } from '@/deps.ts';
import { isParameterizedReplaceableKind } from '@/kinds.ts';
import { jsonMetaContentSchema } from '@/schemas/nostr.ts';
import { EventData } from '@/types.ts';
@ -7,6 +7,8 @@ import { isNostrId, isURL } from '@/utils.ts';
import type { DittoFilter, GetFiltersOpts } from '@/filter.ts';
const debug = Debug('ditto:db:events');
/** Function to decide whether or not to index a tag. */
type TagCondition = ({ event, count, value }: {
event: Event;
@ -28,6 +30,8 @@ const tagConditions: Record<string, TagCondition> = {
/** Insert an event (and its tags) into the database. */
function insertEvent(event: Event, data: EventData): Promise<void> {
debug('insertEvent', event);
return db.transaction().execute(async (trx) => {
/** Insert the event into the database. */
async function addEvent() {
@ -224,6 +228,7 @@ async function getFilters<K extends number>(
opts: GetFiltersOpts = {},
): Promise<DittoEvent<K>[]> {
if (!filters.length) return Promise.resolve([]);
debug('getFilters', JSON.stringify(filters));
let query = getFiltersQuery(filters);
if (typeof opts.limit === 'number') {
@ -276,6 +281,7 @@ async function getFilters<K extends number>(
/** Delete events based on filters from the database. */
function deleteFilters<K extends number>(filters: DittoFilter<K>[]) {
if (!filters.length) return Promise.resolve([]);
debug('deleteFilters', JSON.stringify(filters));
return db.transaction().execute(async (trx) => {
const query = getFiltersQuery(filters).clearSelect().select('id');
@ -293,6 +299,7 @@ function deleteFilters<K extends number>(filters: DittoFilter<K>[]) {
/** Get number of events that would be returned by filters. */
async function countFilters<K extends number>(filters: DittoFilter<K>[]): Promise<number> {
if (!filters.length) return Promise.resolve(0);
debug('countFilters', JSON.stringify(filters));
const query = getFiltersQuery(filters);
const [{ count }] = await query

View File

@ -1,5 +1,7 @@
import { getActiveRelays } from '@/db/relays.ts';
import { type Event, RelayPool } from '@/deps.ts';
import { Debug, type Event, RelayPool } from '@/deps.ts';
const debug = Debug('ditto:pool');
const activeRelays = await getActiveRelays();
@ -14,6 +16,7 @@ const pool = new RelayPool(activeRelays, {
/** Publish an event to the given relays, or the entire pool. */
function publish(event: Event, relays: string[] = activeRelays) {
debug('publish', event);
return pool.publish(event, relays);
}