MCPcopy Create free account
hub / github.com/cameri/nostream / upsert

Method upsert

src/repositories/event-repository.ts:277–310  ·  view source on GitHub ↗
(event: Event)

Source from the content-addressed store, hash-verified

275 }
276
277 public upsert(event: Event): Promise<number> {
278 logger('upserting event: %o', event)
279
280 const row = this.toUpsertRow(event)
281
282 const query = this.masterDbClient('events')
283 .insert(row)
284 // NIP-16: Replaceable Events
285 // NIP-33: Parameterized Replaceable Events
286 .onConflict(
287 this.masterDbClient.raw(
288 '(event_pubkey, event_kind, event_deduplication) WHERE (event_kind = 0 OR event_kind = 3 OR event_kind = 41 OR (event_kind >= 10000 AND event_kind < 20000)) OR (event_kind >= 30000 AND event_kind < 40000)',
289 ),
290 )
291 .merge(omit(['event_pubkey', 'event_kind', 'event_deduplication'])(row))
292 .where(function () {
293 this.where('events.event_created_at', '<', row.event_created_at).orWhere(function () {
294 this.where('events.event_created_at', '=', row.event_created_at).andWhere(
295 'events.event_id',
296 '>',
297 row.event_id,
298 )
299 })
300 })
301
302 return {
303 then: <T1, T2>(
304 onfulfilled: (value: number) => T1 | PromiseLike<T1>,
305 onrejected: (reason: any) => T2 | PromiseLike<T2>,
306 ) => query.then(prop('rowCount') as () => number).then(onfulfilled, onrejected),
307 catch: <T>(onrejected: (reason: any) => T | PromiseLike<T>) => query.catch(onrejected),
308 toString: (): string => query.toString(),
309 } as Promise<number>
310 }
311
312 public async upsertMany(events: Event[]): Promise<number> {
313 if (!events.length) {

Callers

nothing calls this directly

Calls 3

toUpsertRowMethod · 0.95
insertMethod · 0.80
toStringMethod · 0.80

Tested by

no test coverage detected