@e22m4u/js-repository-mongodb-adapter 0.7.0 → 0.7.1

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.
@@ -30,204 +30,12 @@ module.exports = __toCommonJS(index_exports);
30
30
  var import_mongodb2 = require("mongodb");
31
31
  var import_mongodb3 = require("mongodb");
32
32
 
33
- // src/utils/pluralize.js
34
- var singularExceptions = [
35
- /access$/i,
36
- /address$/i,
37
- /alias$/i,
38
- /bonus$/i,
39
- /boss$/i,
40
- /bus$/i,
41
- /business$/i,
42
- /canvas$/i,
43
- /class$/i,
44
- /cross$/i,
45
- /dress$/i,
46
- /focus$/i,
47
- /gas$/i,
48
- /glass$/i,
49
- /kiss$/i,
50
- /lens$/i,
51
- /loss$/i,
52
- /pass$/i,
53
- /plus$/i,
54
- /process$/i,
55
- /status$/i,
56
- /success$/i,
57
- /virus$/i
58
- ];
59
- function pluralize(input) {
60
- if (!input || typeof input !== "string") {
61
- return input;
62
- }
63
- if (/s$/i.test(input) && !singularExceptions.some((re) => re.test(input))) {
64
- return input;
65
- }
66
- const lastChar = input.slice(-1);
67
- const isLastCharUpper = lastChar === lastChar.toUpperCase() && lastChar !== lastChar.toLowerCase();
68
- if (/(s|x|z|ch|sh)$/i.test(input)) {
69
- return input + (isLastCharUpper ? "ES" : "es");
70
- }
71
- if (/[^aeiou]y$/i.test(input)) {
72
- return input.slice(0, -1) + (isLastCharUpper ? "IES" : "ies");
73
- }
74
- return input + (isLastCharUpper ? "S" : "s");
75
- }
76
- __name(pluralize, "pluralize");
77
-
78
- // src/utils/is-iso-date.js
79
- function isIsoDate(value) {
80
- if (!value) return false;
81
- if (value instanceof Date) return true;
82
- if (!/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(value)) return false;
83
- const d = new Date(value);
84
- return d instanceof Date && !isNaN(d.getTime()) && d.toISOString() === value;
85
- }
86
- __name(isIsoDate, "isIsoDate");
87
-
88
- // src/utils/is-object-id.js
89
- var import_mongodb = require("mongodb");
90
- function isObjectId(value) {
91
- if (!value) return false;
92
- if (value instanceof import_mongodb.ObjectId) return true;
93
- if (typeof value !== "string") return false;
94
- return value.match(/^[a-fA-F0-9]{24}$/) != null;
95
- }
96
- __name(isObjectId, "isObjectId");
97
-
98
- // src/utils/to-camel-case.js
99
- function toCamelCase(input) {
100
- if (!input) return "";
101
- const spacedString = String(input).replace(/([-_])/g, " ").replace(/([a-z])([A-Z])/g, "$1 $2");
102
- const intermediateCased = spacedString.toLowerCase().replace(/\s(.)/g, ($1) => $1.toUpperCase()).replace(/\s/g, "");
103
- if (!intermediateCased) return "";
104
- return intermediateCased.charAt(0).toLowerCase() + intermediateCased.slice(1);
105
- }
106
- __name(toCamelCase, "toCamelCase");
107
-
108
- // src/utils/create-mongodb-url.js
109
- var import_js_repository = require("@e22m4u/js-repository");
110
- function createMongodbUrl(options = {}) {
111
- if (!options || typeof options !== "object" || Array.isArray(options))
112
- throw new import_js_repository.InvalidArgumentError(
113
- 'The first argument of "createMongodbUrl" must be an Object, but %v given.',
114
- options
115
- );
116
- if (options.protocol && typeof options.protocol !== "string")
117
- throw new import_js_repository.InvalidArgumentError(
118
- 'MongoDB option "protocol" must be a String, but %v given.',
119
- options.protocol
120
- );
121
- if (options.hostname && typeof options.hostname !== "string")
122
- throw new import_js_repository.InvalidArgumentError(
123
- 'MongoDB option "hostname" must be a String, but %v given.',
124
- options.hostname
125
- );
126
- if (options.host && typeof options.host !== "string")
127
- throw new import_js_repository.InvalidArgumentError(
128
- 'MongoDB option "host" must be a String, but %v given.',
129
- options.host
130
- );
131
- if (options.port && typeof options.port !== "number" && typeof options.port !== "string") {
132
- throw new import_js_repository.InvalidArgumentError(
133
- 'MongoDB option "port" must be a Number or a String, but %v given.',
134
- options.port
135
- );
136
- }
137
- if (options.database && typeof options.database !== "string")
138
- throw new import_js_repository.InvalidArgumentError(
139
- 'MongoDB option "database" must be a String, but %v given.',
140
- options.database
141
- );
142
- if (options.db && typeof options.db !== "string")
143
- throw new import_js_repository.InvalidArgumentError(
144
- 'MongoDB option "db" must be a String, but %v given.',
145
- options.db
146
- );
147
- if (options.username && typeof options.username !== "string")
148
- throw new import_js_repository.InvalidArgumentError(
149
- 'MongoDB option "username" must be a String, but %v given.',
150
- options.username
151
- );
152
- if (options.password && typeof options.password !== "string" && typeof options.password !== "number") {
153
- throw new import_js_repository.InvalidArgumentError(
154
- 'MongoDB option "password" must be a String or a Number, but %v given.',
155
- options.password
156
- );
157
- }
158
- if (options.pass && typeof options.pass !== "string" && typeof options.pass !== "number") {
159
- throw new import_js_repository.InvalidArgumentError(
160
- 'MongoDB option "pass" must be a String or a Number, but %v given.',
161
- options.pass
162
- );
163
- }
164
- const protocol = options.protocol || "mongodb";
165
- const hostname = options.hostname || options.host || "127.0.0.1";
166
- const port = options.port || 27017;
167
- const database = options.database || options.db || "database";
168
- const username = options.username || options.user;
169
- const password = options.password || options.pass || void 0;
170
- let portUrl = "";
171
- if (protocol !== "mongodb+srv") {
172
- portUrl = ":" + port;
173
- }
174
- if (username && password) {
175
- return `${protocol}://${username}:${password}@${hostname}${portUrl}/${database}`;
176
- } else {
177
- return `${protocol}://${hostname}${portUrl}/${database}`;
178
- }
179
- }
180
- __name(createMongodbUrl, "createMongodbUrl");
181
-
182
- // src/utils/transform-values-deep.js
183
- var import_js_repository2 = require("@e22m4u/js-repository");
184
- function transformValuesDeep(value, transformer) {
185
- if (!transformer || typeof transformer !== "function")
186
- throw new import_js_repository2.InvalidArgumentError(
187
- 'The second argument of "transformValuesDeep" must be a Function, but %v given.',
188
- transformer
189
- );
190
- if (Array.isArray(value)) {
191
- value.forEach((v, i) => value[i] = transformValuesDeep(v, transformer));
192
- return value;
193
- } else if (value && typeof value === "object") {
194
- if (!value.constructor || value.constructor && value.constructor.name === "Object") {
195
- Object.keys(value).forEach((key) => {
196
- if (Object.prototype.hasOwnProperty.call(value, key))
197
- value[key] = transformValuesDeep(value[key], transformer);
198
- });
199
- return value;
200
- } else {
201
- return transformer(value);
202
- }
203
- } else {
204
- return transformer(value);
205
- }
206
- }
207
- __name(transformValuesDeep, "transformValuesDeep");
208
-
209
- // src/utils/model-name-to-collection-name.js
210
- function modelNameToCollectionName(modelName) {
211
- const ccName = toCamelCase(modelName);
212
- const woModel = ccName.replace(/Model$/i, "");
213
- if (woModel.length <= 2) {
214
- return pluralize(ccName);
215
- }
216
- return pluralize(woModel);
217
- }
218
- __name(modelNameToCollectionName, "modelNameToCollectionName");
219
-
220
- // src/mongodb-adapter.js
221
- var import_js_repository3 = require("@e22m4u/js-repository");
222
- var import_js_repository4 = require("@e22m4u/js-repository");
223
- var import_js_repository5 = require("@e22m4u/js-repository");
224
-
225
33
  // node_modules/@e22m4u/js-service/src/errors/invalid-argument-error.js
