@cyberskill/shared 3.2.0 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (237) hide show
  1. package/dist/config/commitlint/index.js +8 -9
  2. package/dist/config/commitlint/index.js.map +1 -1
  3. package/dist/config/config.type.js +8 -5
  4. package/dist/config/config.type.js.map +1 -1
  5. package/dist/config/config.util.js +33 -42
  6. package/dist/config/config.util.js.map +1 -1
  7. package/dist/config/env/env.constant.js +6 -5
  8. package/dist/config/env/env.constant.js.map +1 -1
  9. package/dist/config/env/env.util.js +26 -27
  10. package/dist/config/env/env.util.js.map +1 -1
  11. package/dist/config/env/index.js +2 -7
  12. package/dist/config/eslint/index.js +23 -19
  13. package/dist/config/eslint/index.js.map +1 -1
  14. package/dist/config/graphql-codegen/graphql-codegen.util.js +30 -38
  15. package/dist/config/graphql-codegen/graphql-codegen.util.js.map +1 -1
  16. package/dist/config/graphql-codegen/index.js +2 -5
  17. package/dist/config/index.js +3 -7
  18. package/dist/config/lint-staged/index.js +8 -7
  19. package/dist/config/lint-staged/index.js.map +1 -1
  20. package/dist/config/storybook/index.js +2 -6
  21. package/dist/config/storybook/storybook.main.js +19 -18
  22. package/dist/config/storybook/storybook.main.js.map +1 -1
  23. package/dist/config/storybook/storybook.preview.js +31 -37
  24. package/dist/config/storybook/storybook.preview.js.map +1 -1
  25. package/dist/config/vitest/index.js +3 -7
  26. package/dist/config/vitest/vitest.e2e.js +14 -17
  27. package/dist/config/vitest/vitest.e2e.js.map +1 -1
  28. package/dist/config/vitest/vitest.unit.js +31 -32
  29. package/dist/config/vitest/vitest.unit.js.map +1 -1
  30. package/dist/constant/common.js +6 -5
  31. package/dist/constant/common.js.map +1 -1
  32. package/dist/constant/index.js +3 -7
  33. package/dist/constant/response-status.d.ts +186 -186
  34. package/dist/constant/response-status.js +254 -253
  35. package/dist/constant/response-status.js.map +1 -1
  36. package/dist/node/apollo-server/apollo-server.util.js +27 -29
  37. package/dist/node/apollo-server/apollo-server.util.js.map +1 -1
  38. package/dist/node/apollo-server/index.js +2 -7
  39. package/dist/node/cli/index.js +112 -113
  40. package/dist/node/cli/index.js.map +1 -1
  41. package/dist/node/command/command.type.js +8 -5
  42. package/dist/node/command/command.type.js.map +1 -1
  43. package/dist/node/command/command.util.js +136 -149
  44. package/dist/node/command/command.util.js.map +1 -1
  45. package/dist/node/command/index.js +3 -12
  46. package/dist/node/express/express.type.d.ts +11 -0
  47. package/dist/node/express/express.type.js +2 -0
  48. package/dist/node/express/express.util.js +74 -65
  49. package/dist/node/express/express.util.js.map +1 -1
  50. package/dist/node/express/index.js +3 -14
  51. package/dist/node/fs/fs.util.js +27 -51
  52. package/dist/node/fs/fs.util.js.map +1 -1
  53. package/dist/node/fs/index.js +2 -19
  54. package/dist/node/log/index.js +2 -8
  55. package/dist/node/log/log.type.js +8 -5
  56. package/dist/node/log/log.type.js.map +1 -1
  57. package/dist/node/log/log.util.js +51 -68
  58. package/dist/node/log/log.util.js.map +1 -1
  59. package/dist/node/mongo/index.js +8 -26
  60. package/dist/node/mongo/mongo.constant.js +6 -6
  61. package/dist/node/mongo/mongo.constant.js.map +1 -1
  62. package/dist/node/mongo/mongo.controller.mongoose.d.ts +6 -1
  63. package/dist/node/mongo/mongo.controller.mongoose.js +333 -467
  64. package/dist/node/mongo/mongo.controller.mongoose.js.map +1 -1
  65. package/dist/node/mongo/mongo.controller.native.js +153 -212
  66. package/dist/node/mongo/mongo.controller.native.js.map +1 -1
  67. package/dist/node/mongo/mongo.dynamic-populate.js +164 -192
  68. package/dist/node/mongo/mongo.dynamic-populate.js.map +1 -1
  69. package/dist/node/mongo/mongo.populate.js +229 -262
  70. package/dist/node/mongo/mongo.populate.js.map +1 -1
  71. package/dist/node/mongo/mongo.type.js +8 -17
  72. package/dist/node/mongo/mongo.type.js.map +1 -1
  73. package/dist/node/mongo/mongo.util.js +134 -293
  74. package/dist/node/mongo/mongo.util.js.map +1 -1
  75. package/dist/node/package/index.js +3 -11
  76. package/dist/node/package/package.type.js +8 -5
  77. package/dist/node/package/package.type.js.map +1 -1
  78. package/dist/node/package/package.util.js +216 -225
  79. package/dist/node/package/package.util.js.map +1 -1
  80. package/dist/node/path/index.js +3 -58
  81. package/dist/node/path/path.constant.js +211 -298
  82. package/dist/node/path/path.constant.js.map +1 -1
  83. package/dist/node/path/path.util.js +13 -16
  84. package/dist/node/path/path.util.js.map +1 -1
  85. package/dist/node/storage/index.js +3 -10
  86. package/dist/node/storage/storage.constant.js +6 -8
  87. package/dist/node/storage/storage.constant.js.map +1 -1
  88. package/dist/node/storage/storage.util.js +127 -178
  89. package/dist/node/storage/storage.util.js.map +1 -1
  90. package/dist/node/upload/index.js +4 -16
  91. package/dist/node/upload/upload.constant.js +58 -32
  92. package/dist/node/upload/upload.constant.js.map +1 -1
  93. package/dist/node/upload/upload.type.js +8 -5
  94. package/dist/node/upload/upload.type.js.map +1 -1
  95. package/dist/node/upload/upload.util.js +125 -137
  96. package/dist/node/upload/upload.util.js.map +1 -1
  97. package/dist/node/ws/index.js +2 -6
  98. package/dist/node/ws/ws.type.d.ts +7 -8
  99. package/dist/node/ws/ws.util.js +46 -44
  100. package/dist/node/ws/ws.util.js.map +1 -1
  101. package/dist/node_modules/.pnpm/vitest@4.1.0_@types_node@25.5.0_jsdom@29.0.0_@noble_hashes@1.8.0__vite@8.0.0_@types_nod_53aa4254f295b3c40bb8f17b6ab226b5/node_modules/vitest/dist/config.js +8 -0
  102. package/dist/node_modules/.pnpm/vitest@4.1.0_@types_node@25.5.0_jsdom@29.0.0_@noble_hashes@1.8.0__vite@8.0.0_@types_nod_53aa4254f295b3c40bb8f17b6ab226b5/node_modules/vitest/dist/config.js.map +1 -0
  103. package/dist/react/apollo-client/apollo-client.component.js +16 -18
  104. package/dist/react/apollo-client/apollo-client.component.js.map +1 -1
  105. package/dist/react/apollo-client/apollo-client.constant.js +6 -5
  106. package/dist/react/apollo-client/apollo-client.constant.js.map +1 -1
  107. package/dist/react/apollo-client/apollo-client.context.js +10 -13
  108. package/dist/react/apollo-client/apollo-client.context.js.map +1 -1
  109. package/dist/react/apollo-client/apollo-client.module.scss.js +8 -7
  110. package/dist/react/apollo-client/apollo-client.module.scss.js.map +1 -1
  111. package/dist/react/apollo-client/apollo-client.util.js +57 -75
  112. package/dist/react/apollo-client/apollo-client.util.js.map +1 -1
  113. package/dist/react/apollo-client/index.js +7 -30
  114. package/dist/react/apollo-client/links/index.js +2 -5
  115. package/dist/react/apollo-client/links/upload.js +63 -107
  116. package/dist/react/apollo-client/links/upload.js.map +1 -1
  117. package/dist/react/apollo-client-nextjs/apollo-client-nextjs.component.js +14 -16
  118. package/dist/react/apollo-client-nextjs/apollo-client-nextjs.component.js.map +1 -1
  119. package/dist/react/apollo-client-nextjs/apollo-client-nextjs.rsc.js +8 -7
  120. package/dist/react/apollo-client-nextjs/apollo-client-nextjs.rsc.js.map +1 -1
  121. package/dist/react/apollo-client-nextjs/apollo-client-nextjs.util.js +14 -14
  122. package/dist/react/apollo-client-nextjs/apollo-client-nextjs.util.js.map +1 -1
  123. package/dist/react/apollo-client-nextjs/index.js +3 -7
  124. package/dist/react/apollo-error/apollo-error.component.js +57 -69
  125. package/dist/react/apollo-error/apollo-error.component.js.map +1 -1
  126. package/dist/react/apollo-error/apollo-error.context.js +7 -6
  127. package/dist/react/apollo-error/apollo-error.context.js.map +1 -1
  128. package/dist/react/apollo-error/apollo-error.hook.js +10 -10
  129. package/dist/react/apollo-error/apollo-error.hook.js.map +1 -1
  130. package/dist/react/apollo-error/apollo-error.module.scss.js +15 -11
  131. package/dist/react/apollo-error/apollo-error.module.scss.js.map +1 -1
  132. package/dist/react/apollo-error/apollo-error.provider.js +31 -25
  133. package/dist/react/apollo-error/apollo-error.provider.js.map +1 -1
  134. package/dist/react/apollo-error/apollo-error.util.js +14 -16
  135. package/dist/react/apollo-error/apollo-error.util.js.map +1 -1
  136. package/dist/react/apollo-error/index.js +5 -15
  137. package/dist/react/i18next/i18next.hook.js +7 -6
  138. package/dist/react/i18next/i18next.hook.js.map +1 -1
  139. package/dist/react/i18next/i18next.util.js +9 -8
  140. package/dist/react/i18next/i18next.util.js.map +1 -1
  141. package/dist/react/i18next/index.js +3 -7
  142. package/dist/react/loading/index.js +3 -9
  143. package/dist/react/loading/loading.component.js +47 -48
  144. package/dist/react/loading/loading.component.js.map +1 -1
  145. package/dist/react/loading/loading.context.js +7 -6
  146. package/dist/react/loading/loading.context.js.map +1 -1
  147. package/dist/react/loading/loading.hook.js +11 -11
  148. package/dist/react/loading/loading.hook.js.map +1 -1
  149. package/dist/react/loading/loading.module.scss.js +18 -15
  150. package/dist/react/loading/loading.module.scss.js.map +1 -1
  151. package/dist/react/loading/loading.provider.js +26 -18
  152. package/dist/react/loading/loading.provider.js.map +1 -1
  153. package/dist/react/log/index.js +2 -6
  154. package/dist/react/log/log.util.d.ts +8 -0
  155. package/dist/react/log/log.util.js +33 -29
  156. package/dist/react/log/log.util.js.map +1 -1
  157. package/dist/react/next-intl/index.js +6 -14
  158. package/dist/react/next-intl/next-intl.constant.js +16 -15
  159. package/dist/react/next-intl/next-intl.constant.js.map +1 -1
  160. package/dist/react/next-intl/next-intl.context.js +7 -6
  161. package/dist/react/next-intl/next-intl.context.js.map +1 -1
  162. package/dist/react/next-intl/next-intl.hoc.js +20 -23
  163. package/dist/react/next-intl/next-intl.hoc.js.map +1 -1
  164. package/dist/react/next-intl/next-intl.hook.js +13 -14
  165. package/dist/react/next-intl/next-intl.hook.js.map +1 -1
  166. package/dist/react/next-intl/next-intl.provider.js +28 -23
  167. package/dist/react/next-intl/next-intl.provider.js.map +1 -1
  168. package/dist/react/storage/index.js +3 -7
  169. package/dist/react/storage/storage.hook.js +61 -58
  170. package/dist/react/storage/storage.hook.js.map +1 -1
  171. package/dist/react/storage/storage.util.js +36 -68
  172. package/dist/react/storage/storage.util.js.map +1 -1
  173. package/dist/react/toast/index.js +2 -6
  174. package/dist/react/userback/index.js +2 -5
  175. package/dist/react/userback/userback.component.js +27 -27
  176. package/dist/react/userback/userback.component.js.map +1 -1
  177. package/dist/style.css +2 -1
  178. package/dist/typescript/common.type.js +8 -5
  179. package/dist/typescript/common.type.js.map +1 -1
  180. package/dist/typescript/index.js +2 -5
  181. package/dist/util/common/common.util.js +119 -48
  182. package/dist/util/common/common.util.js.map +1 -1
  183. package/dist/util/common/index.js +2 -9
  184. package/dist/util/index.js +6 -27
  185. package/dist/util/object/index.d.ts +3 -0
  186. package/dist/util/object/index.js +2 -10
  187. package/dist/util/object/object.util.js +94 -128
  188. package/dist/util/object/object.util.js.map +1 -1
  189. package/dist/util/serializer/index.js +2 -5
  190. package/dist/util/serializer/serializer.util.js +73 -78
  191. package/dist/util/serializer/serializer.util.js.map +1 -1
  192. package/dist/util/string/index.d.ts +3 -0
  193. package/dist/util/string/index.js +2 -10
  194. package/dist/util/string/string.util.js +59 -71
  195. package/dist/util/string/string.util.js.map +1 -1
  196. package/dist/util/validate/index.d.ts +3 -0
  197. package/dist/util/validate/index.js +2 -5
  198. package/dist/util/validate/validate.util.js +13 -39
  199. package/dist/util/validate/validate.util.js.map +1 -1
  200. package/package.json +29 -27
  201. package/dist/config/env/index.js.map +0 -1
  202. package/dist/config/graphql-codegen/index.js.map +0 -1
  203. package/dist/config/index.js.map +0 -1
  204. package/dist/config/storybook/index.js.map +0 -1
  205. package/dist/config/vitest/index.js.map +0 -1
  206. package/dist/constant/index.js.map +0 -1
  207. package/dist/node/apollo-server/index.js.map +0 -1
  208. package/dist/node/command/index.js.map +0 -1
  209. package/dist/node/express/index.js.map +0 -1
  210. package/dist/node/fs/index.js.map +0 -1
  211. package/dist/node/log/index.js.map +0 -1
  212. package/dist/node/mongo/index.js.map +0 -1
  213. package/dist/node/package/index.js.map +0 -1
  214. package/dist/node/path/index.js.map +0 -1
  215. package/dist/node/storage/index.js.map +0 -1
  216. package/dist/node/upload/index.js.map +0 -1
  217. package/dist/node/ws/index.js.map +0 -1
  218. package/dist/node_modules/.pnpm/vitest@4.0.18_@types_node@25.3.5_jiti@2.6.1_jsdom@28.1.0_@noble_hashes@1.8.0__sass@1.97.3_tsx@4.21.0_yaml@2.8.2/node_modules/vitest/dist/config.js +0 -7
  219. package/dist/node_modules/.pnpm/vitest@4.0.18_@types_node@25.3.5_jiti@2.6.1_jsdom@28.1.0_@noble_hashes@1.8.0__sass@1.97.3_tsx@4.21.0_yaml@2.8.2/node_modules/vitest/dist/config.js.map +0 -1
  220. package/dist/react/apollo-client/index.js.map +0 -1
  221. package/dist/react/apollo-client/links/index.js.map +0 -1
  222. package/dist/react/apollo-client-nextjs/index.js.map +0 -1
  223. package/dist/react/apollo-error/index.js.map +0 -1
  224. package/dist/react/i18next/index.js.map +0 -1
  225. package/dist/react/loading/index.js.map +0 -1
  226. package/dist/react/log/index.js.map +0 -1
  227. package/dist/react/next-intl/index.js.map +0 -1
  228. package/dist/react/storage/index.js.map +0 -1
  229. package/dist/react/toast/index.js.map +0 -1
  230. package/dist/react/userback/index.js.map +0 -1
  231. package/dist/typescript/index.js.map +0 -1
  232. package/dist/util/common/index.js.map +0 -1
  233. package/dist/util/index.js.map +0 -1
  234. package/dist/util/object/index.js.map +0 -1
  235. package/dist/util/serializer/index.js.map +0 -1
  236. package/dist/util/string/index.js.map +0 -1
  237. package/dist/util/validate/index.js.map +0 -1
