#How To Connect with Datasource?
1 messages · Page 1 of 1 (latest)
What's your TypeOrm.forRoot() configuration look like?
https://github.com/khaldgh/Wejhat-back/blob/master/src/app.module.ts#L33
This is invalid with TypeORM 0.3.0 because there's no longer a reading from an ormconfig. How are you tyring to actually use the datasource.ts file? What are you needing it for?
I know that ormconfig is no longer valid, that's why I commented it and replaced it with datasource
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
as for datasource I'm not really sure how to use it,
Why do you want to use it?
to connect to the database
MissingDriverError: Wrong driver: "undefined" given. Supported drivers are: "aurora-mysql",
this is the error I get
Okay, but why not just pass the options to TypeOrmModule.forRoot() or TypeOrmModule.forRootAsync()? Why do you need a datasource file?
it worked and the connection is made.
but now when I send a request using Repository i get a 400 error
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();
}
What error do you get?
I just get a 400 bad request in postman
Anything else? any other information?
Okay, so then where do you throw the error from?
the error ususally comes from Postman, strangely now it doesn't
only the 400 bad request
Do you have some sort of filter in place?
There's not enough info here to know what's happening
what do you mean by filter? like a guard?
An exception filter
then no
Do you throw an error anywhere? A throw new BadRequestException() or similar?
no, but back when ormconfig was working, errors were showing up without exceptions
That doesn't help me, because I don't know what it looked like before hand, nor why this error is showing up. There's no info here
From a brief check of your repo, I can't see where that would be generating an error from.
I made it work, now I'm confused how to dynamically choose the current database?
yes
Use forRootAsync and the ConfigService like the docs show how to do
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],
});
thanks
I used this, with .env.development and I got this error
Error: Access denied for user 'kall-'@'localhost' (using password: YES)
Do you have a user for the daycaredatabase you're currently running?
what do you mean by daycare?
A typo from my phone
yes, and it should be root instead of kall-
kall- is my machine's username
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]
}),
As the module is global you don't need to import it again
so it should be like this?
Looks right now
still the same error
And your .env.development file has USERNAME=root and is in the workspace root?
yes
If you log out console.log(configService.get('USERNAME')) inside of the useFactory what do you get?
kall- !
strange, it's root in the .env.development file
Yeah, something doesn't seem right there. Is this the only place you have ConfigModule.forRoot()?
yes
Weird. Is your updated code on GitHub?
juust updated
It's reading the .env.development file just fine for me. I'd suggest removing the dist and restarting the server
I changed USERNAME to USER and it worked
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