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

Function bulkSetInternal

packages/infra/src/Store/Cosmos.ts:193–299  ·  view source on GitHub ↗
(items: NonEmptyReadonlyArray<PM>, ns: string)

Source from the content-addressed store, hash-verified

191 yield* cached
192 })
193 const bulkSetInternal = (items: NonEmptyReadonlyArray<PM>, ns: string) =>
194 Effect
195 .gen(function*() {
196 // TODO: disable batching if need atomicity
197 // we delay and batch to keep low amount of RUs
198 const b = [...items]
199 .map(
200 (x) =>
201 [
202 x,
203 Option.match(Option.fromNullishOr(x._etag), {
204 onNone: () =>
205 dropUndefinedT({
206 operationType: "Create" as const,
207 resourceBody: {
208 ...Struct.omit(x, ["_etag", idKey]),
209 id: x[idKey],
210 _partitionKey: nsPartitionValue(ns, x)
211 }
212 // don't use this or we get an error that the request and some item partition key dont match - makese no sense
213 // partitionKey: config?.partitionValue(x)
214 }),
215 onSome: (eTag) =>
216 dropUndefinedT({
217 operationType: "Replace" as const,
218 id: x[idKey],
219 resourceBody: {
220 ...Struct.omit(x, ["_etag", idKey]),
221 id: x[idKey],
222 _partitionKey: nsPartitionValue(ns, x)
223 },
224 ifMatch: eTag
225 // don't use this or we get an error that the request and some item partition key dont match - makese no sense
226 // partitionKey: config?.partitionValue(x)
227 })
228 })
229 ] as const
230 )
231 const batches = Array.chunksOf(b, config?.maxBulkSize ?? 10)
232
233 const batchResult = yield* Effect.forEach(
234 batches
235 .map((x, i) => [i, x] as const),
236 ([i, batch]) =>
237 tryCosmos(() => bulk(batch.map(([, op]) => op)))
238 .pipe(
239 Effect
240 .delay(Duration.millis(i === 0 ? 0 : 150)),
241 Effect
242 .flatMap((responses) =>
243 Effect.gen(function*() {
244 const r = responses.find((x) =>
245 x.statusCode === 412 || x.statusCode === 404 || x.statusCode === 409
246 )
247 if (r) {
248 return yield* Effect.fail(
249 new OptimisticConcurrencyException(
250 {

Callers 2

makeSeedEffectFunction · 0.70
bulkSetFunction · 0.70

Calls 10

dropUndefinedTFunction · 0.90
annotateDbFunction · 0.90
nsPartitionValueFunction · 0.85
tryCosmosFunction · 0.85
statusIsTransientFunction · 0.85
pipeMethod · 0.65
mapMethod · 0.65
omitMethod · 0.65
forEachMethod · 0.65
failMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…