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

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