import { Column, Entity, JoinColumn, ManyToOne, OneToMany, Unique, } from 'typeorm'; import { SpaceDto, SpaceTypeDto } from '../dtos'; import { AbstractEntity } from '../../abstract/entities/abstract.entity'; import { UserSpaceEntity } from '../../user/entities'; import { DeviceEntity } from '../../device/entities'; import { CommunityEntity } from '../../community/entities'; import { SubspaceEntity } from './subspace.entity'; import { SpaceProductEntity } from './space-product.entity'; @Entity({ name: 'space-type' }) export class SpaceTypeEntity extends AbstractEntity { @Column({ type: 'uuid', default: () => 'gen_random_uuid()', nullable: false, }) public uuid: string; @Column({ nullable: false, }) type: string; constructor(partial: Partial) { super(); Object.assign(this, partial); } } @Entity({ name: 'space' }) @Unique(['invitationCode']) export class SpaceEntity extends AbstractEntity { @Column({ type: 'uuid', default: () => 'gen_random_uuid()', nullable: false, }) public uuid: string; @Column({ nullable: true, }) public spaceTuyaUuid: string; @Column({ nullable: false, }) public spaceName: string; @ManyToOne(() => CommunityEntity, (community) => community.spaces, { nullable: false, onDelete: 'CASCADE', }) @JoinColumn({ name: 'community_id' }) community: CommunityEntity; @Column({ nullable: true, }) public invitationCode: string; @ManyToOne(() => SpaceEntity, (space) => space.children, { nullable: true }) parent: SpaceEntity; @OneToMany(() => SpaceEntity, (space) => space.parent) children: SpaceEntity[]; @OneToMany(() => UserSpaceEntity, (userSpace) => userSpace.space) userSpaces: UserSpaceEntity[]; @OneToMany(() => SubspaceEntity, (subspace) => subspace.space, { nullable: true, }) subspaces?: SubspaceEntity[]; @OneToMany( () => DeviceEntity, (devicesSpaceEntity) => devicesSpaceEntity.spaceDevice, ) devices: DeviceEntity[]; @OneToMany(() => SpaceProductEntity, (spaceProduct) => spaceProduct.space) spaceProducts: SpaceProductEntity[]; constructor(partial: Partial) { super(); Object.assign(this, partial); } }