index.cjs 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
  6. var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
  7. var __export = (target, all) => {
  8. for (var name in all)
  9. __defProp(target, name, { get: all[name], enumerable: true });
  10. };
  11. var __copyProps = (to, from, except, desc) => {
  12. if (from && typeof from === "object" || typeof from === "function") {
  13. for (let key of __getOwnPropNames(from))
  14. if (!__hasOwnProp.call(to, key) && key !== except)
  15. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  16. }
  17. return to;
  18. };
  19. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  20. var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
  21. // src/index.js
  22. var index_exports = {};
  23. __export(index_exports, {
  24. MongodbAdapter: () => MongodbAdapter
  25. });
  26. module.exports = __toCommonJS(index_exports);
  27. // src/mongodb-adapter.js
  28. var import_mongodb2 = require("mongodb");
  29. // node_modules/@e22m4u/js-service/src/errors/invalid-argument-error.js
  30. var import_js_format = require("@e22m4u/js-format");
  31. var _InvalidArgumentError = class _InvalidArgumentError extends import_js_format.Errorf {
  32. };
  33. __name(_InvalidArgumentError, "InvalidArgumentError");
  34. var InvalidArgumentError = _InvalidArgumentError;
  35. // node_modules/@e22m4u/js-service/src/service-container.js
  36. var SERVICE_CONTAINER_CLASS_NAME = "ServiceContainer";
  37. var _ServiceContainer = class _ServiceContainer {
  38. /**
  39. * Services map.
  40. *
  41. * @type {Map<any, any>}
  42. * @private
  43. */
  44. _services = /* @__PURE__ */ new Map();
  45. /**
  46. * Parent container.
  47. *
  48. * @type {ServiceContainer}
  49. * @private
  50. */
  51. _parent;
  52. /**
  53. * Constructor.
  54. *
  55. * @param {ServiceContainer|undefined} parent
  56. */
  57. constructor(parent = void 0) {
  58. if (parent != null) {
  59. if (!(parent instanceof _ServiceContainer))
  60. throw new InvalidArgumentError(
  61. 'The provided parameter "parent" of ServicesContainer.constructor must be an instance ServiceContainer, but %v given.',
  62. parent
  63. );
  64. this._parent = parent;
  65. }
  66. }
  67. /**
  68. * Получить родительский сервис-контейнер или выбросить ошибку.
  69. *
  70. * @returns {ServiceContainer}
  71. */
  72. getParent() {
  73. if (!this._parent)
  74. throw new InvalidArgumentError("The service container has no parent.");
  75. return this._parent;
  76. }
  77. /**
  78. * Проверить наличие родительского сервис-контейнера.
  79. *
  80. * @returns {boolean}
  81. */
  82. hasParent() {
  83. return Boolean(this._parent);
  84. }
  85. /**
  86. * Получить существующий или новый экземпляр.
  87. *
  88. * @param {*} ctor
  89. * @param {*} args
  90. * @returns {*}
  91. */
  92. get(ctor, ...args) {
  93. if (!ctor || typeof ctor !== "function")
  94. throw new InvalidArgumentError(
  95. "The first argument of ServicesContainer.get must be a class constructor, but %v given.",
  96. ctor
  97. );
  98. const isCtorRegistered = this._services.has(ctor);
  99. let service = this._services.get(ctor);
  100. let inheritedCtor = void 0;
  101. if (!service) {
  102. const ctors = Array.from(this._services.keys());
  103. inheritedCtor = ctors.find((v) => v.prototype instanceof ctor);
  104. if (inheritedCtor) service = this._services.get(inheritedCtor);
  105. }
  106. if (!service && !isCtorRegistered && !inheritedCtor && this._parent && this._parent.has(ctor)) {
  107. return this._parent.get(ctor, ...args);
  108. }
  109. if (!isCtorRegistered && inheritedCtor) {
  110. ctor = inheritedCtor;
  111. }
  112. if (!service || args.length) {
  113. service = Array.isArray(ctor.kinds) && ctor.kinds.includes(SERVICE_CLASS_NAME) ? new ctor(this, ...args) : new ctor(...args);
  114. this._services.set(ctor, service);
  115. } else if (typeof service === "function") {
  116. service = service();
  117. this._services.set(ctor, service);
  118. }
  119. return service;
  120. }
  121. /**
  122. * Получить существующий или новый экземпляр,
  123. * только если конструктор зарегистрирован.
  124. *
  125. * @param {*} ctor
  126. * @param {*} args
  127. * @returns {*}
  128. */
  129. getRegistered(ctor, ...args) {
  130. if (!this.has(ctor))
  131. throw new InvalidArgumentError(
  132. "The constructor %v is not registered.",
  133. ctor
  134. );
  135. return this.get(ctor, ...args);
  136. }
  137. /**
  138. * Проверить существование конструктора в контейнере.
  139. *
  140. * @param {*} ctor
  141. * @returns {boolean}
  142. */
  143. has(ctor) {
  144. if (this._services.has(ctor)) return true;
  145. const ctors = Array.from(this._services.keys());
  146. const inheritedCtor = ctors.find((v) => v.prototype instanceof ctor);
  147. if (inheritedCtor) return true;
  148. if (this._parent) return this._parent.has(ctor);
  149. return false;
  150. }
  151. /**
  152. * Добавить конструктор в контейнер.
  153. *
  154. * @param {*} ctor
  155. * @param {*} args
  156. * @returns {this}
  157. */
  158. add(ctor, ...args) {
  159. if (!ctor || typeof ctor !== "function")
  160. throw new InvalidArgumentError(
  161. "The first argument of ServicesContainer.add must be a class constructor, but %v given.",
  162. ctor
  163. );
  164. const factory = /* @__PURE__ */ __name(() => Array.isArray(ctor.kinds) && ctor.kinds.includes(SERVICE_CLASS_NAME) ? new ctor(this, ...args) : new ctor(...args), "factory");
  165. this._services.set(ctor, factory);
  166. return this;
  167. }
  168. /**
  169. * Добавить конструктор и создать экземпляр.
  170. *
  171. * @param {*} ctor
  172. * @param {*} args
  173. * @returns {this}
  174. */
  175. use(ctor, ...args) {
  176. if (!ctor || typeof ctor !== "function")
  177. throw new InvalidArgumentError(
  178. "The first argument of ServicesContainer.use must be a class constructor, but %v given.",
  179. ctor
  180. );
  181. const service = Array.isArray(ctor.kinds) && ctor.kinds.includes(SERVICE_CLASS_NAME) ? new ctor(this, ...args) : new ctor(...args);
  182. this._services.set(ctor, service);
  183. return this;
  184. }
  185. /**
  186. * Добавить конструктор и связанный экземпляр.
  187. *
  188. * @param {*} ctor
  189. * @param {*} service
  190. * @returns {this}
  191. */
  192. set(ctor, service) {
  193. if (!ctor || typeof ctor !== "function")
  194. throw new InvalidArgumentError(
  195. "The first argument of ServicesContainer.set must be a class constructor, but %v given.",
  196. ctor
  197. );
  198. if (!service || typeof service !== "object" || Array.isArray(service))
  199. throw new InvalidArgumentError(
  200. "The second argument of ServicesContainer.set must be an Object, but %v given.",
  201. service
  202. );
  203. this._services.set(ctor, service);
  204. return this;
  205. }
  206. /**
  207. * Найти сервис удовлетворяющий условию.
  208. *
  209. * @param {function(Function, ServiceContainer): boolean} predicate
  210. * @param {boolean} noParent
  211. * @returns {*}
  212. */
  213. find(predicate, noParent = false) {
  214. if (typeof predicate !== "function") {
  215. throw new InvalidArgumentError(
  216. "The first argument of ServiceContainer.find must be a function, but %v given.",
  217. predicate
  218. );
  219. }
  220. const isRecursive = !noParent;
  221. let currentContainer = this;
  222. do {
  223. for (const ctor of currentContainer._services.keys()) {
  224. if (predicate(ctor, currentContainer) === true) {
  225. return this.get(ctor);
  226. }
  227. }
  228. if (isRecursive && currentContainer.hasParent()) {
  229. currentContainer = currentContainer.getParent();
  230. } else {
  231. currentContainer = null;
  232. }
  233. } while (currentContainer);
  234. return void 0;
  235. }
  236. };
  237. __name(_ServiceContainer, "ServiceContainer");
  238. /**
  239. * Kinds.
  240. *
  241. * @type {string[]}
  242. */
  243. __publicField(_ServiceContainer, "kinds", [SERVICE_CONTAINER_CLASS_NAME]);
  244. var ServiceContainer = _ServiceContainer;
  245. // node_modules/@e22m4u/js-service/src/utils/is-service-container.js
  246. function isServiceContainer(container) {
  247. return Boolean(
  248. container && typeof container === "object" && typeof container.constructor === "function" && Array.isArray(container.constructor.kinds) && container.constructor.kinds.includes(SERVICE_CONTAINER_CLASS_NAME)
  249. );
  250. }
  251. __name(isServiceContainer, "isServiceContainer");
  252. // node_modules/@e22m4u/js-service/src/service.js
  253. var SERVICE_CLASS_NAME = "Service";
  254. var _Service = class _Service {
  255. /**
  256. * Container.
  257. *
  258. * @type {ServiceContainer}
  259. */
  260. container;
  261. /**
  262. * Constructor.
  263. *
  264. * @param {ServiceContainer|undefined} container
  265. */
  266. constructor(container = void 0) {
  267. this.container = isServiceContainer(container) ? container : new ServiceContainer();
  268. }
  269. /**
  270. * Получить существующий или новый экземпляр.
  271. *
  272. * @param {*} ctor
  273. * @param {*} args
  274. * @returns {*}
  275. */
  276. getService(ctor, ...args) {
  277. return this.container.get(ctor, ...args);
  278. }
  279. /**
  280. * Получить существующий или новый экземпляр,
  281. * только если конструктор зарегистрирован.
  282. *
  283. * @param {*} ctor
  284. * @param {*} args
  285. * @returns {*}
  286. */
  287. getRegisteredService(ctor, ...args) {
  288. return this.container.getRegistered(ctor, ...args);
  289. }
  290. /**
  291. * Проверка существования конструктора в контейнере.
  292. *
  293. * @param {*} ctor
  294. * @returns {boolean}
  295. */
  296. hasService(ctor) {
  297. return this.container.has(ctor);
  298. }
  299. /**
  300. * Добавить конструктор в контейнер.
  301. *
  302. * @param {*} ctor
  303. * @param {*} args
  304. * @returns {this}
  305. */
  306. addService(ctor, ...args) {
  307. this.container.add(ctor, ...args);
  308. return this;
  309. }
  310. /**
  311. * Добавить конструктор и создать экземпляр.
  312. *
  313. * @param {*} ctor
  314. * @param {*} args
  315. * @returns {this}
  316. */
  317. useService(ctor, ...args) {
  318. this.container.use(ctor, ...args);
  319. return this;
  320. }
  321. /**
  322. * Добавить конструктор и связанный экземпляр.
  323. *
  324. * @param {*} ctor
  325. * @param {*} service
  326. * @returns {this}
  327. */
  328. setService(ctor, service) {
  329. this.container.set(ctor, service);
  330. return this;
  331. }
  332. /**
  333. * Найти сервис удовлетворяющий условию.
  334. *
  335. * @param {function(Function, ServiceContainer): boolean} predicate
  336. * @param {boolean} noParent
  337. * @returns {*}
  338. */
  339. findService(predicate, noParent = false) {
  340. return this.container.find(predicate, noParent);
  341. }
  342. };
  343. __name(_Service, "Service");
  344. /**
  345. * Kinds.
  346. *
  347. * @type {string[]}
  348. */
  349. __publicField(_Service, "kinds", [SERVICE_CLASS_NAME]);
  350. var Service = _Service;
  351. // node_modules/@e22m4u/js-debug/src/utils/to-camel-case.js
  352. function toCamelCase(input) {
  353. return input.replace(/(^\w|[A-Z]|\b\w)/g, (c) => c.toUpperCase()).replace(/\W+/g, "").replace(/(^\w)/g, (c) => c.toLowerCase());
  354. }
  355. __name(toCamelCase, "toCamelCase");
  356. // node_modules/@e22m4u/js-debug/src/utils/is-non-array-object.js
  357. function isNonArrayObject(input) {
  358. return Boolean(input && typeof input === "object" && !Array.isArray(input));
  359. }
  360. __name(isNonArrayObject, "isNonArrayObject");
  361. // node_modules/@e22m4u/js-debug/src/utils/generate-random-hex.js
  362. function generateRandomHex(length = 4) {
  363. if (length <= 0) {
  364. return "";
  365. }
  366. const firstCharCandidates = "abcdef";
  367. const restCharCandidates = "0123456789abcdef";
  368. let result = "";
  369. const firstCharIndex = Math.floor(Math.random() * firstCharCandidates.length);
  370. result += firstCharCandidates[firstCharIndex];
  371. for (let i = 1; i < length; i++) {
  372. const randomIndex = Math.floor(Math.random() * restCharCandidates.length);
  373. result += restCharCandidates[randomIndex];
  374. }
  375. return result;
  376. }
  377. __name(generateRandomHex, "generateRandomHex");
  378. // node_modules/@e22m4u/js-debug/src/create-debugger.js
  379. var import_js_format2 = require("@e22m4u/js-format");
  380. // node_modules/@e22m4u/js-debug/src/create-colorized-dump.js
  381. var import_util = require("util");
  382. var INSPECT_OPTIONS = {
  383. showHidden: false,
  384. depth: null,
  385. colors: true,
  386. compact: false
  387. };
  388. function createColorizedDump(value) {
  389. return (0, import_util.inspect)(value, INSPECT_OPTIONS);
  390. }
  391. __name(createColorizedDump, "createColorizedDump");
  392. // node_modules/@e22m4u/js-debug/src/create-debugger.js
  393. var AVAILABLE_COLORS = [
  394. 20,
  395. 21,
  396. 26,
  397. 27,
  398. 32,
  399. 33,
  400. 38,
  401. 39,
  402. 40,
  403. 41,
  404. 42,
  405. 43,
  406. 44,
  407. 45,
  408. 56,
  409. 57,
  410. 62,
  411. 63,
  412. 68,
  413. 69,
  414. 74,
  415. 75,
  416. 76,
  417. 77,
  418. 78,
  419. 79,
  420. 80,
  421. 81,
  422. 92,
  423. 93,
  424. 98,
  425. 99,
  426. 112,
  427. 113,
  428. 128,
  429. 129,
  430. 134,
  431. 135,
  432. 148,
  433. 149,
  434. 160,
  435. 161,
  436. 162,
  437. 163,
  438. 164,
  439. 165,
  440. 166,
  441. 167,
  442. 168,
  443. 169,
  444. 170,
  445. 171,
  446. 172,
  447. 173,
  448. 178,
  449. 179,
  450. 184,
  451. 185,
  452. 196,
  453. 197,
  454. 198,
  455. 199,
  456. 200,
  457. 201,
  458. 202,
  459. 203,
  460. 204,
  461. 205,
  462. 206,
  463. 207,
  464. 208,
  465. 209,
  466. 214,
  467. 215,
  468. 220,
  469. 221
  470. ];
  471. var DEFAULT_OFFSET_STEP_SPACES = 2;
  472. function pickColorCode(input) {
  473. if (typeof input !== "string")
  474. throw new import_js_format2.Errorf(
  475. 'The parameter "input" of the function pickColorCode must be a String, but %v given.',
  476. input
  477. );
  478. let hash = 0;
  479. for (let i = 0; i < input.length; i++) {
  480. hash = (hash << 5) - hash + input.charCodeAt(i);
  481. hash |= 0;
  482. }
  483. return AVAILABLE_COLORS[Math.abs(hash) % AVAILABLE_COLORS.length];
  484. }
  485. __name(pickColorCode, "pickColorCode");
  486. function wrapStringByColorCode(input, color) {
  487. if (typeof input !== "string")
  488. throw new import_js_format2.Errorf(
  489. 'The parameter "input" of the function wrapStringByColorCode must be a String, but %v given.',
  490. input
  491. );
  492. if (typeof color !== "number")
  493. throw new import_js_format2.Errorf(
  494. 'The parameter "color" of the function wrapStringByColorCode must be a Number, but %v given.',
  495. color
  496. );
  497. const colorCode = "\x1B[3" + (Number(color) < 8 ? color : "8;5;" + color);
  498. return `${colorCode};1m${input}\x1B[0m`;
  499. }
  500. __name(wrapStringByColorCode, "wrapStringByColorCode");
  501. function matchPattern(pattern, input) {
  502. if (typeof pattern !== "string")
  503. throw new import_js_format2.Errorf(
  504. 'The parameter "pattern" of the function matchPattern must be a String, but %v given.',
  505. pattern
  506. );
  507. if (typeof input !== "string")
  508. throw new import_js_format2.Errorf(
  509. 'The parameter "input" of the function matchPattern must be a String, but %v given.',
  510. input
  511. );
  512. const regexpStr = pattern.replace(/\*/g, ".*?");
  513. const regexp = new RegExp("^" + regexpStr + "$");
  514. return regexp.test(input);
  515. }
  516. __name(matchPattern, "matchPattern");
  517. function createDebugger(namespaceOrOptions = void 0, ...namespaceSegments) {
  518. if (namespaceOrOptions && typeof namespaceOrOptions !== "string" && !isNonArrayObject(namespaceOrOptions)) {
  519. throw new import_js_format2.Errorf(
  520. 'The parameter "namespace" of the function createDebugger must be a String or an Object, but %v given.',
  521. namespaceOrOptions
  522. );
  523. }
  524. const withCustomState = isNonArrayObject(namespaceOrOptions);
  525. const state = withCustomState ? namespaceOrOptions : {};
  526. state.envNsSegments = Array.isArray(state.envNsSegments) ? state.envNsSegments : [];
  527. state.nsSegments = Array.isArray(state.nsSegments) ? state.nsSegments : [];
  528. state.pattern = typeof state.pattern === "string" ? state.pattern : "";
  529. state.hash = typeof state.hash === "string" ? state.hash : "";
  530. state.offsetSize = typeof state.offsetSize === "number" ? state.offsetSize : 0;
  531. state.offsetStep = typeof state.offsetStep !== "string" ? " ".repeat(DEFAULT_OFFSET_STEP_SPACES) : state.offsetStep;
  532. state.delimiter = state.delimiter && typeof state.delimiter === "string" ? state.delimiter : ":";
  533. if (!withCustomState) {
  534. if (typeof process !== "undefined" && process.env && process.env["DEBUGGER_NAMESPACE"]) {
  535. state.envNsSegments.push(process.env.DEBUGGER_NAMESPACE);
  536. }
  537. if (typeof namespaceOrOptions === "string")
  538. state.nsSegments.push(namespaceOrOptions);
  539. }
  540. namespaceSegments.forEach((segment) => {
  541. if (!segment || typeof segment !== "string")
  542. throw new import_js_format2.Errorf(
  543. "Namespace segment must be a non-empty String, but %v given.",
  544. segment
  545. );
  546. state.nsSegments.push(segment);
  547. });
  548. if (typeof process !== "undefined" && process.env && process.env["DEBUG"]) {
  549. state.pattern = process.env["DEBUG"];
  550. } else if (typeof localStorage !== "undefined" && typeof localStorage.getItem("debug") === "string") {
  551. state.pattern = localStorage.getItem("debug");
  552. }
  553. const isDebuggerEnabled = /* @__PURE__ */ __name(() => {
  554. const nsStr = [...state.envNsSegments, ...state.nsSegments].join(
  555. state.delimiter
  556. );
  557. const patterns = state.pattern.split(/[\s,]+/).filter((p) => p.length > 0);
  558. if (patterns.length === 0 && state.pattern !== "*") return false;
  559. for (const singlePattern of patterns) {
  560. if (matchPattern(singlePattern, nsStr)) return true;
  561. }
  562. return false;
  563. }, "isDebuggerEnabled");
  564. const getPrefix = /* @__PURE__ */ __name(() => {
  565. let tokens = [];
  566. [...state.envNsSegments, ...state.nsSegments, state.hash].filter(Boolean).forEach((token) => {
  567. const extractedTokens = token.split(state.delimiter).filter(Boolean);
  568. tokens = [...tokens, ...extractedTokens];
  569. });
  570. let res = tokens.reduce((acc, token, index) => {
  571. const isLast = tokens.length - 1 === index;
  572. const tokenColor = pickColorCode(token);
  573. acc += wrapStringByColorCode(token, tokenColor);
  574. if (!isLast) acc += state.delimiter;
  575. return acc;
  576. }, "");
  577. if (state.offsetSize > 0) res += state.offsetStep.repeat(state.offsetSize);
  578. return res;
  579. }, "getPrefix");
  580. function debugFn(messageOrData, ...args) {
  581. if (!isDebuggerEnabled()) return;
  582. const prefix = getPrefix();
  583. const multiString = (0, import_js_format2.format)(messageOrData, ...args);
  584. const rows = multiString.split("\n");
  585. rows.forEach((message) => {
  586. prefix ? console.log(`${prefix} ${message}`) : console.log(message);
  587. });
  588. }
  589. __name(debugFn, "debugFn");
  590. debugFn.withNs = function(namespace, ...args) {
  591. const stateCopy = JSON.parse(JSON.stringify(state));
  592. [namespace, ...args].forEach((ns) => {
  593. if (!ns || typeof ns !== "string")
  594. throw new import_js_format2.Errorf(
  595. "Debugger namespace must be a non-empty String, but %v given.",
  596. ns
  597. );
  598. stateCopy.nsSegments.push(ns);
  599. });
  600. return createDebugger(stateCopy);
  601. };
  602. debugFn.withHash = function(hashLength = 4) {
  603. const stateCopy = JSON.parse(JSON.stringify(state));
  604. if (!hashLength || typeof hashLength !== "number" || hashLength < 1) {
  605. throw new import_js_format2.Errorf(
  606. "Debugger hash must be a positive Number, but %v given.",
  607. hashLength
  608. );
  609. }
  610. stateCopy.hash = generateRandomHex(hashLength);
  611. return createDebugger(stateCopy);
  612. };
  613. debugFn.withOffset = function(offsetSize) {
  614. const stateCopy = JSON.parse(JSON.stringify(state));
  615. if (!offsetSize || typeof offsetSize !== "number" || offsetSize < 1) {
  616. throw new import_js_format2.Errorf(
  617. "Debugger offset must be a positive Number, but %v given.",
  618. offsetSize
  619. );
  620. }
  621. stateCopy.offsetSize = offsetSize;
  622. return createDebugger(stateCopy);
  623. };
  624. debugFn.withoutEnvNs = function() {
  625. const stateCopy = JSON.parse(JSON.stringify(state));
  626. stateCopy.envNsSegments = [];
  627. return createDebugger(stateCopy);
  628. };
  629. debugFn.inspect = function(valueOrDesc, ...args) {
  630. if (!isDebuggerEnabled()) return;
  631. const prefix = getPrefix();
  632. let multiString = "";
  633. if (typeof valueOrDesc === "string" && args.length) {
  634. multiString += `${valueOrDesc}
  635. `;
  636. const multilineDump = args.map((v) => createColorizedDump(v)).join("\n");
  637. const dumpRows = multilineDump.split("\n");
  638. multiString += dumpRows.map((v) => `${state.offsetStep}${v}`).join("\n");
  639. } else {
  640. multiString += [valueOrDesc, ...args].map((v) => createColorizedDump(v)).join("\n");
  641. }
  642. const rows = multiString.split("\n");
  643. rows.forEach((message) => {
  644. prefix ? console.log(`${prefix} ${message}`) : console.log(message);
  645. });
  646. };
  647. debugFn.state = state;
  648. return debugFn;
  649. }
  650. __name(createDebugger, "createDebugger");
  651. // node_modules/@e22m4u/js-debug/src/debuggable.js
  652. var _Debuggable = class _Debuggable {
  653. /**
  654. * Debug.
  655. *
  656. * @type {*}
  657. */
  658. debug;
  659. /**
  660. * Ctor Debug.
  661. *
  662. * @type {Function}
  663. */
  664. ctorDebug;
  665. /**
  666. * Возвращает функцию-отладчик с сегментом пространства имен
  667. * указанного в параметре метода.
  668. *
  669. * @param {Function} method
  670. * @returns {Function}
  671. */
  672. getDebuggerFor(method) {
  673. const name = method.name || "anonymous";
  674. return this.debug.withHash().withNs(name);
  675. }
  676. /**
  677. * Constructor.
  678. *
  679. * @param {DebuggableOptions} [options]
  680. */
  681. constructor(options = void 0) {
  682. const className = toCamelCase(this.constructor.name);
  683. options = typeof options === "object" && options || {};
  684. const namespace = options.namespace && String(options.namespace) || void 0;
  685. if (namespace) {
  686. this.debug = createDebugger(namespace, className);
  687. } else {
  688. this.debug = createDebugger(className);
  689. }
  690. const noEnvironmentNamespace = Boolean(options.noEnvironmentNamespace);
  691. if (noEnvironmentNamespace) this.debug = this.debug.withoutEnvNs();
  692. this.ctorDebug = this.debug.withNs("constructor").withHash();
  693. const noInstantiationMessage = Boolean(options.noInstantiationMessage);
  694. if (!noInstantiationMessage)
  695. this.ctorDebug(_Debuggable.INSTANTIATION_MESSAGE);
  696. }
  697. };
  698. __name(_Debuggable, "Debuggable");
  699. /**
  700. * Instantiation message.
  701. *
  702. * @type {string}
  703. */
  704. __publicField(_Debuggable, "INSTANTIATION_MESSAGE", "Instantiated.");
  705. var Debuggable = _Debuggable;
  706. // node_modules/@e22m4u/js-service/src/debuggable-service.js
  707. var _DebuggableService = class _DebuggableService extends Debuggable {
  708. /**
  709. * Service.
  710. *
  711. * @type {Service}
  712. */
  713. _service;
  714. /**
  715. * Container.
  716. *
  717. * @type {ServiceContainer}
  718. */
  719. get container() {
  720. return this._service.container;
  721. }
  722. /**
  723. * Получить существующий или новый экземпляр.
  724. *
  725. * @type {Service['getService']}
  726. */
  727. get getService() {
  728. return this._service.getService;
  729. }
  730. /**
  731. * Получить существующий или новый экземпляр,
  732. * только если конструктор зарегистрирован.
  733. *
  734. * @type {Service['getRegisteredService']}
  735. */
  736. get getRegisteredService() {
  737. return this._service.getRegisteredService;
  738. }
  739. /**
  740. * Проверка существования конструктора в контейнере.
  741. *
  742. * @type {Service['hasService']}
  743. */
  744. get hasService() {
  745. return this._service.hasService;
  746. }
  747. /**
  748. * Добавить конструктор в контейнер.
  749. *
  750. * @type {Service['addService']}
  751. */
  752. get addService() {
  753. return this._service.addService;
  754. }
  755. /**
  756. * Добавить конструктор и создать экземпляр.
  757. *
  758. * @type {Service['useService']}
  759. */
  760. get useService() {
  761. return this._service.useService;
  762. }
  763. /**
  764. * Добавить конструктор и связанный экземпляр.
  765. *
  766. * @type {Service['setService']}
  767. */
  768. get setService() {
  769. return this._service.setService;
  770. }
  771. /**
  772. * Найти сервис удовлетворяющий условию.
  773. *
  774. * @type {Service['findService']}
  775. */
  776. get findService() {
  777. return this._service.findService;
  778. }
  779. /**
  780. * Constructor.
  781. *
  782. * @param {ServiceContainer|undefined} container
  783. * @param {import('@e22m4u/js-debug').DebuggableOptions|undefined} options
  784. */
  785. constructor(container = void 0, options = void 0) {
  786. super(options);
  787. this._service = new Service(container);
  788. }
  789. };
  790. __name(_DebuggableService, "DebuggableService");
  791. /**
  792. * Kinds.
  793. *
  794. * @type {string[]}
  795. */
  796. __publicField(_DebuggableService, "kinds", Service.kinds);
  797. var DebuggableService = _DebuggableService;
  798. // src/mongodb-adapter.js
  799. var import_js_repository3 = require("@e22m4u/js-repository");
  800. // src/utils/pluralize.js
  801. var singularExceptions = [
  802. /access$/i,
  803. /address$/i,
  804. /alias$/i,
  805. /bonus$/i,
  806. /boss$/i,
  807. /bus$/i,
  808. /business$/i,
  809. /canvas$/i,
  810. /class$/i,
  811. /cross$/i,
  812. /dress$/i,
  813. /focus$/i,
  814. /gas$/i,
  815. /glass$/i,
  816. /kiss$/i,
  817. /lens$/i,
  818. /loss$/i,
  819. /pass$/i,
  820. /plus$/i,
  821. /process$/i,
  822. /status$/i,
  823. /success$/i,
  824. /virus$/i
  825. ];
  826. function pluralize(input) {
  827. if (!input || typeof input !== "string") {
  828. return input;
  829. }
  830. if (/s$/i.test(input) && !singularExceptions.some((re) => re.test(input))) {
  831. return input;
  832. }
  833. const lastChar = input.slice(-1);
  834. const isLastCharUpper = lastChar === lastChar.toUpperCase() && lastChar !== lastChar.toLowerCase();
  835. if (/(s|x|z|ch|sh)$/i.test(input)) {
  836. return input + (isLastCharUpper ? "ES" : "es");
  837. }
  838. if (/[^aeiou]y$/i.test(input)) {
  839. return input.slice(0, -1) + (isLastCharUpper ? "IES" : "ies");
  840. }
  841. return input + (isLastCharUpper ? "S" : "s");
  842. }
  843. __name(pluralize, "pluralize");
  844. // src/utils/is-iso-date.js
  845. function isIsoDate(value) {
  846. if (!value) return false;
  847. if (value instanceof Date) return true;
  848. if (!/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(value)) return false;
  849. const d = new Date(value);
  850. return d instanceof Date && !isNaN(d.getTime()) && d.toISOString() === value;
  851. }
  852. __name(isIsoDate, "isIsoDate");
  853. // src/utils/is-object-id.js
  854. var import_mongodb = require("mongodb");
  855. function isObjectId(value) {
  856. if (!value) return false;
  857. if (value instanceof import_mongodb.ObjectId) return true;
  858. if (typeof value !== "string") return false;
  859. return value.match(/^[a-fA-F0-9]{24}$/) != null;
  860. }
  861. __name(isObjectId, "isObjectId");
  862. // src/utils/to-camel-case.js
  863. function toCamelCase2(input) {
  864. if (!input) return "";
  865. const spacedString = String(input).replace(/([-_])/g, " ").replace(/([a-z])([A-Z])/g, "$1 $2");
  866. const intermediateCased = spacedString.toLowerCase().replace(/\s(.)/g, ($1) => $1.toUpperCase()).replace(/\s/g, "");
  867. if (!intermediateCased) return "";
  868. return intermediateCased.charAt(0).toLowerCase() + intermediateCased.slice(1);
  869. }
  870. __name(toCamelCase2, "toCamelCase");
  871. // src/utils/create-mongodb-url.js
  872. var import_js_repository = require("@e22m4u/js-repository");
  873. function createMongodbUrl(options = {}) {
  874. if (!options || typeof options !== "object" || Array.isArray(options))
  875. throw new import_js_repository.InvalidArgumentError(
  876. 'The first argument of "createMongodbUrl" must be an Object, but %v given.',
  877. options
  878. );
  879. if (options.protocol && typeof options.protocol !== "string")
  880. throw new import_js_repository.InvalidArgumentError(
  881. 'MongoDB option "protocol" must be a String, but %v given.',
  882. options.protocol
  883. );
  884. if (options.hostname && typeof options.hostname !== "string")
  885. throw new import_js_repository.InvalidArgumentError(
  886. 'MongoDB option "hostname" must be a String, but %v given.',
  887. options.hostname
  888. );
  889. if (options.host && typeof options.host !== "string")
  890. throw new import_js_repository.InvalidArgumentError(
  891. 'MongoDB option "host" must be a String, but %v given.',
  892. options.host
  893. );
  894. if (options.port && typeof options.port !== "number" && typeof options.port !== "string") {
  895. throw new import_js_repository.InvalidArgumentError(
  896. 'MongoDB option "port" must be a Number or a String, but %v given.',
  897. options.port
  898. );
  899. }
  900. if (options.database && typeof options.database !== "string")
  901. throw new import_js_repository.InvalidArgumentError(
  902. 'MongoDB option "database" must be a String, but %v given.',
  903. options.database
  904. );
  905. if (options.db && typeof options.db !== "string")
  906. throw new import_js_repository.InvalidArgumentError(
  907. 'MongoDB option "db" must be a String, but %v given.',
  908. options.db
  909. );
  910. if (options.username && typeof options.username !== "string")
  911. throw new import_js_repository.InvalidArgumentError(
  912. 'MongoDB option "username" must be a String, but %v given.',
  913. options.username
  914. );
  915. if (options.password && typeof options.password !== "string" && typeof options.password !== "number") {
  916. throw new import_js_repository.InvalidArgumentError(
  917. 'MongoDB option "password" must be a String or a Number, but %v given.',
  918. options.password
  919. );
  920. }
  921. if (options.pass && typeof options.pass !== "string" && typeof options.pass !== "number") {
  922. throw new import_js_repository.InvalidArgumentError(
  923. 'MongoDB option "pass" must be a String or a Number, but %v given.',
  924. options.pass
  925. );
  926. }
  927. const protocol = options.protocol || "mongodb";
  928. const hostname = options.hostname || options.host || "127.0.0.1";
  929. const port = options.port || 27017;
  930. const database = options.database || options.db || "database";
  931. const username = options.username || options.user;
  932. const password = options.password || options.pass || void 0;
  933. let portUrl = "";
  934. if (protocol !== "mongodb+srv") {
  935. portUrl = ":" + port;
  936. }
  937. if (username && password) {
  938. return `${protocol}://${username}:${password}@${hostname}${portUrl}/${database}`;
  939. } else {
  940. return `${protocol}://${hostname}${portUrl}/${database}`;
  941. }
  942. }
  943. __name(createMongodbUrl, "createMongodbUrl");
  944. // src/utils/transform-values-deep.js
  945. var import_js_repository2 = require("@e22m4u/js-repository");
  946. function transformValuesDeep(value, transformer) {
  947. if (!transformer || typeof transformer !== "function")
  948. throw new import_js_repository2.InvalidArgumentError(
  949. 'The second argument of "transformValuesDeep" must be a Function, but %v given.',
  950. transformer
  951. );
  952. if (Array.isArray(value)) {
  953. value.forEach((v, i) => value[i] = transformValuesDeep(v, transformer));
  954. return value;
  955. } else if (value && typeof value === "object") {
  956. if (!value.constructor || value.constructor && value.constructor.name === "Object") {
  957. Object.keys(value).forEach((key) => {
  958. if (Object.prototype.hasOwnProperty.call(value, key))
  959. value[key] = transformValuesDeep(value[key], transformer);
  960. });
  961. return value;
  962. } else {
  963. return transformer(value);
  964. }
  965. } else {
  966. return transformer(value);
  967. }
  968. }
  969. __name(transformValuesDeep, "transformValuesDeep");
  970. // src/mongodb-adapter.js
  971. var MONGODB_OPTION_NAMES = [
  972. "ALPNProtocols",
  973. "allowPartialTrustChain",
  974. "appName",
  975. "auth",
  976. "authMechanism",
  977. "authMechanismProperties",
  978. "authSource",
  979. "autoEncryption",
  980. "autoSelectFamily",
  981. "autoSelectFamilyAttemptTimeout",
  982. "bsonRegExp",
  983. "ca",
  984. "cert",
  985. "checkKeys",
  986. "checkServerIdentity",
  987. "ciphers",
  988. "compressors",
  989. "connectTimeoutMS",
  990. "crl",
  991. "directConnection",
  992. "driverInfo",
  993. "ecdhCurve",
  994. "enableUtf8Validation",
  995. "family",
  996. "fieldsAsRaw",
  997. "forceServerObjectId",
  998. "heartbeatFrequencyMS",
  999. "hints",
  1000. "ignoreUndefined",
  1001. "journal",
  1002. "keepAliveInitialDelay",
  1003. "key",
  1004. "loadBalanced",
  1005. "localAddress",
  1006. "localPort",
  1007. "localThresholdMS",
  1008. "lookup",
  1009. "maxConnecting",
  1010. "maxIdleTimeMS",
  1011. "maxPoolSize",
  1012. "maxStalenessSeconds",
  1013. "minDHSize",
  1014. "minHeartbeatFrequencyMS",
  1015. "minPoolSize",
  1016. "mongodbLogComponentSeverities",
  1017. "mongodbLogMaxDocumentLength",
  1018. "mongodbLogPath",
  1019. "monitorCommands",
  1020. "noDelay",
  1021. "passphrase",
  1022. "pfx",
  1023. "pkFactory",
  1024. "promoteBuffers",
  1025. "promoteLongs",
  1026. "promoteValues",
  1027. "proxyHost",
  1028. "proxyPassword",
  1029. "proxyPort",
  1030. "proxyUsername",
  1031. "raw",
  1032. "readConcern",
  1033. "readConcernLevel",
  1034. "readPreference",
  1035. "readPreferenceTags",
  1036. "rejectUnauthorized",
  1037. "replicaSet",
  1038. "retryReads",
  1039. "retryWrites",
  1040. "secureContext",
  1041. "secureProtocol",
  1042. "serializeFunctions",
  1043. "serverApi",
  1044. "serverMonitoringMode",
  1045. "serverSelectionTimeoutMS",
  1046. "servername",
  1047. "session",
  1048. "socketTimeoutMS",
  1049. "srvMaxHosts",
  1050. "srvServiceName",
  1051. "ssl",
  1052. "timeoutMS",
  1053. "tls",
  1054. "tlsAllowInvalidCertificates",
  1055. "tlsAllowInvalidHostnames",
  1056. "tlsCAFile",
  1057. "tlsCRLFile",
  1058. "tlsCertificateKeyFile",
  1059. "tlsCertificateKeyFilePassword",
  1060. "tlsInsecure",
  1061. "useBigInt64",
  1062. "w",
  1063. "waitQueueTimeoutMS",
  1064. "writeConcern",
  1065. "wtimeoutMS",
  1066. "zlibCompressionLevel"
  1067. ];
  1068. var DEFAULT_SETTINGS = {
  1069. // connectTimeoutMS: 2500,
  1070. // serverSelectionTimeoutMS: 2500,
  1071. };
  1072. var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapter {
  1073. /**
  1074. * Mongodb instance.
  1075. *
  1076. * @type {MongoClient}
  1077. */
  1078. _client;
  1079. /**
  1080. * Client.
  1081. *
  1082. * @returns {MongoClient}
  1083. */
  1084. get client() {
  1085. return this._client;
  1086. }
  1087. /**
  1088. * Collections.
  1089. *
  1090. * @type {Map<any, any>}
  1091. */
  1092. _collections = /* @__PURE__ */ new Map();
  1093. /**
  1094. * Constructor.
  1095. *
  1096. * @param {ServiceContainer} container
  1097. * @param {object} settings
  1098. */
  1099. constructor(container, settings) {
  1100. settings = Object.assign({}, DEFAULT_SETTINGS, settings || {});
  1101. settings.protocol = settings.protocol || "mongodb";
  1102. settings.hostname = settings.hostname || settings.host || "127.0.0.1";
  1103. settings.port = settings.port || 27017;
  1104. settings.database = settings.database || settings.db || "database";
  1105. super(container, settings);
  1106. const options = (0, import_js_repository3.selectObjectKeys)(this.settings, MONGODB_OPTION_NAMES);
  1107. const url = createMongodbUrl(this.settings);
  1108. this._client = new import_mongodb2.MongoClient(url, options);
  1109. }
  1110. /**
  1111. * Get id prop name.
  1112. *
  1113. * @param {string} modelName
  1114. * @returns {string}
  1115. */
  1116. _getIdPropName(modelName) {
  1117. return this.getService(import_js_repository3.ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  1118. modelName
  1119. );
  1120. }
  1121. /**
  1122. * Get id col name.
  1123. *
  1124. * @param {string} modelName
  1125. * @returns {string}
  1126. */
  1127. _getIdColName(modelName) {
  1128. return this.getService(import_js_repository3.ModelDefinitionUtils).getPrimaryKeyAsColumnName(
  1129. modelName
  1130. );
  1131. }
  1132. /**
  1133. * Coerce id.
  1134. *
  1135. * @param {*} value
  1136. * @returns {ObjectId|*}
  1137. */
  1138. _coerceId(value) {
  1139. if (value == null) return value;
  1140. if (isObjectId(value)) return new import_mongodb2.ObjectId(value);
  1141. return value;
  1142. }
  1143. /**
  1144. * Coerce date.
  1145. *
  1146. * @param {Date|string|*} value
  1147. * @returns {Date|*}
  1148. */
  1149. _coerceDate(value) {
  1150. if (value == null) return value;
  1151. if (value instanceof Date) return value;
  1152. if (isIsoDate(value)) return new Date(value);
  1153. return value;
  1154. }
  1155. /**
  1156. * To database.
  1157. *
  1158. * @param {string} modelName
  1159. * @param {object} modelData
  1160. * @returns {object}
  1161. */
  1162. _toDatabase(modelName, modelData) {
  1163. const tableData = this.getService(
  1164. import_js_repository3.ModelDefinitionUtils
  1165. ).convertPropertyNamesToColumnNames(modelName, modelData);
  1166. const idColName = this._getIdColName(modelName);
  1167. if (idColName !== "id" && idColName !== "_id")
  1168. throw new import_js_repository3.InvalidArgumentError(
  1169. 'MongoDB is not supporting custom names of the primary key. Do use "id" as a primary key instead of %v.',
  1170. idColName
  1171. );
  1172. if (idColName in tableData && idColName !== "_id") {
  1173. tableData._id = tableData[idColName];
  1174. delete tableData[idColName];
  1175. }
  1176. return transformValuesDeep(tableData, (value) => {
  1177. if (value instanceof import_mongodb2.ObjectId) return value;
  1178. if (value instanceof Date) return value;
  1179. if (isObjectId(value)) return new import_mongodb2.ObjectId(value);
  1180. if (isIsoDate(value)) return new Date(value);
  1181. return value;
  1182. });
  1183. }
  1184. /**
  1185. * From database.
  1186. *
  1187. * @param {string} modelName
  1188. * @param {object} tableData
  1189. * @returns {object}
  1190. */
  1191. _fromDatabase(modelName, tableData) {
  1192. if ("_id" in tableData) {
  1193. const idColName = this._getIdColName(modelName);
  1194. if (idColName !== "id" && idColName !== "_id")
  1195. throw new import_js_repository3.InvalidArgumentError(
  1196. 'MongoDB is not supporting custom names of the primary key. Do use "id" as a primary key instead of %v.',
  1197. idColName
  1198. );
  1199. if (idColName !== "_id") {
  1200. tableData[idColName] = tableData._id;
  1201. delete tableData._id;
  1202. }
  1203. }
  1204. const modelData = this.getService(
  1205. import_js_repository3.ModelDefinitionUtils
  1206. ).convertColumnNamesToPropertyNames(modelName, tableData);
  1207. return transformValuesDeep(modelData, (value) => {
  1208. if (value instanceof import_mongodb2.ObjectId) return String(value);
  1209. if (value instanceof Date) return value.toISOString();
  1210. return value;
  1211. });
  1212. }
  1213. /**
  1214. * Get collection name by model name.
  1215. *
  1216. * @param {string} modelName
  1217. * @returns {string}
  1218. */
  1219. _getCollectionNameByModelName(modelName) {
  1220. const modelDef = this.getService(import_js_repository3.DefinitionRegistry).getModel(modelName);
  1221. if (modelDef.tableName != null) return modelDef.tableName;
  1222. return pluralize(toCamelCase2(modelDef.name));
  1223. }
  1224. /**
  1225. * Get collection.
  1226. *
  1227. * @param {string} modelName
  1228. * @returns {*}
  1229. */
  1230. _getCollection(modelName) {
  1231. let collection = this._collections.get(modelName);
  1232. if (collection) return collection;
  1233. const collectionName = this._getCollectionNameByModelName(modelName);
  1234. collection = this.client.db(this.settings.database).collection(collectionName);
  1235. this._collections.set(modelName, collection);
  1236. return collection;
  1237. }
  1238. /**
  1239. * Get id type.
  1240. *
  1241. * @param {string} modelName
  1242. * @returns {string|*}
  1243. */
  1244. _getIdType(modelName) {
  1245. const utils = this.getService(import_js_repository3.ModelDefinitionUtils);
  1246. const pkPropName = utils.getPrimaryKeyAsPropertyName(modelName);
  1247. return utils.getDataTypeByPropertyName(modelName, pkPropName);
  1248. }
  1249. /**
  1250. * Get col name.
  1251. *
  1252. * @param {string} modelName
  1253. * @param {string} propName
  1254. * @returns {string}
  1255. */
  1256. _getColName(modelName, propName) {
  1257. if (!propName || typeof propName !== "string")
  1258. throw new import_js_repository3.InvalidArgumentError(
  1259. "Property name must be a non-empty String, but %v given.",
  1260. propName
  1261. );
  1262. const utils = this.getService(import_js_repository3.ModelDefinitionUtils);
  1263. let colName = propName;
  1264. try {
  1265. colName = utils.getColumnNameByPropertyName(modelName, propName);
  1266. } catch (error) {
  1267. if (!(error instanceof import_js_repository3.InvalidArgumentError) || error.message.indexOf("does not have the property") === -1) {
  1268. throw error;
  1269. }
  1270. }
  1271. return colName;
  1272. }
  1273. /**
  1274. * Convert prop names chain to col names chain.
  1275. *
  1276. * @param {string} modelName
  1277. * @param {string} propsChain
  1278. * @returns {string}
  1279. */
  1280. _convertPropNamesChainToColNamesChain(modelName, propsChain) {
  1281. if (!modelName || typeof modelName !== "string")
  1282. throw new import_js_repository3.InvalidArgumentError(
  1283. "Model name must be a non-empty String, but %v given.",
  1284. modelName
  1285. );
  1286. if (!propsChain || typeof propsChain !== "string")
  1287. throw new import_js_repository3.InvalidArgumentError(
  1288. "Properties chain must be a non-empty String, but %v given.",
  1289. propsChain
  1290. );
  1291. propsChain = propsChain.replace(/\.{2,}/g, ".");
  1292. const propNames = propsChain.split(".");
  1293. const utils = this.getService(import_js_repository3.ModelDefinitionUtils);
  1294. let currModelName = modelName;
  1295. return propNames.map((currPropName) => {
  1296. if (!currModelName) return currPropName;
  1297. const colName = this._getColName(currModelName, currPropName);
  1298. currModelName = utils.getModelNameOfPropertyValueIfDefined(
  1299. currModelName,
  1300. currPropName
  1301. );
  1302. return colName;
  1303. }).join(".");
  1304. }
  1305. /**
  1306. * Build projection.
  1307. *
  1308. * @param {string} modelName
  1309. * @param {string|string[]} fields
  1310. * @returns {Record<string, number>|undefined}
  1311. */
  1312. _buildProjection(modelName, fields) {
  1313. if (fields == null) return;
  1314. if (Array.isArray(fields) === false) fields = [fields];
  1315. if (!fields.length) return;
  1316. if (fields.indexOf("_id") === -1) fields.push("_id");
  1317. return fields.reduce((acc, field) => {
  1318. if (!field || typeof field !== "string")
  1319. throw new import_js_repository3.InvalidArgumentError(
  1320. 'The provided option "fields" should be a non-empty String or an Array of non-empty String, but %v given.',
  1321. field
  1322. );
  1323. let colName = this._convertPropNamesChainToColNamesChain(
  1324. modelName,
  1325. field
  1326. );
  1327. acc[colName] = 1;
  1328. return acc;
  1329. }, {});
  1330. }
  1331. /**
  1332. * Build sort.
  1333. *
  1334. * @param {string} modelName
  1335. * @param {string|string[]} clause
  1336. * @returns {object|undefined}
  1337. */
  1338. _buildSort(modelName, clause) {
  1339. if (clause == null) return;
  1340. if (Array.isArray(clause) === false) clause = [clause];
  1341. if (!clause.length) return;
  1342. const idPropName = this._getIdPropName(modelName);
  1343. return clause.reduce((acc, order) => {
  1344. if (!order || typeof order !== "string")
  1345. throw new import_js_repository3.InvalidArgumentError(
  1346. 'The provided option "order" should be a non-empty String or an Array of non-empty String, but %v given.',
  1347. order
  1348. );
  1349. const direction = order.match(/\s+(A|DE)SC$/);
  1350. let field = order.replace(/\s+(A|DE)SC$/, "").trim();
  1351. if (field === idPropName) {
  1352. field = "_id";
  1353. } else {
  1354. try {
  1355. field = this._convertPropNamesChainToColNamesChain(modelName, field);
  1356. } catch (error) {
  1357. if (!(error instanceof import_js_repository3.InvalidArgumentError) || error.message.indexOf("does not have the property") === -1) {
  1358. throw error;
  1359. }
  1360. }
  1361. }
  1362. acc[field] = direction && direction[1] === "DE" ? -1 : 1;
  1363. return acc;
  1364. }, {});
  1365. }
  1366. /**
  1367. * Build query.
  1368. *
  1369. * @param {string} modelName
  1370. * @param {object} clause
  1371. * @returns {object|undefined}
  1372. */
  1373. _buildQuery(modelName, clause) {
  1374. if (clause == null) return;
  1375. if (typeof clause !== "object" || Array.isArray(clause))
  1376. throw new import_js_repository3.InvalidArgumentError(
  1377. 'The provided option "where" should be an Object, but %v given.',
  1378. clause
  1379. );
  1380. const query = {};
  1381. const idPropName = this._getIdPropName(modelName);
  1382. Object.keys(clause).forEach((key) => {
  1383. var _a, _b;
  1384. if (String(key).indexOf("$") !== -1)
  1385. throw new import_js_repository3.InvalidArgumentError(
  1386. 'The symbol "$" is not supported, but %v given.',
  1387. key
  1388. );
  1389. let cond = clause[key];
  1390. if (key === "and" || key === "or" || key === "nor") {
  1391. if (cond == null) return;
  1392. if (!Array.isArray(cond))
  1393. throw new import_js_repository3.InvalidOperatorValueError(key, "an Array", cond);
  1394. if (cond.length === 0) return;
  1395. cond = cond.map((c) => this._buildQuery(modelName, c));
  1396. cond = cond.filter((c) => c != null);
  1397. const opKey = "$" + key;
  1398. query[opKey] = (_a = query[opKey]) != null ? _a : [];
  1399. query[opKey] = [...query[opKey], ...cond];
  1400. return;
  1401. }
  1402. if (key === idPropName) {
  1403. key = "_id";
  1404. } else {
  1405. key = this._convertPropNamesChainToColNamesChain(modelName, key);
  1406. }
  1407. if (typeof cond === "string") {
  1408. query[key] = this._coerceId(cond);
  1409. query[key] = this._coerceDate(query[key]);
  1410. return;
  1411. }
  1412. if (cond instanceof import_mongodb2.ObjectId) {
  1413. query[key] = cond;
  1414. return;
  1415. }
  1416. if (cond && cond.constructor && cond.constructor.name === "Object") {
  1417. const opConds = [];
  1418. if ("eq" in cond) {
  1419. let eq = this._coerceId(cond.eq);
  1420. eq = this._coerceDate(eq);
  1421. opConds.push({ $eq: eq });
  1422. }
  1423. if ("neq" in cond) {
  1424. let neq = this._coerceId(cond.neq);
  1425. neq = this._coerceDate(neq);
  1426. opConds.push({ $ne: neq });
  1427. }
  1428. if ("gt" in cond) {
  1429. const gt = this._coerceDate(cond.gt);
  1430. opConds.push({ $gt: gt });
  1431. }
  1432. if ("lt" in cond) {
  1433. const lt = this._coerceDate(cond.lt);
  1434. opConds.push({ $lt: lt });
  1435. }
  1436. if ("gte" in cond) {
  1437. const gte = this._coerceDate(cond.gte);
  1438. opConds.push({ $gte: gte });
  1439. }
  1440. if ("lte" in cond) {
  1441. const lte = this._coerceDate(cond.lte);
  1442. opConds.push({ $lte: lte });
  1443. }
  1444. if ("inq" in cond) {
  1445. if (!cond.inq || !Array.isArray(cond.inq))
  1446. throw new import_js_repository3.InvalidOperatorValueError(
  1447. "inq",
  1448. "an Array of possible values",
  1449. cond.inq
  1450. );
  1451. const inq = cond.inq.map((v) => {
  1452. v = this._coerceId(v);
  1453. v = this._coerceDate(v);
  1454. return v;
  1455. });
  1456. opConds.push({ $in: inq });
  1457. }
  1458. if ("nin" in cond) {
  1459. if (!cond.nin || !Array.isArray(cond.nin))
  1460. throw new import_js_repository3.InvalidOperatorValueError(
  1461. "nin",
  1462. "an Array of possible values",
  1463. cond
  1464. );
  1465. const nin = cond.nin.map((v) => {
  1466. v = this._coerceId(v);
  1467. v = this._coerceDate(v);
  1468. return v;
  1469. });
  1470. opConds.push({ $nin: nin });
  1471. }
  1472. if ("between" in cond) {
  1473. if (!Array.isArray(cond.between) || cond.between.length !== 2)
  1474. throw new import_js_repository3.InvalidOperatorValueError(
  1475. "between",
  1476. "an Array of 2 elements",
  1477. cond.between
  1478. );
  1479. const gte = this._coerceDate(cond.between[0]);
  1480. const lte = this._coerceDate(cond.between[1]);
  1481. opConds.push({ $gte: gte, $lte: lte });
  1482. }
  1483. if ("exists" in cond) {
  1484. if (typeof cond.exists !== "boolean")
  1485. throw new import_js_repository3.InvalidOperatorValueError(
  1486. "exists",
  1487. "a Boolean",
  1488. cond.exists
  1489. );
  1490. opConds.push({ $exists: cond.exists });
  1491. }
  1492. if ("like" in cond) {
  1493. if (typeof cond.like !== "string" && !(cond.like instanceof RegExp))
  1494. throw new import_js_repository3.InvalidOperatorValueError(
  1495. "like",
  1496. "a String or RegExp",
  1497. cond.like
  1498. );
  1499. opConds.push({ $regex: (0, import_js_repository3.likeToRegexp)(cond.like) });
  1500. }
  1501. if ("nlike" in cond) {
  1502. if (typeof cond.nlike !== "string" && !(cond.nlike instanceof RegExp))
  1503. throw new import_js_repository3.InvalidOperatorValueError(
  1504. "nlike",
  1505. "a String or RegExp",
  1506. cond.nlike
  1507. );
  1508. opConds.push({ $not: (0, import_js_repository3.likeToRegexp)(cond.nlike) });
  1509. }
  1510. if ("ilike" in cond) {
  1511. if (typeof cond.ilike !== "string" && !(cond.ilike instanceof RegExp))
  1512. throw new import_js_repository3.InvalidOperatorValueError(
  1513. "ilike",
  1514. "a String or RegExp",
  1515. cond.ilike
  1516. );
  1517. opConds.push({ $regex: (0, import_js_repository3.likeToRegexp)(cond.ilike, true) });
  1518. }
  1519. if ("nilike" in cond) {
  1520. if (typeof cond.nilike !== "string" && !(cond.nilike instanceof RegExp)) {
  1521. throw new import_js_repository3.InvalidOperatorValueError(
  1522. "nilike",
  1523. "a String or RegExp",
  1524. cond.nilike
  1525. );
  1526. }
  1527. opConds.push({ $not: (0, import_js_repository3.likeToRegexp)(cond.nilike, true) });
  1528. }
  1529. if ("regexp" in cond) {
  1530. if (typeof cond.regexp !== "string" && !(cond.regexp instanceof RegExp)) {
  1531. throw new import_js_repository3.InvalidOperatorValueError(
  1532. "regexp",
  1533. "a String or RegExp",
  1534. cond.regexp
  1535. );
  1536. }
  1537. const flags = cond.flags || void 0;
  1538. if (flags && typeof flags !== "string")
  1539. throw new import_js_repository3.InvalidArgumentError(
  1540. "RegExp flags must be a String, but %v given.",
  1541. cond.flags
  1542. );
  1543. opConds.push({ $regex: (0, import_js_repository3.stringToRegexp)(cond.regexp, flags) });
  1544. }
  1545. if (opConds.length === 1) {
  1546. query[key] = opConds[0];
  1547. } else if (opConds.length > 1) {
  1548. query["$and"] = (_b = query["$and"]) != null ? _b : [];
  1549. opConds.forEach((c) => query["$and"].push({ [key]: c }));
  1550. }
  1551. return;
  1552. }
  1553. query[key] = cond;
  1554. });
  1555. return Object.keys(query).length ? query : void 0;
  1556. }
  1557. /**
  1558. * Create.
  1559. *
  1560. * @param {string} modelName
  1561. * @param {object} modelData
  1562. * @param {object|undefined} filter
  1563. * @returns {Promise<object>}
  1564. */
  1565. async create(modelName, modelData, filter = void 0) {
  1566. const idPropName = this._getIdPropName(modelName);
  1567. const idValue = modelData[idPropName];
  1568. if (idValue == null || idValue === "" || idValue === 0) {
  1569. const pkType = this._getIdType(modelName);
  1570. if (pkType !== import_js_repository3.DataType.STRING && pkType !== import_js_repository3.DataType.ANY)
  1571. throw new import_js_repository3.InvalidArgumentError(
  1572. "MongoDB unable to generate primary keys of %s. Do provide your own value for the %v property or set property type to String.",
  1573. (0, import_js_repository3.capitalize)(pkType),
  1574. idPropName
  1575. );
  1576. delete modelData[idPropName];
  1577. }
  1578. const tableData = this._toDatabase(modelName, modelData);
  1579. const table = this._getCollection(modelName);
  1580. const { insertedId } = await table.insertOne(tableData);
  1581. const projection = this._buildProjection(
  1582. modelName,
  1583. filter && filter.fields
  1584. );
  1585. const insertedData = await table.findOne({ _id: insertedId }, { projection });
  1586. return this._fromDatabase(modelName, insertedData);
  1587. }
  1588. /**
  1589. * Replace by id.
  1590. *
  1591. * @param {string} modelName
  1592. * @param {string|number} id
  1593. * @param {object} modelData
  1594. * @param {object|undefined} filter
  1595. * @returns {Promise<object>}
  1596. */
  1597. async replaceById(modelName, id, modelData, filter = void 0) {
  1598. id = this._coerceId(id);
  1599. const idPropName = this._getIdPropName(modelName);
  1600. modelData[idPropName] = id;
  1601. const tableData = this._toDatabase(modelName, modelData);
  1602. const table = this._getCollection(modelName);
  1603. const { matchedCount } = await table.replaceOne({ _id: id }, tableData);
  1604. if (matchedCount < 1)
  1605. throw new import_js_repository3.InvalidArgumentError("Identifier %v is not found.", String(id));
  1606. const projection = this._buildProjection(
  1607. modelName,
  1608. filter && filter.fields
  1609. );
  1610. const replacedData = await table.findOne({ _id: id }, { projection });
  1611. return this._fromDatabase(modelName, replacedData);
  1612. }
  1613. /**
  1614. * Replace or create.
  1615. *
  1616. * @param {string} modelName
  1617. * @param {object} modelData
  1618. * @param {object|undefined} filter
  1619. * @returns {Promise<object>}
  1620. */
  1621. async replaceOrCreate(modelName, modelData, filter = void 0) {
  1622. const idPropName = this._getIdPropName(modelName);
  1623. let idValue = modelData[idPropName];
  1624. idValue = this._coerceId(idValue);
  1625. if (idValue == null || idValue === "" || idValue === 0) {
  1626. const pkType = this._getIdType(modelName);
  1627. if (pkType !== import_js_repository3.DataType.STRING && pkType !== import_js_repository3.DataType.ANY)
  1628. throw new import_js_repository3.InvalidArgumentError(
  1629. "MongoDB unable to generate primary keys of %s. Do provide your own value for the %v property or set property type to String.",
  1630. (0, import_js_repository3.capitalize)(pkType),
  1631. idPropName
  1632. );
  1633. delete modelData[idPropName];
  1634. idValue = void 0;
  1635. }
  1636. const tableData = this._toDatabase(modelName, modelData);
  1637. const table = this._getCollection(modelName);
  1638. if (idValue == null) {
  1639. const { insertedId } = await table.insertOne(tableData);
  1640. idValue = insertedId;
  1641. } else {
  1642. const { upsertedId } = await table.replaceOne({ _id: idValue }, tableData, {
  1643. upsert: true
  1644. });
  1645. if (upsertedId) idValue = upsertedId;
  1646. }
  1647. const projection = this._buildProjection(
  1648. modelName,
  1649. filter && filter.fields
  1650. );
  1651. const upsertedData = await table.findOne({ _id: idValue }, { projection });
  1652. return this._fromDatabase(modelName, upsertedData);
  1653. }
  1654. /**
  1655. * Patch.
  1656. *
  1657. * @param {string} modelName
  1658. * @param {object} modelData
  1659. * @param {object|undefined} where
  1660. * @returns {Promise<number>}
  1661. */
  1662. async patch(modelName, modelData, where = void 0) {
  1663. const idPropName = this._getIdPropName(modelName);
  1664. delete modelData[idPropName];
  1665. const query = this._buildQuery(modelName, where) || {};
  1666. const tableData = this._toDatabase(modelName, modelData);
  1667. const table = this._getCollection(modelName);
  1668. const { matchedCount } = await table.updateMany(query, { $set: tableData });
  1669. return matchedCount;
  1670. }
  1671. /**
  1672. * Patch by id.
  1673. *
  1674. * @param {string} modelName
  1675. * @param {string|number} id
  1676. * @param {object} modelData
  1677. * @param {object|undefined} filter
  1678. * @returns {Promise<object>}
  1679. */
  1680. async patchById(modelName, id, modelData, filter = void 0) {
  1681. id = this._coerceId(id);
  1682. const idPropName = this._getIdPropName(modelName);
  1683. delete modelData[idPropName];
  1684. const tableData = this._toDatabase(modelName, modelData);
  1685. const table = this._getCollection(modelName);
  1686. const { matchedCount } = await table.updateOne({ _id: id }, { $set: tableData });
  1687. if (matchedCount < 1)
  1688. throw new import_js_repository3.InvalidArgumentError("Identifier %v is not found.", String(id));
  1689. const projection = this._buildProjection(
  1690. modelName,
  1691. filter && filter.fields
  1692. );
  1693. const patchedData = await table.findOne({ _id: id }, { projection });
  1694. return this._fromDatabase(modelName, patchedData);
  1695. }
  1696. /**
  1697. * Find.
  1698. *
  1699. * @param {string} modelName
  1700. * @param {object|undefined} filter
  1701. * @returns {Promise<object[]>}
  1702. */
  1703. async find(modelName, filter = void 0) {
  1704. filter = filter || {};
  1705. const query = this._buildQuery(modelName, filter.where);
  1706. const sort = this._buildSort(modelName, filter.order);
  1707. const limit = filter.limit || void 0;
  1708. const skip = filter.skip || void 0;
  1709. const projection = this._buildProjection(modelName, filter.fields);
  1710. const collection = this._getCollection(modelName);
  1711. const options = { sort, limit, skip, projection };
  1712. const tableItems = await collection.find(query, options).toArray();
  1713. return tableItems.map((v) => this._fromDatabase(modelName, v));
  1714. }
  1715. /**
  1716. * Find by id.
  1717. *
  1718. * @param {string} modelName
  1719. * @param {string|number} id
  1720. * @param {object|undefined} filter
  1721. * @returns {Promise<object>}
  1722. */
  1723. async findById(modelName, id, filter = void 0) {
  1724. id = this._coerceId(id);
  1725. const table = this._getCollection(modelName);
  1726. const projection = this._buildProjection(
  1727. modelName,
  1728. filter && filter.fields
  1729. );
  1730. const patchedData = await table.findOne({ _id: id }, { projection });
  1731. if (!patchedData)
  1732. throw new import_js_repository3.InvalidArgumentError("Identifier %v is not found.", String(id));
  1733. return this._fromDatabase(modelName, patchedData);
  1734. }
  1735. /**
  1736. * Delete.
  1737. *
  1738. * @param {string} modelName
  1739. * @param {object|undefined} where
  1740. * @returns {Promise<number>}
  1741. */
  1742. async delete(modelName, where = void 0) {
  1743. const table = this._getCollection(modelName);
  1744. const query = this._buildQuery(modelName, where);
  1745. const { deletedCount } = await table.deleteMany(query);
  1746. return deletedCount;
  1747. }
  1748. /**
  1749. * Delete by id.
  1750. *
  1751. * @param {string} modelName
  1752. * @param {string|number} id
  1753. * @returns {Promise<boolean>}
  1754. */
  1755. async deleteById(modelName, id) {
  1756. id = this._coerceId(id);
  1757. const table = this._getCollection(modelName);
  1758. const { deletedCount } = await table.deleteOne({ _id: id });
  1759. return deletedCount > 0;
  1760. }
  1761. /**
  1762. * Exists.
  1763. *
  1764. * @param {string} modelName
  1765. * @param {string|number} id
  1766. * @returns {Promise<boolean>}
  1767. */
  1768. async exists(modelName, id) {
  1769. id = this._coerceId(id);
  1770. const table = this._getCollection(modelName);
  1771. const result = await table.findOne({ _id: id }, {});
  1772. return result != null;
  1773. }
  1774. /**
  1775. * Count.
  1776. *
  1777. * @param {string} modelName
  1778. * @param {object|undefined} where
  1779. * @returns {Promise<number>}
  1780. */
  1781. async count(modelName, where = void 0) {
  1782. const query = this._buildQuery(modelName, where);
  1783. const table = this._getCollection(modelName);
  1784. return await table.countDocuments(query);
  1785. }
  1786. };
  1787. __name(_MongodbAdapter, "MongodbAdapter");
  1788. var MongodbAdapter = _MongodbAdapter;
  1789. // Annotate the CommonJS export names for ESM import in node:
  1790. 0 && (module.exports = {
  1791. MongodbAdapter
  1792. });