Skip to content

Instantly share code, notes, and snippets.

@albert-dias
Created April 16, 2020 02:41
Show Gist options
  • Save albert-dias/cb6275d92d330ef7a30ac31978c8d6f5 to your computer and use it in GitHub Desktop.
Save albert-dias/cb6275d92d330ef7a30ac31978c8d6f5 to your computer and use it in GitHub Desktop.
import { MigrationInterface, QueryRunner, Table } from 'typeorm';
export default class CreateAppointments1586978594841
implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable(
new Table({
name: 'appointments',
columns: [
{
name: 'id',
type: 'uuid',
isPrimary: true,
generationStrategy: 'uuid',
default: 'uuid_generate_v4()',
},
{
name: 'provider',
type: 'varchar',
isNullable: false,
},
{
name: 'date',
type: 'timestamp with time zone',
isNullable: false,
},
{
name: 'created_at',
type: 'timestamp',
default: 'now()',
},
{
name: 'updated_at',
type: 'timestamp',
default: 'now()',
},
],
}),
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropTable('appointments');
}
}
import {
MigrationInterface,
QueryRunner,
TableColumn,
TableForeignKey,
} from 'typeorm';
export default class AlterProviderFieldToProviderId1586995140083
implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropColumn('appointments', 'provider');
await queryRunner.addColumn(
'appointments',
new TableColumn({
name: 'provider_id',
type: 'uuid',
isNullable: true,
}),
);
await queryRunner.createForeignKey(
'appointments',
new TableForeignKey({
name: 'AppointmentProvider',
columnNames: ['provider_id'],
referencedColumnNames: ['id'],
referencedTableName: 'users',
onDelete: 'SET NULL',
onUpdate: 'CASCADE',
}),
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropForeignKey('appointments', 'AppointmentProvider');
await queryRunner.dropColumn('appointments', 'provider_id');
await queryRunner.addColumn(
'appointments',
new TableColumn({
name: 'provider',
type: 'varchar',
}),
);
}
}
import {
Entity,
Column,
PrimaryGeneratedColumn,
CreateDateColumn,
UpdateDateColumn,
ManyToOne,
JoinColumn,
} from 'typeorm';
import User from './Users';
@Entity('appointments')
class Appointment {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column()
@JoinColumn({ name: 'provider_id' })
provider_id: string;
@ManyToOne(() => User)
provider: User;
@Column('time with time zone')
date: Date;
@CreateDateColumn()
created_at: Date;
@UpdateDateColumn()
updated_at: Date;
}
export default Appointment;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment