#58 added model Member and related fields in related models

parent 6a213935
...@@ -2,6 +2,7 @@ import { DataSource } from "typeorm"; ...@@ -2,6 +2,7 @@ import { DataSource } from "typeorm";
import {User} from './models/User'; import {User} from './models/User';
import {Task} from './models/Task'; import {Task} from './models/Task';
import { Project } from "./models/Project"; import { Project } from "./models/Project";
import { Member } from "./models/Member";
export const myDataSource = new DataSource({ export const myDataSource = new DataSource({
type: "postgres", type: "postgres",
...@@ -10,7 +11,7 @@ export const myDataSource = new DataSource({ ...@@ -10,7 +11,7 @@ export const myDataSource = new DataSource({
username: "pluser", username: "pluser",
password: "pluser", password: "pluser",
database: "planner", database: "planner",
entities: [User,Task,Project], entities: [User,Task,Project,Member],
logging: true, logging: true,
synchronize: true, // in build switch to false synchronize: true, // in build switch to false
migrationsRun: false migrationsRun: false
......
import {
Column,
Entity,
PrimaryGeneratedColumn,
CreateDateColumn,
BaseEntity,
ManyToOne,
ManyToMany,
OneToMany,
JoinTable
} from 'typeorm';
import {User} from './User';
import {Project} from './Project';
import {Task} from './Task';
export enum MemberRole {USER="user" ,WATCHER= "watcher",ADMIN="admin"}
interface IMember{
id: string;
user: User;
project: Project;
createdAt: Date;
role: MemberRole;
}
@Entity({name:"Member"})
export class Member extends BaseEntity implements IMember{
@PrimaryGeneratedColumn("uuid")
id!: string;
@CreateDateColumn({ name: 'createdAt', type: Date, default: new Date() })
createdAt!: Date;
@ManyToOne(() => User, (user: { members: Member[]; }) => user.members,{cascade: true, onUpdate:'CASCADE'})
user!: User;
@ManyToOne(() => Project, (project: { members: Member[]; }) => project.members,{cascade: true, onUpdate:'CASCADE'})
project!: Project;
@Column({
type: "enum",
enum: MemberRole,
default: MemberRole.ADMIN
})
role!: MemberRole
}
\ No newline at end of file
...@@ -9,8 +9,9 @@ import { ...@@ -9,8 +9,9 @@ import {
OneToMany, OneToMany,
JoinTable JoinTable
} from 'typeorm'; } from 'typeorm';
import {User} from './User'; // import {User} from './User';
import {Task} from './Task'; import {Task} from './Task';
import {Member} from './Member';
...@@ -18,12 +19,13 @@ import { ...@@ -18,12 +19,13 @@ import {
id: string; id: string;
title: string; title: string;
color: string; color: string;
admin:User; // admin:User;
workers:User[]; // workers:User[];
tasks:Task[]|null; tasks:Task[]|null;
createdAt: Date; createdAt: Date;
dateDue: Date| null; dateDue: Date| null;
department:boolean; // department:boolean;
members: Member[];
} }
@Entity({ name: 'Project' }) @Entity({ name: 'Project' })
...@@ -44,16 +46,19 @@ import { ...@@ -44,16 +46,19 @@ import {
@Column({ name: 'dateDue', type: Date, default: null }) @Column({ name: 'dateDue', type: Date, default: null })
dateDue!: Date| null; dateDue!: Date| null;
@Column({ name: 'department', type: Boolean,nullable:true ,default: false}) // @Column({ name: 'department', type: Boolean,nullable:true ,default: false})
department!: boolean; // department!: boolean;
@ManyToOne(() => User, (user: { projects: Project[]; }) => user.projects,{eager : true}) // @ManyToOne(() => User, (user: { projects: Project[]; }) => user.projects,{eager : true})
admin!: User; // admin!: User;
@OneToMany(() => Task, (task: { project: Project; })=>task.project) @OneToMany(() => Task, (task: { project: Project; })=>task.project)
tasks!:Task[]|null; tasks!:Task[]|null;
@ManyToMany(() => User, (user: { projects: Project[]; }) => user.projects,{eager : true,cascade: true, onUpdate:'CASCADE',onDelete: 'CASCADE'}) @OneToMany(() => Member, (member: { project: Project; })=>member.project)
@JoinTable() members!:Member[];
workers!: User[];
// @ManyToMany(() => User, (user: { projects: Project[]; }) => user.projects,{eager : true,cascade: true, onUpdate:'CASCADE',onDelete: 'CASCADE'})
// @JoinTable()
// workers!: User[];
} }
\ No newline at end of file
...@@ -16,6 +16,7 @@ import bcrypt from 'bcrypt'; ...@@ -16,6 +16,7 @@ import bcrypt from 'bcrypt';
import {nanoid} from 'nanoid'; import {nanoid} from 'nanoid';
import {Task} from './Task'; import {Task} from './Task';
import {Project} from './Project'; import {Project} from './Project';
import {Member} from './Member';
const SALT_WORK_FACTOR= 10; const SALT_WORK_FACTOR= 10;
...@@ -33,8 +34,9 @@ interface IUser { ...@@ -33,8 +34,9 @@ interface IUser {
token: string; token: string;
createdAt: Date; createdAt: Date;
createdTasks:Task[]; createdTasks:Task[];
workerInProjects:Project[]; // workerInProjects:Project[];
adminInProjects:Project[]; // adminInProjects:Project[];
membership: Member[];
} }
...@@ -84,12 +86,12 @@ export class User extends BaseEntity implements IUser { ...@@ -84,12 +86,12 @@ export class User extends BaseEntity implements IUser {
tasks!: Task[]; tasks!: Task[];
@OneToMany(() => Project, (project: { user: User }) => project.user) @OneToMany(() => Member, (member: { user: User }) => member.user)
adminInProjects!: Project[]; membership!: Member[];
@ManyToMany(() => Project,(project: { user: User }) => project.user) // @ManyToMany(() => Project,(project: { user: User }) => project.user)
@JoinTable() // @JoinTable()
workerInProjects!: Project[]; // workerInProjects!: Project[];
@BeforeInsert() @BeforeInsert()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment