@orion-js/mongodb 4.1.7 → 4.1.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
@@ -250,6 +250,7 @@ var getMongoConnection = ({ name, uri }) => {
250
250
  // src/types/index.ts
251
251
  var MongoDB = __toESM(require("mongodb"), 1);
252
252
  var import_schema = require("@orion-js/schema");
253
+ var import_helpers2 = require("@orion-js/helpers");
253
254
  var BaseCollection = class {
254
255
  name;
255
256
  connectionName;
@@ -310,7 +311,8 @@ function typedId(prefix) {
310
311
  return {
311
312
  ...import_schema.fieldTypes.string,
312
313
  name: `typedId:${prefix}`,
313
- toSerializedType: async () => "string"
314
+ toSerializedType: async () => "string",
315
+ generateId: () => (0, import_helpers2.generateUUIDWithPrefix)(prefix)
314
316
  };
315
317
  }
316
318
 
@@ -947,13 +949,13 @@ var insertOne_default = (collection) => {
947
949
  };
948
950
 
949
951
  // src/createCollection/getMethods/insertMany.ts
950
- var import_helpers2 = require("@orion-js/helpers");
952
+ var import_helpers3 = require("@orion-js/helpers");
951
953
  var import_schema10 = require("@orion-js/schema");
952
954
  var insertMany_default = (collection) => {
953
955
  const insertMany = async (docs, options = {}) => {
954
956
  await collection.connectionPromise;
955
957
  for (let index = 0; index < docs.length; index++) {
956
- let doc = (0, import_helpers2.clone)(docs[index]);
958
+ let doc = (0, import_helpers3.clone)(docs[index]);
957
959
  if (!doc || type(doc) !== "Object") {
958
960
  throw new Error(`Item at index ${index} is not a document`);
959
961
  }
@@ -1070,7 +1072,7 @@ function loadOne_default(collection) {
1070
1072
  }
1071
1073
 
1072
1074
  // src/createCollection/getMethods/dataLoader/loadData.ts
1073
- var import_helpers4 = require("@orion-js/helpers");
1075
+ var import_helpers5 = require("@orion-js/helpers");
1074
1076
 
1075
1077
  // src/createCollection/getMethods/dataLoader/dataLoad/getDataLoader.ts
1076
1078
  var import_dataloader = __toESM(require("dataloader"), 1);
@@ -1092,10 +1094,10 @@ var getDataLoader = (params) => {
1092
1094
  };
1093
1095
 
1094
1096
  // src/createCollection/getMethods/dataLoader/dataLoad/index.ts
1095
- var import_helpers3 = require("@orion-js/helpers");
1097
+ var import_helpers4 = require("@orion-js/helpers");
1096
1098
  var dataLoad = async (options) => {
1097
1099
  const dataLoader = getDataLoader({
1098
- key: (0, import_helpers3.hashObject)(options.loaderKey),
1100
+ key: (0, import_helpers4.hashObject)(options.loaderKey),
1099
1101
  func: options.load,
1100
1102
  timeout: options.timeout || 5
1101
1103
  });
@@ -1124,10 +1126,10 @@ function loadData_default(collection) {
1124
1126
  timeout: options.timeout,
1125
1127
  load: async (values) => {
1126
1128
  const query = {
1127
- ...(0, import_helpers4.clone)(options.match),
1129
+ ...(0, import_helpers5.clone)(options.match),
1128
1130
  [options.key]: { $in: values }
1129
1131
  };
1130
- const cursor = collection.find(query);
1132
+ const cursor = collection.find(query, { readPreference: "secondaryPreferred" });
1131
1133
  if (options.sort) {
1132
1134
  cursor.sort(options.sort);
1133
1135
  }
@@ -1138,7 +1140,7 @@ function loadData_default(collection) {
1138
1140
  console.info(`Will execute data loading query now on ${collection.name}: `, query);
1139
1141
  }
1140
1142
  const items = await cursor.toArray();
1141
- const itemsMap = (0, import_helpers4.createMapArray)(items, options.key);
1143
+ const itemsMap = (0, import_helpers5.createMapArray)(items, options.key);
1142
1144
  return values.map((value) => {
1143
1145
  return itemsMap[value] || [];
1144
1146
  });
@@ -1150,7 +1152,7 @@ function loadData_default(collection) {
1150
1152
  }
1151
1153
 
1152
1154
  // src/createCollection/generateId.ts
1153
- var import_helpers5 = require("@orion-js/helpers");
1155
+ var import_helpers6 = require("@orion-js/helpers");
1154
1156
  var import_bson = require("bson");
1155
1157
  var getIdGenerator = (options) => {
1156
1158
  var _a, _b;
@@ -1158,23 +1160,21 @@ var getIdGenerator = (options) => {
1158
1160
  const idField = options.schema._id.type;
1159
1161
  if ((_b = idField.name) == null ? void 0 : _b.startsWith("typedId:")) {
1160
1162
  return () => {
1161
- const prefix = idField.name.split(":")[1];
1162
- const random = (0, import_helpers5.generateUUID)();
1163
- return `${prefix}-${random}`;
1163
+ return idField.generateId();
1164
1164
  };
1165
1165
  }
1166
1166
  }
1167
1167
  if (options.idPrefix || options.idGeneration === "uuid") {
1168
1168
  return () => {
1169
1169
  const prefix = options.idPrefix || "";
1170
- const random = (0, import_helpers5.generateUUID)();
1170
+ const random = (0, import_helpers6.generateUUID)();
1171
1171
  return `${prefix}${random}`;
1172
1172
  };
1173
1173
  }
1174
1174
  if (options.idGeneration === "random") {
1175
1175
  return () => {
1176
1176
  const prefix = options.idPrefix || "";
1177
- const random = (0, import_helpers5.generateId)();
1177
+ const random = (0, import_helpers6.generateId)();
1178
1178
  return `${prefix}${random}`;
1179
1179
  };
1180
1180
  }
@@ -1246,7 +1246,7 @@ async function loadIndexes(collection) {
1246
1246
  }
1247
1247
 
1248
1248
  // src/createCollection/getSchemaAndModel.ts
1249
- var import_helpers6 = require("@orion-js/helpers");
1249
+ var import_helpers7 = require("@orion-js/helpers");
1250
1250
  Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
1251
1251
  function prepareShema(schema) {
1252
1252
  if (!schema._id) {
@@ -1268,7 +1268,7 @@ function getSchema(options) {
1268
1268
  }
1269
1269
  if (options.schema.getModel) {
1270
1270
  const model = options.schema.getModel();
1271
- const schema = model ? (0, import_helpers6.clone)(model.getSchema()) : {};
1271
+ const schema = model ? (0, import_helpers7.clone)(model.getSchema()) : {};
1272
1272
  return prepareShema(schema);
1273
1273
  }
1274
1274
  if (type(options.schema) === "Object") {