Background
I'm using:
"@nestjs/core": "^8.1.1""@nestjs/typeorm": "^8.0.3"
My project structure is as follows:
src
|- api
| |- common
| | |- entities
| | | |- base.entity.ts <- important
| | |- common.module.ts
| |- Foo
| | |- entities
| | | |- some.entity.ts <- important
| | |- foo.module.ts
|- common
| |- database
| | |- config
| | | |- typeorm.config.ts
| | |- database.module.ts
|- app.module.ts
The modules:
// src/api/common/common.module.ts
@Module({
imports: [TypeOrmModule.forFeature([Base])],
})
export class CommonModule {}
// src/api/foo/foo.module.ts
@Module({
imports: [TypeOrmModule.forFeature([FoosRepository])],
controllers: [FoosController],
providers: [FoosService],
})
export class FooModule {}
All modules are imported in the app.module.ts as following:
// src/app.module.ts
@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
DatabaseModule,
CommonModule,
FooModule,
],
})
export class AppModule {}
My TypeORM configuration looks like this:
// src/common/database/config/typeorm.config.ts
import { ConnectionOptions } from 'typeorm';
const Config: ConnectionOptions = {
// ...
entities: [__dirname + '/../src/**/*.entity{.js,.ts}'],
// ...
migrations: [__dirname + '/../migrations/**/*{.ts,.js}'],
cli: {
// Location of migration should be inside src folder
// to be compiled into dist/ folder.
migrationsDir: 'migrations',
},
};
export default Config;
Entities:
// src/common/entities/base.entity.ts
import { CreateDateColumn, UpdateDateColumn } from 'typeorm';
export abstract class Base {
@CreateDateColumn({
type: 'timestamptz',
default: () => 'CURRENT_TIMESTAMP(6)',
})
public createdAt: Date;
@UpdateDateColumn({
type: 'timestamptz',
default: () => 'CURRENT_TIMESTAMP(6)',
onUpdate: 'CURRENT_TIMESTAMP(6)',
})
public updatedAt: Date;
}
// src/api/foo/entities/foo.entity.ts
@Entity()
export class Foo extends Base {}
Goal
I want to use base entity in some entity. The base is an abstract Entity that is 1:1 copy of the example from the TypeORM docs.
Problem
I get this error when running npm run start:dev command:
[Nest] 37068 - 02/27/2022, 6:18:22 PM ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)...
/Users/XX/Workspace/project/src/api/common/entities/base.entity.ts:1
import { CreateDateColumn, UpdateDateColumn } from 'typeorm';
^^^^^^
I looked through similar issues however my dist folder contains only main.js, no entity files at all (which was suggested for example here: TypeORM Entity in NESTJS - Cannot use import statement outside a module