forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.test.ts
91 lines (80 loc) · 2.82 KB
/
plugin.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
import { connection, HydratedDocument, Model, Query, Schema } from 'mongoose';
function pluginVirtuals(schema: Schema<Test, any, any, any, TestVirtuals>): void {
schema.virtual('fullName').get(function(this: TestDocument) {
return `${this.firstName} ${this.lastName}`;
});
schema.virtual('notDefined').get(function(this: TestDocument) {
return 'foobar';
});
}
function pluginQueryHelpers(schema: Schema<Test, any, any, TestQueryHelpers>): void {
schema.query.whereSomething = function() {
return this.where({ name: 'something' });
};
}
function pluginMethods(schema: Schema<Test, any, TestInstanceMethods>): void {
schema.methods.doSomething = function() {
return 'test';
};
}
function pluginStatics(schema: Schema<Test, TestModel, any, TestQueryHelpers, any, TestStaticMethods>): void {
schema.statics.findSomething = function() {
return this.findOne().orFail().exec();
};
schema.static({
findSomething: function() {
return this.findOne().orFail().exec();
}
});
schema.static('findSomething', function() {
return this.findOne().orFail().exec();
});
schema.static('findSomethingElse', function() {
return this.findOne();
});
}
function pluginGeneric(schema: Schema): void {
schema.static('test', function() {
return 0;
});
}
type Test = { firstName: string; lastName: string };
type TestVirtuals = {
fullName: string;
};
interface TestInstanceMethods {
doSomething(this: TestDocument): string;
}
interface TestStaticMethods {
findSomething(this: TestModel): Promise<TestDocument>;
}
type TestDocument = HydratedDocument<Test, TestInstanceMethods, TestVirtuals>;
type TestQuery = Query<any, TestDocument, TestQueryHelpers> & TestQueryHelpers;
interface TestQueryHelpers {
whereSomething(this: TestQuery): this
}
type TestModel = Model<Test, TestQueryHelpers, TestInstanceMethods, TestVirtuals> & TestStaticMethods;
const testSchema = new Schema<Test, TestModel, TestInstanceMethods, TestQueryHelpers, TestVirtuals, TestStaticMethods>({
firstName: { type: String, required: true },
lastName: { type: String, required: true }
});
testSchema.plugin(pluginVirtuals);
testSchema.plugin(pluginQueryHelpers);
testSchema.plugin(pluginMethods);
testSchema.plugin(pluginStatics);
testSchema.plugin(pluginGeneric);
const Foo = connection.model<Test, TestModel, TestQueryHelpers>('Test', testSchema);
async function registerPlugin(): Promise<void> {
const test = await Foo.findOne().orFail();
console.log(test.firstName);
console.log(test.fullName);
console.log(test.doSomething());
const test2 = await Foo.findSomething();
console.log(test2.firstName);
console.log(test2.fullName);
console.log(test2.doSomething());
const test3 = await Foo.findOne().whereSomething().orFail();
console.log(test3.firstName);
console.log(test3.fullName);
console.log(test3.doSomething());
}