UNPKG

@unchainedshop/core-events

Version:

This package defines the event module which ensures the emitted events are written to the database as an Event History and offers a core module to search for events.

37 lines (31 loc) 941 B
import { isDocumentDBCompatModeEnabled, mongodb } from '@unchainedshop/mongodb'; import { buildDbIndexes } from '@unchainedshop/mongodb'; import { TimestampFields } from '@unchainedshop/mongodb'; const TWO_DAYS_SEC = 172800; export type Event = { _id?: string; type: string; context?: Record<string, unknown>; payload?: Record<string, unknown>; } & TimestampFields; export const EventsCollection = async (db: mongodb.Db) => { const Events = db.collection<Event>('events'); await buildDbIndexes(Events, [ { index: { created: -1 }, options: { expireAfterSeconds: TWO_DAYS_SEC, name: 'created' }, }, { index: { type: 1 }, options: { name: 'type' } }, !isDocumentDBCompatModeEnabled() && { index: { _id: 'text', type: 'text' }, options: { weights: { _id: 8, type: 4, }, name: 'events_fulltext_search', }, }, ]); return Events; };