@tsed/mongoose 6.87.2 → 6.90.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 (288) hide show
  1. package/lib/{MongooseModule.d.ts → cjs/MongooseModule.d.ts} +0 -0
  2. package/lib/cjs/MongooseModule.js +36 -0
  3. package/lib/cjs/MongooseModule.js.map +1 -0
  4. package/lib/{constants → cjs/constants}/index.d.ts +0 -0
  5. package/lib/cjs/constants/index.js +24 -0
  6. package/lib/cjs/constants/index.js.map +1 -0
  7. package/lib/{decorators → cjs/decorators}/auto.d.ts +0 -0
  8. package/lib/cjs/decorators/auto.js +28 -0
  9. package/lib/cjs/decorators/auto.js.map +1 -0
  10. package/lib/{decorators → cjs/decorators}/discriminatorKey.d.ts +0 -0
  11. package/lib/cjs/decorators/discriminatorKey.js +14 -0
  12. package/lib/cjs/decorators/discriminatorKey.js.map +1 -0
  13. package/lib/{decorators → cjs/decorators}/dynamicRef.d.ts +0 -0
  14. package/lib/cjs/decorators/dynamicRef.js +46 -0
  15. package/lib/cjs/decorators/dynamicRef.js.map +1 -0
  16. package/lib/{decorators → cjs/decorators}/excludeIndexes.d.ts +0 -0
  17. package/lib/cjs/decorators/excludeIndexes.js +28 -0
  18. package/lib/cjs/decorators/excludeIndexes.js.map +1 -0
  19. package/lib/{decorators → cjs/decorators}/expires.d.ts +0 -0
  20. package/lib/cjs/decorators/expires.js +28 -0
  21. package/lib/cjs/decorators/expires.js.map +1 -0
  22. package/lib/{decorators → cjs/decorators}/immutable.d.ts +0 -0
  23. package/lib/cjs/decorators/immutable.js +30 -0
  24. package/lib/cjs/decorators/immutable.js.map +1 -0
  25. package/lib/{decorators → cjs/decorators}/index.d.ts +0 -0
  26. package/lib/cjs/decorators/index.js +30 -0
  27. package/lib/cjs/decorators/index.js.map +1 -0
  28. package/lib/{decorators → cjs/decorators}/indexed.d.ts +0 -0
  29. package/lib/cjs/decorators/indexed.js +26 -0
  30. package/lib/cjs/decorators/indexed.js.map +1 -0
  31. package/lib/{decorators → cjs/decorators}/lowercase.d.ts +0 -0
  32. package/lib/cjs/decorators/lowercase.js +28 -0
  33. package/lib/cjs/decorators/lowercase.js.map +1 -0
  34. package/lib/{decorators → cjs/decorators}/model.d.ts +0 -0
  35. package/lib/cjs/decorators/model.js +67 -0
  36. package/lib/cjs/decorators/model.js.map +1 -0
  37. package/lib/{decorators → cjs/decorators}/mongooseIndex.d.ts +0 -0
  38. package/lib/cjs/decorators/mongooseIndex.js +39 -0
  39. package/lib/cjs/decorators/mongooseIndex.js.map +1 -0
  40. package/lib/cjs/decorators/mongooseIndexes.d.ts +32 -0
  41. package/lib/cjs/decorators/mongooseIndexes.js +39 -0
  42. package/lib/cjs/decorators/mongooseIndexes.js.map +1 -0
  43. package/lib/{decorators → cjs/decorators}/mongoosePlugin.d.ts +0 -0
  44. package/lib/cjs/decorators/mongoosePlugin.js +22 -0
  45. package/lib/cjs/decorators/mongoosePlugin.js.map +1 -0
  46. package/lib/{decorators → cjs/decorators}/numberDecimal.d.ts +0 -0
  47. package/lib/cjs/decorators/numberDecimal.js +78 -0
  48. package/lib/cjs/decorators/numberDecimal.js.map +1 -0
  49. package/lib/{decorators → cjs/decorators}/objectID.d.ts +0 -0
  50. package/lib/cjs/decorators/objectID.js +26 -0
  51. package/lib/cjs/decorators/objectID.js.map +1 -0
  52. package/lib/{decorators → cjs/decorators}/postHook.d.ts +0 -0
  53. package/lib/cjs/decorators/postHook.js +26 -0
  54. package/lib/cjs/decorators/postHook.js.map +1 -0
  55. package/lib/{decorators → cjs/decorators}/preHook.d.ts +0 -0
  56. package/lib/cjs/decorators/preHook.js +71 -0
  57. package/lib/cjs/decorators/preHook.js.map +1 -0
  58. package/lib/{decorators → cjs/decorators}/ref.d.ts +0 -0
  59. package/lib/cjs/decorators/ref.js +65 -0
  60. package/lib/cjs/decorators/ref.js.map +1 -0
  61. package/lib/{decorators → cjs/decorators}/schema.d.ts +0 -0
  62. package/lib/cjs/decorators/schema.js +32 -0
  63. package/lib/cjs/decorators/schema.js.map +1 -0
  64. package/lib/{decorators → cjs/decorators}/schemaIgnore.d.ts +0 -0
  65. package/lib/cjs/decorators/schemaIgnore.js +27 -0
  66. package/lib/cjs/decorators/schemaIgnore.js.map +1 -0
  67. package/lib/{decorators → cjs/decorators}/select.d.ts +0 -0
  68. package/lib/cjs/decorators/select.js +28 -0
  69. package/lib/cjs/decorators/select.js.map +1 -0
  70. package/lib/{decorators → cjs/decorators}/sparse.d.ts +0 -0
  71. package/lib/cjs/decorators/sparse.js +30 -0
  72. package/lib/cjs/decorators/sparse.js.map +1 -0
  73. package/lib/{decorators → cjs/decorators}/text.d.ts +0 -0
  74. package/lib/cjs/decorators/text.js +29 -0
  75. package/lib/cjs/decorators/text.js.map +1 -0
  76. package/lib/{decorators → cjs/decorators}/trim.d.ts +0 -0
  77. package/lib/cjs/decorators/trim.js +27 -0
  78. package/lib/cjs/decorators/trim.js.map +1 -0
  79. package/lib/{decorators → cjs/decorators}/unique.d.ts +0 -0
  80. package/lib/cjs/decorators/unique.js +27 -0
  81. package/lib/cjs/decorators/unique.js.map +1 -0
  82. package/lib/{decorators → cjs/decorators}/uppercase.d.ts +0 -0
  83. package/lib/cjs/decorators/uppercase.js +28 -0
  84. package/lib/cjs/decorators/uppercase.js.map +1 -0
  85. package/lib/{decorators → cjs/decorators}/versionKey.d.ts +0 -0
  86. package/lib/cjs/decorators/versionKey.js +12 -0
  87. package/lib/cjs/decorators/versionKey.js.map +1 -0
  88. package/lib/{decorators → cjs/decorators}/virtualRef.d.ts +0 -0
  89. package/lib/cjs/decorators/virtualRef.js +37 -0
  90. package/lib/cjs/decorators/virtualRef.js.map +1 -0
  91. package/lib/{index.d.ts → cjs/index.d.ts} +0 -0
  92. package/lib/cjs/index.js +10 -0
  93. package/lib/cjs/index.js.map +1 -0
  94. package/lib/{interfaces → cjs/interfaces}/MongooseConnectionOptions.d.ts +0 -0
  95. package/lib/cjs/interfaces/MongooseConnectionOptions.js +3 -0
  96. package/lib/cjs/interfaces/MongooseConnectionOptions.js.map +1 -0
  97. package/lib/{interfaces → cjs/interfaces}/MongooseDocument.d.ts +0 -0
  98. package/lib/cjs/interfaces/MongooseDocument.js +3 -0
  99. package/lib/cjs/interfaces/MongooseDocument.js.map +1 -0
  100. package/lib/{interfaces → cjs/interfaces}/MongooseModel.d.ts +0 -0
  101. package/lib/cjs/interfaces/MongooseModel.js +3 -0
  102. package/lib/cjs/interfaces/MongooseModel.js.map +1 -0
  103. package/lib/{interfaces → cjs/interfaces}/MongooseModelOptions.d.ts +0 -0
  104. package/lib/cjs/interfaces/MongooseModelOptions.js +3 -0
  105. package/lib/cjs/interfaces/MongooseModelOptions.js.map +1 -0
  106. package/lib/{interfaces → cjs/interfaces}/MongooseSchemaOptions.d.ts +0 -0
  107. package/lib/cjs/interfaces/MongooseSchemaOptions.js +3 -0
  108. package/lib/cjs/interfaces/MongooseSchemaOptions.js.map +1 -0
  109. package/lib/{interfaces → cjs/interfaces}/MongooseSchemaTypes.d.ts +0 -0
  110. package/lib/cjs/interfaces/MongooseSchemaTypes.js +10 -0
  111. package/lib/cjs/interfaces/MongooseSchemaTypes.js.map +1 -0
  112. package/lib/{interfaces → cjs/interfaces}/MongooseVirtualRefOptions.d.ts +1 -0
  113. package/lib/cjs/interfaces/MongooseVirtualRefOptions.js +3 -0
  114. package/lib/cjs/interfaces/MongooseVirtualRefOptions.js.map +1 -0
  115. package/lib/{interfaces → cjs/interfaces}/index.d.ts +0 -0
  116. package/lib/cjs/interfaces/index.js +10 -0
  117. package/lib/cjs/interfaces/index.js.map +1 -0
  118. package/lib/{registries → cjs/registries}/MongooseModels.d.ts +0 -0
  119. package/lib/cjs/registries/MongooseModels.js +5 -0
  120. package/lib/cjs/registries/MongooseModels.js.map +1 -0
  121. package/lib/{services → cjs/services}/MongooseConnections.d.ts +0 -0
  122. package/lib/cjs/services/MongooseConnections.js +51 -0
  123. package/lib/cjs/services/MongooseConnections.js.map +1 -0
  124. package/lib/{services → cjs/services}/MongooseService.d.ts +0 -0
  125. package/lib/cjs/services/MongooseService.js +73 -0
  126. package/lib/cjs/services/MongooseService.js.map +1 -0
  127. package/lib/{utils → cjs/utils}/createModel.d.ts +1 -1
  128. package/lib/cjs/utils/createModel.js +58 -0
  129. package/lib/cjs/utils/createModel.js.map +1 -0
  130. package/lib/{utils → cjs/utils}/createSchema.d.ts +0 -0
  131. package/lib/cjs/utils/createSchema.js +160 -0
  132. package/lib/cjs/utils/createSchema.js.map +1 -0
  133. package/lib/{utils → cjs/utils}/index.d.ts +0 -0
  134. package/lib/cjs/utils/index.js +6 -0
  135. package/lib/cjs/utils/index.js.map +1 -0
  136. package/lib/{utils → cjs/utils}/resolveRefType.d.ts +0 -0
  137. package/lib/cjs/utils/resolveRefType.js +19 -0
  138. package/lib/cjs/utils/resolveRefType.js.map +1 -0
  139. package/lib/{utils → cjs/utils}/schemaOptions.d.ts +0 -0
  140. package/lib/cjs/utils/schemaOptions.js +60 -0
  141. package/lib/cjs/utils/schemaOptions.js.map +1 -0
  142. package/lib/esm/MongooseModule.js +33 -0
  143. package/lib/esm/MongooseModule.js.map +1 -0
  144. package/lib/esm/constants/index.js +21 -0
  145. package/lib/esm/constants/index.js.map +1 -0
  146. package/lib/esm/decorators/auto.js +24 -0
  147. package/lib/esm/decorators/auto.js.map +1 -0
  148. package/lib/esm/decorators/discriminatorKey.js +10 -0
  149. package/lib/esm/decorators/discriminatorKey.js.map +1 -0
  150. package/lib/esm/decorators/dynamicRef.js +42 -0
  151. package/lib/esm/decorators/dynamicRef.js.map +1 -0
  152. package/lib/esm/decorators/excludeIndexes.js +24 -0
  153. package/lib/esm/decorators/excludeIndexes.js.map +1 -0
  154. package/lib/esm/decorators/expires.js +24 -0
  155. package/lib/esm/decorators/expires.js.map +1 -0
  156. package/lib/esm/decorators/immutable.js +26 -0
  157. package/lib/esm/decorators/immutable.js.map +1 -0
  158. package/lib/esm/decorators/index.js +27 -0
  159. package/lib/esm/decorators/index.js.map +1 -0
  160. package/lib/esm/decorators/indexed.js +22 -0
  161. package/lib/esm/decorators/indexed.js.map +1 -0
  162. package/lib/esm/decorators/lowercase.js +24 -0
  163. package/lib/esm/decorators/lowercase.js.map +1 -0
  164. package/lib/esm/decorators/model.js +63 -0
  165. package/lib/esm/decorators/model.js.map +1 -0
  166. package/lib/esm/decorators/mongooseIndex.js +35 -0
  167. package/lib/esm/decorators/mongooseIndex.js.map +1 -0
  168. package/lib/esm/decorators/mongooseIndexes.js +35 -0
  169. package/lib/esm/decorators/mongooseIndexes.js.map +1 -0
  170. package/lib/esm/decorators/mongoosePlugin.js +18 -0
  171. package/lib/esm/decorators/mongoosePlugin.js.map +1 -0
  172. package/lib/esm/decorators/numberDecimal.js +74 -0
  173. package/lib/esm/decorators/numberDecimal.js.map +1 -0
  174. package/lib/esm/decorators/objectID.js +22 -0
  175. package/lib/esm/decorators/objectID.js.map +1 -0
  176. package/lib/esm/decorators/postHook.js +22 -0
  177. package/lib/esm/decorators/postHook.js.map +1 -0
  178. package/lib/esm/decorators/preHook.js +67 -0
  179. package/lib/esm/decorators/preHook.js.map +1 -0
  180. package/lib/esm/decorators/ref.js +61 -0
  181. package/lib/esm/decorators/ref.js.map +1 -0
  182. package/lib/esm/decorators/schema.js +27 -0
  183. package/lib/esm/decorators/schema.js.map +1 -0
  184. package/lib/esm/decorators/schemaIgnore.js +23 -0
  185. package/lib/esm/decorators/schemaIgnore.js.map +1 -0
  186. package/lib/esm/decorators/select.js +24 -0
  187. package/lib/esm/decorators/select.js.map +1 -0
  188. package/lib/esm/decorators/sparse.js +26 -0
  189. package/lib/esm/decorators/sparse.js.map +1 -0
  190. package/lib/esm/decorators/text.js +25 -0
  191. package/lib/esm/decorators/text.js.map +1 -0
  192. package/lib/esm/decorators/trim.js +23 -0
  193. package/lib/esm/decorators/trim.js.map +1 -0
  194. package/lib/esm/decorators/unique.js +23 -0
  195. package/lib/esm/decorators/unique.js.map +1 -0
  196. package/lib/esm/decorators/uppercase.js +24 -0
  197. package/lib/esm/decorators/uppercase.js.map +1 -0
  198. package/lib/esm/decorators/versionKey.js +8 -0
  199. package/lib/esm/decorators/versionKey.js.map +1 -0
  200. package/lib/esm/decorators/virtualRef.js +33 -0
  201. package/lib/esm/decorators/virtualRef.js.map +1 -0
  202. package/lib/esm/index.js +7 -0
  203. package/lib/esm/index.js.map +1 -0
  204. package/lib/esm/interfaces/MongooseConnectionOptions.js +2 -0
  205. package/lib/esm/interfaces/MongooseConnectionOptions.js.map +1 -0
  206. package/lib/esm/interfaces/MongooseDocument.js +2 -0
  207. package/lib/esm/interfaces/MongooseDocument.js.map +1 -0
  208. package/lib/esm/interfaces/MongooseModel.js +2 -0
  209. package/lib/esm/interfaces/MongooseModel.js.map +1 -0
  210. package/lib/esm/interfaces/MongooseModelOptions.js +2 -0
  211. package/lib/esm/interfaces/MongooseModelOptions.js.map +1 -0
  212. package/lib/esm/interfaces/MongooseSchemaOptions.js +2 -0
  213. package/lib/esm/interfaces/MongooseSchemaOptions.js.map +1 -0
  214. package/lib/esm/interfaces/MongooseSchemaTypes.js +7 -0
  215. package/lib/esm/interfaces/MongooseSchemaTypes.js.map +1 -0
  216. package/lib/esm/interfaces/MongooseVirtualRefOptions.js +2 -0
  217. package/lib/esm/interfaces/MongooseVirtualRefOptions.js.map +1 -0
  218. package/lib/esm/interfaces/index.js +7 -0
  219. package/lib/esm/interfaces/index.js.map +1 -0
  220. package/lib/esm/registries/MongooseModels.js +2 -0
  221. package/lib/esm/registries/MongooseModels.js.map +1 -0
  222. package/lib/esm/services/MongooseConnections.js +48 -0
  223. package/lib/esm/services/MongooseConnections.js.map +1 -0
  224. package/lib/esm/services/MongooseService.js +70 -0
  225. package/lib/esm/services/MongooseService.js.map +1 -0
  226. package/lib/esm/utils/createModel.js +52 -0
  227. package/lib/esm/utils/createModel.js.map +1 -0
  228. package/lib/esm/utils/createSchema.js +151 -0
  229. package/lib/esm/utils/createSchema.js.map +1 -0
  230. package/lib/esm/utils/index.js +3 -0
  231. package/lib/esm/utils/index.js.map +1 -0
  232. package/lib/esm/utils/resolveRefType.js +15 -0
  233. package/lib/esm/utils/resolveRefType.js.map +1 -0
  234. package/lib/esm/utils/schemaOptions.js +54 -0
  235. package/lib/esm/utils/schemaOptions.js.map +1 -0
  236. package/lib/types/MongooseModule.d.ts +13 -0
  237. package/lib/types/constants/index.d.ts +20 -0
  238. package/lib/types/decorators/auto.d.ts +20 -0
  239. package/lib/types/decorators/discriminatorKey.d.ts +1 -0
  240. package/lib/types/decorators/dynamicRef.d.ts +33 -0
  241. package/lib/types/decorators/excludeIndexes.d.ts +20 -0
  242. package/lib/types/decorators/expires.d.ts +20 -0
  243. package/lib/types/decorators/immutable.d.ts +22 -0
  244. package/lib/types/decorators/index.d.ts +26 -0
  245. package/lib/types/decorators/indexed.d.ts +19 -0
  246. package/lib/types/decorators/lowercase.d.ts +20 -0
  247. package/lib/types/decorators/model.d.ts +39 -0
  248. package/lib/types/decorators/mongooseIndex.d.ts +27 -0
  249. package/lib/types/decorators/mongooseIndexes.d.ts +32 -0
  250. package/lib/types/decorators/mongoosePlugin.d.ts +11 -0
  251. package/lib/types/decorators/numberDecimal.d.ts +32 -0
  252. package/lib/types/decorators/objectID.d.ts +19 -0
  253. package/lib/types/decorators/postHook.d.ts +48 -0
  254. package/lib/types/decorators/preHook.d.ts +49 -0
  255. package/lib/types/decorators/ref.d.ts +33 -0
  256. package/lib/types/decorators/schema.d.ts +54 -0
  257. package/lib/types/decorators/schemaIgnore.d.ts +19 -0
  258. package/lib/types/decorators/select.d.ts +20 -0
  259. package/lib/types/decorators/sparse.d.ts +22 -0
  260. package/lib/types/decorators/text.d.ts +21 -0
  261. package/lib/types/decorators/trim.d.ts +19 -0
  262. package/lib/types/decorators/unique.d.ts +19 -0
  263. package/lib/types/decorators/uppercase.d.ts +20 -0
  264. package/lib/types/decorators/versionKey.d.ts +1 -0
  265. package/lib/types/decorators/virtualRef.d.ts +27 -0
  266. package/lib/types/index.d.ts +6 -0
  267. package/lib/types/interfaces/MongooseConnectionOptions.d.ts +9 -0
  268. package/lib/types/interfaces/MongooseDocument.d.ts +2 -0
  269. package/lib/types/interfaces/MongooseModel.d.ts +9 -0
  270. package/lib/types/interfaces/MongooseModelOptions.d.ts +8 -0
  271. package/lib/types/interfaces/MongooseSchemaOptions.d.ts +35 -0
  272. package/lib/types/interfaces/MongooseSchemaTypes.d.ts +5 -0
  273. package/lib/types/interfaces/MongooseVirtualRefOptions.d.ts +13 -0
  274. package/lib/types/interfaces/index.d.ts +14 -0
  275. package/lib/types/registries/MongooseModels.d.ts +2 -0
  276. package/lib/types/services/MongooseConnections.d.ts +9 -0
  277. package/lib/types/services/MongooseService.d.ts +25 -0
  278. package/lib/types/utils/createModel.d.ts +19 -0
  279. package/lib/types/utils/createSchema.d.ts +28 -0
  280. package/lib/types/utils/index.d.ts +2 -0
  281. package/lib/types/utils/resolveRefType.d.ts +5 -0
  282. package/lib/types/utils/schemaOptions.d.ts +14 -0
  283. package/package.json +20 -16
  284. package/tsconfig.compile.esm.json +23 -0
  285. package/lib/index.js +0 -1407
  286. package/lib/index.js.map +0 -1
  287. package/lib/index.modern.js +0 -1367
  288. package/lib/index.modern.js.map +0 -1
