@tsed/mongoose 7.0.0-alpha.2 → 7.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (244) hide show
  1. package/jest.config.js +14 -0
  2. package/lib/cjs/MongooseModule.js +36 -0
  3. package/lib/cjs/MongooseModule.js.map +1 -0
  4. package/lib/cjs/constants/index.js +24 -0
  5. package/lib/cjs/constants/index.js.map +1 -0
  6. package/lib/cjs/decorators/auto.js +28 -0
  7. package/lib/cjs/decorators/auto.js.map +1 -0
  8. package/lib/cjs/decorators/discriminatorKey.js +14 -0
  9. package/lib/cjs/decorators/discriminatorKey.js.map +1 -0
  10. package/lib/cjs/decorators/dynamicRef.js +46 -0
  11. package/lib/cjs/decorators/dynamicRef.js.map +1 -0
  12. package/lib/cjs/decorators/excludeIndexes.js +28 -0
  13. package/lib/cjs/decorators/excludeIndexes.js.map +1 -0
  14. package/lib/cjs/decorators/expires.js +28 -0
  15. package/lib/cjs/decorators/expires.js.map +1 -0
  16. package/lib/cjs/decorators/immutable.js +30 -0
  17. package/lib/cjs/decorators/immutable.js.map +1 -0
  18. package/lib/cjs/decorators/index.js +30 -0
  19. package/lib/cjs/decorators/index.js.map +1 -0
  20. package/lib/cjs/decorators/indexed.js +26 -0
  21. package/lib/cjs/decorators/indexed.js.map +1 -0
  22. package/lib/cjs/decorators/lowercase.js +28 -0
  23. package/lib/cjs/decorators/lowercase.js.map +1 -0
  24. package/lib/cjs/decorators/model.js +67 -0
  25. package/lib/cjs/decorators/model.js.map +1 -0
  26. package/lib/cjs/decorators/mongooseIndex.js +39 -0
  27. package/lib/cjs/decorators/mongooseIndex.js.map +1 -0
  28. package/lib/cjs/decorators/mongooseIndexes.js +39 -0
  29. package/lib/cjs/decorators/mongooseIndexes.js.map +1 -0
  30. package/lib/cjs/decorators/mongoosePlugin.js +22 -0
  31. package/lib/cjs/decorators/mongoosePlugin.js.map +1 -0
  32. package/lib/cjs/decorators/numberDecimal.js +78 -0
  33. package/lib/cjs/decorators/numberDecimal.js.map +1 -0
  34. package/lib/cjs/decorators/objectID.js +26 -0
  35. package/lib/cjs/decorators/objectID.js.map +1 -0
  36. package/lib/cjs/decorators/postHook.js +26 -0
  37. package/lib/cjs/decorators/postHook.js.map +1 -0
  38. package/lib/cjs/decorators/preHook.js +71 -0
  39. package/lib/cjs/decorators/preHook.js.map +1 -0
  40. package/lib/cjs/decorators/ref.js +75 -0
  41. package/lib/cjs/decorators/ref.js.map +1 -0
  42. package/lib/cjs/decorators/schema.js +32 -0
  43. package/lib/cjs/decorators/schema.js.map +1 -0
  44. package/lib/cjs/decorators/schemaIgnore.js +27 -0
  45. package/lib/cjs/decorators/schemaIgnore.js.map +1 -0
  46. package/lib/cjs/decorators/select.js +28 -0
  47. package/lib/cjs/decorators/select.js.map +1 -0
  48. package/lib/cjs/decorators/sparse.js +30 -0
  49. package/lib/cjs/decorators/sparse.js.map +1 -0
  50. package/lib/cjs/decorators/text.js +29 -0
  51. package/lib/cjs/decorators/text.js.map +1 -0
  52. package/lib/cjs/decorators/trim.js +27 -0
  53. package/lib/cjs/decorators/trim.js.map +1 -0
  54. package/lib/cjs/decorators/unique.js +27 -0
  55. package/lib/cjs/decorators/unique.js.map +1 -0
  56. package/lib/cjs/decorators/uppercase.js +28 -0
  57. package/lib/cjs/decorators/uppercase.js.map +1 -0
  58. package/lib/cjs/decorators/versionKey.js +12 -0
  59. package/lib/cjs/decorators/versionKey.js.map +1 -0
  60. package/lib/cjs/decorators/virtualRef.js +38 -0
  61. package/lib/cjs/decorators/virtualRef.js.map +1 -0
  62. package/lib/cjs/index.js +10 -0
  63. package/lib/cjs/index.js.map +1 -0
  64. package/lib/cjs/interfaces/MongooseConnectionOptions.js +3 -0
  65. package/lib/cjs/interfaces/MongooseConnectionOptions.js.map +1 -0
  66. package/lib/cjs/interfaces/MongooseDocument.js +3 -0
  67. package/lib/cjs/interfaces/MongooseDocument.js.map +1 -0
  68. package/lib/cjs/interfaces/MongooseModel.js +3 -0
  69. package/lib/cjs/interfaces/MongooseModel.js.map +1 -0
  70. package/lib/cjs/interfaces/MongooseModelOptions.js +3 -0
  71. package/lib/cjs/interfaces/MongooseModelOptions.js.map +1 -0
  72. package/lib/cjs/interfaces/MongooseSchemaOptions.js +3 -0
  73. package/lib/cjs/interfaces/MongooseSchemaOptions.js.map +1 -0
  74. package/lib/cjs/interfaces/MongooseSchemaTypes.js +10 -0
  75. package/lib/cjs/interfaces/MongooseSchemaTypes.js.map +1 -0
  76. package/lib/cjs/interfaces/MongooseVirtualRefOptions.js +3 -0
  77. package/lib/cjs/interfaces/MongooseVirtualRefOptions.js.map +1 -0
  78. package/lib/cjs/interfaces/index.js +10 -0
  79. package/lib/cjs/interfaces/index.js.map +1 -0
  80. package/lib/cjs/registries/MongooseModels.js +5 -0
  81. package/lib/cjs/registries/MongooseModels.js.map +1 -0
  82. package/lib/cjs/services/MongooseConnections.js +51 -0
  83. package/lib/cjs/services/MongooseConnections.js.map +1 -0
  84. package/lib/cjs/services/MongooseService.js +73 -0
  85. package/lib/cjs/services/MongooseService.js.map +1 -0
  86. package/lib/cjs/utils/createModel.js +58 -0
  87. package/lib/cjs/utils/createModel.js.map +1 -0
  88. package/lib/cjs/utils/createSchema.js +160 -0
  89. package/lib/cjs/utils/createSchema.js.map +1 -0
  90. package/lib/cjs/utils/index.js +6 -0
  91. package/lib/cjs/utils/index.js.map +1 -0
  92. package/lib/cjs/utils/resolveRefType.js +19 -0
  93. package/lib/cjs/utils/resolveRefType.js.map +1 -0
  94. package/lib/cjs/utils/schemaOptions.js +60 -0
  95. package/lib/cjs/utils/schemaOptions.js.map +1 -0
  96. package/lib/esm/MongooseModule.js +33 -0
  97. package/lib/esm/MongooseModule.js.map +1 -0
  98. package/lib/esm/constants/index.js +21 -0
  99. package/lib/esm/constants/index.js.map +1 -0
  100. package/lib/esm/decorators/auto.js +24 -0
  101. package/lib/esm/decorators/auto.js.map +1 -0
  102. package/lib/esm/decorators/discriminatorKey.js +10 -0
  103. package/lib/esm/decorators/discriminatorKey.js.map +1 -0
  104. package/lib/esm/decorators/dynamicRef.js +42 -0
  105. package/lib/esm/decorators/dynamicRef.js.map +1 -0
  106. package/lib/esm/decorators/excludeIndexes.js +24 -0
  107. package/lib/esm/decorators/excludeIndexes.js.map +1 -0
  108. package/lib/esm/decorators/expires.js +24 -0
  109. package/lib/esm/decorators/expires.js.map +1 -0
  110. package/lib/esm/decorators/immutable.js +26 -0
  111. package/lib/esm/decorators/immutable.js.map +1 -0
  112. package/lib/esm/decorators/index.js +27 -0
  113. package/lib/esm/decorators/index.js.map +1 -0
  114. package/lib/esm/decorators/indexed.js +22 -0
  115. package/lib/esm/decorators/indexed.js.map +1 -0
  116. package/lib/esm/decorators/lowercase.js +24 -0
  117. package/lib/esm/decorators/lowercase.js.map +1 -0
  118. package/lib/esm/decorators/model.js +63 -0
  119. package/lib/esm/decorators/model.js.map +1 -0
  120. package/lib/esm/decorators/mongooseIndex.js +35 -0
  121. package/lib/esm/decorators/mongooseIndex.js.map +1 -0
  122. package/lib/esm/decorators/mongooseIndexes.js +35 -0
  123. package/lib/esm/decorators/mongooseIndexes.js.map +1 -0
  124. package/lib/esm/decorators/mongoosePlugin.js +18 -0
  125. package/lib/esm/decorators/mongoosePlugin.js.map +1 -0
  126. package/lib/esm/decorators/numberDecimal.js +74 -0
  127. package/lib/esm/decorators/numberDecimal.js.map +1 -0
  128. package/lib/esm/decorators/objectID.js +22 -0
  129. package/lib/esm/decorators/objectID.js.map +1 -0
  130. package/lib/esm/decorators/postHook.js +22 -0
  131. package/lib/esm/decorators/postHook.js.map +1 -0
  132. package/lib/esm/decorators/preHook.js +67 -0
  133. package/lib/esm/decorators/preHook.js.map +1 -0
  134. package/lib/esm/decorators/ref.js +71 -0
  135. package/lib/esm/decorators/ref.js.map +1 -0
  136. package/lib/esm/decorators/schema.js +27 -0
  137. package/lib/esm/decorators/schema.js.map +1 -0
  138. package/lib/esm/decorators/schemaIgnore.js +23 -0
  139. package/lib/esm/decorators/schemaIgnore.js.map +1 -0
  140. package/lib/esm/decorators/select.js +24 -0
  141. package/lib/esm/decorators/select.js.map +1 -0
  142. package/lib/esm/decorators/sparse.js +26 -0
  143. package/lib/esm/decorators/sparse.js.map +1 -0
  144. package/lib/esm/decorators/text.js +25 -0
  145. package/lib/esm/decorators/text.js.map +1 -0
  146. package/lib/esm/decorators/trim.js +23 -0
  147. package/lib/esm/decorators/trim.js.map +1 -0
  148. package/lib/esm/decorators/unique.js +23 -0
  149. package/lib/esm/decorators/unique.js.map +1 -0
  150. package/lib/esm/decorators/uppercase.js +24 -0
  151. package/lib/esm/decorators/uppercase.js.map +1 -0
  152. package/lib/esm/decorators/versionKey.js +8 -0
  153. package/lib/esm/decorators/versionKey.js.map +1 -0
  154. package/lib/esm/decorators/virtualRef.js +34 -0
  155. package/lib/esm/decorators/virtualRef.js.map +1 -0
  156. package/lib/esm/index.js +7 -0
  157. package/lib/esm/index.js.map +1 -0
  158. package/lib/esm/interfaces/MongooseConnectionOptions.js +2 -0
  159. package/lib/esm/interfaces/MongooseConnectionOptions.js.map +1 -0
  160. package/lib/esm/interfaces/MongooseDocument.js +2 -0
  161. package/lib/esm/interfaces/MongooseDocument.js.map +1 -0
  162. package/lib/esm/interfaces/MongooseModel.js +2 -0
  163. package/lib/esm/interfaces/MongooseModel.js.map +1 -0
  164. package/lib/esm/interfaces/MongooseModelOptions.js +2 -0
  165. package/lib/esm/interfaces/MongooseModelOptions.js.map +1 -0
  166. package/lib/esm/interfaces/MongooseSchemaOptions.js +2 -0
  167. package/lib/esm/interfaces/MongooseSchemaOptions.js.map +1 -0
  168. package/lib/esm/interfaces/MongooseSchemaTypes.js +7 -0
  169. package/lib/esm/interfaces/MongooseSchemaTypes.js.map +1 -0
  170. package/lib/esm/interfaces/MongooseVirtualRefOptions.js +2 -0
  171. package/lib/esm/interfaces/MongooseVirtualRefOptions.js.map +1 -0
  172. package/lib/esm/interfaces/index.js +7 -0
  173. package/lib/esm/interfaces/index.js.map +1 -0
  174. package/lib/esm/registries/MongooseModels.js +2 -0
  175. package/lib/esm/registries/MongooseModels.js.map +1 -0
  176. package/lib/esm/services/MongooseConnections.js +48 -0
  177. package/lib/esm/services/MongooseConnections.js.map +1 -0
  178. package/lib/esm/services/MongooseService.js +70 -0
  179. package/lib/esm/services/MongooseService.js.map +1 -0
  180. package/lib/esm/utils/createModel.js +52 -0
  181. package/lib/esm/utils/createModel.js.map +1 -0
  182. package/lib/esm/utils/createSchema.js +151 -0
  183. package/lib/esm/utils/createSchema.js.map +1 -0
  184. package/lib/esm/utils/index.js +3 -0
  185. package/lib/esm/utils/index.js.map +1 -0
  186. package/lib/esm/utils/resolveRefType.js +15 -0
  187. package/lib/esm/utils/resolveRefType.js.map +1 -0
  188. package/lib/esm/utils/schemaOptions.js +54 -0
  189. package/lib/esm/utils/schemaOptions.js.map +1 -0
  190. package/lib/{MongooseModule.d.ts → types/MongooseModule.d.ts} +0 -0
  191. package/lib/{constants → types/constants}/index.d.ts +0 -0
  192. package/lib/{decorators → types/decorators}/auto.d.ts +0 -0
  193. package/lib/{decorators → types/decorators}/discriminatorKey.d.ts +0 -0
  194. package/lib/{decorators → types/decorators}/dynamicRef.d.ts +0 -0
  195. package/lib/{decorators → types/decorators}/excludeIndexes.d.ts +0 -0
  196. package/lib/{decorators → types/decorators}/expires.d.ts +0 -0
  197. package/lib/{decorators → types/decorators}/immutable.d.ts +0 -0
  198. package/lib/{decorators → types/decorators}/index.d.ts +0 -0
  199. package/lib/{decorators → types/decorators}/indexed.d.ts +0 -0
  200. package/lib/{decorators → types/decorators}/lowercase.d.ts +0 -0
  201. package/lib/{decorators → types/decorators}/model.d.ts +0 -0
  202. package/lib/{decorators → types/decorators}/mongooseIndex.d.ts +0 -0
  203. package/lib/types/decorators/mongooseIndexes.d.ts +32 -0
  204. package/lib/{decorators → types/decorators}/mongoosePlugin.d.ts +0 -0
  205. package/lib/{decorators → types/decorators}/numberDecimal.d.ts +0 -0
  206. package/lib/{decorators → types/decorators}/objectID.d.ts +0 -0
  207. package/lib/{decorators → types/decorators}/postHook.d.ts +0 -0
  208. package/lib/{decorators → types/decorators}/preHook.d.ts +0 -0
  209. package/lib/{decorators → types/decorators}/ref.d.ts +7 -2
  210. package/lib/{decorators → types/decorators}/schema.d.ts +0 -0
  211. package/lib/{decorators → types/decorators}/schemaIgnore.d.ts +0 -0
  212. package/lib/{decorators → types/decorators}/select.d.ts +0 -0
  213. package/lib/{decorators → types/decorators}/sparse.d.ts +0 -0
  214. package/lib/{decorators → types/decorators}/text.d.ts +0 -0
  215. package/lib/{decorators → types/decorators}/trim.d.ts +0 -0
  216. package/lib/{decorators → types/decorators}/unique.d.ts +0 -0
  217. package/lib/{decorators → types/decorators}/uppercase.d.ts +0 -0
  218. package/lib/{decorators → types/decorators}/versionKey.d.ts +0 -0
  219. package/lib/{decorators → types/decorators}/virtualRef.d.ts +0 -0
  220. package/lib/{index.d.ts → types/index.d.ts} +0 -0
  221. package/lib/{interfaces → types/interfaces}/MongooseConnectionOptions.d.ts +0 -0
  222. package/lib/{interfaces → types/interfaces}/MongooseDocument.d.ts +0 -0
  223. package/lib/{interfaces → types/interfaces}/MongooseModel.d.ts +0 -0
  224. package/lib/{interfaces → types/interfaces}/MongooseModelOptions.d.ts +0 -0
  225. package/lib/{interfaces → types/interfaces}/MongooseSchemaOptions.d.ts +0 -0
  226. package/lib/{interfaces → types/interfaces}/MongooseSchemaTypes.d.ts +0 -0
  227. package/lib/{interfaces → types/interfaces}/MongooseVirtualRefOptions.d.ts +1 -0
  228. package/lib/{interfaces → types/interfaces}/index.d.ts +0 -0
  229. package/lib/{registries → types/registries}/MongooseModels.d.ts +0 -0
  230. package/lib/{services → types/services}/MongooseConnections.d.ts +0 -0
  231. package/lib/{services → types/services}/MongooseService.d.ts +0 -0
  232. package/lib/{utils → types/utils}/createModel.d.ts +2 -1
  233. package/lib/{utils → types/utils}/createSchema.d.ts +0 -0
  234. package/lib/{utils → types/utils}/index.d.ts +0 -0
  235. package/lib/{utils → types/utils}/resolveRefType.d.ts +0 -0
  236. package/lib/{utils → types/utils}/schemaOptions.d.ts +0 -0
  237. package/package.json +25 -24
  238. package/tsconfig.compile.esm.json +23 -0
  239. package/.mocharc.js +0 -5
  240. package/.nycrc +0 -27
  241. package/lib/index.js +0 -1402
  242. package/lib/index.js.map +0 -1
  243. package/lib/index.modern.js +0 -1362
  244. package/lib/index.modern.js.map +0 -1