226
34
  var import_js_format = require("@e22m4u/js-format");
227
35
  var _InvalidArgumentError = class _InvalidArgumentError extends import_js_format.Errorf {
228
36
  };
229
37
  __name(_InvalidArgumentError, "InvalidArgumentError");
230
- var InvalidArgumentError3 = _InvalidArgumentError;
38
+ var InvalidArgumentError = _InvalidArgumentError;
231
39
 
232
40
  // node_modules/@e22m4u/js-service/src/service-container.js
233
41
  var SERVICE_CONTAINER_CLASS_NAME = "ServiceContainer";
@@ -254,7 +62,7 @@ var _ServiceContainer = class _ServiceContainer {
254
62
  constructor(parent = void 0) {
255
63
  if (parent != null) {
256
64
  if (!(parent instanceof _ServiceContainer))
257
- throw new InvalidArgumentError3(
65
+ throw new InvalidArgumentError(
258
66
  'The provided parameter "parent" of ServicesContainer.constructor must be an instance ServiceContainer, but %v given.',
259
67
  parent
260
68
  );
@@ -268,7 +76,7 @@ var _ServiceContainer = class _ServiceContainer {
268
76
  */
269
77
  getParent() {
270
78
  if (!this._parent)
271
- throw new InvalidArgumentError3("The service container has no parent.");
79
+ throw new InvalidArgumentError("The service container has no parent.");
272
80
  return this._parent;
273
81
  }
274
82
  /**
@@ -288,7 +96,7 @@ var _ServiceContainer = class _ServiceContainer {
288
96
  */
289
97
  get(ctor, ...args) {
290
98
  if (!ctor || typeof ctor !== "function")
291
- throw new InvalidArgumentError3(
99
+ throw new InvalidArgumentError(
292
100
  "The first argument of ServicesContainer.get must be a class constructor, but %v given.",
293
101
  ctor
294
102
  );
@@ -296,14 +104,14 @@ var _ServiceContainer = class _ServiceContainer {
296
104
  let service = this._services.get(ctor);
297
105
  let inheritedCtor = void 0;
298
106
  if (!service) {
299
- const ctors = this._services.keys();
300
- const inheritedCtor2 = ctors.find((v) => v.prototype instanceof ctor);
301
- if (inheritedCtor2) service = this._services.get(inheritedCtor2);
107
+ const ctors = Array.from(this._services.keys());
108
+ inheritedCtor = ctors.find((v) => v.prototype instanceof ctor);
109
+ if (inheritedCtor) service = this._services.get(inheritedCtor);
302
110
  }
303
111
  if (!service && !isCtorRegistered && !inheritedCtor && this._parent && this._parent.has(ctor)) {
304
112
  return this._parent.get(ctor, ...args);
305
113
  }
306
- if (!service && !isCtorRegistered && inheritedCtor) {
114
+ if (!isCtorRegistered && inheritedCtor) {
307
115
  ctor = inheritedCtor;
308
116
  }
309
117
  if (!service || args.length) {
@@ -325,7 +133,7 @@ var _ServiceContainer = class _ServiceContainer {
325
133
  */
326
134
  getRegistered(ctor, ...args) {
327
135
  if (!this.has(ctor))
328
- throw new InvalidArgumentError3(
136
+ throw new InvalidArgumentError(
329
137
  "The constructor %v is not registered.",
330
138
  ctor
331
139
  );
@@ -339,7 +147,7 @@ var _ServiceContainer = class _ServiceContainer {
339
147
  */
340
148
  has(ctor) {
341
149
  if (this._services.has(ctor)) return true;
342
- const ctors = this._services.keys();
150
+ const ctors = Array.from(this._services.keys());
343
151
  const inheritedCtor = ctors.find((v) => v.prototype instanceof ctor);
344
152
  if (inheritedCtor) return true;
345
153
  if (this._parent) return this._parent.has(ctor);
@@ -354,7 +162,7 @@ var _ServiceContainer = class _ServiceContainer {
354
162
  */
355
163
  add(ctor, ...args) {
356
164
  if (!ctor || typeof ctor !== "function")
357
- throw new InvalidArgumentError3(
165
+ throw new InvalidArgumentError(
358
166
  "The first argument of ServicesContainer.add must be a class constructor, but %v given.",
359
167
  ctor
360
168
  );
@@ -371,7 +179,7 @@ var _ServiceContainer = class _ServiceContainer {
371
179
  */
372
180
  use(ctor, ...args) {
373
181
  if (!ctor || typeof ctor !== "function")
374
- throw new InvalidArgumentError3(
182
+ throw new InvalidArgumentError(
375
183
  "The first argument of ServicesContainer.use must be a class constructor, but %v given.",
376
184
  ctor
377
185
  );
@@ -388,18 +196,48 @@ var _ServiceContainer = class _ServiceContainer {
388
196
  */
389
197
  set(ctor, service) {
390
198
  if (!ctor || typeof ctor !== "function")
391
- throw new InvalidArgumentError3(
199
+ throw new InvalidArgumentError(
392
200
  "The first argument of ServicesContainer.set must be a class constructor, but %v given.",
393
201
  ctor
394
202
  );
395
203
  if (!service || typeof service !== "object" || Array.isArray(service))
396
- throw new InvalidArgumentError3(
204
+ throw new InvalidArgumentError(
397
205
  "The second argument of ServicesContainer.set must be an Object, but %v given.",
398
206
  service
399
207
  );
400
208
  this._services.set(ctor, service);
401
209
  return this;
402
210
  }
211
+ /**
212
+ * Найти сервис удовлетворяющий условию.
213
+ *
214
+ * @param {function(Function, ServiceContainer): boolean} predicate
215
+ * @param {boolean} noParent
216
+ * @returns {*}
217
+ */
218
+ find(predicate, noParent = false) {
219
+ if (typeof predicate !== "function") {
220
+ throw new InvalidArgumentError(
221
+ "The first argument of ServiceContainer.find must be a function, but %v given.",
222
+ predicate
223
+ );
224
+ }
225
+ const isRecursive = !noParent;
226
+ let currentContainer = this;
227
+ do {
228
+ for (const ctor of currentContainer._services.keys()) {
229
+ if (predicate(ctor, currentContainer) === true) {
230
+ return this.get(ctor);
231
+ }
232
+ }
233
+ if (isRecursive && currentContainer.hasParent()) {
234
+ currentContainer = currentContainer.getParent();
235
+ } else {
236
+ currentContainer = null;
237
+ }
238
+ } while (currentContainer);
239
+ return void 0;
240
+ }
403
241
  };
404
242
  __name(_ServiceContainer, "ServiceContainer");
405
243
  /**
@@ -498,6 +336,16 @@ var _Service = class _Service {
498
336
  this.container.set(ctor, service);
499
337
  return this;
500
338
  }
339
+ /**
340
+ * Найти сервис удовлетворяющий условию.
341
+ *
342
+ * @param {function(Function, ServiceContainer): boolean} predicate
343
+ * @param {boolean} noParent
344
+ * @returns {*}
345
+ */
346
+ findService(predicate, noParent = false) {
347
+ return this.container.find(predicate, noParent);
348
+ }
501
349
  };
502
350
  __name(_Service, "Service");
503
351
  /**
@@ -509,10 +357,10 @@ __publicField(_Service, "kinds", [SERVICE_CLASS_NAME]);
509
357
  var Service = _Service;
510
358
 
511
359
  // node_modules/@e22m4u/js-debug/src/utils/to-camel-case.js
512
- function toCamelCase2(input) {
360
+ function toCamelCase(input) {
513
361
  return input.replace(/(^\w|[A-Z]|\b\w)/g, (c) => c.toUpperCase()).replace(/\W+/g, "").replace(/(^\w)/g, (c) => c.toLowerCase());
514
362
  }
515
- __name(toCamelCase2, "toCamelCase");
363
+ __name(toCamelCase, "toCamelCase");
516
364
 
517
365
  // node_modules/@e22m4u/js-debug/src/utils/is-non-array-object.js
518
366
  function isNonArrayObject(input) {
@@ -569,7 +417,7 @@ var _Debuggable = class _Debuggable {
569
417
  * @param {DebuggableOptions|undefined} options
570
418
  */
571
419
  constructor(options = void 0) {
572
- const className = toCamelCase2(this.constructor.name);
420
+ const className = toCamelCase(this.constructor.name);
573
421
  options = typeof options === "object" && options || {};
574
422
  const namespace = options.namespace && String(options.namespace) || void 0;
575
423
  if (namespace) {
@@ -936,6 +784,14 @@ var _DebuggableService = class _DebuggableService extends Debuggable {
936
784
  get setService() {
937
785
  return this._service.setService;
938
786
  }
787
+ /**
788
+ * Найти сервис удовлетворяющий условию.
789
+ *
790
+ * @type {Service['findService']}
791
+ */
792
+ get findService() {
793
+ return this._service.findService;
794
+ }
939
795
  /**
940
796
  * Constructor.
941
797
  *
@@ -957,12 +813,196 @@ __publicField(_DebuggableService, "kinds", Service.kinds);
957
813
  var DebuggableService = _DebuggableService;
958
814
 
959
815
  // src/mongodb-adapter.js
960
- var import_js_repository6 = require("@e22m4u/js-repository");
961
- var import_js_repository7 = require("@e22m4u/js-repository");
962
- var import_js_repository8 = require("@e22m4u/js-repository");
963
- var import_js_repository9 = require("@e22m4u/js-repository");
964
- var import_js_repository10 = require("@e22m4u/js-repository");
965
- var import_js_repository11 = require("@e22m4u/js-repository");
816
+ var import_js_repository3 = require("@e22m4u/js-repository");
817
+
818
+ // src/utils/pluralize.js
819
+ var singularExceptions = [
820
+ /access$/i,
821
+ /address$/i,
822
+ /alias$/i,
823
+ /bonus$/i,
824
+ /boss$/i,
825
+ /bus$/i,
826
+ /business$/i,
827
+ /canvas$/i,
828
+ /class$/i,
829
+ /cross$/i,
830
+ /dress$/i,
831
+ /focus$/i,
832
+ /gas$/i,
833
+ /glass$/i,
834
+ /kiss$/i,
835
+ /lens$/i,
836
+ /loss$/i,
837
+ /pass$/i,
838
+ /plus$/i,
839
+ /process$/i,
840
+ /status$/i,
841
+ /success$/i,
842
+ /virus$/i
843
+ ];
844
+ function pluralize(input) {
845
+ if (!input || typeof input !== "string") {
846
+ return input;
847
+ }
848
+ if (/s$/i.test(input) && !singularExceptions.some((re) => re.test(input))) {
849
+ return input;
850
+ }
851
+ const lastChar = input.slice(-1);
852
+ const isLastCharUpper = lastChar === lastChar.toUpperCase() && lastChar !== lastChar.toLowerCase();
853
+ if (/(s|x|z|ch|sh)$/i.test(input)) {
854
+ return input + (isLastCharUpper ? "ES" : "es");
855
+ }
856
+ if (/[^aeiou]y$/i.test(input)) {
857
+ return input.slice(0, -1) + (isLastCharUpper ? "IES" : "ies");
858
+ }
859
+ return input + (isLastCharUpper ? "S" : "s");
860
+ }
861
+ __name(pluralize, "pluralize");
862
+
863
+ // src/utils/is-iso-date.js
864
+ function isIsoDate(value) {
865
+ if (!value) return false;
866
+ if (value instanceof Date) return true;
867
+ if (!/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(value)) return false;
868
+ const d = new Date(value);
869
+ return d instanceof Date && !isNaN(d.getTime()) && d.toISOString() === value;
870
+ }
871
+ __name(isIsoDate, "isIsoDate");
872
+
873
+ // src/utils/is-object-id.js
874
+ var import_mongodb = require("mongodb");
875
+ function isObjectId(value) {
876
+ if (!value) return false;
877
+ if (value instanceof import_mongodb.ObjectId) return true;
878
+ if (typeof value !== "string") return false;
879
+ return value.match(/^[a-fA-F0-9]{24}$/) != null;
880
+ }
881
+ __name(isObjectId, "isObjectId");
882
+
883
+ // src/utils/to-camel-case.js
884
+ function toCamelCase2(input) {
885
+ if (!input) return "";
886
+ const spacedString = String(input).replace(/([-_])/g, " ").replace(/([a-z])([A-Z])/g, "$1 $2");
887
+ const intermediateCased = spacedString.toLowerCase().replace(/\s(.)/g, ($1) => $1.toUpperCase()).replace(/\s/g, "");
888
+ if (!intermediateCased) return "";
889
+ return intermediateCased.charAt(0).toLowerCase() + intermediateCased.slice(1);
890
+ }
891
+ __name(toCamelCase2, "toCamelCase");
892
+
893
+ // src/utils/create-mongodb-url.js
894
+ var import_js_repository = require("@e22m4u/js-repository");
895
+ function createMongodbUrl(options = {}) {
896
+ if (!options || typeof options !== "object" || Array.isArray(options))
897
+ throw new import_js_repository.InvalidArgumentError(
898
+ 'The first argument of "createMongodbUrl" must be an Object, but %v given.',
899
+ options
900
+ );
901
+ if (options.protocol && typeof options.protocol !== "string")
902
+ throw new import_js_repository.InvalidArgumentError(
903
+ 'MongoDB option "protocol" must be a String, but %v given.',
904
+ options.protocol
905
+ );
906
+ if (options.hostname && typeof options.hostname !== "string")
907
+ throw new import_js_repository.InvalidArgumentError(
908
+ 'MongoDB option "hostname" must be a String, but %v given.',
909
+ options.hostname
910
+ );
911
+ if (options.host && typeof options.host !== "string")
912
+ throw new import_js_repository.InvalidArgumentError(
913
+ 'MongoDB option "host" must be a String, but %v given.',
914
+ options.host
915
+ );
916
+ if (options.port && typeof options.port !== "number" && typeof options.port !== "string") {
917
+ throw new import_js_repository.InvalidArgumentError(
918
+ 'MongoDB option "port" must be a Number or a String, but %v given.',
919
+ options.port
920
+ );
921
+ }
922
+ if (options.database && typeof options.database !== "string")
923
+ throw new import_js_repository.InvalidArgumentError(
924
+ 'MongoDB option "database" must be a String, but %v given.',
925
+ options.database
926
+ );
927
+ if (options.db && typeof options.db !== "string")
928
+ throw new import_js_repository.InvalidArgumentError(
929
+ 'MongoDB option "db" must be a String, but %v given.',
930
+ options.db
931
+ );
932
+ if (options.username && typeof options.username !== "string")
933
+ throw new import_js_repository.InvalidArgumentError(
934
+ 'MongoDB option "username" must be a String, but %v given.',
935
+ options.username
936
+ );
937
+ if (options.password && typeof options.password !== "string" && typeof options.password !== "number") {
938
+ throw new import_js_repository.InvalidArgumentError(
939
+ 'MongoDB option "password" must be a String or a Number, but %v given.',
940
+ options.password
941
+ );
942
+ }
943
+ if (options.pass && typeof options.pass !== "string" && typeof options.pass !== "number") {
944
+ throw new import_js_repository.InvalidArgumentError(
945
+ 'MongoDB option "pass" must be a String or a Number, but %v given.',
946
+ options.pass
947
+ );
948
+ }
949
+ const protocol = options.protocol || "mongodb";
950
+ const hostname = options.hostname || options.host || "127.0.0.1";
951
+ const port = options.port || 27017;
952
+ const database = options.database || options.db || "database";
953
+ const username = options.username || options.user;
954
+ const password = options.password || options.pass || void 0;
955
+ let portUrl = "";
956
+ if (protocol !== "mongodb+srv") {
957
+ portUrl = ":" + port;
958
+ }
959
+ if (username && password) {
960
+ return `${protocol}://${username}:${password}@${hostname}${portUrl}/${database}`;
961
+ } else {
962
+ return `${protocol}://${hostname}${portUrl}/${database}`;
963
+ }
964
+ }
965
+ __name(createMongodbUrl, "createMongodbUrl");
966
+
967
+ // src/utils/transform-values-deep.js
968
+ var import_js_repository2 = require("@e22m4u/js-repository");
969
+ function transformValuesDeep(value, transformer) {
970
+ if (!transformer || typeof transformer !== "function")
971
+ throw new import_js_repository2.InvalidArgumentError(
972
+ 'The second argument of "transformValuesDeep" must be a Function, but %v given.',
973
+ transformer
974
+ );
975
+ if (Array.isArray(value)) {
976
+ value.forEach((v, i) => value[i] = transformValuesDeep(v, transformer));
977
+ return value;
978
+ } else if (value && typeof value === "object") {
979
+ if (!value.constructor || value.constructor && value.constructor.name === "Object") {
980
+ Object.keys(value).forEach((key) => {
981
+ if (Object.prototype.hasOwnProperty.call(value, key))
982
+ value[key] = transformValuesDeep(value[key], transformer);
983
+ });
984
+ return value;
985
+ } else {
986
+ return transformer(value);
987
+ }
988
+ } else {
989
+ return transformer(value);
990
+ }
991
+ }
992
+ __name(transformValuesDeep, "transformValuesDeep");
993
+
994
+ // src/utils/model-name-to-collection-name.js
995
+ function modelNameToCollectionName(modelName) {
996
+ const ccName = toCamelCase2(modelName);
997
+ const woModel = ccName.replace(/Model$/i, "");
998
+ if (woModel.length <= 2) {
999
+ return pluralize(ccName);
1000
+ }
1001
+ return pluralize(woModel);
1002
+ }
1003
+ __name(modelNameToCollectionName, "modelNameToCollectionName");
1004
+
1005
+ // src/mongodb-adapter.js
966
1006
  var MONGODB_OPTION_NAMES = [
967
1007
  "ALPNProtocols",
968
1008
  "allowPartialTrustChain",
@@ -1100,7 +1140,7 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1100
1140
  settings.port = settings.port || 27017;
1101
1141
  settings.database = settings.database || settings.db || "database";
1102
1142
  super(container, settings);
1103
- const options = (0, import_js_repository7.selectObjectKeys)(this.settings, MONGODB_OPTION_NAMES);
1143
+ const options = (0, import_js_repository3.selectObjectKeys)(this.settings, MONGODB_OPTION_NAMES);
1104
1144
  const url = createMongodbUrl(this.settings);
1105
1145
  this._client = new import_mongodb3.MongoClient(url, options);
1106
1146
  }
@@ -1111,7 +1151,7 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1111
1151
  * @private
1112
1152
  */
1113
1153
  _getIdPropName(modelName) {
1114
- return this.getService(import_js_repository9.ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
1154
+ return this.getService(import_js_repository3.ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
1115
1155
  modelName
1116
1156
  );
1117
1157
  }
@@ -1122,7 +1162,7 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1122
1162
  * @private
1123
1163
  */
1124
1164
  _getIdColName(modelName) {
1125
- return this.getService(import_js_repository9.ModelDefinitionUtils).getPrimaryKeyAsColumnName(
1165
+ return this.getService(import_js_repository3.ModelDefinitionUtils).getPrimaryKeyAsColumnName(
1126
1166
  modelName
1127
1167
  );
1128
1168
  }
@@ -1161,11 +1201,11 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1161
1201
  */
1162
1202
  _toDatabase(modelName, modelData) {
1163
1203
  const tableData = this.getService(
1164
- import_js_repository9.ModelDefinitionUtils
1204
+ import_js_repository3.ModelDefinitionUtils
1165
1205
  ).convertPropertyNamesToColumnNames(modelName, modelData);
1166
1206
  const idColName = this._getIdColName(modelName);
1167
1207
  if (idColName !== "id" && idColName !== "_id")
1168
- throw new import_js_repository10.InvalidArgumentError(
1208
+ throw new import_js_repository3.InvalidArgumentError(
1169
1209
  'MongoDB is not supporting custom names of the primary key. Do use "id" as a primary key instead of %v.',
1170
1210
  idColName
1171
1211
  );
@@ -1193,7 +1233,7 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1193
1233
  if ("_id" in tableData) {
1194
1234
  const idColName = this._getIdColName(modelName);
1195
1235
  if (idColName !== "id" && idColName !== "_id")
1196
- throw new import_js_repository10.InvalidArgumentError(
1236
+ throw new import_js_repository3.InvalidArgumentError(
1197
1237
  'MongoDB is not supporting custom names of the primary key. Do use "id" as a primary key instead of %v.',
1198
1238
  idColName
1199
1239
  );
@@ -1203,7 +1243,7 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1203
1243
  }
1204
1244
  }
1205
1245
  const modelData = this.getService(
1206
- import_js_repository9.ModelDefinitionUtils
1246
+ import_js_repository3.ModelDefinitionUtils
1207
1247
  ).convertColumnNamesToPropertyNames(modelName, tableData);
1208
1248
  return transformValuesDeep(modelData, (value) => {
1209
1249
  if (value instanceof import_mongodb2.ObjectId) return String(value);
@@ -1217,7 +1257,7 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1217
1257
  * @param {string} modelName
1218
1258
  */
1219
1259
  _getCollectionNameByModelName(modelName) {
1220
- const modelDef = this.getService(import_js_repository8.DefinitionRegistry).getModel(modelName);
1260
+ const modelDef = this.getService(import_js_repository3.DefinitionRegistry).getModel(modelName);
1221
1261
  if (modelDef.tableName != null) return modelDef.tableName;
1222
1262
  return modelNameToCollectionName(modelDef.name);
1223
1263
  }
@@ -1244,7 +1284,7 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1244
1284
  * @private
1245
1285
  */
1246
1286
  _getIdType(modelName) {
1247
- const utils = this.getService(import_js_repository9.ModelDefinitionUtils);
1287
+ const utils = this.getService(import_js_repository3.ModelDefinitionUtils);
1248
1288
  const pkPropName = utils.getPrimaryKeyAsPropertyName(modelName);
1249
1289
  return utils.getDataTypeByPropertyName(modelName, pkPropName);
1250
1290
  }
@@ -1258,16 +1298,16 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1258
1298
  */
1259
1299
  _getColName(modelName, propName) {
1260
1300
  if (!propName || typeof propName !== "string")
1261
- throw new import_js_repository10.InvalidArgumentError(
1301
+ throw new import_js_repository3.InvalidArgumentError(
1262
1302
  "Property name must be a non-empty String, but %v given.",
1263
1303
  propName
1264
1304
  );
1265
- const utils = this.getService(import_js_repository9.ModelDefinitionUtils);
1305
+ const utils = this.getService(import_js_repository3.ModelDefinitionUtils);
1266
1306
  let colName = propName;
1267
1307
  try {
1268
1308
  colName = utils.getColumnNameByPropertyName(modelName, propName);
1269
1309
  } catch (error) {
1270
- if (!(error instanceof import_js_repository10.InvalidArgumentError) || error.message.indexOf("does not have the property") === -1) {
1310
+ if (!(error instanceof import_js_repository3.InvalidArgumentError) || error.message.indexOf("does not have the property") === -1) {
1271
1311
  throw error;
1272
1312
  }
1273
1313
  }
@@ -1283,18 +1323,18 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1283
1323
  */
1284
1324
  _convertPropNamesChainToColNamesChain(modelName, propsChain) {
1285
1325
  if (!modelName || typeof modelName !== "string")
1286
- throw new import_js_repository10.InvalidArgumentError(
1326
+ throw new import_js_repository3.InvalidArgumentError(
1287
1327
  "Model name must be a non-empty String, but %v given.",
1288
1328
  modelName
1289
1329
  );
1290
1330
  if (!propsChain || typeof propsChain !== "string")
1291
- throw new import_js_repository10.InvalidArgumentError(
1331
+ throw new import_js_repository3.InvalidArgumentError(
1292
1332
  "Properties chain must be a non-empty String, but %v given.",
1293
1333
  propsChain
1294
1334
  );
1295
1335
  propsChain = propsChain.replace(/\.{2,}/g, ".");
1296
1336
  const propNames = propsChain.split(".");
1297
- const utils = this.getService(import_js_repository9.ModelDefinitionUtils);
1337
+ const utils = this.getService(import_js_repository3.ModelDefinitionUtils);
1298
1338
  let currModelName = modelName;
1299
1339
  return propNames.map((currPropName) => {
1300
1340
  if (!currModelName) return currPropName;
@@ -1321,7 +1361,7 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1321
1361
  if (fields.indexOf("_id") === -1) fields.push("_id");
1322
1362
  return fields.reduce((acc, field) => {
1323
1363
  if (!field || typeof field !== "string")
1324
- throw new import_js_repository10.InvalidArgumentError(
1364
+ throw new import_js_repository3.InvalidArgumentError(
1325
1365
  'The provided option "fields" should be a non-empty String or an Array of non-empty String, but %v given.',
1326
1366
  field
1327
1367
  );
@@ -1348,7 +1388,7 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1348
1388
  const idPropName = this._getIdPropName(modelName);
1349
1389
  return clause.reduce((acc, order) => {
1350
1390
  if (!order || typeof order !== "string")
1351
- throw new import_js_repository10.InvalidArgumentError(
1391
+ throw new import_js_repository3.InvalidArgumentError(
1352
1392
  'The provided option "order" should be a non-empty String or an Array of non-empty String, but %v given.',
1353
1393
  order
1354
1394
  );
@@ -1360,7 +1400,7 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1360
1400
  try {
1361
1401
  field = this._convertPropNamesChainToColNamesChain(modelName, field);
1362
1402
  } catch (error) {
1363
- if (!(error instanceof import_js_repository10.InvalidArgumentError) || error.message.indexOf("does not have the property") === -1) {
1403
+ if (!(error instanceof import_js_repository3.InvalidArgumentError) || error.message.indexOf("does not have the property") === -1) {
1364
1404
  throw error;
1365
1405
  }
1366
1406
  }
@@ -1380,7 +1420,7 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1380
1420
  _buildQuery(modelName, clause) {
1381
1421
  if (clause == null) return;
1382
1422
  if (typeof clause !== "object" || Array.isArray(clause))
1383
- throw new import_js_repository10.InvalidArgumentError(
1423
+ throw new import_js_repository3.InvalidArgumentError(
1384
1424
  'The provided option "where" should be an Object, but %v given.',
1385
1425
  clause
1386
1426
  );
@@ -1389,7 +1429,7 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1389
1429
  Object.keys(clause).forEach((key) => {
1390
1430
  var _a, _b;
1391
1431
  if (String(key).indexOf("$") !== -1)
1392
- throw new import_js_repository10.InvalidArgumentError(
1432
+ throw new import_js_repository3.InvalidArgumentError(
1393
1433
  'The symbol "$" is not supported, but %v given.',
1394
1434
  key
1395
1435
  );
@@ -1397,7 +1437,7 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1397
1437
  if (key === "and" || key === "or" || key === "nor") {
1398
1438
  if (cond == null) return;
1399
1439
  if (!Array.isArray(cond))
1400
- throw new import_js_repository11.InvalidOperatorValueError(key, "an Array", cond);
1440
+ throw new import_js_repository3.InvalidOperatorValueError(key, "an Array", cond);
1401
1441
  if (cond.length === 0) return;
1402
1442
  cond = cond.map((c) => this._buildQuery(modelName, c));
1403
1443
  cond = cond.filter((c) => c != null);
@@ -1450,7 +1490,7 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1450
1490
  }
1451
1491
  if ("inq" in cond) {
1452
1492
  if (!cond.inq || !Array.isArray(cond.inq))
1453
- throw new import_js_repository11.InvalidOperatorValueError(
1493
+ throw new import_js_repository3.InvalidOperatorValueError(
1454
1494
  "inq",
1455
1495
  "an Array of possible values",
1456
1496
  cond.inq
@@ -1464,7 +1504,7 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1464
1504
  }
1465
1505
  if ("nin" in cond) {
1466
1506
  if (!cond.nin || !Array.isArray(cond.nin))
1467
- throw new import_js_repository11.InvalidOperatorValueError(
1507
+ throw new import_js_repository3.InvalidOperatorValueError(
1468
1508
  "nin",
1469
1509
  "an Array of possible values",
1470
1510
  cond
@@ -1478,7 +1518,7 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1478
1518
  }
1479
1519
  if ("between" in cond) {
1480
1520
  if (!Array.isArray(cond.between) || cond.between.length !== 2)
1481
- throw new import_js_repository11.InvalidOperatorValueError(
1521
+ throw new import_js_repository3.InvalidOperatorValueError(
1482
1522
  "between",
1483
1523
  "an Array of 2 elements",
1484
1524
  cond.between
@@ -1489,7 +1529,7 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1489
1529
  }
1490
1530
  if ("exists" in cond) {
1491
1531
  if (typeof cond.exists !== "boolean")
1492
- throw new import_js_repository11.InvalidOperatorValueError(
1532
+ throw new import_js_repository3.InvalidOperatorValueError(
1493
1533
  "exists",
1494
1534
  "a Boolean",
1495
1535
  cond.exists
@@ -1498,44 +1538,44 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1498
1538
  }
1499
1539
  if ("like" in cond) {
1500
1540
  if (typeof cond.like !== "string" && !(cond.like instanceof RegExp))
1501
- throw new import_js_repository11.InvalidOperatorValueError(
1541
+ throw new import_js_repository3.InvalidOperatorValueError(
1502
1542
  "like",
1503
1543
  "a String or RegExp",
1504
1544
  cond.like
1505
1545
  );
1506
- opConds.push({ $regex: (0, import_js_repository6.stringToRegexp)(cond.like) });
1546
+ opConds.push({ $regex: (0, import_js_repository3.likeToRegexp)(cond.like) });
1507
1547
  }
1508
1548
  if ("nlike" in cond) {
1509
1549
  if (typeof cond.nlike !== "string" && !(cond.nlike instanceof RegExp))
1510
- throw new import_js_repository11.InvalidOperatorValueError(
1550
+ throw new import_js_repository3.InvalidOperatorValueError(
1511
1551
  "nlike",
1512
1552
  "a String or RegExp",
1513
1553
  cond.nlike
1514
1554
  );
1515
- opConds.push({ $not: (0, import_js_repository6.stringToRegexp)(cond.nlike) });
1555
+ opConds.push({ $not: (0, import_js_repository3.likeToRegexp)(cond.nlike) });
1516
1556
  }
1517
1557
  if ("ilike" in cond) {
1518
1558
  if (typeof cond.ilike !== "string" && !(cond.ilike instanceof RegExp))
1519
- throw new import_js_repository11.InvalidOperatorValueError(
1559
+ throw new import_js_repository3.InvalidOperatorValueError(
1520
1560
  "ilike",
1521
1561
  "a String or RegExp",
1522
1562
  cond.ilike
1523
1563
  );
1524
- opConds.push({ $regex: (0, import_js_repository6.stringToRegexp)(cond.ilike, "i") });
1564
+ opConds.push({ $regex: (0, import_js_repository3.likeToRegexp)(cond.ilike, true) });
1525
1565
  }
1526
1566
  if ("nilike" in cond) {
1527
1567
  if (typeof cond.nilike !== "string" && !(cond.nilike instanceof RegExp)) {
1528
- throw new import_js_repository11.InvalidOperatorValueError(
1568
+ throw new import_js_repository3.InvalidOperatorValueError(
1529
1569
  "nilike",
1530
1570
  "a String or RegExp",
1531
1571
  cond.nilike
1532
1572
  );
1533
1573
  }
1534
- opConds.push({ $not: (0, import_js_repository6.stringToRegexp)(cond.nilike, "i") });
1574
+ opConds.push({ $not: (0, import_js_repository3.likeToRegexp)(cond.nilike, true) });
1535
1575
  }
1536
1576
  if ("regexp" in cond) {
1537
1577
  if (typeof cond.regexp !== "string" && !(cond.regexp instanceof RegExp)) {
1538
- throw new import_js_repository11.InvalidOperatorValueError(
1578
+ throw new import_js_repository3.InvalidOperatorValueError(
1539
1579
  "regexp",
1540
1580
  "a String or RegExp",
1541
1581
  cond.regexp
@@ -1543,11 +1583,11 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1543
1583
  }
1544
1584
  const flags = cond.flags || void 0;
1545
1585
  if (flags && typeof flags !== "string")
1546
- throw new import_js_repository10.InvalidArgumentError(
1586
+ throw new import_js_repository3.InvalidArgumentError(
1547
1587
  "RegExp flags must be a String, but %v given.",
1548
1588
  cond.flags
1549
1589
  );
1550
- opConds.push({ $regex: (0, import_js_repository6.stringToRegexp)(cond.regexp, flags) });
1590
+ opConds.push({ $regex: (0, import_js_repository3.stringToRegexp)(cond.regexp, flags) });
1551
1591
  }
1552
1592
  if (opConds.length === 1) {
1553
1593
  query[key] = opConds[0];
@@ -1574,10 +1614,10 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1574
1614
  const idValue = modelData[idPropName];
1575
1615
  if (idValue == null || idValue === "" || idValue === 0) {
1576
1616
  const pkType = this._getIdType(modelName);
1577
- if (pkType !== import_js_repository4.DataType.STRING && pkType !== import_js_repository4.DataType.ANY)
1578
- throw new import_js_repository10.InvalidArgumentError(
1617
+ if (pkType !== import_js_repository3.DataType.STRING && pkType !== import_js_repository3.DataType.ANY)
1618
+ throw new import_js_repository3.InvalidArgumentError(
1579
1619
  "MongoDB unable to generate primary keys of %s. Do provide your own value for the %v property or set property type to String.",
1580
- (0, import_js_repository5.capitalize)(pkType),
1620
+ (0, import_js_repository3.capitalize)(pkType),
1581
1621
  idPropName
1582
1622
  );
1583
1623
  delete modelData[idPropName];
@@ -1609,7 +1649,7 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1609
1649
  const table = this._getCollection(modelName);
1610
1650
  const { matchedCount } = await table.replaceOne({ _id: id }, tableData);
1611
1651
  if (matchedCount < 1)
1612
- throw new import_js_repository10.InvalidArgumentError("Identifier %v is not found.", String(id));
1652
+ throw new import_js_repository3.InvalidArgumentError("Identifier %v is not found.", String(id));
1613
1653
  const projection = this._buildProjection(
1614
1654
  modelName,
1615
1655
  filter && filter.fields
@@ -1631,10 +1671,10 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1631
1671
  idValue = this._coerceId(idValue);
1632
1672
  if (idValue == null || idValue === "" || idValue === 0) {
1633
1673
  const pkType = this._getIdType(modelName);
1634
- if (pkType !== import_js_repository4.DataType.STRING && pkType !== import_js_repository4.DataType.ANY)
1635
- throw new import_js_repository10.InvalidArgumentError(
1674
+ if (pkType !== import_js_repository3.DataType.STRING && pkType !== import_js_repository3.DataType.ANY)
1675
+ throw new import_js_repository3.InvalidArgumentError(
1636
1676
  "MongoDB unable to generate primary keys of %s. Do provide your own value for the %v property or set property type to String.",
1637
- (0, import_js_repository5.capitalize)(pkType),
1677
+ (0, import_js_repository3.capitalize)(pkType),
1638
1678
  idPropName
1639
1679
  );
1640
1680
  delete modelData[idPropName];
@@ -1692,7 +1732,7 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1692
1732
  const table = this._getCollection(modelName);
1693
1733
  const { matchedCount } = await table.updateOne({ _id: id }, { $set: tableData });
1694
1734
  if (matchedCount < 1)
1695
- throw new import_js_repository10.InvalidArgumentError("Identifier %v is not found.", String(id));
1735
+ throw new import_js_repository3.InvalidArgumentError("Identifier %v is not found.", String(id));
1696
1736
  const projection = this._buildProjection(
1697
1737
  modelName,
1698
1738
  filter && filter.fields
@@ -1736,7 +1776,7 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1736
1776
  );
1737
1777
  const patchedData = await table.findOne({ _id: id }, { projection });
1738
1778
  if (!patchedData)
1739
- throw new import_js_repository10.InvalidArgumentError("Identifier %v is not found.", String(id));
1779
+ throw new import_js_repository3.InvalidArgumentError("Identifier %v is not found.", String(id));
1740
1780
  return this._fromDatabase(modelName, patchedData);
1741
1781
  }
1742
1782
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e22m4u/js-repository-mongodb-adapter",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "MongoDB адаптер для @e22m4u/js-repository",
5
5
  "author": "Mikhail Evstropov <e22m4u@yandex.ru>",
6
6
  "license": "MIT",
@@ -42,25 +42,25 @@
42
42
  },
43
43
  "peerDependencies": {
44
44
  "@e22m4u/js-format": "~0.2.0",
45
- "@e22m4u/js-repository": "~0.6.0"
45
+ "@e22m4u/js-repository": "~0.6.3"
46
46
  },
47
47
  "devDependencies": {
48
48
  "@commitlint/cli": "~20.1.0",
49
49
  "@commitlint/config-conventional": "~20.0.0",
50
- "@eslint/js": "~9.38.0",
50
+ "@eslint/js": "~9.39.0",
51
51
  "c8": "~10.1.3",
52
52
  "chai": "~6.2.0",
53
53
  "chai-as-promised": "~8.0.2",
54
54
  "dotenv": "~17.2.3",
55
- "esbuild": "~0.25.11",
56
- "eslint": "~9.38.0",
55
+ "esbuild": "~0.25.12",
56
+ "eslint": "~9.39.0",
57
57
  "eslint-config-prettier": "~10.1.8",
58
58
  "eslint-plugin-chai-expect": "~3.1.0",
59
59
  "eslint-plugin-mocha": "~11.2.0",
60
- "globals": "~16.4.0",
60
+ "globals": "~16.5.0",
61
61
  "husky": "~9.1.7",
62
62
  "mocha": "~11.7.4",
63
63
  "prettier": "~3.6.2",
64
- "rimraf": "~6.0.1"
64
+ "rimraf": "~6.1.0"
65
65
  }
66
66
  }
@@ -1,21 +1,28 @@
1
1
  /* eslint no-unused-vars: 0 */
2
2
  import {ObjectId} from 'mongodb';
3
3
  import {MongoClient} from 'mongodb';
4
- import {isIsoDate} from './utils/index.js';
5
- import {isObjectId} from './utils/index.js';
6
- import {Adapter} from '@e22m4u/js-repository';
7
- import {DataType} from '@e22m4u/js-repository';
8
- import {capitalize} from '@e22m4u/js-repository';
9
- import {createMongodbUrl} from './utils/index.js';
10
4
  import {ServiceContainer} from '@e22m4u/js-service';
11
- import {transformValuesDeep} from './utils/index.js';
12
- import {stringToRegexp} from '@e22m4u/js-repository';
13
- import {selectObjectKeys} from '@e22m4u/js-repository';
14
- import {DefinitionRegistry} from '@e22m4u/js-repository';
15
- import {ModelDefinitionUtils} from '@e22m4u/js-repository';
16
- import {InvalidArgumentError} from '@e22m4u/js-repository';
17
- import {modelNameToCollectionName} from './utils/index.js';
18
- import {InvalidOperatorValueError} from '@e22m4u/js-repository';
5
+
6
+ import {
7
+ Adapter,
8
+ DataType,
9
+ likeToRegexp,
10
+ capitalize,
11
+ stringToRegexp,
12
+ selectObjectKeys,
13
+ DefinitionRegistry,
14
+ ModelDefinitionUtils,
15
+ InvalidArgumentError,
16
+ InvalidOperatorValueError,
17
+ } from '@e22m4u/js-repository';
18
+
19
+ import {
20
+ isIsoDate,
21
+ isObjectId,
22
+ createMongodbUrl,
23
+ transformValuesDeep,
24
+ modelNameToCollectionName,
25
+ } from './utils/index.js';
19
26
 
20
27
  /**
21
28
  * Mongodb option names.
@@ -630,7 +637,7 @@ export class MongodbAdapter extends Adapter {
630
637
  'a String or RegExp',
631
638
  cond.like,
632
639
  );
633
- opConds.push({$regex: stringToRegexp(cond.like)});
640
+ opConds.push({$regex: likeToRegexp(cond.like)});
634
641
  }
635
642
  // nlike
636
643
  if ('nlike' in cond) {
@@ -640,7 +647,7 @@ export class MongodbAdapter extends Adapter {
640
647
  'a String or RegExp',
641
648
  cond.nlike,
642
649
  );
643
- opConds.push({$not: stringToRegexp(cond.nlike)});
650
+ opConds.push({$not: likeToRegexp(cond.nlike)});
644
651
  }
645
652
  // ilike
646
653
  if ('ilike' in cond) {
@@ -650,7 +657,7 @@ export class MongodbAdapter extends Adapter {
650
657
  'a String or RegExp',
651
658
  cond.ilike,
652
659
  );
653
- opConds.push({$regex: stringToRegexp(cond.ilike, 'i')});
660
+ opConds.push({$regex: likeToRegexp(cond.ilike, true)});
654
661
  }
655
662
  // nilike
656
663
  if ('nilike' in cond) {
@@ -664,7 +671,7 @@ export class MongodbAdapter extends Adapter {
664
671
  cond.nilike,
665
672
  );
666
673
  }
667
- opConds.push({$not: stringToRegexp(cond.nilike, 'i')});
674
+ opConds.push({$not: likeToRegexp(cond.nilike, true)});
668
675
  }
669
676
  // regexp and flags (optional)
670
677
  if ('regexp' in cond) {
@@ -1164,39 +1164,63 @@ describe('MongodbAdapter', function () {
1164
1164
  });
1165
1165
 
1166
1166
  it('converts the "like" operator to "$regex"', async function () {
1167
- const input = {foo: {like: 'test'}};
1167
+ const input1 = {foo: {like: 'test'}};
1168
+ const input2 = {foo: {like: 'test%'}};
1169
+ const input3 = {foo: {like: 'test_'}};
1168
1170
  const schema = createSchema();
1169
1171
  schema.defineModel({name: 'model', datasource: 'mongodb'});
1170
1172
  const A = await schema.getService(AdapterRegistry).getAdapter('mongodb');
1171
- const res = A._buildQuery('model', input);
1172
- expect(res).to.be.eql({foo: {$regex: /test/}});
1173
+ const res1 = A._buildQuery('model', input1);
1174
+ const res2 = A._buildQuery('model', input2);
1175
+ const res3 = A._buildQuery('model', input3);
1176
+ expect(res1).to.be.eql({foo: {$regex: /^test$/}});
1177
+ expect(res2).to.be.eql({foo: {$regex: /^test.*$/}});
1178
+ expect(res3).to.be.eql({foo: {$regex: /^test.$/}});
1173
1179
  });
1174
1180
 
1175
1181
  it('converts the "nlike" operator to "$not"', async function () {
1176
- const input = {foo: {nlike: 'test'}};
1182
+ const input1 = {foo: {nlike: 'test'}};
1183
+ const input2 = {foo: {nlike: 'test%'}};
1184
+ const input3 = {foo: {nlike: 'test_'}};
1177
1185
  const schema = createSchema();
1178
1186
  schema.defineModel({name: 'model', datasource: 'mongodb'});
1179
1187
  const A = await schema.getService(AdapterRegistry).getAdapter('mongodb');
1180
- const res = A._buildQuery('model', input);
1181
- expect(res).to.be.eql({foo: {$not: /test/}});
1188
+ const res1 = A._buildQuery('model', input1);
1189
+ const res2 = A._buildQuery('model', input2);
1190
+ const res3 = A._buildQuery('model', input3);
1191
+ expect(res1).to.be.eql({foo: {$not: /^test$/}});
1192
+ expect(res2).to.be.eql({foo: {$not: /^test.*$/}});
1193
+ expect(res3).to.be.eql({foo: {$not: /^test.$/}});
1182
1194
  });
1183
1195
 
1184
1196
  it('converts the "ilike" operator to "$regex" with "i" flag', async function () {
1185
- const input = {foo: {ilike: 'test'}};
1197
+ const input1 = {foo: {ilike: 'test'}};
1198
+ const input2 = {foo: {ilike: 'test%'}};
1199
+ const input3 = {foo: {ilike: 'test_'}};
1186
1200
  const schema = createSchema();
1187
1201
  schema.defineModel({name: 'model', datasource: 'mongodb'});
1188
1202
  const A = await schema.getService(AdapterRegistry).getAdapter('mongodb');
1189
- const res = A._buildQuery('model', input);
1190
- expect(res).to.be.eql({foo: {$regex: /test/i}});
1203
+ const res1 = A._buildQuery('model', input1);
1204
+ const res2 = A._buildQuery('model', input2);
1205
+ const res3 = A._buildQuery('model', input3);
1206
+ expect(res1).to.be.eql({foo: {$regex: /^test$/i}});
1207
+ expect(res2).to.be.eql({foo: {$regex: /^test.*$/i}});
1208
+ expect(res3).to.be.eql({foo: {$regex: /^test.$/i}});
1191
1209
  });
1192
1210
 
1193
1211
  it('converts the "nilike" operator to "$not" with "i" flag', async function () {
1194
- const input = {foo: {nilike: 'test'}};
1212
+ const input1 = {foo: {nilike: 'test'}};
1213
+ const input2 = {foo: {nilike: 'test%'}};
1214
+ const input3 = {foo: {nilike: 'test_'}};
1195
1215
  const schema = createSchema();
1196
1216
  schema.defineModel({name: 'model', datasource: 'mongodb'});
1197
1217
  const A = await schema.getService(AdapterRegistry).getAdapter('mongodb');
1198
- const res = A._buildQuery('model', input);
1199
- expect(res).to.be.eql({foo: {$not: /test/i}});
1218
+ const res1 = A._buildQuery('model', input1);
1219
+ const res2 = A._buildQuery('model', input2);
1220
+ const res3 = A._buildQuery('model', input3);
1221
+ expect(res1).to.be.eql({foo: {$not: /^test$/i}});
1222
+ expect(res2).to.be.eql({foo: {$not: /^test.*$/i}});
1223
+ expect(res3).to.be.eql({foo: {$not: /^test.$/i}});
1200
1224
  });
1201
1225
 
1202
1226
  it('converts property value to an instance of ObjectId', async function () {
@@ -1442,10 +1466,10 @@ describe('MongodbAdapter', function () {
1442
1466
  {foo: {$nin: ['qwe']}},
1443
1467
  {foo: {$gte: 100, $lte: 200}},
1444
1468
  {foo: {$exists: true}},
1445
- {foo: {$regex: /asd/}},
1446
- {foo: {$not: /zxc/}},
1447
- {foo: {$regex: /rty/i}},
1448
- {foo: {$not: /fgh/i}},
1469
+ {foo: {$regex: /^asd$/}},
1470
+ {foo: {$not: /^zxc$/}},
1471
+ {foo: {$regex: /^rty$/i}},
1472
+ {foo: {$not: /^fgh$/i}},
1449
1473
  {foo: {$regex: /vbn/i}},
1450
1474
  ],
1451
1475
  };
@@ -1492,10 +1516,10 @@ describe('MongodbAdapter', function () {
1492
1516
  {foo: {$nin: ['qwe']}},
1493
1517
  {foo: {$gte: 100, $lte: 200}},
1494
1518
  {foo: {$exists: true}},
1495
- {foo: {$regex: /asd/}},
1496
- {foo: {$not: /zxc/}},
1497
- {foo: {$regex: /rty/i}},
1498
- {foo: {$not: /fgh/i}},
1519
+ {foo: {$regex: /^asd$/}},
1520
+ {foo: {$not: /^zxc$/}},
1521
+ {foo: {$regex: /^rty$/i}},
1522
+ {foo: {$not: /^fgh$/i}},
1499
1523
  {foo: {$regex: /vbn/i}},
1500
1524
  ],
1501
1525
  };
@@ -3814,7 +3838,7 @@ describe('MongodbAdapter', function () {
3814
3838
  const id4 = input4[DEF_PK];
3815
3839
  const result = await rep.patch(
3816
3840
  {foo: 'test'},
3817
- {foo: {like: 'sit amet'}},
3841
+ {foo: {like: '%sit amet%'}},
3818
3842
  );
3819
3843
  expect(result).to.be.eq(2);
3820
3844
  const rawData = await MDB_CLIENT.db()
@@ -3843,7 +3867,7 @@ describe('MongodbAdapter', function () {
3843
3867
  const id4 = input4[DEF_PK];
3844
3868
  const result = await rep.patch(
3845
3869
  {foo: 'test'},
3846
- {foo: {nlike: 'sit amet'}},
3870
+ {foo: {nlike: '%sit amet%'}},
3847
3871
  );
3848
3872
  expect(result).to.be.eq(2);
3849
3873
  const rawData = await MDB_CLIENT.db()
@@ -3872,7 +3896,7 @@ describe('MongodbAdapter', function () {
3872
3896
  const id4 = input4[DEF_PK];
3873
3897
  const result = await rep.patch(
3874
3898
  {foo: 'test'},
3875
- {foo: {ilike: 'sit amet'}},
3899
+ {foo: {ilike: '%sit amet%'}},
3876
3900
  );
3877
3901
  expect(result).to.be.eq(3);
3878
3902
  const rawData = await MDB_CLIENT.db()
@@ -3901,7 +3925,7 @@ describe('MongodbAdapter', function () {
3901
3925
  const id4 = input4[DEF_PK];
3902
3926
  const result = await rep.patch(
3903
3927
  {foo: 'test'},
3904
- {foo: {nilike: 'sit amet'}},
3928
+ {foo: {nilike: '%sit amet%'}},
3905
3929
  );
3906
3930
  expect(result).to.be.eq(1);
3907
3931
  const rawData = await MDB_CLIENT.db()
@@ -4703,7 +4727,7 @@ describe('MongodbAdapter', function () {
4703
4727
  const created2 = await rep.create({foo: 'dolor sit amet'});
4704
4728
  await rep.create({foo: 'DOLOR SIT AMET'});
4705
4729
  const created4 = await rep.create({foo: 'sit amet'});
4706
- const result = await rep.find({where: {foo: {like: 'sit amet'}}});
4730
+ const result = await rep.find({where: {foo: {like: '%sit amet%'}}});
4707
4731
  expect(result).to.be.eql([
4708
4732
  {[DEF_PK]: created2[DEF_PK], foo: 'dolor sit amet'},
4709
4733
  {[DEF_PK]: created4[DEF_PK], foo: 'sit amet'},
@@ -4718,7 +4742,7 @@ describe('MongodbAdapter', function () {
4718
4742
  const created2 = await rep.create({foo: 'dolor sit amet'});
4719
4743
  const created3 = await rep.create({foo: 'DOLOR SIT AMET'});
4720
4744
  const created4 = await rep.create({foo: 'sit amet'});
4721
- const result = await rep.find({where: {foo: {ilike: 'sit amet'}}});
4745
+ const result = await rep.find({where: {foo: {ilike: '%sit amet%'}}});
4722
4746
  expect(result).to.be.eql([
4723
4747
  {[DEF_PK]: created2[DEF_PK], foo: 'dolor sit amet'},
4724
4748
  {[DEF_PK]: created3[DEF_PK], foo: 'DOLOR SIT AMET'},
@@ -4734,7 +4758,7 @@ describe('MongodbAdapter', function () {
4734
4758
  await rep.create({foo: 'dolor sit amet'});
4735
4759
  const created3 = await rep.create({foo: 'DOLOR SIT AMET'});
4736
4760
  await rep.create({foo: 'sit amet'});
4737
- const result = await rep.find({where: {foo: {nlike: 'sit amet'}}});
4761
+ const result = await rep.find({where: {foo: {nlike: '%sit amet%'}}});
4738
4762
  expect(result).to.be.eql([
4739
4763
  {[DEF_PK]: created1[DEF_PK], foo: 'lorem ipsum'},
4740
4764
  {[DEF_PK]: created3[DEF_PK], foo: 'DOLOR SIT AMET'},
@@ -4749,7 +4773,7 @@ describe('MongodbAdapter', function () {
4749
4773
  await rep.create({foo: 'dolor sit amet'});
4750
4774
  await rep.create({foo: 'DOLOR SIT AMET'});
4751
4775
  await rep.create({foo: 'sit amet'});
4752
- const result = await rep.find({where: {foo: {nilike: 'sit amet'}}});
4776
+ const result = await rep.find({where: {foo: {nilike: '%sit amet%'}}});
4753
4777
  expect(result).to.be.eql([
4754
4778
  {[DEF_PK]: created1[DEF_PK], foo: 'lorem ipsum'},
4755
4779
  ]);
@@ -5077,7 +5101,7 @@ describe('MongodbAdapter', function () {
5077
5101
  await rep.create({foo: 'dolor sit amet'});
5078
5102
  const created3 = await rep.create({foo: 'DOLOR SIT AMET'});
5079
5103
  await rep.create({foo: 'sit amet'});
5080
- const result = await rep.delete({foo: {like: 'sit amet'}});
5104
+ const result = await rep.delete({foo: {like: '%sit amet%'}});
5081
5105
  expect(result).to.be.eq(2);
5082
5106
  const rawData = await MDB_CLIENT.db()
5083
5107
  .collection('models')
@@ -5097,7 +5121,7 @@ describe('MongodbAdapter', function () {
5097
5121
  const created2 = await rep.create({foo: 'dolor sit amet'});
5098
5122
  await rep.create({foo: 'DOLOR SIT AMET'});
5099
5123
  const created4 = await rep.create({foo: 'sit amet'});
5100
- const result = await rep.delete({foo: {nlike: 'sit amet'}});
5124
+ const result = await rep.delete({foo: {nlike: '%sit amet%'}});
5101
5125
  expect(result).to.be.eq(2);
5102
5126
  const rawData = await MDB_CLIENT.db()
5103
5127
  .collection('models')
@@ -5117,7 +5141,7 @@ describe('MongodbAdapter', function () {
5117
5141
  await rep.create({foo: 'dolor sit amet'});
5118
5142
  await rep.create({foo: 'DOLOR SIT AMET'});
5119
5143
  await rep.create({foo: 'sit amet'});
5120
- const result = await rep.delete({foo: {ilike: 'sit amet'}});
5144
+ const result = await rep.delete({foo: {ilike: '%sit amet%'}});
5121
5145
  expect(result).to.be.eq(3);
5122
5146
  const rawData = await MDB_CLIENT.db()
5123
5147
  .collection('models')
@@ -5136,7 +5160,7 @@ describe('MongodbAdapter', function () {
5136
5160
  const created2 = await rep.create({foo: 'dolor sit amet'});
5137
5161
  const created3 = await rep.create({foo: 'DOLOR SIT AMET'});
5138
5162
  const created4 = await rep.create({foo: 'sit amet'});
5139
- const result = await rep.delete({foo: {nilike: 'sit amet'}});
5163
+ const result = await rep.delete({foo: {nilike: '%sit amet%'}});
5140
5164
  expect(result).to.be.eq(1);
5141
5165
  const rawData = await MDB_CLIENT.db()
5142
5166
  .collection('models')
@@ -5401,7 +5425,7 @@ describe('MongodbAdapter', function () {
5401
5425
  await rep.create({foo: 'dolor sit amet'});
5402
5426
  await rep.create({foo: 'DOLOR SIT AMET'});
5403
5427
  await rep.create({foo: 'sit amet'});
5404
- const result = await rep.count({foo: {like: 'sit amet'}});
5428
+ const result = await rep.count({foo: {like: '%sit amet%'}});
5405
5429
  expect(result).to.be.eql(2);
5406
5430
  });
5407
5431
 
@@ -5413,7 +5437,7 @@ describe('MongodbAdapter', function () {
5413
5437
  await rep.create({foo: 'dolor sit amet'});
5414
5438
  await rep.create({foo: 'DOLOR SIT AMET'});
5415
5439
  await rep.create({foo: 'sit amet'});
5416
- const result = await rep.count({foo: {nlike: 'sit amet'}});
5440
+ const result = await rep.count({foo: {nlike: '%sit amet%'}});
5417
5441
  expect(result).to.be.eql(2);
5418
5442
  });
5419
5443
 
@@ -5425,7 +5449,7 @@ describe('MongodbAdapter', function () {
5425
5449
  await rep.create({foo: 'dolor sit amet'});
5426
5450
  await rep.create({foo: 'DOLOR SIT AMET'});
5427
5451
  await rep.create({foo: 'sit amet'});
5428
- const result = await rep.count({foo: {ilike: 'sit amet'}});
5452
+ const result = await rep.count({foo: {ilike: '%sit amet%'}});
5429
5453
  expect(result).to.be.eql(3);
5430
5454
  });
5431
5455
 
@@ -5437,7 +5461,7 @@ describe('MongodbAdapter', function () {
5437
5461
  await rep.create({foo: 'dolor sit amet'});
5438
5462
  await rep.create({foo: 'DOLOR SIT AMET'});
5439
5463
  await rep.create({foo: 'sit amet'});
5440
- const result = await rep.count({foo: {nilike: 'sit amet'}});
5464
+ const result = await rep.count({foo: {nilike: '%sit amet%'}});
5441
5465
  expect(result).to.be.eql(1);
5442
5466
  });
5443
5467