#How To Connect with Datasource?

1 messages · Page 1 of 1 (latest)

pallid breach
#

I'm not really sure how to use Datasource, I tried putting it in a new file but when I run npm run start:dev I get
ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)... MissingDriverError: Wrong driver: "undefined" given. Supported drivers are: "aurora-mysql",

vernal cedar
#

What's your TypeOrm.forRoot() configuration look like?

pallid breach
vernal cedar
pallid breach
#

as for datasource I'm not really sure how to use it, I'm trying to replace it with Repository and call entities and query them

vernal cedar
#

as for datasource I'm not really sure how to use it,
Why do you want to use it?

pallid breach
#

MissingDriverError: Wrong driver: "undefined" given. Supported drivers are: "aurora-mysql",

#

this is the error I get

vernal cedar
#

Okay, but why not just pass the options to TypeOrmModule.forRoot() or TypeOrmModule.forRootAsync()? Why do you need a datasource file?

pallid breach
#
export class CategoriesService {
  constructor(
    @InjectRepository(Category) private repo: Repository<Category>,
    @InjectRepository(User) private userRepo: Repository<User>,
  ) {}

  getCategories() {
    return this.repo.createQueryBuilder().select('*')
    .where('category_id NOT IN (75, 135, 145)')
      .getRawMany();
  }
vernal cedar
#

What error do you get?

pallid breach
vernal cedar
#

Anything else? any other information?

vernal cedar
#

Okay, so then where do you throw the error from?

pallid breach
#

only the 400 bad request

vernal cedar
#

Do you have some sort of filter in place?

#

There's not enough info here to know what's happening

pallid breach
vernal cedar
#

An exception filter

pallid breach
vernal cedar
#

Do you throw an error anywhere? A throw new BadRequestException() or similar?

pallid breach
vernal cedar
#

From a brief check of your repo, I can't see where that would be generating an error from.

pallid breach
vernal cedar
#

You have multiple databases?

#

Or is this a per-environment change?

pallid breach
vernal cedar
#

https://docs.nestjs.com/techniques/database#async-configuration

TypeOrmModule.forRootAsync({
  imports: [ConfigModule],
  useFactory: (configService: ConfigService) => ({
    type: 'mysql',
    host: configService.get('HOST'),
    port: +configService.get('PORT'),
    username: configService.get('USERNAME'),
    password: configService.get('PASSWORD'),
    database: configService.get('DATABASE'),
    entities: [],
    synchronize: true,
  }),
  inject: [ConfigService],
});
pallid breach
vernal cedar
#

Do you have a user for the daycaredatabase you're currently running?

pallid breach
vernal cedar
pallid breach
#

kall- is my machine's username

vernal cedar
#

How did you set up the config service? Seems it ndidn't read your .env. file

pallid breach
# vernal cedar How did you set up the config service? Seems it ndidn't read your .env. file
@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: `.env.development`,
    }),
    TypeOrmModule.forRootAsync({
      // imports: [ConfigModule],
      useFactory: (configService: ConfigService) => ({
        type: 'mysql',
        host: configService.get('HOST'),
        port: +configService.get('PORT'),
        username:configService.get('USERNAME'),
        password:configService.get('PASSWORD'),
        database:configService.get('DATABASE'),
        entities:['**/*.js'],
      }),
      inject: [ConfigService]
    }),
vernal cedar
#

As the module is global you don't need to import it again

pallid breach
vernal cedar
#

Looks right now

pallid breach
vernal cedar
#

And your .env.development file has USERNAME=root and is in the workspace root?

vernal cedar
#

If you log out console.log(configService.get('USERNAME')) inside of the useFactory what do you get?

pallid breach
#

strange, it's root in the .env.development file

vernal cedar
#

Yeah, something doesn't seem right there. Is this the only place you have ConfigModule.forRoot()?

vernal cedar
#

Weird. Is your updated code on GitHub?

pallid breach
pallid breach
vernal cedar
#

It's reading the .env.development file just fine for me. I'd suggest removing the dist and restarting the server

pallid breach
#

but now I'm getting
Error: EMFILE: too many open files, open 'D:\Apps\Wejhat-back\node_modules\class-transformer\esm5\TransformOperationExecutor.js'

#

I tried deleting dist and node_modules and it also didn't work