@@ -1,1362 +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
- * @returns {Model<T extends Document>}
498
- */
499
-
500
- function createModel(target, schema, name = nameOf(target), collection, skipInit, connection, discriminatorValue) {
501
- // if ancestor has a discriminatorKey we need to link model with anscestor schema
502
- const ancestor = ancestorsOf(target).find(ancestor => {
503
- const options = Store.from(ancestor).get(MONGOOSE_SCHEMA_OPTIONS) || {};
504
- return !!options.discriminatorKey;
505
- });
506
-
507
- if (ancestor && ancestor !== target) {
508
- const ancestorModel = Store.from(ancestor).get(MONGOOSE_MODEL);
509
-
510
- if (ancestorModel) {
511
- const discriminatorName = discriminatorValue || name;
512
-
513
- if (ancestorModel.discriminators && ancestorModel.discriminators[discriminatorName]) {
514
- return ancestorModel.discriminators[discriminatorName];
515
- } else {
516
- return ancestorModel.discriminator(discriminatorName, schema);
517
- }
518
- }
519
- }
520
- /* istanbul ignore else */
521
-
522
-
523
- if (connection) {
524
- const _model = connection.model(name, schema, collection);
525
-
526
- Store.from(target).set(MONGOOSE_MODEL, _model);
527
- return _model;
528
- }
529
-
530
- const model = mongoose.model(name, schema, collection, skipInit);
531
- Store.from(target).set(MONGOOSE_MODEL, model);
532
- return model;
533
- }
534
-
535
- function Schema(options = {}) {
536
- return (...parameters) => {
537
- switch (decoratorTypeOf(parameters)) {
538
- case "property":
539
- return useDecorators(Property(), StoreMerge(MONGOOSE_SCHEMA, options))(parameters[0], parameters[1], parameters[2]);
540
-
541
- case "class":
542
- const {
543
- token
544
- } = getSchemaToken(parameters[0], options);
545
- registerProvider({
546
- provide: token,
547
- deps: [],
548
-
549
- useFactory() {
550
- return getSchema(parameters[0], options);
551
- }
552
-
553
- });
554
- break;
555
- }
556
- };
557
- }
558
- function MongooseSchema(options = {}) {
559
- return Schema(options);
560
- }
561
-
562
- /**
563
- * If true, uses Mongoose's default _id settings. Only allowed for ObjectIds
564
- *
565
- * ### Example
566
- *
567
- * ```typescript
568
- * @Model()
569
- * export class EventModel {
570
- * @ExcludeIndexes()
571
- * field: string;
572
- * }
573
- * ```
574
- *
575
- * @param {boolean} auto
576
- * @returns {Function}
577
- * @decorator
578
- * @mongoose
579
- * @property
580
- */
581
-
582
- function Auto(auto = true) {
583
- return MongooseSchema({
584
- auto
585
- });
586
- }
587
-
588
- function DiscriminatorKey() {
589
- return (target, propertyKey) => {
590
- Store.from(target).merge(MONGOOSE_SCHEMA_OPTIONS, {
591
- discriminatorKey: propertyKey
592
- });
593
- };
594
- }
595
-
596
- /**
597
- * Define a property as mongoose reference to other Model (decorated with @Model).
598
- *
599
- * ### Example
600
- *
601
- * ```typescript
602
- * @Model()
603
- * class FooModel {
604
- *
605
- * @DynamicRef('type')
606
- * field: DynamicRef<OtherFooModel | OtherModel>
607
- *
608
- * @Enum(['OtherFooModel', 'OtherModel'])
609
- * type: string
610
- * }
611
- *
612
- * @Model()
613
- * class OtherFooModel {
614
- * }
615
- *
616
- * @Model()
617
- * class OtherModel {
618
- * }
619
- * ```
620
- *
621
- * @param refPath
622
- * @returns {Function}
623
- * @decorator
624
- * @mongoose
625
- * @property
626
- */
627
-
628
- function DynamicRef(refPath) {
629
- return useDecorators(Property(String), Example("5ce7ad3028890bd71749d477"), Description("Mongoose Ref ObjectId"), StoreMerge(MONGOOSE_SCHEMA, {
630
- type: Schema$1.Types.ObjectId,
631
- refPath
632
- }));
633
- }
634
-
635
- /**
636
- * If true, Mongoose will skip gathering indexes on subpaths. Only allowed for subdocuments and subdocument arrays.
637
- *
638
- * ### Example
639
- *
640
- * ```typescript
641
- * @Model()
642
- * export class EventModel {
643
- * @ExcludeIndexes()
644
- * field: string;
645
- * }
646
- * ```
647
- *
648
- * @param {boolean} excludeIndexes
649
- * @returns {Function}
650
- * @decorator
651
- * @mongoose
652
- * @property
653
- */
654
-
655
- function ExcludeIndexes(excludeIndexes = true) {
656
- return MongooseSchema({
657
- excludeIndexes
658
- });
659
- }
660
-
661
- /**
662
- * Defines a TTL index on this path. Only allowed for dates.
663
- *
664
- * ### Example
665
- *
666
- * ```typescript
667
- * @Model()
668
- * export class EventModel {
669
- * @Expires('5d')
670
- * field: string;
671
- * }
672
- * ```
673
- *
674
- * @param {boolean | any} expires
675
- * @returns {Function}
676
- * @decorator
677
- * @mongoose
678
- * @property
679
- */
680
-
681
- function Expires(expires) {
682
- return MongooseSchema({
683
- expires
684
- });
685
- }
686
-
687
- /**
688
- * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose will
689
- * disallow changes to this path once the document is saved to the database for the first time. Read more
690
- * about [immutability in Mongoose here](http://thecodebarbarian.com/whats-new-in-mongoose-5-6-immutable-properties.html).
691
- *
692
- * ### Example
693
- *
694
- * ```typescript
695
- * @Model()
696
- * export class EventModel {
697
- * @Immutable()
698
- * field: string;
699
- * }
700
- * ```
701
- *
702
- * @param {boolean | any} immutable
703
- * @returns {Function}
704
- * @decorator
705
- * @mongoose
706
- * @property
707
- */
708
-
709
- function Immutable(immutable = true) {
710
- return MongooseSchema({
711
- immutable
712
- });
713
- }
714
-
715
- /**
716
- * Tell Mongoose whether to define an index for the property.
717
- * ### Example
718
- *
719
- * ```typescript
720
- * @Model()
721
- * export class EventModel {
722
- * @Indexed()
723
- * field: string;
724
- * }
725
- * ```
726
- *
727
- * @returns {Function}
728
- * @decorator
729
- * @mongoose
730
- * @property
731
- */
732
-
733
- function Indexed(index = true) {
734
- return MongooseSchema({
735
- index
736
- });
737
- }
738
-
739
- /**
740
- * If truthy, Mongoose will add a custom setter that lowercases this string using JavaScript's built-in String#toLowerCase().
741
- *
742
- * ### Example
743
- *
744
- * ```typescript
745
- * @Model()
746
- * export class EventModel {
747
- * @Lowercase()
748
- * field: string;
749
- * }
750
- * ```
751
- *
752
- * @param {boolean} lowercase
753
- * @returns {Function}
754
- * @decorator
755
- * @mongoose
756
- * @property
757
- */
758
-
759
- function Lowercase(lowercase = true) {
760
- return MongooseSchema({
761
- lowercase
762
- });
763
- }
764
-
765
- /**
766
- * Define a class as a Mongoose Model. The model can be injected to the Service, Controller, Middleware, Converters or Filter with
767
- * `@Inject` annotation.
768
- *
769
- * ### Example
770
- *
771
- * ```typescript
772
- * @Model()
773
- * export class EventModel {
774
- * @Property()
775
- * field: string;
776
- * }
777
- * ```
778
- *
779
- * Then inject the model into a service:
780
- *
781
- * ```typescript
782
- * class MyService {
783
- * constructor(@Inject(EventModel) eventModel: MongooseModel<EventModel>) {
784
- * eventModel.findById().exec();
785
- * }
786
- * }
787
- * ```
788
- *
789
- * ### Options
790
- *
791
- * - `schemaOptions` (mongoose.SchemaOptions): Option to configure the schema behavior.
792
- * - `name` (String): model name.
793
- * - `collection` (String): collection (optional, induced from model name).
794
- * - `skipInit` (Boolean): skipInit whether to skip initialization (defaults to false).
795
- *
796
- * @param {MongooseModelOptions} options
797
- * @returns {(target: any) => void}
798
- * @decorator
799
- * @mongoose
800
- * @class
801
- */
802
-
803
- function Model(options = {}) {
804
- return target => {
805
- const {
806
- token,
807
- collectionName
808
- } = getModelToken(target, options);
809
- registerProvider({
810
- provide: token,
811
- deps: [],
812
-
813
- useFactory() {
814
- return getSchema(target, options);
815
- }
816
-
817
- });
818
- registerProvider({
819
- provide: target,
820
- type: "mongoose:model",
821
- deps: [MONGOOSE_CONNECTIONS, token],
822
-
823
- useFactory(connections, schema) {
824
- applySchemaOptions(schema, schemaOptions(target));
825
- return createModel(target, schema, collectionName, options.collection, options.skipInit, connections.get(options.connection), options.discriminatorValue);
826
- }
827
-
828
- });
829
- };
830
- }
831
-
832
- /**
833
- * Calls schema.index() to define an index (most likely compound) for the schema.
834
- *
835
- * ### Example
836
- *
837
- * ```typescript
838
- * @Model()
839
- * @MongooseIndex({first: 1, second: 1}, {unique: 1})
840
- * export class EventModel {
841
- *
842
- * @Property()
843
- * first: string;
844
- *
845
- * @Property()
846
- * second: string;
847
- *
848
- * }
849
- * ```
850
- *
851
- * @param fields
852
- * @param options
853
- * @returns {Function}
854
- * @decorator
855
- * @mongoose
856
- * @class
857
- */
858
-
859
- function MongooseIndex(fields, options) {
860
- return target => {
861
- schemaOptions(target, {
862
- indexes: [{
863
- fields,
864
- options
865
- }]
866
- });
867
- };
868
- }
869
-
870
- /**
871
- *
872
- * @param {(schema: "mongoose".Schema, options?: any) => void} plugin
873
- * @param options
874
- * @returns {Function}
875
- * @decorator
876
- * @mongoose
877
- * @class
878
- */
879
-
880
- function MongoosePlugin(plugin, options) {
881
- return target => {
882
- schemaOptions(target, {
883
- plugins: [{
884
- plugin,
885
- options
886
- }]
887
- });
888
- };
889
- }
890
-
891
- let DecimalFormat = class DecimalFormat {
892
- validate(num) {
893
- return isString(num) || isNumber(num);
894
- }
895
-
896
- };
897
- DecimalFormat = __decorate([Formats("decimal", {
898
- type: "number"
899
- })], DecimalFormat);
900
-
901
- function isDecimal(value) {
902
- return value && value._bsontype === "Decimal128";
903
- }
904
- /**
905
- * Tell Mongoose whether to define an Decimal128 property.
906
- * Will be serialized as `number` with format as `decimal`.
907
- * ### Example
908
- *
909
- * ```typescript
910
- * @Model()
911
- * export class PriceModel {
912
- * @NumberDecimal()
913
- * price: Decimal128;
914
- * }
915
- * ```
916
- * Optionally using custom decimal type, such as `Big` from big.js
917
- * ```typescript
918
- * @Model()
919
- * export class PriceModel {
920
- * @NumberDecimal(Big)
921
- * price: Big;
922
- * }
923
- * ```
924
- * @param type Optional decimal type constructor
925
- * @decorator
926
- * @mongoose
927
- * @schema
928
- */
929
-
930
-
931
- function NumberDecimal(type) {
932
- const schema = {
933
- type: Schema$1.Types.Decimal128
934
- };
935
-
936
- if (type) {
937
- // Define property getter to convert Decimal128 to custom type
938
- schema.get = value => {
939
- return isDecimal(value) ? new type(value) : value;
940
- };
941
- }
942
-
943
- return useDecorators(Property(Number), Format("decimal"), Example(12.34), StoreMerge(MONGOOSE_SCHEMA, schema), // Deserialize number value from JSON to Decimal128
944
- OnDeserialize(value => {
945
- if (type) {
946
- return new type(value);
947
- }
948
-
949
- if (isString(value)) {
950
- return Types.Decimal128.fromString(value);
951
- }
952
-
953
- return Types.Decimal128.fromString(`${value}`);
954
- }), // Serialize decimal value to floating point number
955
- OnSerialize((value, ctx) => {
956
- return value && Number(value);
957
- }));
958
- }
959
-
960
- /**
961
- * Tell Mongoose whether to define an ObjectId property.
962
- * ### Example
963
- *
964
- * ```typescript
965
- * @Model()
966
- * export class EventModel {
967
- * @ObjectId('id')
968
- * _id: string;
969
- * }
970
- * ```
971
- * @param name
972
- * @decorator
973
- * @mongoose
974
- * @schema
975
- */
976
-
977
- function ObjectID(name) {
978
- return useDecorators(name && Name(name), Pattern(/^[0-9a-fA-F]{24}$/), Description("Mongoose ObjectId"), Example("5ce7ad3028890bd71749d477"));
979
- }
980
-
981
- function PostHook(method, ...params) {
982
- return (...args) => {
983
- let options = params[1];
984
- let fn = params[0];
985
-
986
- if (decoratorTypeOf(args) === DecoratorTypes.METHOD_STC) {
987
- options = params[0];
988
- fn = args[0][args[1]].bind(args[0]);
989
- }
990
-
991
- schemaOptions(args[0], {
992
- post: [{
993
- method,
994
- fn,
995
- options
996
- }]
997
- });
998
- };
999
- }
1000
-
1001
- /**
1002
- *
1003
- * We can simply attach a `@PreHook` decorator to your model class and
1004
- * define the hook function like you normally would in Mongoose.
1005
- *
1006
- * ```typescript
1007
- * import {Ignore, Required} from "@tsed/common";
1008
- * import {PreHook, Model} from "@tsed/mongoose";
1009
- *
1010
- * @Model()
1011
- * @PreHook("save", (car: CarModel, next) => {
1012
- * if (car.model === 'Tesla') {
1013
- * car.isFast = true;
1014
- * }
1015
- * next();
1016
- *})
1017
- * export class CarModel {
1018
- *
1019
- * @Ignore()
1020
- * _id: string;
1021
- *
1022
- * @Required()
1023
- * model: string;
1024
- *
1025
- * @Required()
1026
- * isFast: boolean;
1027
- *
1028
- * // or Prehook on static method
1029
- * @PreHook("save")
1030
- * static preSave(car: CarModel, next) {
1031
- * if (car.model === 'Tesla') {
1032
- * car.isFast = true;
1033
- * }
1034
- * next();
1035
- * }
1036
- * }
1037
- * ```
1038
- *
1039
- * This will execute the pre-save hook each time a `CarModel` document is saved.
1040
- *
1041
- * @param {string} method
1042
- * @param fn
1043
- * @param options
1044
- * @returns {Function}
1045
- * @decorator
1046
- * @class
1047
- */
1048
-
1049
- function PreHook(method, fn, options) {
1050
- return (...args) => {
1051
- if (decoratorTypeOf(args) === DecoratorTypes.METHOD_STC) {
1052
- options = fn;
1053
- fn = args[0][args[1]].bind(args[0]);
1054
- }
1055
-
1056
- schemaOptions(args[0], {
1057
- pre: [{
1058
- method,
1059
- fn: fn,
1060
- options
1061
- }]
1062
- });
1063
- };
1064
- }
1065
-
1066
- var MongooseSchemaTypes;
1067
-
1068
- (function (MongooseSchemaTypes) {
1069
- MongooseSchemaTypes["OBJECT_ID"] = "ObjectId";
1070
- MongooseSchemaTypes["STRING"] = "String";
1071
- MongooseSchemaTypes["NUMBER"] = "Number";
1072
- })(MongooseSchemaTypes || (MongooseSchemaTypes = {}));
1073
-
1074
- function isRef(value) {
1075
- return value && value._bsontype || isString(value);
1076
- }
1077
- /**
1078
- * Define a property as mongoose reference to other Model (decorated with @Model).
1079
- *
1080
- * ### Example
1081
- *
1082
- * ```typescript
1083
- *
1084
- * @Model()
1085
- * class FooModel {
1086
- *
1087
- * @Ref(Foo2Model)
1088
- * field: Ref<Foo2Model>
1089
- *
1090
- * @Ref(Foo2Model)
1091
- * list: Ref<Foo2Model>[]
1092
- * }
1093
- *
1094
- * @Model()
1095
- * class Foo2Model {
1096
- * }
1097
- * ```
1098
- *
1099
- * @param model
1100
- * @param type
1101
- * @returns {Function}
1102
- * @decorator
1103
- * @mongoose
1104
- * @property
1105
- */
1106
-
1107
-
1108
- function Ref(model, type = MongooseSchemaTypes.OBJECT_ID) {
1109
- const getType = () => isString(model) ? MongooseModels.get(model) : isArrowFn(model) ? model() : model;
1110
-
1111
- return useDecorators(Property(Object), StoreMerge(MONGOOSE_SCHEMA, {
1112
- type: Schema$1.Types[type],
1113
- ref: model
1114
- }), OnDeserialize(value => {
1115
- if (isRef(value)) {
1116
- return value.toString();
1117
- }
1118
-
1119
- return value;
1120
- }), OnSerialize((value, ctx) => {
1121
- if (isRef(value)) {
1122
- return value.toString();
1123
- }
1124
-
1125
- const type = getType();
1126
- return serialize(value, _extends({}, ctx, {
1127
- type
1128
- }));
1129
- }), JsonEntityFn(async store => {
1130
- store.itemSchema.oneOf([string().example("5ce7ad3028890bd71749d477").description("Mongoose Ref ObjectId"), lazyRef(getType)]);
1131
- store.type = Object;
1132
- }));
1133
- }
1134
-
1135
- /**
1136
- * Do not apply this property to schema (create virtual property)
1137
- *
1138
- * ### Example
1139
- *
1140
- * ```typescript
1141
- * @Model()
1142
- * @SchemaIgnore()
1143
- * @Property()
1144
- * kind: string;
1145
- *
1146
- * ```
1147
- *
1148
- * @returns {Function}
1149
- * @decorator
1150
- * @mongoose
1151
- * @class
1152
- */
1153
-
1154
- function SchemaIgnore() {
1155
- return MongooseSchema({
1156
- schemaIgnore: true
1157
- });
1158
- }
1159
-
1160
- /**
1161
- * Tell Mongoose to set default select() behavior for this path.
1162
- *
1163
- * ### Example
1164
- *
1165
- * ```typescript
1166
- * @Model()
1167
- * export class EventModel {
1168
- * @Select()
1169
- * field: string;
1170
- * }
1171
- * ```
1172
- *
1173
- * @param {boolean | any} select
1174
- * @returns {Function}
1175
- * @decorator
1176
- * @mongoose
1177
- * @property
1178
- */
1179
-
1180
- function Select(select = true) {
1181
- return MongooseSchema({
1182
- select
1183
- });
1184
- }
1185
-
1186
- /**
1187
- * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose will
1188
- * disallow changes to this path once the document is saved to the database for the first time. Read more
1189
- * about [immutability in Mongoose here](http://thecodebarbarian.com/whats-new-in-mongoose-5-6-immutable-properties.html).
1190
- *
1191
- * ### Example
1192
- *
1193
- * ```typescript
1194
- * @Model()
1195
- * export class EventModel {
1196
- * @Sparse()
1197
- * field: string;
1198
- * }
1199
- * ```
1200
- *
1201
- * @param {boolean | number} sparse
1202
- * @returns {Function}
1203
- * @decorator
1204
- * @mongoose
1205
- * @property
1206
- */
1207
-
1208
- function Sparse(sparse = true) {
1209
- return MongooseSchema({
1210
- sparse
1211
- });
1212
- }
1213
-
1214
- /**
1215
- * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose
1216
- * will build a text index on this path.
1217
- *
1218
- * ### Example
1219
- *
1220
- * ```typescript
1221
- * @Model()
1222
- * export class EventModel {
1223
- * @Text()
1224
- * field: string;
1225
- * }
1226
- * ```
1227
- *
1228
- * @param {boolean | number | any} text
1229
- * @returns {Function}
1230
- * @decorator
1231
- * @mongoose
1232
- * @property
1233
- */
1234
-
1235
- function Text(text = true) {
1236
- return MongooseSchema({
1237
- text
1238
- });
1239
- }
1240
-
1241
- /**
1242
- * Tell Mongoose to make the property trimmable.
1243
- *
1244
- * ### Example
1245
- *
1246
- * ```typescript
1247
- * @Model()
1248
- * export class EventModel {
1249
- * @Trim()
1250
- * name: string;
1251
- * }
1252
- * ```
1253
- *
1254
- * @returns {Function}
1255
- * @decorator
1256
- * @mongoose
1257
- * @property
1258
- */
1259
-
1260
- function Trim() {
1261
- return MongooseSchema({
1262
- trim: true
1263
- });
1264
- }
1265
-
1266
- /**
1267
- * Tell Mongoose to ensure a unique index is created for this path.
1268
- *
1269
- * ### Example
1270
- *
1271
- * ```typescript
1272
- * @Model()
1273
- * export class EventModel {
1274
- * @Unique()
1275
- * index: string;
1276
- * }
1277
- * ```
1278
- *
1279
- * @param {boolean | any} unique
1280
- * @returns {Function}
1281
- * @decorator
1282
- * @mongoose
1283
- */
1284
-
1285
- function Unique(unique = true) {
1286
- return MongooseSchema({
1287
- unique
1288
- });
1289
- }
1290
-
1291
- /**
1292
- * If truthy, Mongoose will add a custom setter that uppercases this string using JavaScript's built-in String#toUpperCase().
1293
- *
1294
- * ### Example
1295
- *
1296
- * ```typescript
1297
- * @Model()
1298
- * export class EventModel {
1299
- * @Uppercase()
1300
- * field: string;
1301
- * }
1302
- * ```
1303
- *
1304
- * @param {boolean} uppercase
1305
- * @returns {Function}
1306
- * @decorator
1307
- * @mongoose
1308
- * @property
1309
- */
1310
-
1311
- function Uppercase(uppercase = true) {
1312
- return MongooseSchema({
1313
- uppercase
1314
- });
1315
- }
1316
-
1317
- function VersionKey() {
1318
- return (target, propertyKey) => {
1319
- Store.from(target).merge(MONGOOSE_SCHEMA_OPTIONS, {
1320
- versionKey: propertyKey
1321
- });
1322
- };
1323
- }
1324
-
1325
- function getRef(opts) {
1326
- const ref = opts.ref;
1327
- return isString(ref) ? ref : Store.from(ref).get(MONGOOSE_MODEL_NAME);
1328
- }
1329
-
1330
- function getType(opts) {
1331
- const ref = opts.ref;
1332
- return !isString(ref) ? ref : Object;
1333
- }
1334
-
1335
- function getInitialOpts(options, foreignField) {
1336
- return isPlainObject(options) ? options : {
1337
- ref: options,
1338
- foreignField
1339
- };
1340
- }
1341
-
1342
- function mapToSchema(opts) {
1343
- const ref = getRef(opts);
1344
- const schema = {
1345
- ref,
1346
- localField: opts.localField || "_id",
1347
- foreignField: opts.foreignField,
1348
- justOne: opts.justOne || false,
1349
- options: opts.options
1350
- };
1351
- return schema;
1352
- }
1353
-
1354
- function VirtualRef(options, foreignField) {
1355
- const opts = getInitialOpts(options, foreignField);
1356
- const schema = mapToSchema(opts);
1357
- const type = getType(opts);
1358
- return useDecorators(StoreMerge(MONGOOSE_SCHEMA, schema), type && (schema.justOne ? Property(type) : CollectionOf(type)));
1359
- }
1360
-
1361
- 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 };
1362
- //# sourceMappingURL=index.modern.js.map