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