From a553481c9aeae93685eb83e653129fbad21a9f93 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Sun, 10 Mar 2024 21:07:13 +0300 Subject: [PATCH] create home entity --- libs/common/src/database/database.module.ts | 3 +- libs/common/src/modules/home/dtos/home.dto.ts | 15 ++++++++++ libs/common/src/modules/home/dtos/index.ts | 1 + .../src/modules/home/entities/home.entity.ts | 29 +++++++++++++++++++ .../common/src/modules/home/entities/index.ts | 1 + .../modules/home/home.repository.module.ts | 11 +++++++ .../home/repositories/home.repository.ts | 10 +++++++ .../src/modules/home/repositories/index.ts | 1 + 8 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 libs/common/src/modules/home/dtos/home.dto.ts create mode 100644 libs/common/src/modules/home/dtos/index.ts create mode 100644 libs/common/src/modules/home/entities/home.entity.ts create mode 100644 libs/common/src/modules/home/entities/index.ts create mode 100644 libs/common/src/modules/home/home.repository.module.ts create mode 100644 libs/common/src/modules/home/repositories/home.repository.ts create mode 100644 libs/common/src/modules/home/repositories/index.ts diff --git a/libs/common/src/database/database.module.ts b/libs/common/src/database/database.module.ts index c3aafad..e263571 100644 --- a/libs/common/src/database/database.module.ts +++ b/libs/common/src/database/database.module.ts @@ -5,6 +5,7 @@ import { SnakeNamingStrategy } from './strategies'; import { UserEntity } from '../modules/user/entities/user.entity'; import { UserSessionEntity } from '../modules/session/entities/session.entity'; import { UserOtpEntity } from '../modules/user-otp/entities'; +import { HomeEntity } from '../modules/home/entities'; @Module({ imports: [ @@ -19,7 +20,7 @@ import { UserOtpEntity } from '../modules/user-otp/entities'; username: configService.get('DB_USER'), password: configService.get('DB_PASSWORD'), database: configService.get('DB_NAME'), - entities: [UserEntity, UserSessionEntity, UserOtpEntity], + entities: [UserEntity, UserSessionEntity, UserOtpEntity, HomeEntity], namingStrategy: new SnakeNamingStrategy(), synchronize: Boolean(JSON.parse(configService.get('DB_SYNC'))), logging: true, diff --git a/libs/common/src/modules/home/dtos/home.dto.ts b/libs/common/src/modules/home/dtos/home.dto.ts new file mode 100644 index 0000000..9db59f2 --- /dev/null +++ b/libs/common/src/modules/home/dtos/home.dto.ts @@ -0,0 +1,15 @@ +import { IsNotEmpty, IsString } from 'class-validator'; + +export class HomeDto { + @IsString() + @IsNotEmpty() + public uuid: string; + + @IsString() + @IsNotEmpty() + public userUuid: string; + + @IsString() + @IsNotEmpty() + public homeId: string; +} diff --git a/libs/common/src/modules/home/dtos/index.ts b/libs/common/src/modules/home/dtos/index.ts new file mode 100644 index 0000000..217847e --- /dev/null +++ b/libs/common/src/modules/home/dtos/index.ts @@ -0,0 +1 @@ +export * from './home.dto'; diff --git a/libs/common/src/modules/home/entities/home.entity.ts b/libs/common/src/modules/home/entities/home.entity.ts new file mode 100644 index 0000000..7760343 --- /dev/null +++ b/libs/common/src/modules/home/entities/home.entity.ts @@ -0,0 +1,29 @@ +import { Column, Entity } from 'typeorm'; +import { HomeDto } from '../dtos'; +import { AbstractEntity } from '../../abstract/entities/abstract.entity'; + +@Entity({ name: 'home' }) +export class HomeEntity extends AbstractEntity { + @Column({ + type: 'uuid', + default: () => 'gen_random_uuid()', // Use gen_random_uuid() for default value + nullable: false, + }) + public uuid: string; + + @Column({ + nullable: false, + }) + userUuid: string; + + @Column({ + nullable: false, + unique: true, + }) + public homeId: string; + + constructor(partial: Partial) { + super(); + Object.assign(this, partial); + } +} diff --git a/libs/common/src/modules/home/entities/index.ts b/libs/common/src/modules/home/entities/index.ts new file mode 100644 index 0000000..dbf8038 --- /dev/null +++ b/libs/common/src/modules/home/entities/index.ts @@ -0,0 +1 @@ +export * from './home.entity'; diff --git a/libs/common/src/modules/home/home.repository.module.ts b/libs/common/src/modules/home/home.repository.module.ts new file mode 100644 index 0000000..e2527b8 --- /dev/null +++ b/libs/common/src/modules/home/home.repository.module.ts @@ -0,0 +1,11 @@ +import { Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { HomeEntity } from './entities/home.entity'; + +@Module({ + providers: [], + exports: [], + controllers: [], + imports: [TypeOrmModule.forFeature([HomeEntity])], +}) +export class HomeRepositoryModule {} diff --git a/libs/common/src/modules/home/repositories/home.repository.ts b/libs/common/src/modules/home/repositories/home.repository.ts new file mode 100644 index 0000000..3f525e2 --- /dev/null +++ b/libs/common/src/modules/home/repositories/home.repository.ts @@ -0,0 +1,10 @@ +import { DataSource, Repository } from 'typeorm'; +import { Injectable } from '@nestjs/common'; +import { HomeEntity } from '../entities/home.entity'; + +@Injectable() +export class HomeRepository extends Repository { + constructor(private dataSource: DataSource) { + super(HomeEntity, dataSource.createEntityManager()); + } +} diff --git a/libs/common/src/modules/home/repositories/index.ts b/libs/common/src/modules/home/repositories/index.ts new file mode 100644 index 0000000..af66ad7 --- /dev/null +++ b/libs/common/src/modules/home/repositories/index.ts @@ -0,0 +1 @@ +export * from './home.repository';