@orion-js/mongodb 4.3.0 → 4.4.0

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.
Files changed (59) hide show
  1. package/LICENSE +21 -0
  2. package/dist/connect/connections.d.ts +56 -0
  3. package/dist/connect/getDBName.d.ts +1 -0
  4. package/dist/connect/getMongoConnection.d.ts +8 -0
  5. package/dist/connect/getMongoURLFromEnv.d.ts +2 -0
  6. package/dist/connect/index.d.ts +4 -0
  7. package/dist/createCollection/collectionsRegistry.d.ts +58 -0
  8. package/dist/createCollection/createIndexes.d.ts +12 -0
  9. package/dist/createCollection/deleteUnusedIndexes.d.ts +45 -0
  10. package/dist/createCollection/generateId.d.ts +3 -0
  11. package/dist/createCollection/getIndexOptions.d.ts +30 -0
  12. package/dist/createCollection/getMethods/cleanModifier.d.ts +5 -0
  13. package/dist/createCollection/getMethods/countDocuments.d.ts +2 -0
  14. package/dist/createCollection/getMethods/dataLoader/dataLoad/getDataLoader.d.ts +9 -0
  15. package/dist/createCollection/getMethods/dataLoader/dataLoad/index.d.ts +9 -0
  16. package/dist/createCollection/getMethods/dataLoader/index.d.ts +5 -0
  17. package/dist/createCollection/getMethods/dataLoader/loadById.d.ts +2 -0
  18. package/dist/createCollection/getMethods/dataLoader/loadData.d.ts +2 -0
  19. package/dist/createCollection/getMethods/dataLoader/loadMany.d.ts +2 -0
  20. package/dist/createCollection/getMethods/dataLoader/loadOne.d.ts +2 -0
  21. package/dist/createCollection/getMethods/deleteMany.d.ts +2 -0
  22. package/dist/createCollection/getMethods/deleteOne.d.ts +2 -0
  23. package/dist/createCollection/getMethods/estimatedDocumentCount.d.ts +3 -0
  24. package/dist/createCollection/getMethods/find.d.ts +2 -0
  25. package/dist/createCollection/getMethods/findOne.d.ts +2 -0
  26. package/dist/createCollection/getMethods/findOneAndUpdate.d.ts +3 -0
  27. package/dist/createCollection/getMethods/getSelector.d.ts +3 -0
  28. package/dist/createCollection/getMethods/index.d.ts +16 -0
  29. package/dist/createCollection/getMethods/insertAndFind.d.ts +3 -0
  30. package/dist/createCollection/getMethods/insertMany.d.ts +3 -0
  31. package/dist/createCollection/getMethods/insertOne.d.ts +3 -0
  32. package/dist/createCollection/getMethods/updateAndFind.d.ts +3 -0
  33. package/dist/createCollection/getMethods/updateItem.d.ts +2 -0
  34. package/dist/createCollection/getMethods/updateMany.d.ts +3 -0
  35. package/dist/createCollection/getMethods/updateOne.d.ts +3 -0
  36. package/dist/createCollection/getMethods/upsert.d.ts +3 -0
  37. package/dist/createCollection/getMethods/validateModifier/index.d.ts +1 -0
  38. package/dist/createCollection/getMethods/validateModifier/validateInc.d.ts +7 -0
  39. package/dist/createCollection/getMethods/validateModifier/validateOperator.d.ts +5 -0
  40. package/dist/createCollection/getMethods/validateModifier/validatePush.d.ts +8 -0
  41. package/dist/createCollection/getMethods/validateModifier/validateSet.d.ts +7 -0
  42. package/dist/createCollection/getMethods/validateModifier/validateUnset.d.ts +7 -0
  43. package/dist/createCollection/getMethods/validateModifier/validateUpsert.d.ts +1 -0
  44. package/dist/createCollection/getMethods/wrapErrors.d.ts +1 -0
  45. package/dist/createCollection/getSchemaAndModel.d.ts +4 -0
  46. package/dist/createCollection/index.d.ts +10 -0
  47. package/dist/createCollection/wrapMethods.d.ts +2 -0
  48. package/dist/encrypted/getOrCreateEncryptionKey.d.ts +17 -0
  49. package/dist/helpers/fromDot.d.ts +1 -0
  50. package/dist/helpers/toDot.d.ts +1 -0
  51. package/dist/index.cjs +102 -95
  52. package/dist/index.cjs.map +1 -1
  53. package/dist/index.d.ts +6 -391
  54. package/dist/index.js +92 -85
  55. package/dist/index.js.map +1 -1
  56. package/dist/service/index.d.ts +3 -0
  57. package/dist/types/index.d.ts +233 -0
  58. package/package.json +18 -19
  59. package/dist/index.d.cts +0 -391
