(items: NonEmptyReadonlyArray<PM>)
| 302 | resolveNamespace.pipe(Effect.flatMap((ns) => bulkSetInternal(items, ns))) |
| 303 | |
| 304 | const batchSet = (items: NonEmptyReadonlyArray<PM>) => { |
| 305 | return resolveNamespace |
| 306 | .pipe(Effect.flatMap((ns) => |
| 307 | Effect |
| 308 | .suspend(() => { |
| 309 | const batch = [...items].map( |
| 310 | (x) => |
| 311 | [ |
| 312 | x, |
| 313 | Option.match(Option.fromNullishOr(x._etag), { |
| 314 | onNone: () => ({ |
| 315 | operationType: "Create" as const, |
| 316 | resourceBody: { |
| 317 | ...Struct.omit(x, ["_etag", idKey]), |
| 318 | id: x[idKey], |
| 319 | _partitionKey: nsPartitionValue(ns, x) |
| 320 | } |
| 321 | // don't use this or we get an error that the request and some item partition key dont match - makese no sense |
| 322 | // partitionKey: config?.partitionValue(x) |
| 323 | }), |
| 324 | onSome: (eTag) => ({ |
| 325 | operationType: "Replace" as const, |
| 326 | id: x[idKey], |
| 327 | resourceBody: { |
| 328 | ...Struct.omit(x, ["_etag", idKey]), |
| 329 | id: x[idKey], |
| 330 | _partitionKey: nsPartitionValue(ns, x) |
| 331 | }, |
| 332 | // don't use this or we get an error that the request and some item partition key dont match - makese no sense |
| 333 | // partitionKey: config?.partitionValue(x) |
| 334 | ifMatch: eTag |
| 335 | }) |
| 336 | }) |
| 337 | ] as const |
| 338 | ) |
| 339 | |
| 340 | const ex = batch.map(([, c]) => c) |
| 341 | |
| 342 | return tryCosmos(() => execBatch(ex, ex[0]?.resourceBody._partitionKey)) |
| 343 | .pipe(Effect.flatMap(Effect.fnUntraced(function*(x) { |
| 344 | const result = x.result ?? [] |
| 345 | const firstFailed = result.find( |
| 346 | (x: any) => x.statusCode > 299 || x.statusCode < 200 |
| 347 | ) |
| 348 | if (firstFailed) { |
| 349 | const code = firstFailed.statusCode ?? 0 |
| 350 | if (code === 412 || code === 404 || code === 409) { |
| 351 | return yield* new OptimisticConcurrencyException({ type: name, id: "batch", code }) |
| 352 | } |
| 353 | |
| 354 | return yield* new DatabaseError({ |
| 355 | message: "not able to update record: " + code, |
| 356 | transient: statusIsTransient(code), |
| 357 | cause: x |
| 358 | }) |
| 359 | } |
| 360 | |
| 361 | return batch.map(([e], i) => ({ |
nothing calls this directly
no test coverage detected
searching dependent graphs…