express-typeorm-rest-boilerplate
Version:
Boilerplate code to get started with building RESTful API Services
51 lines (41 loc) • 967 B
text/typescript
import { Entity, ObjectIdColumn, ObjectID, Column, Index } from 'typeorm';
import { IsEmail, IsString } from 'class-validator';
export type Role = 'user' | 'staff' | 'admin';
export class User {
id?: ObjectID;
firstName?: string;
lastName?: string;
email?: string;
password?: string;
role?: Role = 'user';
public constructor(data?: User) {
if (data) {
this.firstName = data.firstName;
this.lastName = data.lastName;
this.email = data.email;
this.password = data.password;
this.role = data.role || this.role;
}
}
public hasAccessTo?(role: Role): boolean {
const roles = ['user', 'staff', 'admin'];
return roles.indexOf(this.role) >= roles.indexOf(role);
}
}