@orion-js/mongodb 4.2.8 → 4.2.10

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.cjs CHANGED
@@ -54,6 +54,8 @@ module.exports = __toCommonJS(index_exports);
54
54
 
55
55
  // src/connect/connections.ts
56
56
  var import_node_events = require("events");
57
+ var import_helpers = require("@orion-js/helpers");
58
+ var import_logger = require("@orion-js/logger");
57
59
  var import_mongodb = require("mongodb");
58
60
 
59
61
  // src/connect/getDBName.ts
@@ -76,11 +78,6 @@ function getDBName(url) {
76
78
  return dbName;
77
79
  }
78
80
 
79
- // src/connect/connections.ts
80
- var import_node_process = require("process");
81
- var import_logger = require("@orion-js/logger");
82
- var import_helpers = require("@orion-js/helpers");
83
-
84
81
  // src/connect/getMongoURLFromEnv.ts
85
82
  var import_env = require("@orion-js/env");
86
83
  var getMongoURLFromEnv = (connectionName) => {
@@ -185,22 +182,20 @@ var OrionMongoDatabaseWrapper = class {
185
182
  await this.connectWithRetry(this.client);
186
183
  this.state = "connected";
187
184
  this.connectionEvent.emit("connected", this.client);
188
- (0, import_node_process.nextTick)(() => {
189
- this.connectionEvent.removeAllListeners();
190
- this.connectionEvent = null;
191
- });
185
+ this.connectionEvent.removeAllListeners();
192
186
  return this;
193
187
  }
194
188
  async connectWithRetry(client) {
195
- try {
196
- return await client.connect();
197
- } catch (error) {
198
- import_logger.logger.error("Error connecting to mongo. Will retry in 5s", {
199
- error,
200
- connectionName: this.connectionName
201
- });
202
- await (0, import_helpers.sleep)(5e3);
203
- return this.connectWithRetry(client);
189
+ while (true) {
190
+ try {
191
+ return await client.connect();
192
+ } catch (error) {
193
+ import_logger.logger.error("Error connecting to mongo. Will retry in 5s", {
194
+ error,
195
+ connectionName: this.connectionName
196
+ });
197
+ await (0, import_helpers.sleep)(5e3);
198
+ }
204
199
  }
205
200
  }
206
201
  async startConnection() {
@@ -488,19 +483,6 @@ function omit(propsToOmit, obj) {
488
483
  return willReturn;
489
484
  }
490
485
 
491
- // ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/flatten.js
492
- function flatten(list, input) {
493
- const willReturn = input === void 0 ? [] : input;
494
- for (let i = 0; i < list.length; i++) {
495
- if (isArray(list[i])) {
496
- flatten(list[i], willReturn);
497
- } else {
498
- willReturn.push(list[i]);
499
- }
500
- }
501
- return willReturn;
502
- }
503
-
504
486
  // ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/isEmpty.js
505
487
  function isEmpty(input) {
506
488
  const inputType = type(input);
@@ -1075,14 +1057,24 @@ function loadOne_default(collection) {
1075
1057
  // src/createCollection/getMethods/dataLoader/loadData.ts
1076
1058
  var import_helpers5 = require("@orion-js/helpers");
1077
1059
 
1060
+ // src/createCollection/getMethods/dataLoader/dataLoad/index.ts
1061
+ var import_helpers4 = require("@orion-js/helpers");
1062
+
1078
1063
  // src/createCollection/getMethods/dataLoader/dataLoad/getDataLoader.ts
1079
1064
  var import_dataloader = __toESM(require("dataloader"), 1);
1080
1065
  var cache = /* @__PURE__ */ new Map();
1066
+ var staleTimers = /* @__PURE__ */ new Map();
1081
1067
  var getDataLoader = (params) => {
1068
+ var _a;
1082
1069
  const { key, func, timeout } = params;
1083
1070
  const existing = cache.get(key);
1084
1071
  if (existing) return existing;
1085
1072
  const load = async (ids) => {
1073
+ const staleTimer2 = staleTimers.get(key);
1074
+ if (staleTimer2) {
1075
+ clearTimeout(staleTimer2);
1076
+ staleTimers.delete(key);
1077
+ }
1086
1078
  cache.delete(key);
1087
1079
  return await func(ids);
1088
1080
  };
@@ -1090,13 +1082,27 @@ var getDataLoader = (params) => {
1090
1082
  batchScheduleFn: (callback) => setTimeout(callback, timeout)
1091
1083
  };
1092
1084
  const dataLoader = new import_dataloader.default(load, options);
1085
+ const staleTimer = setTimeout(
1086
+ () => {
1087
+ cache.delete(key);
1088
+ staleTimers.delete(key);
1089
+ },
1090
+ Math.max(100, timeout * 20)
1091
+ );
1092
+ (_a = staleTimer.unref) == null ? void 0 : _a.call(staleTimer);
1093
+ staleTimers.set(key, staleTimer);
1093
1094
  cache.set(key, dataLoader);
1094
1095
  return dataLoader;
1095
1096
  };
1096
1097
 
1097
1098
  // src/createCollection/getMethods/dataLoader/dataLoad/index.ts
1098
- var import_helpers4 = require("@orion-js/helpers");
1099
1099
  var dataLoad = async (options) => {
1100
+ if (options.ids && options.ids.length === 0) {
1101
+ return [];
1102
+ }
1103
+ if (!options.ids && typeof options.id === "undefined") {
1104
+ return [];
1105
+ }
1100
1106
  const dataLoader = getDataLoader({
1101
1107
  key: (0, import_helpers4.hashObject)(options.loaderKey),
1102
1108
  func: options.load,
@@ -1104,7 +1110,7 @@ var dataLoad = async (options) => {
1104
1110
  });
1105
1111
  if (options.ids) {
1106
1112
  const resultArray = await dataLoader.loadMany(options.ids);
1107
- return flatten(resultArray);
1113
+ return resultArray.flat();
1108
1114
  }
1109
1115
  return await dataLoader.load(options.id);
1110
1116
  };
@@ -1127,7 +1133,7 @@ function loadData_default(collection) {
1127
1133
  timeout: options.timeout,
1128
1134
  load: async (values) => {
1129
1135
  const query = {
1130
- ...(0, import_helpers5.clone)(options.match),
1136
+ ...options.match || {},
1131
1137
  [options.key]: { $in: values }
1132
1138
  };
1133
1139
  const cursor = collection.find(query, { readPreference: "secondaryPreferred" });