-
Notifications
You must be signed in to change notification settings - Fork 0
/
usage.ts
51 lines (45 loc) · 1.33 KB
/
usage.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
import { Schema, Document, connect } from 'mongoose';
import {
Model, getModelForClass, ModelType, DocType, InstanceType, Ref,
setSchema, prop, arrayProp, setMethod, setStatic,
setPre, setPost, setPlugin
} from '../lib';
import { lastModifiedPlugin, LastModifiedType } from './model/hooks';
export type UserInstanceType = InstanceType<User>;
export type UserModelType = ModelType<User, typeof User>;
// export type UserDocType = DocType<User, { method1 }>;//omit method1
@setSchema({
schemaOptions: {
timestamps: true,
toObject: {
virtuals: true
}
}
})
@setPre<UserInstanceType>('save', function (this, next) {
this.name += '_pre';
next();
})
@setPost<UserInstanceType>('save', function (this, next) {
console.log('save, _id: ', this._id);
})
@setPlugin(lastModifiedPlugin)
export class User extends Model<User> {
@prop()
name?: string;
@prop()
get virtualName() {
return 'virtual-' + this.name;
}
set virtualName(v) {
}
@setMethod
method1() {
console.log('this is method1,' + (this.save ? '' : 'no ') + 'save');
}
@setStatic
static async static1() {
console.log('this is static1,' + (this.findOne ? '' : 'no ') + 'findOne');
}
}
export const UserModel = getModelForClass<User, typeof User, LastModifiedType>(User);