event.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. export * as EventV2 from "./event"
  2. import { Cause, Context, Effect, Layer, Option, PubSub, Schema, Stream } from "effect"
  3. import { and, asc, eq, gt } from "drizzle-orm"
  4. import { Database } from "./database/database"
  5. import { EventSequenceTable, EventTable } from "./event/sql"
  6. import { Location } from "./location"
  7. import { withStatics } from "./schema"
  8. import { Identifier } from "./util/identifier"
  9. import { LayerNode } from "./effect/layer-node"
  10. import { isDeepStrictEqual } from "node:util"
  11. export const ID = Schema.String.check(Schema.isStartsWith("evt_")).pipe(
  12. Schema.brand("Event.ID"),
  13. withStatics((schema) => ({
  14. create: () => schema.make("evt_" + Identifier.ascending()),
  15. })),
  16. )
  17. export type ID = typeof ID.Type
  18. export type Definition<Type extends string = string, DataSchema extends Schema.Top = Schema.Top> = {
  19. readonly type: Type
  20. readonly durable?: {
  21. readonly version: number
  22. readonly aggregate: string
  23. }
  24. readonly data: DataSchema
  25. }
  26. export type Data<D extends Definition> = Schema.Schema.Type<D["data"]>
  27. export type Payload<D extends Definition = Definition> = {
  28. readonly id: ID
  29. readonly type: D["type"]
  30. readonly data: Data<D>
  31. readonly durable?: {
  32. readonly aggregateID: string
  33. readonly seq: number
  34. readonly version: number
  35. }
  36. readonly location?: Location.Ref
  37. readonly metadata?: Record<string, unknown>
  38. }
  39. export type Subscriber<D extends Definition = Definition> = (event: Payload<D>) => Effect.Effect<void>
  40. export type Unsubscribe = Effect.Effect<void>
  41. export const latestSequence = Effect.fn("EventV2.latestSequence")(function* (
  42. db: Database.Interface["db"],
  43. aggregateID: string,
  44. ) {
  45. const row = yield* db
  46. .select({ seq: EventSequenceTable.seq })
  47. .from(EventSequenceTable)
  48. .where(eq(EventSequenceTable.aggregate_id, aggregateID))
  49. .get()
  50. .pipe(Effect.orDie)
  51. return row?.seq ?? -1
  52. })
  53. export type SerializedEvent = {
  54. readonly id: ID
  55. readonly type: string
  56. readonly seq: number
  57. readonly aggregateID: string
  58. readonly data: Record<string, unknown>
  59. }
  60. export class InvalidDurableEventError extends Schema.TaggedErrorClass<InvalidDurableEventError>()(
  61. "EventV2.InvalidDurableEvent",
  62. {
  63. type: Schema.String,
  64. message: Schema.String,
  65. },
  66. ) {}
  67. export function versionedType(type: string, version: number) {
  68. return `${type}.${version}`
  69. }
  70. export const registry = new Map<string, Definition>()
  71. const durableRegistry = new Map<string, Definition>()
  72. export function define<const Type extends string, Fields extends Schema.Struct.Fields>(input: {
  73. readonly type: Type
  74. readonly durable?: {
  75. readonly version: number
  76. readonly aggregate: string
  77. }
  78. readonly schema: Fields
  79. }): Schema.Schema<Payload<Definition<Type, Schema.Struct<Fields>>>> & Definition<Type, Schema.Struct<Fields>> {
  80. const Data = Schema.Struct(input.schema)
  81. const Payload = Schema.Struct({
  82. id: ID,
  83. metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
  84. type: Schema.Literal(input.type),
  85. durable: Schema.optional(Schema.Struct({ aggregateID: Schema.String, seq: Schema.Number, version: Schema.Number })),
  86. location: Schema.optional(Location.Ref),
  87. data: Data,
  88. }).annotate({ identifier: input.type })
  89. const definition = Object.assign(Payload, {
  90. type: input.type,
  91. ...(input.durable === undefined ? {} : { durable: input.durable }),
  92. data: Data,
  93. })
  94. const existing = registry.get(input.type)
  95. if (
  96. input.durable === undefined ||
  97. existing?.durable === undefined ||
  98. input.durable.version >= existing.durable.version
  99. ) {
  100. registry.set(input.type, definition)
  101. }
  102. if (input.durable) durableRegistry.set(versionedType(input.type, input.durable.version), definition)
  103. return definition as Schema.Schema<Payload<Definition<Type, Schema.Struct<Fields>>>> &
  104. Definition<Type, Schema.Struct<Fields>>
  105. }
  106. export function definitions() {
  107. return registry.values().toArray()
  108. }
  109. export interface PublishOptions {
  110. readonly id?: ID
  111. readonly metadata?: Record<string, unknown>
  112. readonly location?: Location.Ref
  113. /** Local operational projection committed atomically with a new durable event. Not replayed or serialized. */
  114. readonly commit?: (seq: number) => Effect.Effect<void>
  115. }
  116. export interface Interface {
  117. readonly publish: <D extends Definition>(
  118. definition: D,
  119. data: Data<D>,
  120. options?: PublishOptions,
  121. ) => Effect.Effect<Payload<D>>
  122. readonly subscribe: <D extends Definition>(definition: D) => Stream.Stream<Payload<D>>
  123. readonly all: () => Stream.Stream<Payload>
  124. readonly durable: (input: { readonly aggregateID: string; readonly after?: number }) => Stream.Stream<Payload>
  125. /** @deprecated Use `all()` and consume the returned stream. */
  126. readonly listen: (listener: Subscriber) => Effect.Effect<Unsubscribe>
  127. readonly project: <D extends Definition>(definition: D, projector: Subscriber<D>) => Effect.Effect<void>
  128. readonly replay: (
  129. event: SerializedEvent,
  130. options?: { readonly publish?: boolean; readonly ownerID?: string; readonly strictOwner?: boolean },
  131. ) => Effect.Effect<void>
  132. readonly replayAll: (
  133. events: SerializedEvent[],
  134. options?: { readonly publish?: boolean; readonly ownerID?: string; readonly strictOwner?: boolean },
  135. ) => Effect.Effect<string | undefined>
  136. readonly remove: (aggregateID: string) => Effect.Effect<void>
  137. readonly claim: (aggregateID: string, ownerID: string) => Effect.Effect<void>
  138. }
  139. export class Service extends Context.Service<Service, Interface>()("@opencode/Event") {}
  140. export interface LayerOptions {
  141. readonly beforeAggregateRead?: (aggregateID: string) => Effect.Effect<void>
  142. }
  143. export const layerWith = (options?: LayerOptions) =>
  144. Layer.effect(
  145. Service,
  146. Effect.gen(function* () {
  147. const pubsub = {
  148. all: yield* PubSub.unbounded<Payload>(),
  149. durable: new Map<string, Set<PubSub.PubSub<void>>>(),
  150. typed: new Map<string, PubSub.PubSub<Payload>>(),
  151. }
  152. const projectors = new Map<string, Subscriber[]>()
  153. const listeners = new Array<Subscriber>()
  154. const { db } = yield* Database.Service
  155. const getOrCreate = (definition: Definition) =>
  156. Effect.gen(function* () {
  157. const existing = pubsub.typed.get(definition.type)
  158. if (existing) return existing
  159. const created = yield* PubSub.unbounded<Payload>()
  160. pubsub.typed.set(definition.type, created)
  161. return created
  162. })
  163. yield* Effect.addFinalizer(() =>
  164. Effect.gen(function* () {
  165. yield* PubSub.shutdown(pubsub.all)
  166. yield* Effect.forEach(
  167. pubsub.durable.values(),
  168. (pubsubs) => Effect.forEach(pubsubs, PubSub.shutdown, { discard: true }),
  169. { discard: true },
  170. )
  171. yield* Effect.forEach(pubsub.typed.values(), PubSub.shutdown, { discard: true })
  172. }),
  173. )
  174. function commitDurableEvent(
  175. event: Payload,
  176. input?: {
  177. readonly seq: number
  178. readonly aggregateID: string
  179. readonly ownerID?: string
  180. readonly strictOwner?: boolean
  181. },
  182. commit?: (seq: number) => Effect.Effect<void>,
  183. ) {
  184. return Effect.gen(function* () {
  185. const definition = registry.get(event.type)
  186. const durable = definition?.durable
  187. if (durable) {
  188. const aggregateID = (event.data as Record<string, unknown>)[durable.aggregate]
  189. if (typeof aggregateID !== "string") {
  190. yield* Effect.die(
  191. new InvalidDurableEventError({
  192. type: event.type,
  193. message: `Expected string aggregate field ${durable.aggregate}`,
  194. }),
  195. )
  196. } else {
  197. if (input && input.aggregateID !== aggregateID) {
  198. yield* Effect.die(
  199. new InvalidDurableEventError({
  200. type: event.type,
  201. message: `Aggregate mismatch: expected ${input.aggregateID}, got ${aggregateID}`,
  202. }),
  203. )
  204. }
  205. const list = projectors.get(event.type) ?? []
  206. return yield* Effect.uninterruptible(
  207. Effect.gen(function* () {
  208. const committed = yield* db
  209. .transaction(
  210. () =>
  211. Effect.gen(function* () {
  212. const row = yield* db
  213. .select({ seq: EventSequenceTable.seq, ownerID: EventSequenceTable.owner_id })
  214. .from(EventSequenceTable)
  215. .where(eq(EventSequenceTable.aggregate_id, aggregateID))
  216. .get()
  217. .pipe(Effect.orDie)
  218. const latest = row?.seq ?? -1
  219. const encoded = Schema.encodeUnknownSync(
  220. definition.data as Schema.Codec<unknown, unknown, never, never>,
  221. )(event.data) as Record<string, unknown>
  222. if (input?.strictOwner && row?.ownerID && row.ownerID !== input.ownerID) {
  223. yield* Effect.die(
  224. new InvalidDurableEventError({
  225. type: event.type,
  226. message: `Replay owner mismatch for aggregate ${aggregateID}: expected ${row.ownerID}, got ${input.ownerID ?? "none"}`,
  227. }),
  228. )
  229. }
  230. if (input && input.seq <= latest) {
  231. const stored = yield* db
  232. .select()
  233. .from(EventTable)
  234. .where(and(eq(EventTable.aggregate_id, aggregateID), eq(EventTable.seq, input.seq)))
  235. .get()
  236. .pipe(Effect.orDie)
  237. if (
  238. stored?.id === event.id &&
  239. stored.type === versionedType(definition.type, durable.version) &&
  240. isDeepStrictEqual(stored.data, encoded)
  241. ) {
  242. if (input.ownerID && row?.ownerID == null) {
  243. yield* db
  244. .update(EventSequenceTable)
  245. .set({ owner_id: input.ownerID })
  246. .where(eq(EventSequenceTable.aggregate_id, aggregateID))
  247. .run()
  248. .pipe(Effect.orDie)
  249. }
  250. return
  251. }
  252. yield* Effect.die(
  253. new InvalidDurableEventError({
  254. type: event.type,
  255. message: `Replay diverged at aggregate ${aggregateID} sequence ${input.seq}`,
  256. }),
  257. )
  258. }
  259. if (input && row?.ownerID && row.ownerID !== input.ownerID) {
  260. return
  261. }
  262. const seq = input?.seq ?? latest + 1
  263. if (input && seq !== latest + 1) {
  264. yield* Effect.die(
  265. new InvalidDurableEventError({
  266. type: event.type,
  267. message: `Sequence mismatch for aggregate ${aggregateID}: expected ${latest + 1}, got ${seq}`,
  268. }),
  269. )
  270. }
  271. const stored = yield* db
  272. .select({ aggregateID: EventTable.aggregate_id, seq: EventTable.seq })
  273. .from(EventTable)
  274. .where(eq(EventTable.id, event.id))
  275. .get()
  276. .pipe(Effect.orDie)
  277. if (stored)
  278. yield* Effect.die(
  279. new InvalidDurableEventError({
  280. type: event.type,
  281. message: `Event ${event.id} already exists at aggregate ${stored.aggregateID} sequence ${stored.seq}`,
  282. }),
  283. )
  284. const committed = {
  285. ...event,
  286. durable: { aggregateID, seq, version: durable.version },
  287. } as Payload
  288. for (const projector of list) {
  289. yield* projector(committed)
  290. }
  291. if (commit) yield* commit(seq)
  292. yield* db
  293. .insert(EventSequenceTable)
  294. .values([{ aggregate_id: aggregateID, seq, owner_id: input?.ownerID }])
  295. .onConflictDoUpdate({
  296. target: EventSequenceTable.aggregate_id,
  297. set: {
  298. seq,
  299. ...(input?.ownerID && row?.ownerID == null ? { owner_id: input.ownerID } : {}),
  300. },
  301. })
  302. .run()
  303. .pipe(Effect.orDie)
  304. yield* db
  305. .insert(EventTable)
  306. .values([
  307. {
  308. id: event.id,
  309. aggregate_id: aggregateID,
  310. seq,
  311. type: versionedType(definition.type, durable.version),
  312. data: encoded,
  313. },
  314. ])
  315. .run()
  316. .pipe(Effect.orDie)
  317. return { aggregateID, seq }
  318. }),
  319. { behavior: "immediate" },
  320. )
  321. .pipe(Effect.orDie)
  322. if (committed) {
  323. yield* Effect.forEach(
  324. pubsub.durable.get(committed.aggregateID) ?? [],
  325. (wake) => PubSub.publish(wake, undefined),
  326. { discard: true },
  327. )
  328. }
  329. return committed
  330. }),
  331. )
  332. }
  333. }
  334. })
  335. }
  336. function publishEvent<D extends Definition>(event: Payload<D>, commit?: PublishOptions["commit"]) {
  337. return Effect.gen(function* () {
  338. const definition = registry.get(event.type)
  339. if (!definition?.durable && commit)
  340. return yield* Effect.die(
  341. new InvalidDurableEventError({
  342. type: event.type,
  343. message: "Local commit hooks require a durable event",
  344. }),
  345. )
  346. if (definition?.durable) {
  347. const committed = yield* commitDurableEvent(event as Payload, undefined, commit)
  348. if (committed) {
  349. event = {
  350. ...event,
  351. durable: {
  352. aggregateID: committed.aggregateID,
  353. seq: committed.seq,
  354. version: definition.durable.version,
  355. },
  356. }
  357. yield* notify(event as Payload, true)
  358. return event
  359. }
  360. }
  361. yield* notify(event as Payload, false)
  362. return event
  363. })
  364. }
  365. const observe = (event: Payload, observer: (event: Payload) => Effect.Effect<void>) =>
  366. Effect.suspend(() => observer(event)).pipe(
  367. Effect.catchCauseIf(
  368. (cause) => !Cause.hasInterrupts(cause),
  369. (cause) => Effect.logError("Event listener failed", { eventID: event.id, eventType: event.type, cause }),
  370. ),
  371. )
  372. function notify(event: Payload, isolateListeners: boolean) {
  373. return Effect.gen(function* () {
  374. yield* Effect.forEach(
  375. listeners,
  376. (listener) => (isolateListeners ? observe(event, listener) : listener(event)),
  377. { discard: true },
  378. )
  379. const typed = pubsub.typed.get(event.type)
  380. if (typed) yield* PubSub.publish(typed, event)
  381. yield* PubSub.publish(pubsub.all, event)
  382. })
  383. }
  384. function publish<D extends Definition>(definition: D, data: Data<D>, options?: PublishOptions) {
  385. return Effect.gen(function* () {
  386. const serviceLocation = Option.getOrUndefined(yield* Effect.serviceOption(Location.Service))
  387. const location =
  388. options?.location ??
  389. (serviceLocation
  390. ? { directory: serviceLocation.directory, workspaceID: serviceLocation.workspaceID }
  391. : undefined)
  392. return yield* publishEvent(
  393. {
  394. id: options?.id ?? ID.create(),
  395. ...(options?.metadata ? { metadata: options.metadata } : {}),
  396. type: definition.type,
  397. ...(location ? { location } : {}),
  398. data,
  399. } as Payload<D>,
  400. options?.commit,
  401. )
  402. })
  403. }
  404. function replay(
  405. event: SerializedEvent,
  406. options?: { readonly publish?: boolean; readonly ownerID?: string; readonly strictOwner?: boolean },
  407. ) {
  408. return Effect.gen(function* () {
  409. const definition = durableRegistry.get(event.type)
  410. if (!definition?.durable) {
  411. yield* Effect.die(
  412. new InvalidDurableEventError({ type: event.type, message: `Unknown durable event type ${event.type}` }),
  413. )
  414. } else {
  415. const payload = {
  416. id: event.id,
  417. type: definition.type,
  418. data: Schema.decodeUnknownSync(definition.data as Schema.Codec<unknown, unknown, never, never>)(
  419. event.data,
  420. ),
  421. } as Payload
  422. const committed = yield* commitDurableEvent(payload, {
  423. seq: event.seq,
  424. aggregateID: event.aggregateID,
  425. ownerID: options?.ownerID,
  426. strictOwner: options?.strictOwner,
  427. })
  428. if (committed && options?.publish) {
  429. yield* notify(
  430. {
  431. ...payload,
  432. durable: {
  433. aggregateID: committed.aggregateID,
  434. seq: committed.seq,
  435. version: definition.durable.version,
  436. },
  437. },
  438. true,
  439. )
  440. }
  441. }
  442. })
  443. }
  444. function replayAll(
  445. events: SerializedEvent[],
  446. options?: { readonly publish?: boolean; readonly ownerID?: string; readonly strictOwner?: boolean },
  447. ) {
  448. return Effect.gen(function* () {
  449. const source = events[0]?.aggregateID
  450. if (!source) return undefined
  451. if (events.some((event) => event.aggregateID !== source)) {
  452. yield* Effect.die(
  453. new InvalidDurableEventError({
  454. type: events[0]?.type ?? "unknown",
  455. message: "Replay events must belong to the same aggregate",
  456. }),
  457. )
  458. }
  459. const start = events[0]?.seq ?? 0
  460. for (const [index, event] of events.entries()) {
  461. const seq = start + index
  462. if (event.seq !== seq) {
  463. yield* Effect.die(
  464. new InvalidDurableEventError({
  465. type: event.type,
  466. message: `Replay sequence mismatch at index ${index}: expected ${seq}, got ${event.seq}`,
  467. }),
  468. )
  469. }
  470. }
  471. for (const event of events) {
  472. yield* replay(event, options)
  473. }
  474. return source
  475. })
  476. }
  477. function remove(aggregateID: string) {
  478. return db
  479. .transaction(() =>
  480. Effect.gen(function* () {
  481. yield* db.delete(EventSequenceTable).where(eq(EventSequenceTable.aggregate_id, aggregateID)).run()
  482. yield* db.delete(EventTable).where(eq(EventTable.aggregate_id, aggregateID)).run()
  483. }),
  484. )
  485. .pipe(Effect.orDie)
  486. }
  487. function claim(aggregateID: string, ownerID: string) {
  488. return db
  489. .update(EventSequenceTable)
  490. .set({ owner_id: ownerID })
  491. .where(eq(EventSequenceTable.aggregate_id, aggregateID))
  492. .run()
  493. .pipe(Effect.orDie)
  494. }
  495. const subscribe = <D extends Definition>(definition: D): Stream.Stream<Payload<D>> =>
  496. Stream.unwrap(getOrCreate(definition).pipe(Effect.map((pubsub) => Stream.fromPubSub(pubsub)))).pipe(
  497. Stream.map((event) => event as Payload<D>),
  498. )
  499. const streamAll = (): Stream.Stream<Payload> => Stream.fromPubSub(pubsub.all)
  500. const decodeSerializedEvent = (event: SerializedEvent): Payload => {
  501. const definition = durableRegistry.get(event.type)
  502. if (!definition?.durable) {
  503. throw new InvalidDurableEventError({ type: event.type, message: `Unknown durable event type ${event.type}` })
  504. }
  505. return {
  506. id: event.id,
  507. type: definition.type,
  508. durable: { aggregateID: event.aggregateID, seq: event.seq, version: definition.durable.version },
  509. data: Schema.decodeUnknownSync(definition.data as Schema.Codec<unknown, unknown, never, never>)(event.data),
  510. }
  511. }
  512. const readAfter = (aggregateID: string, after: number) =>
  513. (options?.beforeAggregateRead?.(aggregateID) ?? Effect.void).pipe(
  514. Effect.andThen(
  515. db
  516. .select()
  517. .from(EventTable)
  518. .where(and(eq(EventTable.aggregate_id, aggregateID), gt(EventTable.seq, after)))
  519. .orderBy(asc(EventTable.seq))
  520. .all(),
  521. ),
  522. Effect.orDie,
  523. Effect.map((rows) =>
  524. rows.map((event) =>
  525. decodeSerializedEvent({
  526. id: event.id,
  527. aggregateID: event.aggregate_id,
  528. seq: event.seq,
  529. type: event.type,
  530. data: event.data,
  531. }),
  532. ),
  533. ),
  534. )
  535. const subscribeDurable = (aggregateID: string) =>
  536. Effect.gen(function* () {
  537. const wake = yield* PubSub.sliding<void>(1)
  538. const subscription = yield* PubSub.subscribe(wake)
  539. yield* Effect.acquireRelease(
  540. Effect.sync(() => {
  541. const wakes = pubsub.durable.get(aggregateID) ?? new Set()
  542. wakes.add(wake)
  543. pubsub.durable.set(aggregateID, wakes)
  544. }),
  545. () =>
  546. Effect.sync(() => {
  547. const wakes = pubsub.durable.get(aggregateID)
  548. wakes?.delete(wake)
  549. if (wakes?.size === 0) pubsub.durable.delete(aggregateID)
  550. }).pipe(Effect.andThen(PubSub.shutdown(wake))),
  551. )
  552. return subscription
  553. })
  554. const durable = (input: { readonly aggregateID: string; readonly after?: number }): Stream.Stream<Payload> =>
  555. Stream.unwrap(
  556. Effect.gen(function* () {
  557. const wakes = yield* subscribeDurable(input.aggregateID)
  558. let sequence = input.after ?? -1
  559. const read = Effect.suspend(() => readAfter(input.aggregateID, sequence)).pipe(
  560. Effect.tap((events) =>
  561. Effect.sync(() => {
  562. sequence = events.at(-1)?.durable?.seq ?? sequence
  563. }),
  564. ),
  565. )
  566. const historical = yield* read
  567. const live = Stream.fromSubscription(wakes).pipe(
  568. Stream.mapEffect(() => read),
  569. Stream.flattenIterable,
  570. )
  571. return Stream.concat(Stream.fromIterable(historical), live)
  572. }),
  573. )
  574. const listen = (listener: Subscriber): Effect.Effect<Unsubscribe> =>
  575. Effect.sync(() => {
  576. listeners.push(listener)
  577. return Effect.sync(() => {
  578. const index = listeners.indexOf(listener)
  579. if (index >= 0) listeners.splice(index, 1)
  580. })
  581. })
  582. const project = <D extends Definition>(definition: D, projector: Subscriber<D>): Effect.Effect<void> =>
  583. Effect.sync(() => {
  584. const list = projectors.get(definition.type) ?? []
  585. list.push((event) => projector(event as Payload<D>))
  586. projectors.set(definition.type, list)
  587. })
  588. return Service.of({
  589. publish,
  590. subscribe,
  591. all: streamAll,
  592. durable,
  593. listen,
  594. project,
  595. replay,
  596. replayAll,
  597. remove,
  598. claim,
  599. })
  600. }),
  601. )
  602. export const layer = layerWith()
  603. export const node = LayerNode.make(layer, [Database.node])
  604. export const defaultLayer = layer.pipe(Layer.provide(Database.defaultLayer))