Merge branch 'order-by-addr' into 'main'
EventsDB: avoid ORDER BY when querying replaceable events by author See merge request soapbox-pub/ditto!235
This commit is contained in:
commit
c6c10e4b7f
|
@ -1,6 +1,7 @@
|
||||||
import { NIP50, NostrEvent, NostrFilter, NSchema as n, NStore } from '@nostrify/nostrify';
|
import { NIP50, NostrEvent, NostrFilter, NSchema as n, NStore } from '@nostrify/nostrify';
|
||||||
import Debug from '@soapbox/stickynotes/debug';
|
import Debug from '@soapbox/stickynotes/debug';
|
||||||
import { Kysely, type SelectQueryBuilder } from 'kysely';
|
import { Kysely, type SelectQueryBuilder } from 'kysely';
|
||||||
|
import { sortEvents } from 'nostr-tools';
|
||||||
|
|
||||||
import { Conf } from '@/config.ts';
|
import { Conf } from '@/config.ts';
|
||||||
import { DittoTables } from '@/db/DittoTables.ts';
|
import { DittoTables } from '@/db/DittoTables.ts';
|
||||||
|
@ -159,8 +160,17 @@ class EventsDB implements NStore {
|
||||||
'events.created_at',
|
'events.created_at',
|
||||||
'events.sig',
|
'events.sig',
|
||||||
])
|
])
|
||||||
.where('events.deleted_at', 'is', null)
|
.where('events.deleted_at', 'is', null);
|
||||||
.orderBy('events.created_at', 'desc');
|
|
||||||
|
/** Whether we are querying for replaceable events by author. */
|
||||||
|
const isAddrQuery = filter.authors &&
|
||||||
|
filter.kinds &&
|
||||||
|
filter.kinds.every((kind) => isReplaceableKind(kind) || isParameterizedReplaceableKind(kind));
|
||||||
|
|
||||||
|
// Avoid ORDER BY when querying for replaceable events by author.
|
||||||
|
if (!isAddrQuery) {
|
||||||
|
query = query.orderBy('events.created_at', 'desc');
|
||||||
|
}
|
||||||
|
|
||||||
for (const [key, value] of Object.entries(filter)) {
|
for (const [key, value] of Object.entries(filter)) {
|
||||||
if (value === undefined) continue;
|
if (value === undefined) continue;
|
||||||
|
@ -275,7 +285,7 @@ class EventsDB implements NStore {
|
||||||
query = query.limit(opts.limit);
|
query = query.limit(opts.limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (await query.execute()).map((row) => {
|
const events = (await query.execute()).map((row) => {
|
||||||
const event: DittoEvent = {
|
const event: DittoEvent = {
|
||||||
id: row.id,
|
id: row.id,
|
||||||
kind: row.kind,
|
kind: row.kind,
|
||||||
|
@ -316,6 +326,8 @@ class EventsDB implements NStore {
|
||||||
|
|
||||||
return event;
|
return event;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return sortEvents(events);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Delete events from each table. Should be run in a transaction! */
|
/** Delete events from each table. Should be run in a transaction! */
|
||||||
|
|
Loading…
Reference in New Issue