-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
user.entity.ts
66 lines (50 loc) · 1.61 KB
/
user.entity.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
import { IsEmail } from 'class-validator';
import crypto from 'crypto';
import {
Collection,
Entity,
EntityRepositoryType,
ManyToMany,
OneToMany,
PrimaryKey,
Property,
wrap,
} from '@mikro-orm/core';
import { Article } from '../article/article.entity';
import { UserRepository } from './user.repository';
@Entity()
export class User {
[EntityRepositoryType]?: UserRepository;
@PrimaryKey()
id: number;
@Property()
username: string;
@Property({ hidden: true })
@IsEmail()
email: string;
@Property()
bio = '';
@Property()
image = '';
@Property({ hidden: true })
password: string;
@ManyToMany({ hidden: true })
favorites = new Collection<Article>(this);
@ManyToMany({ entity: () => User, inversedBy: u => u.followed, owner: true, pivotTable: 'user_to_follower', joinColumn: 'follower', inverseJoinColumn: 'following', hidden: true })
followers = new Collection<User>(this);
@ManyToMany(() => User, u => u.followers, { hidden: true })
followed = new Collection<User>(this);
@OneToMany(() => Article, article => article.author, { hidden: true })
articles = new Collection<Article>(this);
constructor(username: string, email: string, password: string) {
this.username = username;
this.email = email;
this.password = crypto.createHmac('sha256', password).digest('hex');
}
toJSON(user?: User) {
const o = wrap(this).toObject();
o.image = this.image || 'https://static.productionready.io/images/smiley-cyrus.jpg';
o.following = user && user.followers.isInitialized() ? user.followers.contains(this) : false; // TODO or followed?
return o;
}
}