package/dist/index.cjs CHANGED
@@ -541,7 +541,7 @@ function findOne_default(collection) {
541
541
  return findOne;
542
542
  }
543
543
 
544
- // src/createCollection/getMethods/validateModifier/validatePush.ts
544
+ // src/createCollection/getMethods/cleanModifier.ts
545
545
  var import_schema2 = require("@orion-js/schema");
546
546
 
547
547
  // src/helpers/fromDot.ts
@@ -560,7 +560,70 @@ function fromDot(doc) {
560
560
  return import_dot_object2.default.object(doc);
561
561
  }
562
562
 
563
+ // src/createCollection/getMethods/cleanModifier.ts
564
+ var shouldCheck = (key) => {
565
+ if (key === "$pushAll") throw new Error("$pushAll is not supported; use $push + $each");
566
+ return ["$pull", "$pullAll", "$pop", "$slice"].indexOf(key) === -1;
567
+ };
568
+ async function cleanModifier(schema, modifier, { isUpsert } = { isUpsert: false }) {
569
+ const cleanedModifier = {};
570
+ for (const operation of Object.keys(modifier)) {
571
+ const operationDoc = modifier[operation];
572
+ cleanedModifier[operation] = {};
573
+ if (operation.slice(0, 1) !== "$") {
574
+ throw new Error(`Expected '${operation}' to be a modifier operator like '$set'`);
575
+ }
576
+ if (!shouldCheck(operation)) {
577
+ cleanedModifier[operation] = operationDoc;
578
+ continue;
579
+ }
580
+ for (const key of Object.keys(operationDoc)) {
581
+ const value = operationDoc[key];
582
+ const cleanOptions = { forceDoc: operationDoc };
583
+ let cleaned = null;
584
+ if (operation === "$push" || operation === "$addToSet") {
585
+ if (typeof value === "object" && "$each" in value) {
586
+ const $each = await (0, import_schema2.cleanKey)(schema, key, value.$each, cleanOptions);
587
+ cleaned = { ...value, $each };
588
+ } else {
589
+ cleaned = await (0, import_schema2.cleanKey)(schema, `${key}.0`, value, cleanOptions);
590
+ }
591
+ }
592
+ if (operation === "$set") {
593
+ cleaned = await (0, import_schema2.cleanKey)(schema, key, value, cleanOptions);
594
+ }
595
+ if (operation === "$setOnInsert") {
596
+ cleaned = await (0, import_schema2.cleanKey)(schema, key, value, cleanOptions);
597
+ }
598
+ if (operation === "$inc") {
599
+ cleaned = await (0, import_schema2.cleanKey)(schema, key, value, cleanOptions);
600
+ }
601
+ if (operation === "$unset") {
602
+ const isPresent = await (0, import_schema2.cleanKey)(schema, key, "anyvalue", cleanOptions);
603
+ cleaned = !isNil(isPresent) ? "" : null;
604
+ }
605
+ if (cleaned !== void 0) {
606
+ cleanedModifier[operation][key] = cleaned;
607
+ }
608
+ }
609
+ if (isEmpty(cleanedModifier[operation])) {
610
+ delete cleanedModifier[operation];
611
+ }
612
+ }
613
+ if (isUpsert) {
614
+ const cleanedSetOnInsert = await (0, import_schema2.clean)(schema, fromDot(cleanedModifier.$setOnInsert || {}));
615
+ if (!isEmpty(cleanedSetOnInsert)) {
616
+ cleanedModifier.$setOnInsert = cleanedSetOnInsert;
617
+ }
618
+ }
619
+ if (equals(cleanedModifier, {})) {
620
+ throw new Error("After cleaning your modifier is empty");
621
+ }
622
+ return cleanedModifier;
623
+ }
624
+
563
625
  // src/createCollection/getMethods/validateModifier/validatePush.ts
