From ad907a7661dc02a9691db6e997f9ea781e9faeab Mon Sep 17 00:00:00 2001 From: fadlydev Date: Sun, 19 Jan 2025 07:32:48 +0700 Subject: [PATCH] update goods relations --- src/entity/asset_category.ts | 12 ++++++++++ src/entity/goods.ts | 43 ++++++++++++++++++++---------------- src/entity/supplier.ts | 9 +++++++- 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/entity/asset_category.ts b/src/entity/asset_category.ts index eaca487..413e6a3 100644 --- a/src/entity/asset_category.ts +++ b/src/entity/asset_category.ts @@ -5,8 +5,11 @@ import { CreateDateColumn, UpdateDateColumn, DeleteDateColumn, + OneToMany, } from 'typeorm'; +import { Goods } from './goods'; + @Entity('asset_category') export class AssetCategory { @PrimaryGeneratedColumn('uuid') @@ -24,6 +27,15 @@ export class AssetCategory { }) description: string; + @OneToMany(() => Goods, (goods) => goods.category) + goods: Goods[]; + + @Column({ + nullable: false, + default: true, + }) + is_active: boolean; + @Column() @CreateDateColumn() created_at: Date; diff --git a/src/entity/goods.ts b/src/entity/goods.ts index d041420..d66cbdc 100644 --- a/src/entity/goods.ts +++ b/src/entity/goods.ts @@ -6,9 +6,12 @@ import { UpdateDateColumn, DeleteDateColumn, Index, + ManyToOne, + JoinColumn, } from 'typeorm'; -import { Status } from '../types/status'; +import { AssetCategory } from './asset_category'; +import { Supplier } from './supplier'; @Entity('goods') export class Goods { @@ -43,53 +46,55 @@ export class Goods { price: number; @Column({ - nullable: true, + nullable: false, type: 'int', }) quantity: number; @Index() @Column({ - nullable: true, - type: 'uuid', - }) - category_id: string; - - @Index() - @Column({ - nullable: true, + nullable: false, type: 'uuid', }) created_by: string; @Index() @Column({ - nullable: true, + nullable: false, type: 'uuid', }) updated_by: string; - @Column({ + @ManyToOne(() => AssetCategory, (category) => category.goods, { nullable: false, - type: 'uuid', + onDelete: 'SET NULL' }) - supplier_id: string; + + @Index() + @JoinColumn({ name: 'category_id'}) + category: AssetCategory; + + @ManyToOne(() => Supplier, (supplier) => supplier.goods, { + nullable: true, + onDelete: 'SET NULL' + }) + + @Index() + @JoinColumn({ name: 'supplier_id' }) + supplier: Supplier; @Column({ nullable: false, - length: 64, + default: true, }) - status: Status; + is_active: boolean; - @Column() @CreateDateColumn() created_at: Date; - @Column({ nullable: true }) @UpdateDateColumn() updated_at: Date; - @Column({ nullable: true }) @DeleteDateColumn() deleted_at: Date; } diff --git a/src/entity/supplier.ts b/src/entity/supplier.ts index a6aa27f..39f4c2a 100644 --- a/src/entity/supplier.ts +++ b/src/entity/supplier.ts @@ -1,5 +1,6 @@ import { Entity, + OneToMany, PrimaryGeneratedColumn, Column, CreateDateColumn, @@ -7,6 +8,8 @@ import { DeleteDateColumn, } from 'typeorm'; +import { Goods } from './goods'; + @Entity('suppliers') export class Supplier { @PrimaryGeneratedColumn('uuid') @@ -24,6 +27,9 @@ export class Supplier { }) contact_person: string; + @OneToMany(() => Goods, (goods) => goods.supplier) + goods: Goods[]; + @Column({ nullable: true, length: 128, @@ -45,6 +51,7 @@ export class Supplier { @Column({ nullable: false, length: 64, + unique: true, }) code_id: string; @@ -55,7 +62,7 @@ export class Supplier { type: string; @Column({ - type: 'boolean', + nullable: false, default: true, }) is_active: boolean;