@@ -1,468 +1,334 @@
1
- import { MONGO_SLUG_MAX_ATTEMPTS as S } from "./mongo.constant.js";
2
- import { populateDynamicVirtuals as f, filterDynamicVirtualsFromPopulate as y, isObject as O } from "./mongo.dynamic-populate.js";
3
- import { normalizeMongoFilter as d } from "../../util/object/object.util.js";
4
- import { generateShortId as b, generateSlug as p, generateRandomString as V } from "../../util/string/string.util.js";
5
- import { RESPONSE_STATUS as g } from "../../constant/response-status.js";
6
- import { catchError as u } from "../log/log.util.js";
7
- class R {
8
- /**
9
- * Creates a new Mongoose controller instance.
10
- *
11
- * @param model - The Mongoose model to operate on.
12
- */
13
- constructor(r) {
14
- this.model = r;
15
- }
16
- /**
17
- * Gets the model name for logging and error messages.
18
- *
19
- * @returns The name of the model.
20
- */
21
- getModelName() {
22
- return this.model.modelName;
23
- }
24
- /**
25
- * Gets the dynamic virtuals configuration from the model instance.
26
- *
27
- * @returns Array of dynamic virtual configurations or undefined if none exist.
28
- */
29
- getDynamicVirtuals() {
30
- if (this.model._virtualConfigs) {
31
- const s = this.model._virtualConfigs.filter((t) => typeof t.options?.ref == "function");
32
- if (s.length > 0)
33
- return s;
34
- }
35
- return this.model.schema.statics._dynamicVirtuals;
36
- }
37
- /**
38
- * Populates dynamic virtuals for a single document.
39
- *
40
- * @param result - The document to populate dynamic virtuals for.
41
- * @param populate - The populate options to determine which virtuals to populate.
42
- * @returns The document with dynamic virtuals populated.
43
- */
44
- async populateDynamicVirtualsForDocument(r, e) {
45
- const s = this.getDynamicVirtuals();
46
- if (s && s.length > 0) {
47
- const t = await f(this.model.base, [r], s, e, void 0, this.model);
48
- return t && t[0] ? t[0] : r;
49
- }
50
- return r;
51
- }
52
- /**
53
- * Populates dynamic virtuals for an array of documents.
54
- *
55
- * @param results - The documents to populate dynamic virtuals for.
56
- * @param populate - The populate options to determine which virtuals to populate.
57
- * @returns The documents with dynamic virtuals populated.
58
- */
59
- async populateDynamicVirtualsForDocuments(r, e) {
60
- const s = this.getDynamicVirtuals();
61
- return s && s.length > 0 && r.length > 0 ? await f(this.model.base, r, s, e, void 0, this.model) : r;
62
- }
63
- /**
64
- * Finds a single document with optional population and projection.
65
- * Automatically handles dynamic virtual population if configured.
66
- *
67
- * @param filter - The filter criteria to find the document.
68
- * @param projection - The fields to include/exclude in the result.
69
- * @param options - Query options for the operation.
70
- * @param populate - Population configuration for related documents.
71
- * @returns A promise that resolves to a standardized response with the found document.
72
- */
73
- async findOne(r = {}, e = {}, s = {}, t) {
74
- try {
75
- const a = d(r), c = this.model.findOne(a, e, s).maxTimeMS(3e4).lean(), n = this.getDynamicVirtuals(), i = y(t, n);
76
- i && c.populate(i);
77
- const o = await c.exec();
78
- if (!o)
79
- return {
80
- success: !1,
81
- message: `No ${this.getModelName()} found.`,
82
- code: g.NOT_FOUND.CODE
83
- };
84
- const l = await this.populateDynamicVirtualsForDocument(o, t);
85
- return { success: !0, result: l?.toObject?.() ?? l };
86
- } catch (a) {
87
- return u(a);
88
- }
89
- }
90
- /**
91
- * Finds all documents with optional population and projection.
92
- * Automatically handles dynamic virtual population if configured.
93
- *
94
- * @param filter - The filter criteria to find documents.
95
- * @param projection - The fields to include/exclude in the result.
96
- * @param options - Query options for the operation.
97
- * @param populate - Population configuration for related documents.
98
- * @returns A promise that resolves to a standardized response with the found documents.
99
- */
100
- async findAll(r = {}, e = {}, s = {}, t) {
101
- try {
102
- const a = d(r), c = this.model.find(a, e, s).maxTimeMS(3e4).lean();
103
- s.limit || c.limit(1e4);
104
- const n = this.getDynamicVirtuals(), i = y(t, n);
105
- i && c.populate(i);
106
- const o = await c.exec();
107
- return { success: !0, result: (await this.populateDynamicVirtualsForDocuments(o, t)).map((m) => m?.toObject?.() ?? m) };
108
- } catch (a) {
109
- return u(a);
110
- }
111
- }
112
- /**
113
- * Finds documents with pagination support.
114
- * Automatically handles dynamic virtual population if configured.
115
- *
116
- * @param filter - The filter criteria to find documents.
117
- * @param options - Pagination options including page, limit, and population.
118
- * @returns A promise that resolves to a standardized response with paginated results.
119
- */
120
- async findPaging(r = {}, e = {}) {
121
- try {
122
- const s = d(r), t = this.getDynamicVirtuals(), a = { ...e };
123
- e.populate && (a.populate = y(e.populate, t));
124
- const c = await this.model.paginate(s, a);
125
- if (t && t.length > 0) {
126
- const n = await this.populateDynamicVirtualsForDocuments(c.docs, e.populate);
127
- return { success: !0, result: { ...c, docs: n.map((i) => i?.toObject?.() ?? i) } };
128
- }
129
- return { success: !0, result: { ...c, docs: c.docs.map((n) => n?.toObject?.() ?? n) } };
130
- } catch (s) {
131
- return u(s);
132
- }
133
- }
134
- /**
135
- * Performs aggregation with pagination support.
136
- *
137
- * @param pipeline - The aggregation pipeline stages.
138
- * @param options - Pagination options for the aggregation result.
139
- * @returns A promise that resolves to a standardized response with paginated aggregation results.
140
- */
141
- async findPagingAggregate(r, e = {}) {
142
- try {
143
- const s = this.getDynamicVirtuals(), t = { ...e };
144
- e.populate && (t.populate = y(e.populate, s));
145
- const a = await this.model.aggregatePaginate(
146
- this.model.aggregate(r),
147
- t
148
- ), c = await this.populateDynamicVirtualsForDocuments(a.docs, e.populate);
149
- return { success: !0, result: { ...a, docs: c.map((n) => n?.toObject?.() ?? n) } };
150
- } catch (s) {
151
- return u(s);
152
- }
153
- }
154
- /**
155
- * Counts documents matching the filter criteria.
156
- *
157
- * @param filter - The filter criteria to count documents.
158
- * @returns A promise that resolves to a standardized response with the document count.
159
- */
160
- async count(r = {}) {
161
- try {
162
- const e = d(r);
163
- return { success: !0, result: await this.model.countDocuments(e) };
164
- } catch (e) {
165
- return u(e);
166
- }
167
- }
168
- /**
169
- * Creates a single document.
170
- *
171
- * @param doc - The document to create.
172
- * @returns A promise that resolves to a standardized response with the created document.
173
- */
174
- async createOne(r) {
175
- try {
176
- const e = await this.model.create(r);
177
- return { success: !0, result: e?.toObject?.() ?? e };
178
- } catch (e) {
179
- return u(e);
180
- }
181
- }
182
- /**
183
- * Creates multiple documents with bulk insertion.
184
- *
185
- * @param docs - An array of documents to create.
186
- * @param options - Options for the bulk insertion operation.
187
- * @returns A promise that resolves to a standardized response with the created documents.
188
- */
189
- async createMany(r, e = {}) {
190
- try {
191
- return { success: !0, result: (await this.model.insertMany(r, e)).map((t) => t?.toObject?.() ?? t) };
192
- } catch (s) {
193
- return u(s);
194
- }
195
- }
196
- /**
197
- * Updates a single document and returns the updated version.
198
- *
199
- * @param filter - The filter criteria to find the document to update.
200
- * @param update - The update data to apply.
201
- * @param options - Options for the update operation.
202
- * @returns A promise that resolves to a standardized response with the updated document.
203
- */
204
- async updateOne(r = {}, e = {}, s = {}) {
205
- try {
206
- const t = d(r), a = await this.model.findOneAndUpdate(t, e, {
207
- new: !0,
208
- ...s
209
- }).exec();
210
- return a ? { success: !0, result: a?.toObject?.() ?? a } : {
211
- success: !1,
212
- message: `Failed to update ${this.getModelName()}.`,
213
- code: g.NOT_FOUND.CODE
214
- };
215
- } catch (t) {
216
- return u(t);
217
- }
218
- }
219
- /**
220
- * Updates multiple documents matching the filter criteria.
221
- *
222
- * @param filter - The filter criteria to find documents to update.
223
- * @param update - The update data to apply.
224
- * @param options - Options for the update operation.
225
- * @returns A promise that resolves to a standardized response with the update result.
226
- */
227
- async updateMany(r = {}, e = {}, s = {}) {
228
- try {
229
- const t = d(r);
230
- return { success: !0, result: await this.model.updateMany(t, e, s).exec() };
231
- } catch (t) {
232
- return u(t);
233
- }
234
- }
235
- /**
236
- * Deletes a single document and returns the deleted version.
237
- *
238
- * @param filter - The filter criteria to find the document to delete.
239
- * @param options - Options for the delete operation.
240
- * @returns A promise that resolves to a standardized response with the deleted document.
241
- */
242
- async deleteOne(r = {}, e = {}) {
243
- try {
244
- const s = d(r), t = await this.model.findOneAndDelete(s, e).exec();
245
- return t ? { success: !0, result: t?.toObject?.() ?? t } : {
246
- success: !1,
247
- message: `No ${this.getModelName()} found to delete.`,
248
- code: g.NOT_FOUND.CODE
249
- };
250
- } catch (s) {
251
- return u(s);
252
- }
253
- }
254
- /**
255
- * Deletes multiple documents matching the filter criteria.
256
- *
257
- * @param filter - The filter criteria to find documents to delete.
258
- * @param options - Options for the delete operation.
259
- * @returns A promise that resolves to a standardized response with the delete result.
260
- */
261
- async deleteMany(r = {}, e = {}) {
262
- try {
263
- const s = d(r), t = await this.model.deleteMany(s, e).exec();
264
- return t.deletedCount === 0 ? {
265
- success: !1,
266
- message: "No documents found to delete.",
267
- code: g.NOT_FOUND.CODE
268
- } : { success: !0, result: t };
269
- } catch (s) {
270
- return u(s);
271
- }
272
- }
273
- /**
274
- * Creates a unique short ID based on a given ID.
275
- * This method generates multiple short IDs with increasing lengths and finds the first available one.
276
- *
277
- * @param id - The base ID to generate short IDs from.
278
- * @param length - The initial length for short ID generation (default: 4).
279
- * @returns A promise that resolves to a standardized response with the unique short ID.
280
- */
281
- async createShortId(r, e = 4) {
282
- try {
283
- const t = Array.from({ length: 10 }, (n, i) => b(r, i + e)), c = (await Promise.all(
284
- t.map((n) => this.model.exists({ shortId: n }))
285
- )).findIndex((n) => !n);
286
- if (c !== -1) {
287
- const n = t[c];
288
- if (n)
289
- return { success: !0, result: n };
290
- }
291
- return {
292
- success: !1,
293
- message: "Failed to create a unique shortId",
294
- code: g.INTERNAL_SERVER_ERROR.CODE
295
- };
296
- } catch (s) {
297
- return u(s);
298
- }
299
- }
300
- /**
301
- * Creates a query for slug existence checking.
302
- * This method generates a query that checks for slug existence in both current and historical slug fields.
303
- *
304
- * @param options - Configuration for slug query generation including slug, field, and filter.
305
- * @param options.slug - The slug string to check for existence.
306
- * @param options.field - The field name for object-based slug checking.
307
- * @param options.isObject - Whether the slug is stored as an object with nested fields.
308
- * @param options.haveHistory - Whether to check historical slug fields for existence.
309
- * @param options.filter - Additional filter conditions to apply to the query.
310
- * @returns A MongoDB query object for checking slug existence.
311
- */
312
- createSlugQuery({ slug: r, field: e, isObject: s, haveHistory: t = !1, filter: a }) {
313
- const c = { ...a ?? {} };
314
- return s ? {
315
- ...c,
316
- $or: [
317
- { [`slug.${e}`]: r },
318
- ...t ? [{ slugHistory: { $elemMatch: { [`slug.${e}`]: r } } }] : []
319
- ]
320
- } : {
321
- ...c,
322
- $or: [
323
- { slug: r },
324
- ...t ? [{ slugHistory: r }] : []
325
- ]
326
- };
327
- }
328
- /**
329
- * Creates a unique slug based on a given string.
330
- * This method generates multiple slug variations and finds the first available one.
331
- *
332
- * @param options - Configuration for slug generation including slug, field, and filter.
333
- * @param options.slug - The base slug string to make unique.
334
- * @param options.field - The field name for object-based slug checking.
335
- * @param options.isObject - Whether the slug is stored as an object with nested fields.
336
- * @param options.haveHistory - Whether to check historical slug fields for uniqueness.
337
- * @param options.filter - Additional filter conditions to apply when checking slug existence.
338
- * @returns A promise that resolves to a unique slug string.
339
- */
340
- async createUniqueSlug({ slug: r, field: e, isObject: s, haveHistory: t, filter: a }) {
341
- if (!r || typeof r != "string")
342
- throw new Error("Invalid slug provided: must be a non-empty string");
343
- const c = p(r);
344
- if (!await this.model.exists(
345
- this.createSlugQuery({ slug: c, field: e, isObject: s, haveHistory: t, filter: a })
346
- ))
347
- return c;
348
- for (let l = 1; l <= S; l++) {
349
- const m = `${c}-${l}`;
350
- if (!await this.model.exists(
351
- this.createSlugQuery({ slug: m, field: e, isObject: s, haveHistory: t, filter: a })
352
- ))
353
- return m;
354
- }
355
- const i = Date.now(), o = V(6);
356
- return `${c}-${i}-${o}`;
357
- }
358
- /**
359
- * Creates a slug for a document field.
360
- * This method handles both simple string fields and object fields with nested slug generation.
361
- *
362
- * @param options - Configuration for slug creation including field, source document, and filter.
363
- * @param options.field - The field name to create a slug for.
364
- * @param options.from - The source document containing the field value.
365
- * @param options.haveHistory - Whether to check historical slug fields for uniqueness.
366
- * @param options.filter - Additional filter conditions to apply when checking slug existence.
367
- * @returns A promise that resolves to a standardized response with the created slug(s).
368
- */
369
- async createSlug({ field: r, from: e, filter: s, haveHistory: t }) {
370
- try {
371
- const a = e[r];
372
- return O(a) ? { success: !0, result: Object.fromEntries(
373
- await Promise.all(
374
- Object.entries(a).map(async ([o, l]) => {
375
- const m = await this.createUniqueSlug({
376
- slug: l,
377
- field: o,
378
- isObject: !0,
379
- haveHistory: t,
380
- filter: s
381
- });
382
- return [o, m];
383
- })
384
- )
385
- ) } : { success: !0, result: await this.createUniqueSlug({
386
- slug: a,
387
- field: r,
388
- isObject: !1,
389
- haveHistory: t,
390
- filter: s
391
- }) };
392
- } catch (a) {
393
- return u(a);
394
- }
395
- }
396
- /**
397
- * Checks if a slug already exists in the collection.
398
- * This method verifies slug existence in both current and historical slug fields.
399
- *
400
- * @param options - Configuration for slug checking including slug, field, source document, and filter.
401
- * @param options.slug - The slug string to check for existence.
402
- * @param options.field - The field name for object-based slug checking.
403
- * @param options.from - The source document containing the field value.
404
- * @param options.haveHistory - Whether to check historical slug fields for existence.
405
- * @param options.filter - Additional filter conditions to apply to the query.
406
- * @returns A promise that resolves to a standardized response indicating whether the slug exists.
407
- */
408
- async checkSlug({ slug: r, field: e, from: s, filter: t, haveHistory: a }) {
409
- try {
410
- const c = s[e];
411
- if (O(c)) {
412
- const m = Object.values(c).map((h) => p(h));
413
- return (await Promise.all(
414
- m.map(
415
- (h) => this.model.exists(this.createSlugQuery({
416
- slug: h,
417
- field: e,
418
- isObject: !0,
419
- haveHistory: a,
420
- filter: t
421
- }))
422
- )
423
- )).some((h) => h) ? { success: !0, result: !0 } : { success: !0, result: !1 };
424
- }
425
- const i = p(r);
426
- return { success: !0, result: await this.model.exists(this.createSlugQuery({
427
- slug: i,
428
- field: e,
429
- isObject: !1,
430
- filter: t
431
- })) !== null };
432
- } catch (c) {
433
- return u(c);
434
- }
435
- }
436
- /**
437
- * Performs aggregation operations on the collection.
438
- *
439
- * @param pipeline - The aggregation pipeline stages to execute.
440
- * @returns A promise that resolves to a standardized response with the aggregation results.
441
- */
442
- async aggregate(r) {
443
- try {
444
- return { success: !0, result: await this.model.aggregate(r) };
445
- } catch (e) {
446
- return u(e);
447
- }
448
- }
449
- /**
450
- * Retrieves distinct values for the specified key from the collection.
451
- *
452
- * @param key - The field for which to return distinct values.
453
- * @param filter - The filter query to apply (optional).
454
- * @param options - Additional options for the distinct operation (optional).
455
- * @returns A promise that resolves to a standardized response with the array of distinct values.
456
- */
457
- async distinct(r, e = {}, s = {}) {
458
- try {
459
- return { success: !0, result: await this.model.distinct(r, e, s) };
460
- } catch (t) {
461
- return u(t);
462
- }
463
- }
464
- }
465
- export {
466
- R as MongooseController
1
+ import { normalizeMongoFilter as e } from "../../util/object/object.util.js";
2
+ import { generateRandomString as t, generateShortId as n, generateSlug as r } from "../../util/string/string.util.js";
3
+ import { RESPONSE_STATUS as i } from "../../constant/response-status.js";
4
+ import { catchError as a } from "../log/log.util.js";
5
+ import "./mongo.constant.js";
6
+ import { filterDynamicVirtualsFromPopulate as o, isObject as s, populateDynamicVirtuals as c } from "./mongo.dynamic-populate.js";
7
+ //#region src/node/mongo/mongo.controller.mongoose.ts
8
+ var l = class {
9
+ defaultLimit;
10
+ constructor(e, t) {
11
+ this.model = e, this.defaultLimit = t?.defaultLimit ?? 1e4;
12
+ }
13
+ getModelName() {
14
+ return this.model.modelName;
15
+ }
16
+ getDynamicVirtuals() {
17
+ let e = this.model;
18
+ if (e._virtualConfigs) {
19
+ let t = e._virtualConfigs.filter((e) => typeof e.options?.ref == "function");
20
+ if (t.length > 0) return t;
21
+ }
22
+ return this.model.schema.statics._dynamicVirtuals;
23
+ }
24
+ async populateDynamicVirtualsForDocument(e, t) {
25
+ let n = this.getDynamicVirtuals();
26
+ if (n && n.length > 0) {
27
+ let r = await c(this.model.base, [e], n, t, void 0, this.model);
28
+ return r && r[0] ? r[0] : e;
29
+ }
30
+ return e;
31
+ }
32
+ async populateDynamicVirtualsForDocuments(e, t) {
33
+ let n = this.getDynamicVirtuals();
34
+ return n && n.length > 0 && e.length > 0 ? await c(this.model.base, e, n, t, void 0, this.model) : e;
35
+ }
36
+ async findOne(t = {}, n = {}, r = {}, s) {
37
+ try {
38
+ let a = e(t), c = this.model.findOne(a, n, r).maxTimeMS(3e4).lean(), l = o(s, this.getDynamicVirtuals());
39
+ l && c.populate(l);
40
+ let u = await c.exec();
41
+ if (!u) return {
42
+ success: !1,
43
+ message: `No ${this.getModelName()} found.`,
44
+ code: i.NOT_FOUND.CODE
45
+ };
46
+ let d = await this.populateDynamicVirtualsForDocument(u, s);
47
+ return {
48
+ success: !0,
49
+ result: d?.toObject?.() ?? d
50
+ };
51
+ } catch (e) {
52
+ return a(e);
53
+ }
54
+ }
55
+ async findAll(t = {}, n = {}, r = {}, i) {
56
+ try {
57
+ let a = e(t), s = this.model.find(a, n, r).maxTimeMS(3e4).lean();
58
+ r.limit || s.limit(this.defaultLimit);
59
+ let c = o(i, this.getDynamicVirtuals());
60
+ c && s.populate(c);
61
+ let l = await s.exec();
62
+ return {
63
+ success: !0,
64
+ result: (await this.populateDynamicVirtualsForDocuments(l, i)).map((e) => e?.toObject?.() ?? e)
65
+ };
66
+ } catch (e) {
67
+ return a(e);
68
+ }
69
+ }
70
+ async findPaging(t = {}, n = {}) {
71
+ try {
72
+ let r = e(t), i = this.getDynamicVirtuals(), a = { ...n };
73
+ n.populate && (a.populate = o(n.populate, i));
74
+ let s = await this.model.paginate(r, a);
75
+ if (i && i.length > 0) {
76
+ let e = await this.populateDynamicVirtualsForDocuments(s.docs, n.populate);
77
+ return {
78
+ success: !0,
79
+ result: {
80
+ ...s,
81
+ docs: e.map((e) => e?.toObject?.() ?? e)
82
+ }
83
+ };
84
+ }
85
+ return {
86
+ success: !0,
87
+ result: {
88
+ ...s,
89
+ docs: s.docs.map((e) => e?.toObject?.() ?? e)
90
+ }
91
+ };
92
+ } catch (e) {
93
+ return a(e);
94
+ }
95
+ }
96
+ async findPagingAggregate(e, t = {}) {
97
+ try {
98
+ let n = this.getDynamicVirtuals(), r = { ...t };
99
+ t.populate && (r.populate = o(t.populate, n));
100
+ let i = await this.model.aggregatePaginate(this.model.aggregate(e), r), a = await this.populateDynamicVirtualsForDocuments(i.docs, t.populate);
101
+ return {
102
+ success: !0,
103
+ result: {
104
+ ...i,
105
+ docs: a.map((e) => e?.toObject?.() ?? e)
106
+ }
107
+ };
108
+ } catch (e) {
109
+ return a(e);
110
+ }
111
+ }
112
+ async count(t = {}) {
113
+ try {
114
+ let n = e(t);
115
+ return {
116
+ success: !0,
117
+ result: await this.model.countDocuments(n)
118
+ };
119
+ } catch (e) {
120
+ return a(e);
121
+ }
122
+ }
123
+ async createOne(e) {
124
+ try {
125
+ let t = await this.model.create(e);
126
+ return {
127
+ success: !0,
128
+ result: t?.toObject?.() ?? t
129
+ };
130
+ } catch (e) {
131
+ return a(e);
132
+ }
133
+ }
134
+ async createMany(e, t = {}) {
135
+ try {
136
+ return {
137
+ success: !0,
138
+ result: (await this.model.insertMany(e, t)).map((e) => e?.toObject?.() ?? e)
139
+ };
140
+ } catch (e) {
141
+ return a(e);
142
+ }
143
+ }
144
+ async updateOne(t = {}, n = {}, r = {}) {
145
+ try {
146
+ let a = e(t), o = await this.model.findOneAndUpdate(a, n, {
147
+ new: !0,
148
+ ...r
149
+ }).exec();
150
+ return o ? {
151
+ success: !0,
152
+ result: o?.toObject?.() ?? o
153
+ } : {
154
+ success: !1,
155
+ message: `Failed to update ${this.getModelName()}.`,
156
+ code: i.NOT_FOUND.CODE
157
+ };
158
+ } catch (e) {
159
+ return a(e);
160
+ }
161
+ }
162
+ async updateMany(t = {}, n = {}, r = {}) {
163
+ try {
164
+ let i = e(t);
165
+ return {
166
+ success: !0,
167
+ result: await this.model.updateMany(i, n, r).exec()
168
+ };
169
+ } catch (e) {
170
+ return a(e);
171
+ }
172
+ }
173
+ async deleteOne(t = {}, n = {}) {
174
+ try {
175
+ let r = e(t), a = await this.model.findOneAndDelete(r, n).exec();
176
+ return a ? {
177
+ success: !0,
178
+ result: a?.toObject?.() ?? a
179
+ } : {
180
+ success: !1,
181
+ message: `No ${this.getModelName()} found to delete.`,
182
+ code: i.NOT_FOUND.CODE
183
+ };
184
+ } catch (e) {
185
+ return a(e);
186
+ }
187
+ }
188
+ async deleteMany(t = {}, n = {}) {
189
+ try {
190
+ let r = e(t), a = await this.model.deleteMany(r, n).exec();
191
+ return a.deletedCount === 0 ? {
192
+ success: !1,
193
+ message: "No documents found to delete.",
194
+ code: i.NOT_FOUND.CODE
195
+ } : {
196
+ success: !0,
197
+ result: a
198
+ };
199
+ } catch (e) {
200
+ return a(e);
201
+ }
202
+ }
203
+ async createShortId(e, t = 4) {
204
+ try {
205
+ let r = Array.from({ length: 10 }, (r, i) => n(e, i + t)), a = (await Promise.all(r.map((e) => this.model.exists({ shortId: e })))).findIndex((e) => !e);
206
+ if (a !== -1) {
207
+ let e = r[a];
208
+ if (e) return {
209
+ success: !0,
210
+ result: e
211
+ };
212
+ }
213
+ return {
214
+ success: !1,
215
+ message: "Failed to create a unique shortId",
216
+ code: i.INTERNAL_SERVER_ERROR.CODE
217
+ };
218
+ } catch (e) {
219
+ return a(e);
220
+ }
221
+ }
222
+ createSlugQuery({ slug: e, field: t, isObject: n, haveHistory: r = !1, filter: i }) {
223
+ let a = { ...i ?? {} };
224
+ return n ? {
225
+ ...a,
226
+ $or: [{ [`slug.${t}`]: e }, ...r ? [{ slugHistory: { $elemMatch: { [`slug.${t}`]: e } } }] : []]
227
+ } : {
228
+ ...a,
229
+ $or: [{ slug: e }, ...r ? [{ slugHistory: e }] : []]
230
+ };
231
+ }
232
+ async createUniqueSlug({ slug: e, field: n, isObject: i, haveHistory: a, filter: o }) {
233
+ if (!e || typeof e != "string") throw Error("Invalid slug provided: must be a non-empty string");
234
+ let s = r(e);
235
+ if (!await this.model.exists(this.createSlugQuery({
236
+ slug: s,
237
+ field: n,
238
+ isObject: i,
239
+ haveHistory: a,
240
+ filter: o
241
+ }))) return s;
242
+ let c = Array.from({ length: 100 }, (e, t) => `${s}-${t + 1}`), l = c.map((e) => this.createSlugQuery({
243
+ slug: e,
244
+ field: n,
245
+ isObject: i,
246
+ haveHistory: a,
247
+ filter: o
248
+ })), u = i ? `slug.${n}` : "slug", d = await this.model.find({ $or: l.map((e) => e.$or).flat() }).select(u).lean(), f = new Set(d.map((e) => i ? e?.slug?.[n] : e?.slug));
249
+ return c.find((e) => !f.has(e)) || `${s}-${Date.now()}-${t(6)}`;
250
+ }
251
+ async createSlug({ field: e, from: t, filter: n, haveHistory: r }) {
252
+ try {
253
+ let i = t[e];
254
+ return s(i) ? {
255
+ success: !0,
256
+ result: Object.fromEntries(await Promise.all(Object.entries(i).map(async ([e, t]) => [e, await this.createUniqueSlug({
257
+ slug: t,
258
+ field: e,
259
+ isObject: !0,
260
+ haveHistory: r,
261
+ filter: n
262
+ })])))
263
+ } : {
264
+ success: !0,
265
+ result: await this.createUniqueSlug({
266
+ slug: i,
267
+ field: e,
268
+ isObject: !1,
269
+ haveHistory: r,
270
+ filter: n
271
+ })
272
+ };
273
+ } catch (e) {
274
+ return a(e);
275
+ }
276
+ }
277
+ async checkSlug({ slug: e, field: t, from: n, filter: i, haveHistory: o }) {
278
+ try {
279
+ let a = n[t];
280
+ if (s(a)) {
281
+ let e = Object.values(a).map((e) => r(e));
282
+ return (await Promise.all(e.map((e) => this.model.exists(this.createSlugQuery({
283
+ slug: e,
284
+ field: t,
285
+ isObject: !0,
286
+ haveHistory: o,
287
+ filter: i
288
+ }))))).some((e) => e) ? {
289
+ success: !0,
290
+ result: !0
291
+ } : {
292
+ success: !0,
293
+ result: !1
294
+ };
295
+ }
296
+ let c = r(e);
297
+ return {
298
+ success: !0,
299
+ result: await this.model.exists(this.createSlugQuery({
300
+ slug: c,
301
+ field: t,
302
+ isObject: !1,
303
+ filter: i
304
+ })) !== null
305
+ };
306
+ } catch (e) {
307
+ return a(e);
308
+ }
309
+ }
310
+ async aggregate(e) {
311
+ try {
312
+ return {
313
+ success: !0,
314
+ result: await this.model.aggregate(e)
315
+ };
316
+ } catch (e) {
317
+ return a(e);
318
+ }
319
+ }
320
+ async distinct(e, t = {}, n = {}) {
321
+ try {
322
+ return {
323
+ success: !0,
324
+ result: await this.model.distinct(e, t, n)
325
+ };
326
+ } catch (e) {
327
+ return a(e);
328
+ }
329
+ }
467
330
  };
468
- //# sourceMappingURL=mongo.controller.mongoose.js.map
331
+ //#endregion
332
+ export { l as MongooseController };
333
+
334
+ //# sourceMappingURL=mongo.controller.mongoose.js.map