626
+ var import_schema3 = require("@orion-js/schema");
564
627
  async function validatePush_default({ schema, operationDoc, operation }) {
565
628
  for (const key of Object.keys(operationDoc)) {
566
629
  const value = operationDoc[key];
@@ -573,43 +636,43 @@ async function validatePush_default({ schema, operationDoc, operation }) {
573
636
  }
574
637
  }
575
638
  const validationObject = fromDot({ [key]: toValidate });
576
- await (0, import_schema2.validate)(schema, validationObject, { omitRequired: true });
639
+ await (0, import_schema3.validate)(schema, validationObject, { omitRequired: true });
577
640
  }
578
641
  }
579
642
 
580
643
  // src/createCollection/getMethods/validateModifier/validateUnset.ts
581
- var import_schema3 = require("@orion-js/schema");
644
+ var import_schema4 = require("@orion-js/schema");
582
645
  async function validateUnset_default({ schema, operationDoc }) {
583
646
  const errors = {};
584
647
  for (const key of Object.keys(operationDoc)) {
585
- const error = await (0, import_schema3.validateKey)(schema, key, null);
648
+ const error = await (0, import_schema4.validateKey)(schema, key, null);
586
649
  if (error) {
587
650
  errors[key] = error;
588
651
  }
589
652
  }
590
653
  if (Object.keys(errors).length) {
591
- throw new import_schema3.ValidationError(errors);
654
+ throw new import_schema4.ValidationError(errors);
592
655
  }
593
656
  }
594
657
 
595
658
  // src/createCollection/getMethods/validateModifier/validateInc.ts
596
- var import_schema4 = require("@orion-js/schema");
659
+ var import_schema5 = require("@orion-js/schema");
597
660
  async function validateInc_default({ schema, operationDoc }) {
598
661
  const errors = {};
599
662
  for (const key of Object.keys(operationDoc)) {
600
663
  const value = operationDoc[key];
601
- const error = await (0, import_schema4.validateKey)(schema, key, value);
664
+ const error = await (0, import_schema5.validateKey)(schema, key, value);
602
665
  if (error) {
603
666
  errors[key] = error;
604
667
  }
605
668
  }
606
669
  if (Object.keys(errors).length) {
607
- throw new import_schema4.ValidationError(errors);
670
+ throw new import_schema5.ValidationError(errors);
608
671
  }
609
672
  }
610
673
 
611
674
  // src/createCollection/getMethods/validateModifier/validateSet.ts
612
- var import_schema5 = require("@orion-js/schema");
675
+ var import_schema6 = require("@orion-js/schema");
613
676
  async function validateSet_default({ schema, operationDoc }) {
614
677
  let cleaned = toDot_default(operationDoc);
615
678
  const transformedObj = {};
@@ -618,16 +681,16 @@ async function validateSet_default({ schema, operationDoc }) {
618
681
  transformedObj[newKey] = cleaned[key];
619
682
  });
620
683
  cleaned = fromDot(transformedObj);
621
- await (0, import_schema5.validate)(schema, cleaned, { omitRequired: true });
684
+ await (0, import_schema6.validate)(schema, cleaned, { omitRequired: true });
622
685
  }
623
686
 
624
687
  // src/createCollection/getMethods/validateModifier/validateOperator.ts
