#[@nest/typeorm] autoLoadEntities option questions?

1 messages · Page 1 of 1 (latest)

iron compass
#

at the moment this is what my database module looks like

import { Global, InternalServerErrorException, Logger, Module, OnModuleInit } from '@nestjs/common'
//import { databaseProviders } from './database.provider'
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm'
import { ApiConfigModule } from '../config/config.module'
import { ApiConfigService } from '../config/config.service'
import { DatabaseLogger } from './database-logger/database-logger'
import { DataSource } from 'typeorm'

// https://docs.nestjs.com/techniques/database
@Global()
@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      imports: [ApiConfigModule],
      inject: [ApiConfigService],
      useFactory: async (config: ApiConfigService) => ({
        type: config.database.type,
        host: config.database.host,
        port: config.database.port,
        username: config.database.user,
        password: config.database.pass,
        database: config.database.name,
        autoLoadEntities: true,
        synchronize: config.isDev,
        logger: new DatabaseLogger(),
        retryAttempts: 3,
      }),
      dataSourceFactory: async (options) => {
        return await new DataSource(options).initialize().then((dataSource) => {
          const logger = new Logger('TyepOrm')
          logger.verbose('Connected to Database')
          return dataSource
        })
      },
    }),
  ],
})
export class DatabaseModule {}

ps: is this ok how i use the logger here?

carmine flume
#

In the original logic of typo orm, they scan the specified project directory for an entity inside and load it. We had problems with this (I don't remember the exact problems). So my team and I "drew" our own mechanism based on the typeORM code

#

In general, you can implement a similar DbModule with a forReature method. Which will collect all the entities of each module into a single storage and import these entities into the typeaORM configure

cobalt cairn
#

The autoLoadEntities option works by scanning all modules where you used TypeOrmModule.forFeature([Entity]) and loads these automatically. I'm wondering whether you didn't break it with using a custom dataSourceFactory. Try removing is and see if the error goes away.

iron compass
iron compass
# cobalt cairn The `autoLoadEntities` option works by scanning all modules where you used `Type...

managed to resolve it, now i know where the autoLoadEntities is looking for
but now i get something really strange

Data type "Array" in "Role.permissions" is not supported by "postgres" database.
with is odd because typeorm supports arrays just fine

want me to create a new issue for this or continue here?
i will share the entity but will async await your response (going to do something else while i wait)
yes i know that was a bad pun

cobalt cairn
iron compass
# cobalt cairn can you show the entity definition?
import { User } from 'src/account/entities/user.entity'
import { Column, DataSource, Entity, JoinTable, ManyToMany, PrimaryGeneratedColumn } from 'typeorm'

@Entity()
export class Role {
  @PrimaryGeneratedColumn()
  id: number

  @Column({ length: 50, unique: true })
  name: string

  @ManyToMany((type) => User, (user) => user.roles)
  @JoinTable()
  user: User[]

  @Column()
  permissions: string[]
}

export const RoleProviders = [
  {
    provide: 'ROLE_REPOSITORY',
    useFactory: (dataSource: DataSource) => dataSource.getRepository(Role),
    inject: ['DATA_SOURCE'],
  },
]
cobalt cairn
#

You will need to specify the type in the Column decorator manually. Typescript can't reflect generic types - string[] is acutally Array<string>, but the <string> part is lost on compilation, leaving you with Array only.

#

@Column("text", { array: true })

iron compass
tulip gazelleBOT
#

This post has been marked as resolved. :white_check_mark:
Please read through the conversation and resolution, if you are having the same issue. If you were the original author of the post and the issue is still fresh (within a few days) and you are still have having trouble, continue to reply here. If you are not the original author of the post or the post has aged, start a new thread linking this one as relevant to your problem, providing as much additional information as possible.