lynx-framework
Version:
lynx is a NodeJS framework for Web Development, based on decorators and the async/await support.
74 lines (58 loc) • 1.44 kB
text/typescript
import {
Entity,
PrimaryGeneratedColumn,
Column,
ManyToMany,
OneToMany,
JoinTable,
} from 'typeorm';
import BaseEntity from './base.entity';
import Role from './role.entity';
import Media from './media.entity';
let SYNCHRONIZE = true;
export function setSkipSync(skip: boolean) {
SYNCHRONIZE = !skip;
}
('users', { synchronize: SYNCHRONIZE })
export default class User extends BaseEntity {
()
id: number;
({ unique: true })
email: string;
() password: string;
()
firstName: string;
()
lastName: string;
()
nickName: string;
((_) => Role, (role) => role.users, { eager: true })
()
roles: Role[];
((_) => Media, (media) => media.owner)
media: Media[];
hiddenFields = ['password'];
private hasRole(name: string): boolean {
for (let role of this.roles) {
if (role.name == name) {
return true;
}
}
return false;
}
get isAdmin(): boolean {
return this.hasRole(Role.ADMIN);
}
get isStuff(): boolean {
return this.hasRole(Role.STAFF);
}
get level(): number {
let max = 0;
this.roles.forEach((r) => {
if (r.level > max) {
max = r.level;
}
});
return max;
}
}