@tsed/mongoose 7.0.0-beta.1 → 7.0.0-beta.4
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.
- package/jest.config.js +1 -1
- package/lib/cjs/MongooseModule.js +3 -3
- package/lib/cjs/MongooseModule.js.map +1 -1
- package/lib/cjs/constants/{index.js → constants.js} +1 -1
- package/lib/cjs/constants/constants.js.map +1 -0
- package/lib/cjs/decorators/discriminatorKey.js +1 -1
- package/lib/cjs/decorators/discriminatorKey.js.map +1 -1
- package/lib/cjs/decorators/dynamicRef.js +24 -5
- package/lib/cjs/decorators/dynamicRef.js.map +1 -1
- package/lib/cjs/decorators/model.js +5 -4
- package/lib/cjs/decorators/model.js.map +1 -1
- package/lib/cjs/decorators/numberDecimal.js +2 -2
- package/lib/cjs/decorators/numberDecimal.js.map +1 -1
- package/lib/cjs/decorators/postHook.js.map +1 -1
- package/lib/cjs/decorators/preHook.js.map +1 -1
- package/lib/cjs/decorators/ref.js +1 -1
- package/lib/cjs/decorators/ref.js.map +1 -1
- package/lib/cjs/decorators/schema.js +1 -1
- package/lib/cjs/decorators/schema.js.map +1 -1
- package/lib/cjs/decorators/versionKey.js +1 -1
- package/lib/cjs/decorators/versionKey.js.map +1 -1
- package/lib/cjs/decorators/virtualRef.js +1 -1
- package/lib/cjs/decorators/virtualRef.js.map +1 -1
- package/lib/cjs/index.js +47 -6
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/interfaces/MongooseModel.js.map +1 -1
- package/lib/cjs/interfaces/MongooseModelOptions.js.map +1 -1
- package/lib/cjs/interfaces/MongooseSchemaOptions.js.map +1 -1
- package/lib/cjs/interfaces/interfaces.js +3 -0
- package/lib/cjs/interfaces/interfaces.js.map +1 -0
- package/lib/cjs/package.json +3 -0
- package/lib/cjs/services/MongooseConnections.js +1 -5
- package/lib/cjs/services/MongooseConnections.js.map +1 -1
- package/lib/cjs/services/MongooseService.js +4 -4
- package/lib/cjs/services/MongooseService.js.map +1 -1
- package/lib/cjs/utils/createModel.js +7 -11
- package/lib/cjs/utils/createModel.js.map +1 -1
- package/lib/cjs/utils/createSchema.js +2 -2
- package/lib/cjs/utils/createSchema.js.map +1 -1
- package/lib/cjs/utils/resolveRefType.js +1 -1
- package/lib/cjs/utils/resolveRefType.js.map +1 -1
- package/lib/cjs/utils/schemaOptions.js +1 -1
- package/lib/cjs/utils/schemaOptions.js.map +1 -1
- package/lib/esm/MongooseModule.js +2 -2
- package/lib/esm/constants/{index.js → constants.js} +1 -1
- package/lib/esm/constants/constants.js.map +1 -0
- package/lib/esm/decorators/auto.js +1 -1
- package/lib/esm/decorators/discriminatorKey.js +2 -2
- package/lib/esm/decorators/discriminatorKey.js.map +1 -1
- package/lib/esm/decorators/dynamicRef.js +26 -7
- package/lib/esm/decorators/dynamicRef.js.map +1 -1
- package/lib/esm/decorators/excludeIndexes.js +1 -1
- package/lib/esm/decorators/expires.js +1 -1
- package/lib/esm/decorators/immutable.js +1 -1
- package/lib/esm/decorators/indexed.js +1 -1
- package/lib/esm/decorators/lowercase.js +1 -1
- package/lib/esm/decorators/model.js +5 -4
- package/lib/esm/decorators/model.js.map +1 -1
- package/lib/esm/decorators/mongooseIndex.js +1 -1
- package/lib/esm/decorators/mongooseIndexes.js +1 -1
- package/lib/esm/decorators/mongoosePlugin.js +1 -1
- package/lib/esm/decorators/numberDecimal.js +1 -1
- package/lib/esm/decorators/numberDecimal.js.map +1 -1
- package/lib/esm/decorators/postHook.js +1 -1
- package/lib/esm/decorators/postHook.js.map +1 -1
- package/lib/esm/decorators/preHook.js +1 -1
- package/lib/esm/decorators/preHook.js.map +1 -1
- package/lib/esm/decorators/ref.js +3 -3
- package/lib/esm/decorators/ref.js.map +1 -1
- package/lib/esm/decorators/schema.js +2 -2
- package/lib/esm/decorators/schema.js.map +1 -1
- package/lib/esm/decorators/schemaIgnore.js +1 -1
- package/lib/esm/decorators/select.js +1 -1
- package/lib/esm/decorators/sparse.js +1 -1
- package/lib/esm/decorators/text.js +1 -1
- package/lib/esm/decorators/trim.js +1 -1
- package/lib/esm/decorators/unique.js +1 -1
- package/lib/esm/decorators/uppercase.js +1 -1
- package/lib/esm/decorators/versionKey.js +1 -1
- package/lib/esm/decorators/versionKey.js.map +1 -1
- package/lib/esm/decorators/virtualRef.js +2 -2
- package/lib/esm/decorators/virtualRef.js.map +1 -1
- package/lib/esm/index.js +47 -6
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/interfaces/MongooseModel.js.map +1 -1
- package/lib/esm/interfaces/MongooseModelOptions.js.map +1 -1
- package/lib/esm/interfaces/MongooseSchemaOptions.js.map +1 -1
- package/lib/esm/interfaces/interfaces.js +2 -0
- package/lib/esm/interfaces/interfaces.js.map +1 -0
- package/lib/esm/package.json +3 -0
- package/lib/esm/services/MongooseConnections.js +2 -6
- package/lib/esm/services/MongooseConnections.js.map +1 -1
- package/lib/esm/utils/createModel.js +8 -12
- package/lib/esm/utils/createModel.js.map +1 -1
- package/lib/esm/utils/createSchema.js +3 -3
- package/lib/esm/utils/createSchema.js.map +1 -1
- package/lib/esm/utils/resolveRefType.js +1 -1
- package/lib/esm/utils/resolveRefType.js.map +1 -1
- package/lib/esm/utils/schemaOptions.js +1 -1
- package/lib/esm/utils/schemaOptions.js.map +1 -1
- package/lib/types/constants/{index.d.ts → constants.d.ts} +0 -0
- package/lib/types/decorators/dynamicRef.d.ts +4 -2
- package/lib/types/decorators/postHook.d.ts +1 -1
- package/lib/types/decorators/preHook.d.ts +1 -1
- package/lib/types/decorators/schema.d.ts +1 -1
- package/lib/types/decorators/virtualRef.d.ts +0 -4
- package/lib/types/index.d.ts +46 -5
- package/lib/types/interfaces/MongooseModel.d.ts +1 -2
- package/lib/types/interfaces/MongooseModelOptions.d.ts +1 -1
- package/lib/types/interfaces/MongooseSchemaOptions.d.ts +1 -1
- package/lib/types/interfaces/{index.d.ts → interfaces.d.ts} +0 -6
- package/lib/types/utils/createModel.d.ts +2 -2
- package/lib/types/utils/createSchema.d.ts +1 -1
- package/lib/types/utils/schemaOptions.d.ts +1 -1
- package/package.json +20 -18
- package/readme.md +124 -134
- package/tsconfig.compile.esm.json +2 -12
- package/lib/cjs/constants/index.js.map +0 -1
- package/lib/cjs/decorators/index.js +0 -30
- package/lib/cjs/decorators/index.js.map +0 -1
- package/lib/cjs/interfaces/index.js +0 -10
- package/lib/cjs/interfaces/index.js.map +0 -1
- package/lib/cjs/utils/index.js +0 -6
- package/lib/cjs/utils/index.js.map +0 -1
- package/lib/esm/constants/index.js.map +0 -1
- package/lib/esm/decorators/index.js +0 -27
- package/lib/esm/decorators/index.js.map +0 -1
- package/lib/esm/interfaces/index.js +0 -7
- package/lib/esm/interfaces/index.js.map +0 -1
- package/lib/esm/utils/index.js +0 -3
- package/lib/esm/utils/index.js.map +0 -1
- package/lib/types/decorators/index.d.ts +0 -26
- package/lib/types/utils/index.d.ts +0 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/mongoose",
|
|
3
|
-
"version": "7.0.0-beta.
|
|
3
|
+
"version": "7.0.0-beta.4",
|
|
4
4
|
"description": "Mongoose package for Ts.ED framework",
|
|
5
5
|
"private": false,
|
|
6
6
|
"keywords": [
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
],
|
|
13
13
|
"source": "./src/index.ts",
|
|
14
14
|
"main": "./lib/cjs/index.js",
|
|
15
|
+
"module": "./lib/esm/index.js",
|
|
15
16
|
"typings": "./lib/types/index.d.ts",
|
|
16
17
|
"exports": {
|
|
17
18
|
"types": "./lib/types/index.d.ts",
|
|
@@ -20,38 +21,39 @@
|
|
|
20
21
|
"default": "./lib/esm/index.js"
|
|
21
22
|
},
|
|
22
23
|
"scripts": {
|
|
23
|
-
"build": "yarn run build:esm && yarn run build:cjs",
|
|
24
|
+
"build": "yarn barrels && yarn run build:esm && yarn run build:cjs",
|
|
24
25
|
"build:cjs": "tsc --build tsconfig.compile.json",
|
|
25
26
|
"build:esm": "tsc --build tsconfig.compile.esm.json",
|
|
27
|
+
"barrels": "yarn barrelsby --delete -d ./src -e \"\\.spec\\.ts\" -e \"__mock__\" -e \".benchmark.ts\"",
|
|
26
28
|
"test": "cross-env NODE_ENV=test yarn jest"
|
|
27
29
|
},
|
|
28
30
|
"dependencies": {
|
|
29
31
|
"change-case": "4.1.2",
|
|
30
|
-
"tslib": "2.
|
|
32
|
+
"tslib": "2.3.1"
|
|
31
33
|
},
|
|
32
34
|
"devDependencies": {
|
|
33
|
-
"@tsed/ajv": "7.0.0-beta.
|
|
34
|
-
"@tsed/core": "7.0.0-beta.
|
|
35
|
-
"@tsed/di": "7.0.0-beta.
|
|
36
|
-
"@tsed/json-mapper": "7.0.0-beta.
|
|
37
|
-
"@tsed/logger": "
|
|
38
|
-
"@tsed/schema": "7.0.0-beta.
|
|
39
|
-
"mongoose": "^
|
|
35
|
+
"@tsed/ajv": "7.0.0-beta.4",
|
|
36
|
+
"@tsed/core": "7.0.0-beta.4",
|
|
37
|
+
"@tsed/di": "7.0.0-beta.4",
|
|
38
|
+
"@tsed/json-mapper": "7.0.0-beta.4",
|
|
39
|
+
"@tsed/logger": ">=6.1.1",
|
|
40
|
+
"@tsed/schema": "7.0.0-beta.4",
|
|
41
|
+
"mongoose": "^6.1.7"
|
|
40
42
|
},
|
|
41
43
|
"peerDependencies": {
|
|
42
|
-
"@tsed/ajv": "^7.0.0-beta.
|
|
43
|
-
"@tsed/core": "^7.0.0-beta.
|
|
44
|
-
"@tsed/di": "^7.0.0-beta.
|
|
45
|
-
"@tsed/json-mapper": "^7.0.0-beta.
|
|
46
|
-
"@tsed/logger": ">=6.
|
|
47
|
-
"@tsed/schema": "^7.0.0-beta.
|
|
48
|
-
"mongoose": ">=
|
|
44
|
+
"@tsed/ajv": "^7.0.0-beta.4",
|
|
45
|
+
"@tsed/core": "^7.0.0-beta.4",
|
|
46
|
+
"@tsed/di": "^7.0.0-beta.4",
|
|
47
|
+
"@tsed/json-mapper": "^7.0.0-beta.4",
|
|
48
|
+
"@tsed/logger": ">=6.1.1",
|
|
49
|
+
"@tsed/schema": "^7.0.0-beta.4",
|
|
50
|
+
"mongoose": ">=6.0.0"
|
|
49
51
|
},
|
|
50
52
|
"repository": "https://github.com/tsedio/tsed",
|
|
51
53
|
"bugs": {
|
|
52
54
|
"url": "https://github.com/tsedio/tsed/issues"
|
|
53
55
|
},
|
|
54
|
-
"homepage": "https://github.com/tsedio/tsed/tree/
|
|
56
|
+
"homepage": "https://github.com/tsedio/tsed/tree/production/packages/orm/mongoose",
|
|
55
57
|
"author": "Romain Lenzotti",
|
|
56
58
|
"license": "MIT"
|
|
57
59
|
}
|
package/readme.md
CHANGED
|
@@ -59,17 +59,15 @@ import {Configuration} from "@tsed/common";
|
|
|
59
59
|
import "@tsed/mongoose"; // import mongoose ts.ed module
|
|
60
60
|
|
|
61
61
|
@Configuration({
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
62
|
+
mongoose: [
|
|
63
|
+
{
|
|
64
|
+
id: "default",
|
|
65
|
+
url: "mongodb://127.0.0.1:27017/db1",
|
|
66
|
+
connectionOptions: {}
|
|
67
|
+
}
|
|
68
|
+
]
|
|
69
69
|
})
|
|
70
|
-
export class Server {
|
|
71
|
-
|
|
72
|
-
}
|
|
70
|
+
export class Server {}
|
|
73
71
|
```
|
|
74
72
|
|
|
75
73
|
## Multi databases
|
|
@@ -82,27 +80,25 @@ import {Configuration} from "@tsed/common";
|
|
|
82
80
|
import "@tsed/mongoose"; // import mongoose ts.ed module
|
|
83
81
|
|
|
84
82
|
@Configuration({
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
83
|
+
mongoose: [
|
|
84
|
+
{
|
|
85
|
+
id: "default",
|
|
86
|
+
url: "mongodb://127.0.0.1:27017/db1",
|
|
87
|
+
connectionOptions: {}
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
id: "default",
|
|
91
|
+
url: "mongodb://127.0.0.1:27017/db2",
|
|
92
|
+
connectionOptions: {}
|
|
93
|
+
}
|
|
94
|
+
]
|
|
97
95
|
})
|
|
98
|
-
export class Server {
|
|
99
|
-
|
|
100
|
-
}
|
|
96
|
+
export class Server {}
|
|
101
97
|
```
|
|
102
98
|
|
|
103
99
|
## MongooseService
|
|
104
100
|
|
|
105
|
-
MongooseService let you to retrieve an instance of Mongoose.Connection.
|
|
101
|
+
MongooseService let you to retrieve an instance of Mongoose.Connection.
|
|
106
102
|
|
|
107
103
|
```typescript
|
|
108
104
|
import {Service} from "@tsed/common";
|
|
@@ -110,12 +106,12 @@ import {MongooseService} from "@tsed/mongoose";
|
|
|
110
106
|
|
|
111
107
|
@Service()
|
|
112
108
|
export class MyService {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
109
|
+
constructor(mongooseService: MongooseService) {
|
|
110
|
+
mongooseService.get(); // return the default instance of Mongoose.
|
|
111
|
+
// If you have one or more database configured with Ts.ED
|
|
112
|
+
mongooseService.get("default");
|
|
113
|
+
mongooseService.get("db2");
|
|
114
|
+
}
|
|
119
115
|
}
|
|
120
116
|
```
|
|
121
117
|
|
|
@@ -124,55 +120,50 @@ export class MyService {
|
|
|
124
120
|
By default, Ts.ED mongoose will reuse the metadata stored by the decorators dedicated
|
|
125
121
|
to describe a JsonSchema. This decorators come from the `@tsed/common` package.
|
|
126
122
|
|
|
127
|
-
|
|
128
123
|
Here a model example:
|
|
129
124
|
|
|
130
125
|
```typescript
|
|
131
|
-
import {
|
|
132
|
-
|
|
133
|
-
Enum, Pattern, Required,
|
|
134
|
-
CollectionOf
|
|
135
|
-
} from "@tsed/common";
|
|
136
|
-
import {Model, Unique, Indexed, Ref, ObjectID} from "@tsed/mongoose"
|
|
126
|
+
import {Minimum, Maximum, MaxLength, MinLength, Enum, Pattern, Required, CollectionOf} from "@tsed/common";
|
|
127
|
+
import {Model, Unique, Indexed, Ref, ObjectID} from "@tsed/mongoose";
|
|
137
128
|
|
|
138
129
|
enum Categories {
|
|
139
130
|
CAT1 = "cat1",
|
|
140
131
|
CAT2 = "cat2"
|
|
141
132
|
}
|
|
142
133
|
|
|
143
|
-
@Model({dbName:
|
|
134
|
+
@Model({dbName: "default"}) // dbName is optional. By default dbName is equal to default
|
|
144
135
|
export class MyModel {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
136
|
+
@ObjectID()
|
|
137
|
+
_id: string;
|
|
138
|
+
|
|
139
|
+
@Unique()
|
|
140
|
+
@Required()
|
|
141
|
+
unique: string;
|
|
142
|
+
|
|
143
|
+
@Indexed()
|
|
144
|
+
@MinLength(3)
|
|
145
|
+
@MaxLength(50)
|
|
146
|
+
indexed: string;
|
|
147
|
+
|
|
148
|
+
@Minimum(0)
|
|
149
|
+
@Maximum(100)
|
|
150
|
+
rate: Number;
|
|
151
|
+
|
|
152
|
+
@Enum(Categories)
|
|
153
|
+
// or @Enum("type1", "type2")
|
|
154
|
+
category: Categories;
|
|
155
|
+
|
|
156
|
+
@Pattern(/[a-z]/) // equivalent of match field in mongoose
|
|
157
|
+
pattern: String;
|
|
158
|
+
|
|
159
|
+
@CollectionOf(String)
|
|
160
|
+
arrayOf: string[];
|
|
161
|
+
|
|
162
|
+
@Ref(OtherModel)
|
|
163
|
+
ref: Ref<OtherModel>;
|
|
164
|
+
|
|
165
|
+
@Ref(OtherModel)
|
|
166
|
+
refs: Ref<OtherModel>[];
|
|
176
167
|
}
|
|
177
168
|
```
|
|
178
169
|
|
|
@@ -185,31 +176,30 @@ import {MyModel} from "./models/MyModel";
|
|
|
185
176
|
|
|
186
177
|
@Service()
|
|
187
178
|
export class MyService {
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
}
|
|
179
|
+
constructor(@Inject(MyModel) private model: MongooseModel<MyModel>): MyModel {
|
|
180
|
+
console.log(model); // Mongoose.model class
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async save(obj: MyModel): MongooseModel<MyModel> {
|
|
184
|
+
const doc = new this.model(obj);
|
|
185
|
+
await doc.save();
|
|
186
|
+
|
|
187
|
+
return doc;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async find(query: any) {
|
|
191
|
+
const list = await this.model.find(query).exec();
|
|
192
|
+
|
|
193
|
+
console.log(list);
|
|
194
|
+
|
|
195
|
+
return list;
|
|
196
|
+
}
|
|
207
197
|
}
|
|
208
198
|
```
|
|
209
199
|
|
|
210
200
|
## Register hook
|
|
211
201
|
|
|
212
|
-
Mongoose allows the developer to add pre and post [hooks / middlewares](http://mongoosejs.com/docs/middleware.html) to the schema.
|
|
202
|
+
Mongoose allows the developer to add pre and post [hooks / middlewares](http://mongoosejs.com/docs/middleware.html) to the schema.
|
|
213
203
|
With this it is possible to add document transformations and observations before or after validation, save and more.
|
|
214
204
|
|
|
215
205
|
Ts.ED provide class decorator to register middlewares on the pre and post hook.
|
|
@@ -217,83 +207,83 @@ Ts.ED provide class decorator to register middlewares on the pre and post hook.
|
|
|
217
207
|
### Pre hook
|
|
218
208
|
|
|
219
209
|
We can simply attach a `@PreHook` decorator to your model class and
|
|
220
|
-
|
|
221
|
-
|
|
210
|
+
define the hook function like you normally would in Mongoose.
|
|
211
|
+
|
|
222
212
|
```typescript
|
|
223
213
|
import {Required} from "@tsed/common";
|
|
224
214
|
import {PreHook, Model, ObjectID} from "@tsed/mongoose";
|
|
225
215
|
|
|
226
216
|
@Model()
|
|
227
217
|
@PreHook("save", (car: CarModel, next) => {
|
|
228
|
-
if (car.model ===
|
|
218
|
+
if (car.model === "Tesla") {
|
|
229
219
|
car.isFast = true;
|
|
230
220
|
}
|
|
231
221
|
next();
|
|
232
222
|
})
|
|
233
223
|
export class CarModel {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
}
|
|
249
|
-
next();
|
|
224
|
+
@ObjectID()
|
|
225
|
+
_id: string;
|
|
226
|
+
|
|
227
|
+
@Required()
|
|
228
|
+
model: string;
|
|
229
|
+
|
|
230
|
+
@Required()
|
|
231
|
+
isFast: boolean;
|
|
232
|
+
|
|
233
|
+
// or Prehook on static method
|
|
234
|
+
@PreHook("save")
|
|
235
|
+
static preSave(car: CarModel, next) {
|
|
236
|
+
if (car.model === "Tesla") {
|
|
237
|
+
car.isFast = true;
|
|
250
238
|
}
|
|
239
|
+
next();
|
|
240
|
+
}
|
|
251
241
|
}
|
|
252
242
|
```
|
|
253
243
|
|
|
254
|
-
This will execute the pre-save hook each time a `CarModel` document is saved.
|
|
244
|
+
This will execute the pre-save hook each time a `CarModel` document is saved.
|
|
255
245
|
|
|
256
246
|
### Post hook
|
|
257
247
|
|
|
258
248
|
We can simply attach a `@PostHook` decorator to your model class and
|
|
259
|
-
|
|
260
|
-
|
|
249
|
+
define the hook function like you normally would in Mongoose.
|
|
250
|
+
|
|
261
251
|
```typescript
|
|
262
252
|
import {ObjectID, Required} from "@tsed/common";
|
|
263
253
|
import {PostHook, Model} from "@tsed/mongoose";
|
|
264
254
|
|
|
265
255
|
@Model()
|
|
266
256
|
@PostHook("save", (car: CarModel) => {
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
257
|
+
if (car.topSpeedInKmH > 300) {
|
|
258
|
+
console.log(car.model, "is fast!");
|
|
259
|
+
}
|
|
270
260
|
})
|
|
271
261
|
export class CarModel {
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
}
|
|
262
|
+
@ObjectID()
|
|
263
|
+
_id: string;
|
|
264
|
+
|
|
265
|
+
@Required()
|
|
266
|
+
model: string;
|
|
267
|
+
|
|
268
|
+
@Required()
|
|
269
|
+
isFast: boolean;
|
|
270
|
+
|
|
271
|
+
// or Prehook on static method
|
|
272
|
+
@PostHook("save")
|
|
273
|
+
static postSave(car: CarModel) {
|
|
274
|
+
if (car.topSpeedInKmH > 300) {
|
|
275
|
+
console.log(car.model, "is fast!");
|
|
287
276
|
}
|
|
277
|
+
}
|
|
288
278
|
}
|
|
289
279
|
```
|
|
290
280
|
|
|
291
|
-
This will execute the post-save hook each time a `CarModel` document is saved.
|
|
281
|
+
This will execute the post-save hook each time a `CarModel` document is saved.
|
|
292
282
|
|
|
293
283
|
## Plugin
|
|
294
284
|
|
|
295
|
-
Using the `plugin` decorator enables the developer to attach various Mongoose plugins to the schema.
|
|
296
|
-
Just like the regular `schema.plugin()` call, the decorator accepts 1 or 2 parameters: the plugin itself, and an optional configuration object.
|
|
285
|
+
Using the `plugin` decorator enables the developer to attach various Mongoose plugins to the schema.
|
|
286
|
+
Just like the regular `schema.plugin()` call, the decorator accepts 1 or 2 parameters: the plugin itself, and an optional configuration object.
|
|
297
287
|
Multiple `plugin` decorator can be used for a single model class.
|
|
298
288
|
|
|
299
289
|
```typescript
|
|
@@ -8,16 +8,6 @@
|
|
|
8
8
|
"declaration": true,
|
|
9
9
|
"declarationDir": "./lib/types"
|
|
10
10
|
},
|
|
11
|
-
"
|
|
12
|
-
|
|
13
|
-
"test",
|
|
14
|
-
"lib",
|
|
15
|
-
"benchmark",
|
|
16
|
-
"coverage",
|
|
17
|
-
"spec",
|
|
18
|
-
"**/*.benchmark.ts",
|
|
19
|
-
"**/*.spec.ts",
|
|
20
|
-
"keys",
|
|
21
|
-
"jest.config.js"
|
|
22
|
-
]
|
|
11
|
+
"include": ["src"],
|
|
12
|
+
"exclude": ["node_modules", "test", "lib", "benchmark", "coverage", "spec", "**/*.benchmark.ts", "**/*.spec.ts", "keys", "jest.config.js"]
|
|
23
13
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/constants/index.ts"],"names":[],"mappings":";;;AAAA;;;GAGG;AACU,QAAA,eAAe,GAAG,sBAAsB,CAAC;AACtD;;;GAGG;AACU,QAAA,uBAAuB,GAAG,8BAA8B,CAAC;AACtE;;;GAGG;AACU,QAAA,cAAc,GAAG,qBAAqB,CAAC;AACpD;;;GAGG;AACU,QAAA,mBAAmB,GAAG,0BAA0B,CAAC","sourcesContent":["/**\n *\n * @type {string}\n */\nexport const MONGOOSE_SCHEMA = \"tsed:mongoose:schema\";\n/**\n *\n * @type {string}\n */\nexport const MONGOOSE_SCHEMA_OPTIONS = \"tsed:mongoose:schema:options\";\n/**\n *\n * @type {string}\n */\nexport const MONGOOSE_MODEL = \"tsed:mongoose:model\";\n/**\n *\n * @type {string}\n */\nexport const MONGOOSE_MODEL_NAME = \"tsed:mongoose:model:name\";\n"]}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
(0, tslib_1.__exportStar)(require("./auto"), exports);
|
|
5
|
-
(0, tslib_1.__exportStar)(require("./discriminatorKey"), exports);
|
|
6
|
-
(0, tslib_1.__exportStar)(require("./dynamicRef"), exports);
|
|
7
|
-
(0, tslib_1.__exportStar)(require("./excludeIndexes"), exports);
|
|
8
|
-
(0, tslib_1.__exportStar)(require("./expires"), exports);
|
|
9
|
-
(0, tslib_1.__exportStar)(require("./immutable"), exports);
|
|
10
|
-
(0, tslib_1.__exportStar)(require("./indexed"), exports);
|
|
11
|
-
(0, tslib_1.__exportStar)(require("./lowercase"), exports);
|
|
12
|
-
(0, tslib_1.__exportStar)(require("./model"), exports);
|
|
13
|
-
(0, tslib_1.__exportStar)(require("./mongooseIndex"), exports);
|
|
14
|
-
(0, tslib_1.__exportStar)(require("./mongoosePlugin"), exports);
|
|
15
|
-
(0, tslib_1.__exportStar)(require("./numberDecimal"), exports);
|
|
16
|
-
(0, tslib_1.__exportStar)(require("./objectID"), exports);
|
|
17
|
-
(0, tslib_1.__exportStar)(require("./postHook"), exports);
|
|
18
|
-
(0, tslib_1.__exportStar)(require("./preHook"), exports);
|
|
19
|
-
(0, tslib_1.__exportStar)(require("./ref"), exports);
|
|
20
|
-
(0, tslib_1.__exportStar)(require("./schema"), exports);
|
|
21
|
-
(0, tslib_1.__exportStar)(require("./schemaIgnore"), exports);
|
|
22
|
-
(0, tslib_1.__exportStar)(require("./select"), exports);
|
|
23
|
-
(0, tslib_1.__exportStar)(require("./sparse"), exports);
|
|
24
|
-
(0, tslib_1.__exportStar)(require("./text"), exports);
|
|
25
|
-
(0, tslib_1.__exportStar)(require("./trim"), exports);
|
|
26
|
-
(0, tslib_1.__exportStar)(require("./unique"), exports);
|
|
27
|
-
(0, tslib_1.__exportStar)(require("./uppercase"), exports);
|
|
28
|
-
(0, tslib_1.__exportStar)(require("./versionKey"), exports);
|
|
29
|
-
(0, tslib_1.__exportStar)(require("./virtualRef"), exports);
|
|
30
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/decorators/index.ts"],"names":[],"mappings":";;;AAAA,sDAAuB;AACvB,kEAAmC;AACnC,4DAA6B;AAC7B,gEAAiC;AACjC,yDAA0B;AAC1B,2DAA4B;AAC5B,yDAA0B;AAC1B,2DAA4B;AAC5B,uDAAwB;AACxB,+DAAgC;AAChC,gEAAiC;AACjC,+DAAgC;AAChC,0DAA2B;AAC3B,0DAA2B;AAC3B,yDAA0B;AAC1B,qDAAsB;AACtB,wDAAyB;AACzB,8DAA+B;AAC/B,wDAAyB;AACzB,wDAAyB;AACzB,sDAAuB;AACvB,sDAAuB;AACvB,wDAAyB;AACzB,2DAA4B;AAC5B,4DAA6B;AAC7B,4DAA6B","sourcesContent":["export * from \"./auto\";\nexport * from \"./discriminatorKey\";\nexport * from \"./dynamicRef\";\nexport * from \"./excludeIndexes\";\nexport * from \"./expires\";\nexport * from \"./immutable\";\nexport * from \"./indexed\";\nexport * from \"./lowercase\";\nexport * from \"./model\";\nexport * from \"./mongooseIndex\";\nexport * from \"./mongoosePlugin\";\nexport * from \"./numberDecimal\";\nexport * from \"./objectID\";\nexport * from \"./postHook\";\nexport * from \"./preHook\";\nexport * from \"./ref\";\nexport * from \"./schema\";\nexport * from \"./schemaIgnore\";\nexport * from \"./select\";\nexport * from \"./sparse\";\nexport * from \"./text\";\nexport * from \"./trim\";\nexport * from \"./unique\";\nexport * from \"./uppercase\";\nexport * from \"./versionKey\";\nexport * from \"./virtualRef\";\n"]}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
(0, tslib_1.__exportStar)(require("./MongooseConnectionOptions"), exports);
|
|
5
|
-
(0, tslib_1.__exportStar)(require("./MongooseDocument"), exports);
|
|
6
|
-
(0, tslib_1.__exportStar)(require("./MongooseModel"), exports);
|
|
7
|
-
(0, tslib_1.__exportStar)(require("./MongooseModelOptions"), exports);
|
|
8
|
-
(0, tslib_1.__exportStar)(require("./MongooseSchemaOptions"), exports);
|
|
9
|
-
(0, tslib_1.__exportStar)(require("./MongooseVirtualRefOptions"), exports);
|
|
10
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/interfaces/index.ts"],"names":[],"mappings":";;;AAUA,2EAA4C;AAC5C,kEAAmC;AACnC,+DAAgC;AAChC,sEAAuC;AACvC,uEAAwC;AACxC,2EAA4C","sourcesContent":["import {MongooseConnectionOptions} from \"./MongooseConnectionOptions\";\n\ndeclare global {\n namespace TsED {\n interface Configuration {\n mongoose: Omit<MongooseConnectionOptions, \"id\"> | MongooseConnectionOptions[];\n }\n }\n}\n\nexport * from \"./MongooseConnectionOptions\";\nexport * from \"./MongooseDocument\";\nexport * from \"./MongooseModel\";\nexport * from \"./MongooseModelOptions\";\nexport * from \"./MongooseSchemaOptions\";\nexport * from \"./MongooseVirtualRefOptions\";\n"]}
|
package/lib/cjs/utils/index.js
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
(0, tslib_1.__exportStar)(require("./createSchema"), exports);
|
|
5
|
-
(0, tslib_1.__exportStar)(require("./createModel"), exports);
|
|
6
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":";;;AAAA,8DAA+B;AAC/B,6DAA8B","sourcesContent":["export * from \"./createSchema\";\nexport * from \"./createModel\";\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/constants/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,sBAAsB,CAAC;AACtD;;;GAGG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,8BAA8B,CAAC;AACtE;;;GAGG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,qBAAqB,CAAC;AACpD;;;GAGG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,0BAA0B,CAAC","sourcesContent":["/**\n *\n * @type {string}\n */\nexport const MONGOOSE_SCHEMA = \"tsed:mongoose:schema\";\n/**\n *\n * @type {string}\n */\nexport const MONGOOSE_SCHEMA_OPTIONS = \"tsed:mongoose:schema:options\";\n/**\n *\n * @type {string}\n */\nexport const MONGOOSE_MODEL = \"tsed:mongoose:model\";\n/**\n *\n * @type {string}\n */\nexport const MONGOOSE_MODEL_NAME = \"tsed:mongoose:model:name\";\n"]}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
export * from "./auto";
|
|
2
|
-
export * from "./discriminatorKey";
|
|
3
|
-
export * from "./dynamicRef";
|
|
4
|
-
export * from "./excludeIndexes";
|
|
5
|
-
export * from "./expires";
|
|
6
|
-
export * from "./immutable";
|
|
7
|
-
export * from "./indexed";
|
|
8
|
-
export * from "./lowercase";
|
|
9
|
-
export * from "./model";
|
|
10
|
-
export * from "./mongooseIndex";
|
|
11
|
-
export * from "./mongoosePlugin";
|
|
12
|
-
export * from "./numberDecimal";
|
|
13
|
-
export * from "./objectID";
|
|
14
|
-
export * from "./postHook";
|
|
15
|
-
export * from "./preHook";
|
|
16
|
-
export * from "./ref";
|
|
17
|
-
export * from "./schema";
|
|
18
|
-
export * from "./schemaIgnore";
|
|
19
|
-
export * from "./select";
|
|
20
|
-
export * from "./sparse";
|
|
21
|
-
export * from "./text";
|
|
22
|
-
export * from "./trim";
|
|
23
|
-
export * from "./unique";
|
|
24
|
-
export * from "./uppercase";
|
|
25
|
-
export * from "./versionKey";
|
|
26
|
-
export * from "./virtualRef";
|
|
27
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/decorators/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,oBAAoB,CAAC;AACnC,cAAc,cAAc,CAAC;AAC7B,cAAc,kBAAkB,CAAC;AACjC,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,OAAO,CAAC;AACtB,cAAc,UAAU,CAAC;AACzB,cAAc,gBAAgB,CAAC;AAC/B,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,QAAQ,CAAC;AACvB,cAAc,QAAQ,CAAC;AACvB,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC","sourcesContent":["export * from \"./auto\";\nexport * from \"./discriminatorKey\";\nexport * from \"./dynamicRef\";\nexport * from \"./excludeIndexes\";\nexport * from \"./expires\";\nexport * from \"./immutable\";\nexport * from \"./indexed\";\nexport * from \"./lowercase\";\nexport * from \"./model\";\nexport * from \"./mongooseIndex\";\nexport * from \"./mongoosePlugin\";\nexport * from \"./numberDecimal\";\nexport * from \"./objectID\";\nexport * from \"./postHook\";\nexport * from \"./preHook\";\nexport * from \"./ref\";\nexport * from \"./schema\";\nexport * from \"./schemaIgnore\";\nexport * from \"./select\";\nexport * from \"./sparse\";\nexport * from \"./text\";\nexport * from \"./trim\";\nexport * from \"./unique\";\nexport * from \"./uppercase\";\nexport * from \"./versionKey\";\nexport * from \"./virtualRef\";\n"]}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export * from "./MongooseConnectionOptions";
|
|
2
|
-
export * from "./MongooseDocument";
|
|
3
|
-
export * from "./MongooseModel";
|
|
4
|
-
export * from "./MongooseModelOptions";
|
|
5
|
-
export * from "./MongooseSchemaOptions";
|
|
6
|
-
export * from "./MongooseVirtualRefOptions";
|
|
7
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/interfaces/index.ts"],"names":[],"mappings":"AAUA,cAAc,6BAA6B,CAAC;AAC5C,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC;AACxC,cAAc,6BAA6B,CAAC","sourcesContent":["import {MongooseConnectionOptions} from \"./MongooseConnectionOptions\";\n\ndeclare global {\n namespace TsED {\n interface Configuration {\n mongoose: Omit<MongooseConnectionOptions, \"id\"> | MongooseConnectionOptions[];\n }\n }\n}\n\nexport * from \"./MongooseConnectionOptions\";\nexport * from \"./MongooseDocument\";\nexport * from \"./MongooseModel\";\nexport * from \"./MongooseModelOptions\";\nexport * from \"./MongooseSchemaOptions\";\nexport * from \"./MongooseVirtualRefOptions\";\n"]}
|
package/lib/esm/utils/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC","sourcesContent":["export * from \"./createSchema\";\nexport * from \"./createModel\";\n"]}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
export * from "./auto";
|
|
2
|
-
export * from "./discriminatorKey";
|
|
3
|
-
export * from "./dynamicRef";
|
|
4
|
-
export * from "./excludeIndexes";
|
|
5
|
-
export * from "./expires";
|
|
6
|
-
export * from "./immutable";
|
|
7
|
-
export * from "./indexed";
|
|
8
|
-
export * from "./lowercase";
|
|
9
|
-
export * from "./model";
|
|
10
|
-
export * from "./mongooseIndex";
|
|
11
|
-
export * from "./mongoosePlugin";
|
|
12
|
-
export * from "./numberDecimal";
|
|
13
|
-
export * from "./objectID";
|
|
14
|
-
export * from "./postHook";
|
|
15
|
-
export * from "./preHook";
|
|
16
|
-
export * from "./ref";
|
|
17
|
-
export * from "./schema";
|
|
18
|
-
export * from "./schemaIgnore";
|
|
19
|
-
export * from "./select";
|
|
20
|
-
export * from "./sparse";
|
|
21
|
-
export * from "./text";
|
|
22
|
-
export * from "./trim";
|
|
23
|
-
export * from "./unique";
|
|
24
|
-
export * from "./uppercase";
|
|
25
|
-
export * from "./versionKey";
|
|
26
|
-
export * from "./virtualRef";
|