event.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. export * as Event from "./event"
  2. import { Schema } from "effect"
  3. import { ascending } from "./identifier"
  4. import { Location } from "./location"
  5. import { withStatics } from "./schema"
  6. export const ID = Schema.String.check(Schema.isStartsWith("evt_")).pipe(
  7. Schema.brand("Event.ID"),
  8. withStatics((schema) => ({ create: () => schema.make("evt_" + ascending()) })),
  9. )
  10. export type ID = typeof ID.Type
  11. export type Definition<
  12. Type extends string = string,
  13. DataSchema extends Schema.Codec<unknown, unknown> = Schema.Codec<unknown, unknown>,
  14. > = Schema.Top & {
  15. readonly type: Type
  16. readonly durable?: {
  17. readonly version: number
  18. readonly aggregate: string
  19. }
  20. readonly data: DataSchema
  21. }
  22. export type Data<D extends Definition> = Schema.Schema.Type<D["data"]>
  23. export type Payload<D extends Definition = Definition> = {
  24. readonly id: ID
  25. readonly type: D["type"]
  26. readonly data: Data<D>
  27. readonly durable?: {
  28. readonly aggregateID: string
  29. readonly seq: number
  30. readonly version: number
  31. }
  32. readonly location?: Location.Ref
  33. readonly metadata?: Record<string, unknown>
  34. }
  35. export function define<
  36. const Type extends string,
  37. Fields extends Readonly<Record<PropertyKey, Schema.Codec<unknown, unknown>>>,
  38. >(input: {
  39. readonly type: Type
  40. readonly durable?: {
  41. readonly version: number
  42. readonly aggregate: string
  43. }
  44. readonly schema: Fields
  45. }): Schema.Schema<Payload<Definition<Type, Schema.Struct<Fields>>>> & Definition<Type, Schema.Struct<Fields>> {
  46. const data = Schema.Struct(input.schema)
  47. return Object.assign(
  48. Schema.Struct({
  49. id: ID,
  50. metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
  51. type: Schema.Literal(input.type),
  52. durable: Schema.optional(
  53. Schema.Struct({ aggregateID: Schema.String, seq: Schema.Number, version: Schema.Number }),
  54. ),
  55. location: Schema.optional(Location.Ref),
  56. data,
  57. }).annotate({ identifier: input.type }),
  58. {
  59. type: input.type,
  60. ...(input.durable === undefined ? {} : { durable: input.durable }),
  61. data,
  62. },
  63. ) as Schema.Schema<Payload<Definition<Type, Schema.Struct<Fields>>>> & Definition<Type, Schema.Struct<Fields>>
  64. }
  65. export function inventory<const Definitions extends ReadonlyArray<Definition>>(...definitions: Definitions) {
  66. return Object.freeze(definitions)
  67. }
  68. export function latest(definitions: ReadonlyArray<Definition>) {
  69. return readonlyMap(
  70. definitions.reduce((result, definition) => {
  71. const existing = result.get(definition.type)
  72. if (!existing) {
  73. result.set(definition.type, definition)
  74. return result
  75. }
  76. if (definition.durable && existing.durable && definition.durable.version !== existing.durable.version) {
  77. if (definition.durable.version > existing.durable.version) result.set(definition.type, definition)
  78. return result
  79. }
  80. if (definition !== existing) throw new Error(`Duplicate latest event definition for ${definition.type}`)
  81. return result
  82. }, new Map<string, Definition>()),
  83. )
  84. }
  85. export function versionedType(type: string, version: number) {
  86. return `${type}.${version}`
  87. }
  88. export function durable(definitions: ReadonlyArray<Definition>) {
  89. return readonlyMap(
  90. definitions.reduce((result, definition) => {
  91. if (!definition.durable) return result
  92. const key = versionedType(definition.type, definition.durable.version)
  93. if (result.has(key)) throw new Error(`Duplicate durable event definition for ${key}`)
  94. result.set(key, definition)
  95. return result
  96. }, new Map<string, Definition>()),
  97. )
  98. }
  99. function readonlyMap<Key, Value>(map: Map<Key, Value>): ReadonlyMap<Key, Value> {
  100. const result: ReadonlyMap<Key, Value> = Object.freeze({
  101. get size() {
  102. return map.size
  103. },
  104. entries: () => map.entries(),
  105. forEach: (callback: (value: Value, key: Key, map: ReadonlyMap<Key, Value>) => void, thisArg?: unknown) =>
  106. map.forEach((value, key) => callback.call(thisArg, value, key, result)),
  107. get: (key: Key) => map.get(key),
  108. has: (key: Key) => map.has(key),
  109. keys: () => map.keys(),
  110. values: () => map.values(),
  111. [Symbol.iterator]: () => map[Symbol.iterator](),
  112. })
  113. return result
  114. }