MCPcopy Create free account
hub / github.com/effect-app/libs / createIfMissing

Function createIfMissing

packages/infra/src/WorkflowEngineCosmos.ts:264–281  ·  view source on GitHub ↗
(
    body: T
  )

Source from the content-addressed store, hash-verified

262
263 // Atomic create-or-noop using a single-op batch — returns true if created.
264 const createIfMissing = <T extends { readonly id: string; readonly _partitionKey: string }>(
265 body: T
266 ): Effect.Effect<boolean> =>
267 Effect.gen(function*() {
268 const resp = yield* Effect.promise(() =>
269 container.items.batch(
270 [{ operationType: "Create" as const, resourceBody: body }],
271 body._partitionKey
272 )
273 )
274 const r = resp.result?.[0]
275 const code = r?.statusCode ?? 0
276 if (code === 201) return true
277 if (code === 409) return false
278 return yield* Effect.die(
279 new Error(`workflow-engine cosmos createIfMissing for ${body.id} failed: ${code}`)
280 )
281 })
282
283 // Last-writer-wins upsert — used to overwrite a persisted *suspended* activity
284 // result once it finally completes (create-only ops can't transition it).

Callers 2

fireClockFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…