Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/count-duplicate-event-id-projection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nostream": patch
---

fix: return COUNT results for filters with generic tag queries (`#e`, `#p`, `#g`, `#h`), which projected `event_id` twice and failed with "error: unable to count events"
6 changes: 1 addition & 5 deletions src/repositories/event-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,12 @@ export class EventRepository implements IEventRepository {
const queries = filters.map((currentFilter) => {
const builder = this.readReplicaDbClient<DBEvent>('events').select('events.event_id')

const { isTagQuery } = this.applyFilterConditions(builder, currentFilter)
this.applyFilterConditions(builder, currentFilter)

if (typeof currentFilter.limit === 'number') {
builder.limit(currentFilter.limit).orderBy('event_created_at', 'DESC').orderBy('event_id', 'asc')
}

if (isTagQuery) {
builder.select('events.event_id')
}

builder.whereNull('events.deleted_at').andWhere((bd) => {
bd.whereNull('events.expires_at').orWhere('events.expires_at', '>', now)
})
Expand Down
27 changes: 27 additions & 0 deletions test/unit/repositories/event-repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,33 @@ describe('EventRepository', () => {
expect(sql).to.include('"events"."expires_at" is null')
expect(sql).to.include('"events"."expires_at" >')
})

it('projects event_id exactly once for generic tag filters', async () => {
const fromStub = sandbox.stub(rrDbClient, 'from').returns({
countDistinct: () => ({
first: async () => ({ count: '1' }),
}),
} as any)

await repository.countByFilters([{ '#e': ['aaaaaa'] } as any])

const sql = fromStub.firstCall.args[0].toString()
expect(sql).to.include('select "events"."event_id" from "events" left join "event_tags"')
expect(sql).to.not.include('"events"."event_id", "events"."event_id"')
})

it('projects the same columns in both branches of a mixed union', async () => {
const fromStub = sandbox.stub(rrDbClient, 'from').returns({
countDistinct: () => ({
first: async () => ({ count: '1' }),
}),
} as any)

await repository.countByFilters([{ kinds: [1] }, { '#e': ['aaaaaa'] } as any])

const sql = fromStub.firstCall.args[0].toString()
expect(sql.match(/select "events"\."event_id" from "events"/g)).to.have.lengthOf(2)
})
})

describe('.create', () => {
Expand Down
Loading