From 7424b387981a481227d8f4796bec52da5a0bbaf8 Mon Sep 17 00:00:00 2001 From: Supan Adit Pratama Date: Sun, 10 Nov 2024 11:20:32 +0700 Subject: [PATCH] fix: entities not found --- .../config-module-options.interface.ts | 2 +- libs/typeorm/src/typeorm.module.ts | 23 ++++++++----------- src/example/typeorm/entities/user.entity.ts | 2 +- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/libs/typeorm/src/interfaces/config-module-options.interface.ts b/libs/typeorm/src/interfaces/config-module-options.interface.ts index 0ecdb5d..240ab44 100644 --- a/libs/typeorm/src/interfaces/config-module-options.interface.ts +++ b/libs/typeorm/src/interfaces/config-module-options.interface.ts @@ -9,5 +9,5 @@ export interface TypeORMModuleOptions { processRead?; processUpdate?; synchronize?: boolean; - entities?: string[]; + entities?: any[]; } diff --git a/libs/typeorm/src/typeorm.module.ts b/libs/typeorm/src/typeorm.module.ts index 3d3f224..c5aa1de 100644 --- a/libs/typeorm/src/typeorm.module.ts +++ b/libs/typeorm/src/typeorm.module.ts @@ -1,8 +1,3 @@ -import { DynamicModule, Module } from '@nestjs/common'; -import { DataSource, ObjectLiteral } from 'typeorm'; -import { TYPEORM_DATASOURCE, TYPEORM_REPOSITORY } from './constants'; -import { TypeORMModuleOptions } from './interfaces'; -import { TypeORMService } from './typeorm.service'; import { CREATE_PROCESS, DELETE_PROCESS, @@ -11,6 +6,10 @@ import { READ_PROCESS, UPDATE_PROCESS, } from '@aditama-labs/nest-autocrud/skeleton'; +import { DynamicModule, Module } from '@nestjs/common'; +import { DataSource, ObjectLiteral } from 'typeorm'; +import { TYPEORM_DATASOURCE, TYPEORM_REPOSITORY } from './constants'; +import { TypeORMModuleOptions } from './interfaces'; import { TypeORMCreateProcess, TypeORMDeleteProcess, @@ -19,7 +18,7 @@ import { TypeORMReadProcess, TypeORMUpdateProcess, } from './processes'; -import { TypeOrmModule } from '@nestjs/typeorm'; +import { TypeORMService } from './typeorm.service'; const getDatabaseCredential = ( synchronize?: boolean, @@ -27,11 +26,6 @@ const getDatabaseCredential = ( ): any => { const url = new URL(process.env.DATABASE_URL); - let entityList = [__dirname + '/../**/*.entity{.ts,.js}']; - if (entities) { - entityList = entities; - } - const defaultProtocol = url.protocol.substring(0, url.protocol.length - 1); let protocol = defaultProtocol; switch (defaultProtocol) { @@ -56,7 +50,7 @@ const getDatabaseCredential = ( username: username, password: password, database: databaseName, - entities: entityList, + entities, synchronize: synchronize ?? false, }; }; @@ -93,7 +87,10 @@ export class TypeORMModule { provide: TYPEORM_DATASOURCE, useFactory: async () => { const dataSource = new DataSource( - getDatabaseCredential(options.synchronize, options.entities), + getDatabaseCredential(options.synchronize, [ + options.entity, + ...(options.entities ?? []), + ]), ); return dataSource.initialize(); }, diff --git a/src/example/typeorm/entities/user.entity.ts b/src/example/typeorm/entities/user.entity.ts index b9d0e1f..77cc119 100644 --- a/src/example/typeorm/entities/user.entity.ts +++ b/src/example/typeorm/entities/user.entity.ts @@ -1,4 +1,4 @@ -import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; +import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; @Entity('account') export class UserEntity {