625
- var shouldCheck = function(key) {
688
+ var shouldCheck2 = function(key) {
626
689
  if (key === "$pushAll") throw new Error("$pushAll is not supported, use $push + $each");
627
690
  return ["$pull", "$pullAll", "$pop", "$slice"].indexOf(key) === -1;
628
691
  };
629
692
  async function validateOperator_default({ schema, operationDoc, operation }) {
630
- if (!shouldCheck(operation)) return;
693
+ if (!shouldCheck2(operation)) return;
631
694
  if (operation === "$set") {
632
695
  await validateSet_default({ schema, operationDoc });
633
696
  } else if (operation === "$unset") {
@@ -653,7 +716,7 @@ async function validateModifier(schema, modifier) {
653
716
  }
654
717
 
655
718
  // src/createCollection/getMethods/validateModifier/validateUpsert.ts
656
- var import_schema6 = require("@orion-js/schema");
719
+ var import_schema7 = require("@orion-js/schema");
657
720
  var getPushSimulation = ($push) => {
658
721
  if (!$push) return {};
659
722
  const simulation = {};
@@ -677,76 +740,13 @@ var simulateNewDoc = (selector, modifier) => fromDot({
677
740
  });
678
741
  var validateNewDoc = async (schema, selector, modifier) => {
679
742
  const doc = simulateNewDoc(selector, modifier);
680
- await (0, import_schema6.validate)(schema, doc);
743
+ await (0, import_schema7.validate)(schema, doc);
681
744
  };
682
745
  async function validateUpsert_default(schema, selector, modifier) {
683
746
  await validateNewDoc(schema, selector, modifier);
684
747
  await validateModifier(schema, omit(["$setOnInsert"], modifier));
685
748
  }
686
749
 
687
- // src/createCollection/getMethods/cleanModifier.ts
688
- var import_schema7 = require("@orion-js/schema");
689
- var shouldCheck2 = (key) => {
690
- if (key === "$pushAll") throw new Error("$pushAll is not supported; use $push + $each");
691
- return ["$pull", "$pullAll", "$pop", "$slice"].indexOf(key) === -1;
692
- };
693
- async function cleanModifier(schema, modifier, { isUpsert } = { isUpsert: false }) {
694
- const cleanedModifier = {};
695
- for (const operation of Object.keys(modifier)) {
696
- const operationDoc = modifier[operation];
697
- cleanedModifier[operation] = {};
698
- if (operation.slice(0, 1) !== "$") {
699
- throw new Error(`Expected '${operation}' to be a modifier operator like '$set'`);
700
- }
701
- if (!shouldCheck2(operation)) {
702
- cleanedModifier[operation] = operationDoc;
703
- continue;
704
- }
705
- for (const key of Object.keys(operationDoc)) {
706
- const value = operationDoc[key];
707
- const cleanOptions = { forceDoc: operationDoc };
708
- let cleaned = null;
709
- if (operation === "$push" || operation === "$addToSet") {
710
- if (typeof value === "object" && "$each" in value) {
711
- const $each = await (0, import_schema7.cleanKey)(schema, key, value.$each, cleanOptions);
712
- cleaned = { ...value, $each };
713
- } else {
714
- cleaned = await (0, import_schema7.cleanKey)(schema, `${key}.0`, value, cleanOptions);
715
- }
716
- }
717
- if (operation === "$set") {
718
- cleaned = await (0, import_schema7.cleanKey)(schema, key, value, cleanOptions);
719
- }
720
- if (operation === "$setOnInsert") {
721
- cleaned = await (0, import_schema7.cleanKey)(schema, key, value, cleanOptions);
722
- }
723
- if (operation === "$inc") {
724
- cleaned = await (0, import_schema7.cleanKey)(schema, key, value, cleanOptions);
725
- }
726
- if (operation === "$unset") {
727
- const isPresent = await (0, import_schema7.cleanKey)(schema, key, "anyvalue", cleanOptions);
728
- cleaned = !isNil(isPresent) ? "" : null;
729
- }
730
- if (cleaned !== void 0) {
731
- cleanedModifier[operation][key] = cleaned;
732
- }
733
- }
734
- if (isEmpty(cleanedModifier[operation])) {
735
- delete cleanedModifier[operation];
736
- }
737
- }
738
- if (isUpsert) {
739
- const cleanedSetOnInsert = await (0, import_schema7.clean)(schema, fromDot(cleanedModifier.$setOnInsert || {}));
740
- if (!isEmpty(cleanedSetOnInsert)) {
741
- cleanedModifier.$setOnInsert = cleanedSetOnInsert;
742
- }
743
- }
744
- if (equals(cleanedModifier, {})) {
745
- throw new Error("After cleaning your modifier is empty");
746
- }
747
- return cleanedModifier;
748
- }
749
-
750
750
  // src/createCollection/getMethods/wrapErrors.ts
751
751
  var import_schema8 = require("@orion-js/schema");
752
752
  var wrapErrors = async (operation) => {
@@ -775,10 +775,10 @@ var wrapErrors = async (operation) => {
775
775
 
776
776
  // src/createCollection/getMethods/upsert.ts
777
777
  var upsert_default = (collection) => {
778
- const upsert = async function(selectorArg, modifierArg, options = {}) {
778
+ const upsert = async (selectorArg, modifierArg, options = {}) => {
779
779
  await collection.connectionPromise;
780
780
  let modifier = modifierArg;
781
- let selector = getSelector(arguments);
781
+ let selector = getSelector([selectorArg]);
782
782
  modifier.$setOnInsert = { ...modifier.$setOnInsert, _id: collection.generateId() };
783
783
  if (collection.schema) {
784
784
  const schema = collection.getSchema();
@@ -869,16 +869,17 @@ var updateMany_default = (collection) => {
869
869
 
870
870
  // src/createCollection/getMethods/updateAndFind.ts
871
871
  var updateAndFind_default = (collection) => {
872
- const updateAndFind = async function(selector, modifier, options = {}) {
872
+ const updateAndFind = async (selector, modifier, options = {}) => {
873
873
  await collection.connectionPromise;
874
874
  return await wrapErrors(async () => {
875
- return await collection.findOneAndUpdate(selector, modifier, {
875
+ const result = await collection.findOneAndUpdate(selector, modifier, {
876
876
  ...options,
877
877
  mongoOptions: {
878
878
  ...options.mongoOptions,
879
879
  returnDocument: "after"
880
880
  }
881
881
  });
882
+ return result;
882
883
  });
883
884
  };
884
885
  return updateAndFind;
@@ -963,10 +964,14 @@ var insertMany_default = (collection) => {
963
964
 
964
965
  // src/createCollection/getMethods/updateItem.ts
965
966
  function updateItem_default(collection) {
966
- const updateItem = async function(item, modifier, options = {}) {
967
+ const updateItem = async (item, modifier, options = {}) => {
967
968
  await collection.connectionPromise;
968
969
  const updated = await wrapErrors(async () => {
969
- return await collection.updateAndFind(item._id, modifier, options);
970
+ return await collection.updateAndFind(
971
+ item._id,
972
+ modifier,
973
+ options
974
+ );
970
975
  });
971
976
  for (const key in item) {
972
977
  delete item[key];
@@ -1162,10 +1167,11 @@ function loadData_default(collection) {
1162
1167
  var import_helpers6 = require("@orion-js/helpers");
1163
1168
  var import_bson = require("bson");
1164
1169
  var getIdGenerator = (options) => {
1165
- var _a, _b;
1166
- if (!options.idPrefix && ((_a = options == null ? void 0 : options.schema) == null ? void 0 : _a._id)) {
1167
- const idField = options.schema._id.type;
1168
- if ((_b = idField.name) == null ? void 0 : _b.startsWith("typedId:")) {
1170
+ var _a;
1171
+ const schema = options.schema;
1172
+ if (!options.idPrefix && (schema == null ? void 0 : schema._id)) {
1173
+ const idField = schema._id.type;
1174
+ if ((_a = idField.name) == null ? void 0 : _a.startsWith("typedId:")) {
1169
1175
  return () => {
1170
1176
  return idField.generateId();
1171
1177
  };
@@ -1436,20 +1442,21 @@ function prepareShema(schema) {
1436
1442
  function getSchema(options) {
1437
1443
  var _a;
1438
1444
  if (!options.schema) return;
1439
- if ((_a = options.schema[Symbol.metadata]) == null ? void 0 : _a._getModel) {
1440
- return options.schema[Symbol.metadata]._getModel().getSchema();
1445
+ const schemaOption = options.schema;
1446
+ if ((_a = schemaOption[Symbol.metadata]) == null ? void 0 : _a._getModel) {
1447
+ return schemaOption[Symbol.metadata]._getModel().getSchema();
1441
1448
  }
1442
- if (options.schema.getSchema) {
1443
- const schema = options.schema.getSchema();
1449
+ if (schemaOption.getSchema) {
1450
+ const schema = schemaOption.getSchema();
1444
1451
  return prepareShema(schema);
1445
1452
  }
1446
- if (options.schema.getModel) {
1447
- const model = options.schema.getModel();
1453
+ if (schemaOption.getModel) {
1454
+ const model = schemaOption.getModel();
1448
1455
  const schema = model ? (0, import_helpers7.clone)(model.getSchema()) : {};
1449
1456
  return prepareShema(schema);
1450
1457
  }
1451
- if (type(options.schema) === "Object") {
1452
- return prepareShema(options.schema);
1458
+ if (type(schemaOption) === "Object") {
1459
+ return prepareShema(schemaOption);
1453
1460
  }
1454
1461
  }
1455
1462