-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
middleware.test.ts
119 lines (93 loc) · 2.87 KB
/
middleware.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { Schema, model, Model, Document, SaveOptions, Query, Aggregate, HydratedDocument, PreSaveMiddlewareFunction, ApplySchemaOptions } from 'mongoose';
import { expectError, expectType, expectNotType } from 'tsd';
interface ITest extends Document {
name?: string;
}
const preMiddlewareFn: PreSaveMiddlewareFunction<Document> = function(next, opts) {
this.$markValid('name');
if (opts.session) {
next();
} else {
next(new Error('Operation must be in Session.'));
}
};
const schema: Schema<ITest> = new Schema<ITest>({ name: { type: 'String' } });
schema.pre<Query<any, any>>('find', async function() {
console.log('Find', this.getFilter());
});
schema.pre<Query<any, any>>('find', async function() {
expectError(this.notAFunction());
});
schema.pre<Aggregate<any>>('aggregate', async function() {
console.log('Pipeline', this.pipeline());
});
schema.post<Aggregate<any>>('aggregate', async function(res: Array<any>) {
console.log('Pipeline', this.pipeline(), res[0]);
});
schema.post<Aggregate<ITest>>('aggregate', function(res, next) {
expectType<ITest[]>(res);
next();
});
schema.post<Query<ITest, ITest>>('save', function(res, next) {
expectType<Query<ITest, ITest>>(res);
next();
});
schema.pre(['save', 'validate'], { query: false, document: true }, async function applyChanges() {
await Test.findOne({});
});
schema.pre('save', function(next, opts: SaveOptions) {
console.log(opts.session);
next();
});
schema.pre('save', function(next) {
console.log(this.name);
});
schema.post<ITest>('save', function(res, next) {
expectType<ITest>(res);
next();
});
schema.post<ITest>('save', function() {
console.log(this.name);
});
schema.post<ITest>('save', function(err: Error, res: ITest, next: Function) {
console.log(this.name, err.stack);
});
schema.pre<Model<ITest>>('insertMany', function() {
const name: string = this.name;
return Promise.resolve();
});
schema.pre<Model<ITest>>('insertMany', { document: false, query: false }, function() {
console.log(this.name);
});
schema.pre<Model<ITest>>('insertMany', function(next) {
console.log(this.name);
next();
});
schema.pre<Model<ITest>>('insertMany', function(next, doc: ITest) {
console.log(this.name, doc);
next();
});
schema.pre<Model<ITest>>('insertMany', function(next, docs: Array<ITest>) {
console.log(this.name, docs);
next();
});
schema.post<Query<ITest, ITest>>('findOneAndDelete', function(res, next) {
expectType<ITest>(res);
next();
});
const Test = model<ITest>('Test', schema);
function gh11257(): void {
schema.pre('save', { document: true }, function() {
expectType<HydratedDocument<ApplySchemaOptions<ITest>>>(this);
});
}
function gh11480(): void {
type IUserSchema = {
name: string;
};
const UserSchema = new Schema<IUserSchema>({ name: { type: String } });
UserSchema.pre('save', function(next) {
expectNotType<any>(this);
next();
});
}