@orion-js/dogs 4.6.5 → 4.6.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -177,7 +177,8 @@ __runInitializers(_init, 1, JobsHistoryRepo);
177
177
  // src/repos/JobsRepo.ts
178
178
  import { generateId } from "@orion-js/helpers";
179
179
  import { logger } from "@orion-js/logger";
180
- import { MongoCollection as MongoCollection2, Repository as Repository2 } from "@orion-js/mongodb";
180
+ import { MongoCollection as MongoCollection2, MongoDB as MongoDB2, Repository as Repository2 } from "@orion-js/mongodb";
181
+ import { clean, validate } from "@orion-js/schema";
181
182
 
182
183
  // src/services/partitions.ts
183
184
  function validatePartitionsCount(nPartitions) {
@@ -617,13 +618,13 @@ var JobsRepo = class {
617
618
  if (jobs.length === 0) {
618
619
  return { scheduledCount: 0, skippedCount: 0, errors: [] };
619
620
  }
620
- let scheduledCount = 0;
621
- let skippedCount = 0;
622
621
  const errors = [];
622
+ const preparedJobs = [];
623
623
  for (let i = 0; i < jobs.length; i++) {
624
624
  const job = jobs[i];
625
625
  try {
626
- await this.jobs.insertOne({
626
+ const record = await clean(JobRecordSchema, {
627
+ _id: this.jobs.generateId(),
627
628
  jobName: job.name,
628
629
  uniqueIdentifier: job.uniqueIdentifier,
629
630
  params: job.params,
@@ -632,16 +633,60 @@ var JobsRepo = class {
632
633
  type: "event",
633
634
  ...job.partition === void 0 ? {} : { partition: job.partition }
634
635
  });
635
- scheduledCount++;
636
+ await validate(JobRecordSchema, record);
637
+ preparedJobs.push({ index: i, job, record });
636
638
  } catch (error) {
637
- if (error.isValidationError && Object.values(error.validationErrors).includes("notUnique") && job.uniqueIdentifier) {
638
- logger.info(`Job "${job.name}" with identifier "${job.uniqueIdentifier}" already exists`);
639
- skippedCount++;
640
- } else {
639
+ errors.push({
640
+ index: i,
641
+ error: error instanceof Error ? error : new Error(String(error)),
642
+ job
643
+ });
644
+ }
645
+ }
646
+ if (preparedJobs.length === 0) {
647
+ return { scheduledCount: 0, skippedCount: 0, errors };
648
+ }
649
+ const rawCollection = await this.jobs.getRawCollection();
650
+ await this.jobs.createIndexesPromise;
651
+ const operations = preparedJobs.map(
652
+ ({ record }) => record.uniqueIdentifier === void 0 ? { insertOne: { document: record } } : {
653
+ updateOne: {
654
+ filter: { uniqueIdentifier: record.uniqueIdentifier },
655
+ update: { $setOnInsert: record },
656
+ upsert: true
657
+ }
658
+ }
659
+ );
660
+ let scheduledCount = 0;
661
+ let skippedCount = 0;
662
+ try {
663
+ const result = await rawCollection.bulkWrite(operations, { ordered: false });
664
+ scheduledCount = result.insertedCount + result.upsertedCount;
665
+ skippedCount = result.matchedCount;
666
+ } catch (error) {
667
+ const writeErrors = error instanceof MongoDB2.MongoBulkWriteError ? Array.isArray(error.writeErrors) ? error.writeErrors : [error.writeErrors] : [];
668
+ if (!(error instanceof MongoDB2.MongoBulkWriteError) || writeErrors.length === 0) {
669
+ for (const preparedJob of preparedJobs) {
641
670
  errors.push({
642
- index: i,
671
+ index: preparedJob.index,
643
672
  error: error instanceof Error ? error : new Error(String(error)),
644
- job
673
+ job: preparedJob.job
674
+ });
675
+ }
676
+ } else {
677
+ scheduledCount = error.result.insertedCount + error.result.upsertedCount;
678
+ skippedCount = error.result.matchedCount;
679
+ for (const writeError of writeErrors) {
680
+ const preparedJob = preparedJobs[writeError.index];
681
+ if (!preparedJob) continue;
682
+ if (writeError.code === 11e3 && preparedJob.record.uniqueIdentifier !== void 0) {
683
+ skippedCount++;
684
+ continue;
685
+ }
686
+ errors.push({
687
+ index: preparedJob.index,
688
+ error: new Error(writeError.errmsg),
689
+ job: preparedJob.job
645
690
  });
646
691
  }
647
692
  }