Files
backend/libs/common/src/modules/space/entities/space.entity.ts
2024-08-03 23:50:49 +03:00

76 lines
1.8 KiB
TypeScript

import { Column, Entity, 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';
@Entity({ name: 'space-type' })
export class SpaceTypeEntity extends AbstractEntity<SpaceTypeDto> {
@Column({
type: 'uuid',
default: () => 'gen_random_uuid()',
nullable: false,
})
public uuid: string;
@Column({
nullable: false,
})
type: string;
@OneToMany(() => SpaceEntity, (space) => space.spaceType)
spaces: SpaceEntity[];
constructor(partial: Partial<SpaceTypeEntity>) {
super();
Object.assign(this, partial);
}
}
@Entity({ name: 'space' })
@Unique(['invitationCode'])
export class SpaceEntity extends AbstractEntity<SpaceDto> {
@Column({
type: 'uuid',
default: () => 'gen_random_uuid()',
nullable: false,
})
public uuid: string;
@Column({
nullable: true,
})
public spaceTuyaUuid: string;
@Column({
nullable: false,
})
public spaceName: string;
@Column({
nullable: true,
})
public invitationCode: string;
@ManyToOne(() => SpaceEntity, (space) => space.children, { nullable: true })
parent: SpaceEntity;
@OneToMany(() => SpaceEntity, (space) => space.parent)
children: SpaceEntity[];
@ManyToOne(() => SpaceTypeEntity, (spaceType) => spaceType.spaces, {
nullable: false,
})
spaceType: SpaceTypeEntity;
@OneToMany(() => UserSpaceEntity, (userSpace) => userSpace.space)
userSpaces: UserSpaceEntity[];
@OneToMany(
() => DeviceEntity,
(devicesSpaceEntity) => devicesSpaceEntity.spaceDevice,
)
devicesSpaceEntity: DeviceEntity[];
constructor(partial: Partial<SpaceEntity>) {
super();
Object.assign(this, partial);
}
}