fix: entities not found

This commit is contained in:
Supan Adit Pratama 2024-11-10 11:20:32 +07:00
parent 3450fd026a
commit 7424b38798
3 changed files with 12 additions and 15 deletions

View File

@ -9,5 +9,5 @@ export interface TypeORMModuleOptions<T extends ObjectLiteral> {
processRead?; processRead?;
processUpdate?; processUpdate?;
synchronize?: boolean; synchronize?: boolean;
entities?: string[]; entities?: any[];
} }

View File

@ -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 { import {
CREATE_PROCESS, CREATE_PROCESS,
DELETE_PROCESS, DELETE_PROCESS,
@ -11,6 +6,10 @@ import {
READ_PROCESS, READ_PROCESS,
UPDATE_PROCESS, UPDATE_PROCESS,
} from '@aditama-labs/nest-autocrud/skeleton'; } 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 { import {
TypeORMCreateProcess, TypeORMCreateProcess,
TypeORMDeleteProcess, TypeORMDeleteProcess,
@ -19,7 +18,7 @@ import {
TypeORMReadProcess, TypeORMReadProcess,
TypeORMUpdateProcess, TypeORMUpdateProcess,
} from './processes'; } from './processes';
import { TypeOrmModule } from '@nestjs/typeorm'; import { TypeORMService } from './typeorm.service';
const getDatabaseCredential = ( const getDatabaseCredential = (
synchronize?: boolean, synchronize?: boolean,
@ -27,11 +26,6 @@ const getDatabaseCredential = (
): any => { ): any => {
const url = new URL(process.env.DATABASE_URL); 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); const defaultProtocol = url.protocol.substring(0, url.protocol.length - 1);
let protocol = defaultProtocol; let protocol = defaultProtocol;
switch (defaultProtocol) { switch (defaultProtocol) {
@ -56,7 +50,7 @@ const getDatabaseCredential = (
username: username, username: username,
password: password, password: password,
database: databaseName, database: databaseName,
entities: entityList, entities,
synchronize: synchronize ?? false, synchronize: synchronize ?? false,
}; };
}; };
@ -93,7 +87,10 @@ export class TypeORMModule {
provide: TYPEORM_DATASOURCE, provide: TYPEORM_DATASOURCE,
useFactory: async () => { useFactory: async () => {
const dataSource = new DataSource( const dataSource = new DataSource(
getDatabaseCredential(options.synchronize, options.entities), getDatabaseCredential(options.synchronize, [
options.entity,
...(options.entities ?? []),
]),
); );
return dataSource.initialize(); return dataSource.initialize();
}, },

View File

@ -1,4 +1,4 @@
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
@Entity('account') @Entity('account')
export class UserEntity { export class UserEntity {