-
-
Notifications
You must be signed in to change notification settings - Fork 495
/
Author.ts
119 lines (89 loc) · 2.3 KB
/
Author.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 {
AfterCreate, AfterDelete, AfterUpdate, BeforeCreate, BeforeDelete, BeforeUpdate,
Cascade, Collection, Entity, EntityAssigner, ManyToMany, ManyToOne, OneToMany, Property, wrap,
} from '../../lib';
import { Book } from './Book';
import { AuthorRepository } from '../repositories/AuthorRepository';
import { BaseEntity } from './BaseEntity';
@Entity({ customRepository: () => AuthorRepository })
export class Author extends BaseEntity {
static beforeDestroyCalled = 0;
static afterDestroyCalled = 0;
@Property()
name: string;
@Property()
email: string;
@Property()
age?: number;
@Property()
termsAccepted = false;
@Property({ fieldName: 'identitiesArray' })
identities?: string[];
@Property()
born?: Date;
@OneToMany(() => Book, book => book.author, { referenceColumnName: '_id', cascade: [Cascade.PERSIST], orphanRemoval: true })
books = new Collection<Book>(this);
@ManyToMany()
friends: Collection<Author> = new Collection<Author>(this);
@ManyToOne()
favouriteBook!: Book;
@ManyToOne()
favouriteAuthor!: Author;
@Property({ persist: false })
version!: number;
@Property({ persist: false })
versionAsString!: string;
constructor(name: string, email: string) {
super();
this.name = name;
this.email = email;
this.foo = 'bar';
}
@Property({ name: 'code' })
getCode() {
return `${this.email} - ${this.name}`;
}
@Property({ persist: false })
get code2() {
return `${this.email} - ${this.name}`;
}
@BeforeCreate()
beforeCreate() {
this.version = 1;
}
@BeforeCreate()
beforeCreate2() {
// do sth else
}
@AfterCreate()
afterCreate() {
this.versionAsString = 'v' + this.version;
}
@BeforeUpdate()
beforeUpdate() {
this.version += 1;
}
@AfterUpdate()
afterUpdate() {
this.versionAsString = 'v' + this.version;
}
@BeforeDelete()
beforeDelete() {
Author.beforeDestroyCalled += 1;
}
@AfterDelete()
afterDelete() {
Author.afterDestroyCalled += 1;
}
assign(data: any): this {
return EntityAssigner.assign(this, data);
}
toJSON(strict = true, strip = ['id', 'email'], ...args: any[]): { [p: string]: any } {
const o = wrap(this).toObject(...args);
o.fooBar = 123;
if (strict) {
strip.forEach(k => delete o[k]);
}
return o;
}
}