@@ -1,1367 +0,0 @@
1
- import { __decorate, __metadata, __param } from 'tslib';
2
- import { Inject, Injectable, registerProvider, Configuration, Module } from '@tsed/di';
3
- import { Logger } from '@tsed/logger';
4
- import mongoose, { Schema as Schema$1, Types } from 'mongoose';
5
- import { isArray, isString, isClass, Store, nameOf, deepMerge, cleanObject, ancestorsOf, decoratorTypeOf, useDecorators, StoreMerge, isNumber, DecoratorTypes, isArrowFn, isPlainObject } from '@tsed/core';
6
- import { deserialize, serialize, OnDeserialize, OnSerialize } from '@tsed/json-mapper';
7
- import { getProperties, Property, Example, Description, Format, Name, Pattern, JsonEntityFn, string, lazyRef, CollectionOf } from '@tsed/schema';
8
- import { pascalCase } from 'change-case';
9
- import { Formats } from '@tsed/ajv';
10
-
11
- /**
12
- *
13
- * @type {string}
14
- */
15
- const MONGOOSE_SCHEMA = "tsed:mongoose:schema";
16
- /**
17
- *
18
- * @type {string}
19
- */
20
-
21
- const MONGOOSE_SCHEMA_OPTIONS = "tsed:mongoose:schema:options";
22
- /**
23
- *
24
- * @type {string}
25
- */
26
-
27
- const MONGOOSE_MODEL = "tsed:mongoose:model";
28
- /**
29
- *
30
- * @type {string}
31
- */
32
-
33
- const MONGOOSE_MODEL_NAME = "tsed:mongoose:model:name";
34
-
35
- function asPromise(c) {
36
- return c && c.asPromise ? c.asPromise() : c;
37
- }
38
-
39
- let MongooseService = class MongooseService {
40
- constructor() {
41
- this.connections = new Map();
42
- this.defaultConnection = "default";
43
- }
44
- /**
45
- *
46
- * @returns {Promise<"mongoose".Connection>}
47
- */
48
-
49
-
50
- async connect(id, url, connectionOptions, isDefault = false) {
51
- if (this.has(id)) {
52
- return await this.get(id);
53
- }
54
-
55
- this.logger.info(`Connect to mongo database: ${id}`);
56
- this.logger.debug(`Url: ${url}`);
57
- this.logger.debug(`options: ${JSON.stringify(connectionOptions)}`);
58
-
59
- try {
60
- const connection = await asPromise(mongoose.createConnection(url, connectionOptions));
61
- this.connections.set(id, connection);
62
-
63
- if (id === "default" || isDefault) {
64
- this.defaultConnection = id;
65
- }
66
-
67
- return connection;
68
- } catch (er) {
69
- /* istanbul ignore next */
70
- this.logger.error(er);
71
- /* istanbul ignore next */
72
-
73
- process.exit();
74
- }
75
- }
76
- /**
77
- *
78
- * @returns {"mongoose".Connection}
79
- */
80
-
81
-
82
- get(id) {
83
- return this.connections.get(id || this.defaultConnection);
84
- }
85
- /**
86
- *
87
- * @param {string} id
88
- * @returns {boolean}
89
- */
90
-
91
-
92
- has(id) {
93
- return this.connections.has(id || this.defaultConnection);
94
- }
95
-
96
- async closeConnections() {
97
- for (const [id, connection] of this.connections.entries()) {
98
- await connection.close();
99
- this.connections.delete(id);
100
- }
101
- }
102
-
103
- };
104
-
105
- __decorate([Inject(), __metadata("design:type", Logger)], MongooseService.prototype, "logger", void 0);
106
-
107
- MongooseService = __decorate([Injectable()], MongooseService);
108
-
109
- function _extends() {
110
- _extends = Object.assign || function (target) {
111
- for (var i = 1; i < arguments.length; i++) {
112
- var source = arguments[i];
113
-
114
- for (var key in source) {
115
- if (Object.prototype.hasOwnProperty.call(source, key)) {
116
- target[key] = source[key];
117
- }
118
- }
119
- }
120
-
121
- return target;
122
- };
123
-
124
- return _extends.apply(this, arguments);
125
- }
126
-
127
- /**
128
- * @ignore
129
- */
130
- // tslint:disable-next-line:variable-name
131
-
132
- const MONGOOSE_CONNECTIONS = Symbol.for("MONGOOSE_CONNECTIONS");
133
-
134
- function mapOptions(options) {
135
- if (!options) {
136
- return [];
137
- }
138
-
139
- if (!isArray(options)) {
140
- const {
141
- url,
142
- connectionOptions
143
- } = options || {};
144
- return [{
145
- id: "default",
146
- url,
147
- connectionOptions
148
- }];
149
- }
150
-
151
- return options.map(settings => {
152
- return _extends({}, settings, {
153
- connectionOptions: settings.connectionOptions
154
- });
155
- });
156
- }
157
-
158
- registerProvider({
159
- provide: MONGOOSE_CONNECTIONS,
160
- injectable: false,
161
- deps: [Configuration, MongooseService],
162
-
163
- async useAsyncFactory(configuration, mongooseService) {
164
- const settings = mapOptions(configuration.get("mongoose"));
165
- let isDefault = true;
166
-
167
- for (const current of settings) {
168
- await mongooseService.connect(current.id, current.url, current.connectionOptions || {
169
- useCreateIndex: true,
170
- useNewUrlParser: true,
171
- useUnifiedTopology: true
172
- }, isDefault);
173
- isDefault = false;
174
- }
175
-
176
- return mongooseService;
177
- }
178
-
179
- });
180
-
181
- /**
182
- * @ignore
183
- */
184
-
185
- let MongooseModule = class MongooseModule {
186
- constructor(mongooseService, settings) {
187
- this.mongooseService = mongooseService;
188
- this.settings = settings; // auto configure the cache manager when mongoose is used with @tsed/mongoose
189
-
190
- const cache = this.settings.get("cache"); // istanbul ignore next
191
-
192
- if (cache != null && cache.mongoose) {
193
- cache.connection = this.mongooseService.get();
194
- }
195
- }
196
-
197
- $onInit() {}
198
-
199
- $onDestroy() {
200
- return this.mongooseService.closeConnections();
201
- }
202
-
203
- };
204
- MongooseModule = __decorate([Module({
205
- imports: [MONGOOSE_CONNECTIONS]
206
- }), __param(1, Configuration()), __metadata("design:paramtypes", [MongooseService, Object])], MongooseModule);
207
-
208
- /**
209
- * @ignore
210
- */
211
-
212
- function resolveRefType(type) {
213
- if (isString(type)) {
214
- return type;
215
- }
216
-
217
- if (!isClass(type)) {
218
- type = type();
219
- }
220
-
221
- return Store.from(type).get(MONGOOSE_MODEL_NAME) || nameOf(type);
222
- }
223
-
224
- /**
225
- * @ignore
226
- */
227
-
228
- function schemaOptions(target, options) {
229
- const store = Store.from(target);
230
-
231
- if (!store.has(MONGOOSE_SCHEMA_OPTIONS)) {
232
- store.set(MONGOOSE_SCHEMA_OPTIONS, {});
233
- }
234
-
235
- if (options) {
236
- store.set(MONGOOSE_SCHEMA_OPTIONS, deepMerge(store.get(MONGOOSE_SCHEMA_OPTIONS), options));
237
- }
238
-
239
- return store.get(MONGOOSE_SCHEMA_OPTIONS);
240
- }
241
-
242
- function mapHookArgs(hook) {
243
- return [hook.method, hook.options || hook.fn, hook.options ? hook.fn : undefined].filter(Boolean);
244
- }
245
- /**
246
- * @ignore
247
- */
248
-
249
-
250
- function buildPreHook(fn) {
251
- return fn.length === 2 ? function (next) {
252
- return fn(this, next);
253
- } : function (next, options) {
254
- return fn(this, next, options);
255
- };
256
- }
257
- /**
258
- * @ignore
259
- */
260
-
261
- function applySchemaOptions(schema, options) {
262
- if (options.plugins) {
263
- options.plugins.forEach(item => schema.plugin(item.plugin, item.options));
264
- }
265
-
266
- if (options.indexes) {
267
- options.indexes.forEach(item => schema.index(item.fields, item.options));
268
- }
269
-
270
- if (options.pre) {
271
- options.pre.forEach(item => {
272
- item = _extends({}, item, {
273
- fn: buildPreHook(item.fn)
274
- });
275
- schema.pre(...mapHookArgs(item));
276
- });
277
- }
278
-
279
- if (options.post) {
280
- options.post.forEach(item => schema.post(...mapHookArgs(item)));
281
- }
282
- }
283
-
284
- const MONGOOSE_RESERVED_KEYS = ["_id"];
285
- /**
286
- * @ignore
287
- */
288
-
289
- function setUpSchema({
290
- schema,
291
- virtuals
292
- }, options) {
293
- const mongooseSchema = new mongoose.Schema(schema, options);
294
-
295
- for (const [key, _options] of virtuals.entries()) {
296
- mongooseSchema.virtual(key, _options);
297
- }
298
-
299
- return mongooseSchema;
300
- }
301
- /**
302
- * @ignore
303
- */
304
-
305
-
306
- function isVirtualRef(target) {
307
- return !!(target.ref && target.localField && target.foreignField);
308
- }
309
-
310
- function hasVersionField(schema, versionKey) {
311
- // Check if versioning was disabled explicitly
312
- if (!versionKey) return false; // Check for alternative version field in schema
313
-
314
- return versionKey in schema.paths;
315
- }
316
-
317
- function createSchema(target, options = {}) {
318
- var _options$schemaOption;
319
-
320
- const schemaOptionsFromStore = Store.from(target).get(MONGOOSE_SCHEMA_OPTIONS) || {};
321
- options.schemaOptions = _extends({}, options.schemaOptions, schemaOptionsFromStore);
322
- const schema = setUpSchema(buildMongooseSchema(target), options.schemaOptions);
323
- schemaOptions(target, options);
324
- const outputVersionKey = hasVersionField(schema, (_options$schemaOption = options.schemaOptions) == null ? void 0 : _options$schemaOption.versionKey);
325
-
326
- schema.methods.toClass = function toClass() {
327
- return deserialize(this.toObject({
328
- versionKey: outputVersionKey,
329
- flattenMaps: true
330
- }), {
331
- type: target,
332
- useAlias: false,
333
- additionalProperties: true,
334
- groups: false
335
- });
336
- };
337
-
338
- schema.methods.toJSON = function toJSON(options) {
339
- return serialize(this.toClass(), options);
340
- };
341
-
342
- schema.loadClass(target);
343
- Store.from(target).set(MONGOOSE_SCHEMA, schema);
344
- return schema;
345
- }
346
- /**
347
- * Get a schema already created. If the schema doesn't exists in registry, it'll be created.
348
- * @param target
349
- * @param options
350
- */
351
-
352
- function getSchema(target, options = {}) {
353
- const store = Store.from(target);
354
-
355
- if (!store.has(MONGOOSE_SCHEMA)) {
356
- createSchema(target, options);
357
- }
358
-
359
- return store.get(MONGOOSE_SCHEMA);
360
- }
361
- function getSchemaToken(target, options) {
362
- const collectionName = options.name || nameOf(target);
363
- const token = Symbol.for(pascalCase(`${collectionName}Schema`));
364
- return {
365
- token,
366
- collectionName
367
- };
368
- }
369
- /**
370
- * @ignore
371
- */
372
-
373
- function buildMongooseSchema(target) {
374
- const properties = getProperties(target, {
375
- withIgnoredProps: true,
376
- mongoose: true,
377
- groups: false
378
- });
379
- const schema = {
380
- schema: {},
381
- virtuals: new Map()
382
- };
383
- properties.forEach((propertyMetadata, key) => {
384
- if (MONGOOSE_RESERVED_KEYS.includes(key)) {
385
- return;
386
- } // Keeping the Mongoose Schema separate so it can overwrite everything once schema has been built.
387
-
388
-
389
- const schemaTypeOptions = propertyMetadata.store.get(MONGOOSE_SCHEMA) || {};
390
-
391
- if (schemaTypeOptions.schemaIgnore) {
392
- return;
393
- }
394
-
395
- if (schemaTypeOptions.ref) {
396
- schemaTypeOptions.ref = resolveRefType(schemaTypeOptions.ref);
397
- }
398
-
399
- if (isVirtualRef(schemaTypeOptions)) {
400
- schema.virtuals.set(key, schemaTypeOptions);
401
- return;
402
- }
403
-
404
- schema.schema[key] = createSchemaTypeOptions(propertyMetadata);
405
- });
406
- return schema;
407
- }
408
- /**
409
- * @ignore
410
- */
411
-
412
- function createSchemaTypeOptions(propEntity) {
413
- const key = propEntity.propertyKey;
414
- const rawMongooseSchema = propEntity.store.get(MONGOOSE_SCHEMA) || {};
415
- let schemaTypeOptions = {
416
- required: propEntity.required ? function () {
417
- return propEntity.isRequired(this[key]);
418
- } : false
419
- };
420
-
421
- if (!propEntity.isClass) {
422
- const jsonSchema = propEntity.itemSchema.toJSON();
423
- const {
424
- minimum: min,
425
- maximum: max,
426
- minLength: minlength,
427
- maxLength: maxlength
428
- } = jsonSchema;
429
- let match = jsonSchema.pattern;
430
-
431
- if (match) {
432
- match = new RegExp(match);
433
- }
434
-
435
- schemaTypeOptions = _extends({}, schemaTypeOptions, {
436
- type: propEntity.type,
437
- match: match,
438
- min,
439
- max,
440
- minlength,
441
- maxlength,
442
- enum: jsonSchema["enum"],
443
- default: jsonSchema["default"]
444
- });
445
- } else if (!rawMongooseSchema.ref) {
446
- // References are handled by the final merge
447
- schemaTypeOptions = _extends({}, schemaTypeOptions, {
448
- type: getSchema(propEntity.type)
449
- });
450
- }
451
-
452
- schemaTypeOptions = cleanObject(_extends({}, schemaTypeOptions, rawMongooseSchema));
453
-
454
- if (propEntity.isCollection) {
455
- if (propEntity.isArray) {
456
- return [schemaTypeOptions];
457
- } // Can be a Map or a Set;
458
- // Mongoose implements only Map;
459
-
460
-
461
- if (propEntity.collectionType !== Map) {
462
- throw new Error(`Invalid collection type. ${nameOf(propEntity.collectionType)} is not supported.`);
463
- }
464
-
465
- return {
466
- type: Map,
467
- of: schemaTypeOptions
468
- };
469
- }
470
-
471
- return schemaTypeOptions;
472
- }
473
-
474
- const MongooseModels = new Map();
475
-
476
- function getModelToken(target, options) {
477
- const {
478
- collectionName,
479
- token
480
- } = getSchemaToken(target, options);
481
- Store.from(target).set(MONGOOSE_MODEL_NAME, collectionName);
482
- MongooseModels.set(collectionName, target);
483
- return {
484
- token,
485
- collectionName
486
- };
487
- }
488
- /**
489
- * Create an instance of mongoose.model from a class.
490
- *
491
- * @param {Type<any>} target Class attached to the schema and model.
492
- * @param {"mongoose".Schema} schema Schema that will be attached to the model.
493
- * @param name model name
494
- * @param collection (optional, induced from model name)
495
- * @param skipInit whether to skip initialization (defaults to false)
496
- * @param connection
497
- * @param discriminatorValue
498
- * @returns {Model<T extends Document>}
499
- */
500
-
501
- function createModel(target, schema, name = nameOf(target), collection, skipInit, connection, discriminatorValue) {
502
- // if ancestor has a discriminatorKey we need to link model with anscestor schema
503
- const ancestor = ancestorsOf(target).find(ancestor => {
504
- const options = Store.from(ancestor).get(MONGOOSE_SCHEMA_OPTIONS) || {};
505
- return !!options.discriminatorKey;
506
- });
507
-
508
- if (ancestor && ancestor !== target) {
509
- const ancestorModel = Store.from(ancestor).get(MONGOOSE_MODEL);
510
-
511
- if (ancestorModel) {
512
- const discriminatorName = discriminatorValue || name;
513
-
514
- if (ancestorModel.discriminators && ancestorModel.discriminators[discriminatorName]) {
515
- return ancestorModel.discriminators[discriminatorName];
516
- } else {
517
- return ancestorModel.discriminator(discriminatorName, schema);
518
- }
519
- }
520
- }
521
- /* istanbul ignore else */
522
-
523
-
524
- if (connection) {
525
- const _model = connection.model(name, schema, collection);
526
-
527
- Store.from(target).set(MONGOOSE_MODEL, _model);
528
- return _model;
529
- }
530
-
531
- const model = mongoose.model(name, schema, collection, skipInit);
532
- Store.from(target).set(MONGOOSE_MODEL, model);
533
- return model;
534
- }
535
-
536
- function Schema(options = {}) {
537
- return (...parameters) => {
538
- switch (decoratorTypeOf(parameters)) {
539
- case "property":
540
- return useDecorators(Property(), StoreMerge(MONGOOSE_SCHEMA, options))(parameters[0], parameters[1], parameters[2]);
541
-
542
- case "class":
543
- const {
544
- token
545
- } = getSchemaToken(parameters[0], options);
546
- registerProvider({
547
- provide: token,
548
- deps: [],
549
-
550
- useFactory() {
551
- return getSchema(parameters[0], options);
552
- }
553
-
554
- });
555
- break;
556
- }
557
- };
558
- }
559
- function MongooseSchema(options = {}) {
560
- return Schema(options);
561
- }
562
-
563
- /**
564
- * If true, uses Mongoose's default _id settings. Only allowed for ObjectIds
565
- *
566
- * ### Example
567
- *
568
- * ```typescript
569
- * @Model()
570
- * export class EventModel {
571
- * @ExcludeIndexes()
572
- * field: string;
573
- * }
574
- * ```
575
- *
576
- * @param {boolean} auto
577
- * @returns {Function}
578
- * @decorator
579
- * @mongoose
580
- * @property
581
- */
582
-
583
- function Auto(auto = true) {
584
- return MongooseSchema({
585
- auto
586
- });
587
- }
588
-
589
- /**
590
- * Do not apply this property to schema (create virtual property)
591
- *
592
- * ### Example
593
- *
594
- * ```typescript
595
- * @Model()
596
- * @SchemaIgnore()
597
- * @Property()
598
- * kind: string;
599
- *
600
- * ```
601
- *
602
- * @returns {Function}
603
- * @decorator
604
- * @mongoose
605
- * @class
606
- */
607
-
608
- function SchemaIgnore() {
609
- return MongooseSchema({
610
- schemaIgnore: true
611
- });
612
- }
613
-
614
- function DiscriminatorKey() {
615
- return useDecorators(Property(), SchemaIgnore(), (target, propertyKey) => {
616
- Store.from(target).merge(MONGOOSE_SCHEMA_OPTIONS, {
617
- discriminatorKey: propertyKey
618
- });
619
- });
620
- }
621
-
622
- /**
623
- * Define a property as mongoose reference to other Model (decorated with @Model).
624
- *
625
- * ### Example
626
- *
627
- * ```typescript
628
- * @Model()
629
- * class FooModel {
630
- *
631
- * @DynamicRef('type')
632
- * field: DynamicRef<OtherFooModel | OtherModel>
633
- *
634
- * @Enum(['OtherFooModel', 'OtherModel'])
635
- * type: string
636
- * }
637
- *
638
- * @Model()
639
- * class OtherFooModel {
640
- * }
641
- *
642
- * @Model()
643
- * class OtherModel {
644
- * }
645
- * ```
646
- *
647
- * @param refPath
648
- * @returns {Function}
649
- * @decorator
650
- * @mongoose
651
- * @property
652
- */
653
-
654
- function DynamicRef(refPath) {
655
- return useDecorators(Property(String), Example("5ce7ad3028890bd71749d477"), Description("Mongoose Ref ObjectId"), StoreMerge(MONGOOSE_SCHEMA, {
656
- type: Schema$1.Types.ObjectId,
657
- refPath
658
- }));
659
- }
660
-
661
- /**
662
- * If true, Mongoose will skip gathering indexes on subpaths. Only allowed for subdocuments and subdocument arrays.
663
- *
664
- * ### Example
665
- *
666
- * ```typescript
667
- * @Model()
668
- * export class EventModel {
669
- * @ExcludeIndexes()
670
- * field: string;
671
- * }
672
- * ```
673
- *
674
- * @param {boolean} excludeIndexes
675
- * @returns {Function}
676
- * @decorator
677
- * @mongoose
678
- * @property
679
- */
680
-
681
- function ExcludeIndexes(excludeIndexes = true) {
682
- return MongooseSchema({
683
- excludeIndexes
684
- });
685
- }
686
-
687
- /**
688
- * Defines a TTL index on this path. Only allowed for dates.
689
- *
690
- * ### Example
691
- *
692
- * ```typescript
693
- * @Model()
694
- * export class EventModel {
695
- * @Expires('5d')
696
- * field: string;
697
- * }
698
- * ```
699
- *
700
- * @param {boolean | any} expires
701
- * @returns {Function}
702
- * @decorator
703
- * @mongoose
704
- * @property
705
- */
706
-
707
- function Expires(expires) {
708
- return MongooseSchema({
709
- expires
710
- });
711
- }
712
-
713
- /**
714
- * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose will
715
- * disallow changes to this path once the document is saved to the database for the first time. Read more
716
- * about [immutability in Mongoose here](http://thecodebarbarian.com/whats-new-in-mongoose-5-6-immutable-properties.html).
717
- *
718
- * ### Example
719
- *
720
- * ```typescript
721
- * @Model()
722
- * export class EventModel {
723
- * @Immutable()
724
- * field: string;
725
- * }
726
- * ```
727
- *
728
- * @param {boolean | any} immutable
729
- * @returns {Function}
730
- * @decorator
731
- * @mongoose
732
- * @property
733
- */
734
-
735
- function Immutable(immutable = true) {
736
- return MongooseSchema({
737
- immutable
738
- });
739
- }
740
-
741
- /**
742
- * Tell Mongoose whether to define an index for the property.
743
- * ### Example
744
- *
745
- * ```typescript
746
- * @Model()
747
- * export class EventModel {
748
- * @Indexed()
749
- * field: string;
750
- * }
751
- * ```
752
- *
753
- * @returns {Function}
754
- * @decorator
755
- * @mongoose
756
- * @property
757
- */
758
-
759
- function Indexed(index = true) {
760
- return MongooseSchema({
761
- index
762
- });
763
- }
764
-
765
- /**
766
- * If truthy, Mongoose will add a custom setter that lowercases this string using JavaScript's built-in String#toLowerCase().
767
- *
768
- * ### Example
769
- *
770
- * ```typescript
771
- * @Model()
772
- * export class EventModel {
773
- * @Lowercase()
774
- * field: string;
775
- * }
776
- * ```
777
- *
778
- * @param {boolean} lowercase
779
- * @returns {Function}
780
- * @decorator
781
- * @mongoose
782
- * @property
783
- */
784
-
785
- function Lowercase(lowercase = true) {
786
- return MongooseSchema({
787
- lowercase
788
- });
789
- }
790
-
791
- /**
792
- * Define a class as a Mongoose Model. The model can be injected to the Service, Controller, Middleware, Converters or Filter with
793
- * `@Inject` annotation.
794
- *
795
- * ### Example
796
- *
797
- * ```typescript
798
- * @Model()
799
- * export class EventModel {
800
- * @Property()
801
- * field: string;
802
- * }
803
- * ```
804
- *
805
- * Then inject the model into a service:
806
- *
807
- * ```typescript
808
- * class MyService {
809
- * constructor(@Inject(EventModel) eventModel: MongooseModel<EventModel>) {
810
- * eventModel.findById().exec();
811
- * }
812
- * }
813
- * ```
814
- *
815
- * ### Options
816
- *
817
- * - `schemaOptions` (mongoose.SchemaOptions): Option to configure the schema behavior.
818
- * - `name` (String): model name.
819
- * - `collection` (String): collection (optional, induced from model name).
820
- * - `skipInit` (Boolean): skipInit whether to skip initialization (defaults to false).
821
- *
822
- * @param {MongooseModelOptions} options
823
- * @returns {(target: any) => void}
824
- * @decorator
825
- * @mongoose
826
- * @class
827
- */
828
-
829
- function Model(options = {}) {
830
- return target => {
831
- const {
832
- token,
833
- collectionName
834
- } = getModelToken(target, options);
835
- registerProvider({
836
- provide: token,
837
- deps: [],
838
-
839
- useFactory() {
840
- return getSchema(target, options);
841
- }
842
-
843
- });
844
- registerProvider({
845
- provide: target,
846
- type: "mongoose:model",
847
- deps: [MONGOOSE_CONNECTIONS, token],
848
-
849
- useFactory(connections, schema) {
850
- applySchemaOptions(schema, schemaOptions(target));
851
- return createModel(target, schema, collectionName, options.collection, options.skipInit, connections.get(options.connection), options.discriminatorValue);
852
- }
853
-
854
- });
855
- };
856
- }
857
-
858
- /**
859
- * Calls schema.index() to define an index (most likely compound) for the schema.
860
- *
861
- * ### Example
862
- *
863
- * ```typescript
864
- * @Model()
865
- * @MongooseIndex({first: 1, second: 1}, {unique: 1})
866
- * export class EventModel {
867
- *
868
- * @Property()
869
- * first: string;
870
- *
871
- * @Property()
872
- * second: string;
873
- *
874
- * }
875
- * ```
876
- *
877
- * @param fields
878
- * @param options
879
- * @returns {Function}
880
- * @decorator
881
- * @mongoose
882
- * @class
883
- */
884
-
885
- function MongooseIndex(fields, options) {
886
- return target => {
887
- schemaOptions(target, {
888
- indexes: [{
889
- fields,
890
- options
891
- }]
892
- });
893
- };
894
- }
895
-
896
- /**
897
- *
898
- * @param {(schema: "mongoose".Schema, options?: any) => void} plugin
899
- * @param options
900
- * @returns {Function}
901
- * @decorator
902
- * @mongoose
903
- * @class
904
- */
905
-
906
- function MongoosePlugin(plugin, options) {
907
- return target => {
908
- schemaOptions(target, {
909
- plugins: [{
910
- plugin,
911
- options
912
- }]
913
- });
914
- };
915
- }
916
-
917
- let DecimalFormat = class DecimalFormat {
918
- validate(num) {
919
- return isString(num) || isNumber(num);
920
- }
921
-
922
- };
923
- DecimalFormat = __decorate([Formats("decimal", {
924
- type: "number"
925
- })], DecimalFormat);
926
-
927
- function isDecimal(value) {
928
- return value && value._bsontype === "Decimal128";
929
- }
930
- /**
931
- * Tell Mongoose whether to define an Decimal128 property.
932
- * Will be serialized as `number` with format as `decimal`.
933
- * ### Example
934
- *
935
- * ```typescript
936
- * @Model()
937
- * export class PriceModel {
938
- * @NumberDecimal()
939
- * price: Decimal128;
940
- * }
941
- * ```
942
- * Optionally using custom decimal type, such as `Big` from big.js
943
- * ```typescript
944
- * @Model()
945
- * export class PriceModel {
946
- * @NumberDecimal(Big)
947
- * price: Big;
948
- * }
949
- * ```
950
- * @param type Optional decimal type constructor
951
- * @decorator
952
- * @mongoose
953
- * @schema
954
- */
955
-
956
-
957
- function NumberDecimal(type) {
958
- const schema = {
959
- type: Schema$1.Types.Decimal128
960
- };
961
-
962
- if (type) {
963
- // Define property getter to convert Decimal128 to custom type
964
- schema.get = value => {
965
- return isDecimal(value) ? new type(value) : value;
966
- };
967
- }
968
-
969
- return useDecorators(Property(Number), Format("decimal"), Example(12.34), StoreMerge(MONGOOSE_SCHEMA, schema), // Deserialize number value from JSON to Decimal128
970
- OnDeserialize(value => {
971
- if (value === undefined) {
972
- return undefined;
973
- }
974
-
975
- if (type) {
976
- return new type(value);
977
- }
978
-
979
- if (isString(value)) {
980
- return Types.Decimal128.fromString(value);
981
- }
982
-
983
- return Types.Decimal128.fromString(`${value}`);
984
- }), // Serialize decimal value to floating point number
985
- OnSerialize((value, ctx) => {
986
- return value && Number(value);
987
- }));
988
- }
989
-
990
- /**
991
- * Tell Mongoose whether to define an ObjectId property.
992
- * ### Example
993
- *
994
- * ```typescript
995
- * @Model()
996
- * export class EventModel {
997
- * @ObjectId('id')
998
- * _id: string;
999
- * }
1000
- * ```
1001
- * @param name
1002
- * @decorator
1003
- * @mongoose
1004
- * @schema
1005
- */
1006
-
1007
- function ObjectID(name) {
1008
- return useDecorators(name && Name(name), Pattern(/^[0-9a-fA-F]{24}$/), Description("Mongoose ObjectId"), Example("5ce7ad3028890bd71749d477"));
1009
- }
1010
-
1011
- function PostHook(method, ...params) {
1012
- return (...args) => {
1013
- let options = params[1];
1014
- let fn = params[0];
1015
-
1016
- if (decoratorTypeOf(args) === DecoratorTypes.METHOD_STC) {
1017
- options = params[0];
1018
- fn = args[0][args[1]].bind(args[0]);
1019
- }
1020
-
1021
- schemaOptions(args[0], {
1022
- post: [{
1023
- method,
1024
- fn,
1025
- options
1026
- }]
1027
- });
1028
- };
1029
- }
1030
-
1031
- /**
1032
- *
1033
- * We can simply attach a `@PreHook` decorator to your model class and
1034
- * define the hook function like you normally would in Mongoose.
1035
- *
1036
- * ```typescript
1037
- * import {Ignore, Required} from "@tsed/common";
1038
- * import {PreHook, Model} from "@tsed/mongoose";
1039
- *
1040
- * @Model()
1041
- * @PreHook("save", (car: CarModel, next) => {
1042
- * if (car.model === 'Tesla') {
1043
- * car.isFast = true;
1044
- * }
1045
- * next();
1046
- *})
1047
- * export class CarModel {
1048
- *
1049
- * @Ignore()
1050
- * _id: string;
1051
- *
1052
- * @Required()
1053
- * model: string;
1054
- *
1055
- * @Required()
1056
- * isFast: boolean;
1057
- *
1058
- * // or Prehook on static method
1059
- * @PreHook("save")
1060
- * static preSave(car: CarModel, next) {
1061
- * if (car.model === 'Tesla') {
1062
- * car.isFast = true;
1063
- * }
1064
- * next();
1065
- * }
1066
- * }
1067
- * ```
1068
- *
1069
- * This will execute the pre-save hook each time a `CarModel` document is saved.
1070
- *
1071
- * @param {string} method
1072
- * @param fn
1073
- * @param options
1074
- * @returns {Function}
1075
- * @decorator
1076
- * @class
1077
- */
1078
-
1079
- function PreHook(method, fn, options) {
1080
- return (...args) => {
1081
- if (decoratorTypeOf(args) === DecoratorTypes.METHOD_STC) {
1082
- options = fn;
1083
- fn = args[0][args[1]].bind(args[0]);
1084
- }
1085
-
1086
- schemaOptions(args[0], {
1087
- pre: [{
1088
- method,
1089
- fn: fn,
1090
- options
1091
- }]
1092
- });
1093
- };
1094
- }
1095
-
1096
- var MongooseSchemaTypes;
1097
-
1098
- (function (MongooseSchemaTypes) {
1099
- MongooseSchemaTypes["OBJECT_ID"] = "ObjectId";
1100
- MongooseSchemaTypes["STRING"] = "String";
1101
- MongooseSchemaTypes["NUMBER"] = "Number";
1102
- })(MongooseSchemaTypes || (MongooseSchemaTypes = {}));
1103
-
1104
- function isRef(value) {
1105
- return value && value._bsontype || isString(value);
1106
- }
1107
- /**
1108
- * Define a property as mongoose reference to other Model (decorated with @Model).
1109
- *
1110
- * ### Example
1111
- *
1112
- * ```typescript
1113
- *
1114
- * @Model()
1115
- * class FooModel {
1116
- *
1117
- * @Ref(Foo2Model)
1118
- * field: Ref<Foo2Model>
1119
- *
1120
- * @Ref(Foo2Model)
1121
- * list: Ref<Foo2Model>[]
1122
- * }
1123
- *
1124
- * @Model()
1125
- * class Foo2Model {
1126
- * }
1127
- * ```
1128
- *
1129
- * @param model
1130
- * @param type
1131
- * @returns {Function}
1132
- * @decorator
1133
- * @mongoose
1134
- * @property
1135
- */
1136
-
1137
-
1138
- function Ref(model, type = MongooseSchemaTypes.OBJECT_ID) {
1139
- const getType = () => isString(model) ? MongooseModels.get(model) : isArrowFn(model) ? model() : model;
1140
-
1141
- return useDecorators(Property(Object), StoreMerge(MONGOOSE_SCHEMA, {
1142
- type: Schema$1.Types[type],
1143
- ref: model
1144
- }), OnDeserialize(value => {
1145
- if (isRef(value)) {
1146
- return value.toString();
1147
- }
1148
-
1149
- return value;
1150
- }), OnSerialize((value, ctx) => {
1151
- if (isRef(value)) {
1152
- return value.toString();
1153
- }
1154
-
1155
- const type = getType();
1156
- return serialize(value, _extends({}, ctx, {
1157
- type
1158
- }));
1159
- }), JsonEntityFn(async store => {
1160
- store.itemSchema.oneOf([string().example("5ce7ad3028890bd71749d477").description("Mongoose Ref ObjectId"), lazyRef(getType)]);
1161
- store.type = Object;
1162
- }));
1163
- }
1164
-
1165
- /**
1166
- * Tell Mongoose to set default select() behavior for this path.
1167
- *
1168
- * ### Example
1169
- *
1170
- * ```typescript
1171
- * @Model()
1172
- * export class EventModel {
1173
- * @Select()
1174
- * field: string;
1175
- * }
1176
- * ```
1177
- *
1178
- * @param {boolean | any} select
1179
- * @returns {Function}
1180
- * @decorator
1181
- * @mongoose
1182
- * @property
1183
- */
1184
-
1185
- function Select(select = true) {
1186
- return MongooseSchema({
1187
- select
1188
- });
1189
- }
1190
-
1191
- /**
1192
- * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose will
1193
- * disallow changes to this path once the document is saved to the database for the first time. Read more
1194
- * about [immutability in Mongoose here](http://thecodebarbarian.com/whats-new-in-mongoose-5-6-immutable-properties.html).
1195
- *
1196
- * ### Example
1197
- *
1198
- * ```typescript
1199
- * @Model()
1200
- * export class EventModel {
1201
- * @Sparse()
1202
- * field: string;
1203
- * }
1204
- * ```
1205
- *
1206
- * @param {boolean | number} sparse
1207
- * @returns {Function}
1208
- * @decorator
1209
- * @mongoose
1210
- * @property
1211
- */
1212
-
1213
- function Sparse(sparse = true) {
1214
- return MongooseSchema({
1215
- sparse
1216
- });
1217
- }
1218
-
1219
- /**
1220
- * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose
1221
- * will build a text index on this path.
1222
- *
1223
- * ### Example
1224
- *
1225
- * ```typescript
1226
- * @Model()
1227
- * export class EventModel {
1228
- * @Text()
1229
- * field: string;
1230
- * }
1231
- * ```
1232
- *
1233
- * @param {boolean | number | any} text
1234
- * @returns {Function}
1235
- * @decorator
1236
- * @mongoose
1237
- * @property
1238
- */
1239
-
1240
- function Text(text = true) {
1241
- return MongooseSchema({
1242
- text
1243
- });
1244
- }
1245
-
1246
- /**
1247
- * Tell Mongoose to make the property trimmable.
1248
- *
1249
- * ### Example
1250
- *
1251
- * ```typescript
1252
- * @Model()
1253
- * export class EventModel {
1254
- * @Trim()
1255
- * name: string;
1256
- * }
1257
- * ```
1258
- *
1259
- * @returns {Function}
1260
- * @decorator
1261
- * @mongoose
1262
- * @property
1263
- */
1264
-
1265
- function Trim() {
1266
- return MongooseSchema({
1267
- trim: true
1268
- });
1269
- }
1270
-
1271
- /**
1272
- * Tell Mongoose to ensure a unique index is created for this path.
1273
- *
1274
- * ### Example
1275
- *
1276
- * ```typescript
1277
- * @Model()
1278
- * export class EventModel {
1279
- * @Unique()
1280
- * index: string;
1281
- * }
1282
- * ```
1283
- *
1284
- * @param {boolean | any} unique
1285
- * @returns {Function}
1286
- * @decorator
1287
- * @mongoose
1288
- */
1289
-
1290
- function Unique(unique = true) {
1291
- return MongooseSchema({
1292
- unique
1293
- });
1294
- }
1295
-
1296
- /**
1297
- * If truthy, Mongoose will add a custom setter that uppercases this string using JavaScript's built-in String#toUpperCase().
1298
- *
1299
- * ### Example
1300
- *
1301
- * ```typescript
1302
- * @Model()
1303
- * export class EventModel {
1304
- * @Uppercase()
1305
- * field: string;
1306
- * }
1307
- * ```
1308
- *
1309
- * @param {boolean} uppercase
1310
- * @returns {Function}
1311
- * @decorator
1312
- * @mongoose
1313
- * @property
1314
- */
1315
-
1316
- function Uppercase(uppercase = true) {
1317
- return MongooseSchema({
1318
- uppercase
1319
- });
1320
- }
1321
-
1322
- function VersionKey() {
1323
- return (target, propertyKey) => {
1324
- Store.from(target).merge(MONGOOSE_SCHEMA_OPTIONS, {
1325
- versionKey: propertyKey
1326
- });
1327
- };
1328
- }
1329
-
1330
- function getRef(opts) {
1331
- const ref = opts.ref || opts.type;
1332
- return isString(ref) ? ref : Store.from(ref).get(MONGOOSE_MODEL_NAME);
1333
- }
1334
-
1335
- function getType(opts) {
1336
- const ref = opts.ref || opts.type;
1337
- return !isString(ref) ? ref : Object;
1338
- }
1339
-
1340
- function getInitialOpts(options, foreignField) {
1341
- return isPlainObject(options) ? options : {
1342
- type: options,
1343
- foreignField
1344
- };
1345
- }
1346
-
1347
- function mapToSchema(opts) {
1348
- const ref = getRef(opts);
1349
- const schema = {
1350
- ref,
1351
- localField: opts.localField || "_id",
1352
- foreignField: opts.foreignField,
1353
- justOne: opts.justOne || false,
1354
- options: opts.options
1355
- };
1356
- return schema;
1357
- }
1358
-
1359
- function VirtualRef(options, foreignField) {
1360
- const opts = getInitialOpts(options, foreignField);
1361
- const schema = mapToSchema(opts);
1362
- const type = getType(opts);
1363
- return useDecorators(StoreMerge(MONGOOSE_SCHEMA, schema), type && (schema.justOne ? Property(type) : CollectionOf(type)));
1364
- }
1365
-
1366
- export { Auto, DecimalFormat, DiscriminatorKey, DynamicRef, ExcludeIndexes, Expires, Immutable, Indexed, Lowercase, MONGOOSE_MODEL, MONGOOSE_MODEL_NAME, MONGOOSE_SCHEMA, MONGOOSE_SCHEMA_OPTIONS, Model, MongooseIndex, MongooseModule, MongoosePlugin, MongooseSchema, MongooseService, NumberDecimal, ObjectID, PostHook, PreHook, Ref, Schema, SchemaIgnore, Select, Sparse, Text, Trim, Unique, Uppercase, VersionKey, VirtualRef, buildMongooseSchema, createModel, createSchema, createSchemaTypeOptions, getModelToken, getSchema, getSchemaToken };
1367
- //# sourceMappingURL=index.modern.js.map