Created
November 27, 2020 14:23
-
-
Save alanfoandrade/f1452ead3d0d71c21d54a72deff50291 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { MigrationInterface, QueryRunner, Table } from 'typeorm'; | |
export default class CreateUsersXCompanies1606436597898 | |
implements MigrationInterface { | |
public async up(queryRunner: QueryRunner): Promise<void> { | |
await queryRunner.createTable( | |
new Table({ | |
name: 'users_x_companies', | |
columns: [ | |
{ | |
name: 'id', | |
type: 'uuid', | |
isPrimary: true, | |
isUnique: true, | |
generationStrategy: 'uuid', | |
default: 'uuid_generate_v4()', | |
}, | |
{ | |
name: 'user_id', | |
type: 'uuid', | |
}, | |
{ | |
name: 'company_id', | |
type: 'uuid', | |
}, | |
{ | |
name: 'created_at', | |
type: 'timestamp with time zone', | |
default: 'now()', | |
}, | |
{ | |
name: 'updated_at', | |
type: 'timestamp with time zone', | |
default: 'now()', | |
}, | |
], | |
foreignKeys: [ | |
{ | |
name: 'company_user', | |
referencedTableName: 'users', | |
referencedColumnNames: ['id'], | |
columnNames: ['user_id'], | |
onDelete: 'CASCADE', | |
onUpdate: 'CASCADE', | |
}, | |
{ | |
name: 'user_company', | |
referencedTableName: 'companies', | |
referencedColumnNames: ['id'], | |
columnNames: ['company_id'], | |
onDelete: 'CASCADE', | |
onUpdate: 'CASCADE', | |
}, | |
], | |
}), | |
); | |
} | |
public async down(queryRunner: QueryRunner): Promise<void> { | |
await queryRunner.dropTable('users_x_companies'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
Entity, | |
Column, | |
PrimaryGeneratedColumn, | |
CreateDateColumn, | |
UpdateDateColumn, | |
ManyToMany, | |
} from 'typeorm'; | |
import User from './User'; | |
@Entity('companies') | |
class Company { | |
@PrimaryGeneratedColumn('uuid') | |
id: string; | |
@Column() | |
corporate_name: string; | |
@Column() | |
fancy_name: string; | |
@Column() | |
document: string; | |
@Column() | |
email: string; | |
@Column('boolean') | |
active: boolean; | |
@Column('timestamp with time zone') | |
deleted_at: Date; | |
@CreateDateColumn() | |
created_at: Date; | |
@UpdateDateColumn() | |
updated_at: Date; | |
@ManyToMany(() => User, (user) => user.companies) | |
users: User[]; | |
} | |
export default Company; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
Entity, | |
Column, | |
PrimaryGeneratedColumn, | |
CreateDateColumn, | |
UpdateDateColumn, | |
ManyToMany, | |
JoinTable, | |
} from 'typeorm'; | |
import Company from './Company'; | |
@Entity('users') | |
class User { | |
@PrimaryGeneratedColumn('uuid') | |
id: string; | |
@Column() | |
first_name: string; | |
@Column() | |
last_name: string; | |
@Column() | |
document: string; | |
@Column() | |
email: string; | |
@Column('boolean') | |
active: boolean; | |
@Column({ | |
type: 'enum', | |
enum: ['admin', 'manager', 'employee'], | |
default: 'employee', | |
}) | |
role: 'admin' | 'manager' | 'employee'; | |
@Column() | |
password: string; | |
@Column('timestamp with time zone') | |
deleted_at: Date; | |
@CreateDateColumn() | |
created_at: Date; | |
@UpdateDateColumn() | |
updated_at: Date; | |
@ManyToMany(() => Company, (company) => company.users, { cascade: true }) | |
@JoinTable({ name: 'users_x_companies' }) | |
companies: Company[]; | |
} | |
export default User; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment