#How to connect to the database with DataSource?
42 messages · Page 1 of 1 (latest)
What do you mean?
wow so fast lol
when I try npm run start:dev this is what I get
[Nest] 27084 - 11/22/2022, 9:54:20 PM LOG [NestFactory] Starting Nest application...
[Nest] 27084 - 11/22/2022, 9:54:20 PM ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)...
MissingDriverError: Wrong driver: "undefined" given. Supported drivers are: "aurora-mysql", "aurora-postgres", "better-sqlite3", "capacitor", "cockroachdb", "cordova", "expo", "mariadb", "mongodb", "mssql", "mysql", "nativescript", "oracle", "postgres", "react-native", "sap", "sqlite", "sqljs", "spanner".
at DriverFactory.create (D:\Apps\Wejhat-back\src\driver\DriverFactory.ts:72:23)
at new DataSource (D:\Apps\Wejhat-back\src\data-source\DataSource.ts:139:43)
at createTypeormDataSource (D:\Apps\Wejhat-back\node_modules\@nestjs\typeorm\dist\typeorm-core.module.js:172:23)
at Function.<anonymous> (D:\Apps\Wejhat-back\node_modules\@nestjs\typeorm\dist\typeorm-core.module.js:176:46)
at Generator.next (<anonymous>)
at D:\Apps\Wejhat-back\node_modules\@nestjs\typeorm\dist\typeorm-core.module.js:20:71
at new Promise (<anonymous>)
at __awaiter (D:\Apps\Wejhat-back\node_modules\@nestjs\typeorm\dist\typeorm-core.module.js:16:12)
at D:\Apps\Wejhat-back\node_modules\@nestjs\typeorm\dist\typeorm-core.module.js:174:76
at Observable._subscribe (D:\Apps\Wejhat-back\node_modules\rxjs\src\internal\observable\defer.ts:55:15)
[Nest] 27084 - 11/22/2022, 9:54:20 PM LOG [InstanceLoader] TypeOrmModule dependencies initialized +6ms
[Nest] 27084 - 11/22/2022, 9:54:20 PM LOG [InstanceLoader] UsersFavoritesModule dependencies initialized +2ms
[Nest] 27084 - 11/22/2022, 9:54:20 PM LOG [InstanceLoader] ConfigHostModule dependencies initialized +0ms
[Nest] 27084 - 11/22/2022, 9:54:20 PM LOG [InstanceLoader] ConfigModule dependencies initialized +1ms
[Nest] - 11/22/2022, 9:54:20 PM LOG [InstanceLoader] AppModule dependencies initialized +2ms
Data Source has been initialized!
What does your TypeOrmModule.forRoot/Async() look like?
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: `.env`,
}),
TypeOrmModule.forRoot(),
UsersModule,
PlacesModule,
CategoriesModule,
CitiesModule,
NeighborhoodsModule,
CommentsModule,
UsersFavoritesModule,
SubcategoriesModule,
],
You have to pass configuration to the module
what should I add exactly? i didn't find anything in the docs
Seriously?
@Module({
imports: [
TypeOrmModule.forRoot({
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: 'root',
database: 'test',
entities: [],
synchronize: true,
}),
],
})
export class AppModule {}
yeah I saw that, but if I did that, then what's the benefit of using dataSource?
const { AppDataSource } = require("src/datasource");
var dbConfig = {
synchronize: false,
migrations: ['migrations/*.js'],
cli: {
migrationsDir: 'migrations',
},
};
switch (process.env.NODE_ENV) {
case 'development':
Object.assign(dbConfig,
DataSource({
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "",
database: "new_schema",
entities: ['**/*.entity.js']
})
// {
// type: 'mysql',
// database: 'new_schema',
// entities: ['**/*.entity.js'],
// username: 'root',
// password: '',
// host: 'localhost',
// port: 3306,
// logging: true,
// }
);
break;
case 'test':
Object.assign(dbConfig, {
type: 'mysql',
database: 'test_db',
entities: ['**/*.entity.ts'],
migrationsRun: true,
});
break;
case 'production':
Object.assign(dbConfig, {
type: 'mysql',
url: process.env.CLEARDB_DATABASE_URL,
logging: true,
migrationsRun: true,
entities: ['**/*.entity.js'],
ssl: {
rejectUnauthorized: false,
},
});
break;
default:
throw new Error('unknown environment');
}
module.exports = dbConfig;
@rare ridge cause this is how my ormconfig file was set up
TypeOrmModule.forRoot(dbConfig) should be fine then
I tried it and it didn't work
Didn't work how? What error (s) did you get?
[8:31:19 PM] Found 0 errors. Watching for file changes.
[Nest] 9128 - 11/27/2022, 8:31:23 PM LOG [NestFactory] Starting Nest application...
[Nest] 9128 - 11/27/2022, 8:31:23 PM ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)...
MissingDriverError: Wrong driver: "undefined" given. Supported drivers are: "aurora-mysql", "aurora-postgres", "better-sqlite3", "capacitor", "cockroachdb", "cordova", "expo", "mariadb", "mongodb", "mssql", "mysql", "nativescript", "oracle", "postgres", "react-native", "sap", "sqlite", "sqljs", "spanner".
at DriverFactory.create (D:\Apps\Wejhat-back\src\driver\DriverFactory.ts:72:23)
at new DataSource (D:\Apps\Wejhat-back\src\data-source\DataSource.ts:139:43)
at createTypeormDataSource (D:\Apps\Wejhat-back\node_modules\@nestjs\typeorm\dist\typeorm-core.module.js:172:23)
at Function.<anonymous> (D:\Apps\Wejhat-
[Nest] 9128 - 11/27/2022, 8:31:23 PM LOG [InstanceLoader] TypeOrmModule dependencies initialized +9ms
[Nest] 9128 - 11/27/2022, 8:31:23 PM LOG [InstanceLoader] UsersFavoritesModule dependencies initialized +1ms
[Nest] 9128 - 11/27/2022, 8:31:23 PM LOG [InstanceLoader] ConfigHostModule dependencies initialized +1ms
[Nest] 9128 - 11/27/2022, 8:31:23 PM LOG [InstanceLoader] ConfigModule dependencies initialized +1ms
[Nest] 9128 - 11/27/2022, 8:31:23 PM LOG [InstanceLoader] AppModule dependencies initialized +2ms
Data Source has been initialized!
[Nest] 9128 - 11/27/2022, 8:31:26 PM ERROR [TypeOrmModule] Unable to connect to the database. Retrying (2)...
MissingDriverError: Wrong driver: "undefined" given. Supported drivers are: "aurora-mysql", "aurora-postgres", "better-sqlite3", "capacitor", "cockroachdb", "cordova", "expo", "mariadb", "mongodb", "mssql", "mysql", "nativescript", "oracle", "postgres", "react-native", "sap", "sqlite", "sqljs", "spanner".
at DriverFactory.create (D:\Apps\Wejhat-back\src\driver\DriverFactory.ts:72:23)
Why does your development case use a DataSource class reference while the other two use regular objects?
I was testing Datasource
Okay, but why? If you just pass the options to TypeOrmModule.forRoot/Async() Nest will initialize the datasource for you
I'm really confused right now
can you please check my repo and see what's wrong with it?
and btw, dbConfig in app.module is causing an error and I don't know why
D:\Apps\Wejhat-back\src\app.module.ts:31
TypeOrmModule.forRoot(dbConfig),
^
ReferenceError: db is not defined
This is the error I get with your repo:
~/ormconfig.js:14
dbConfig,
^
ReferenceError: DataSource is not defined
at Object.<anonymous> (~/ormconfig.js:14:7)
at Module._compile (node:internal/modules/cjs/loader:1155:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
at Module.load (node:internal/modules/cjs/loader:1033:32)
at Function.Module._load (node:internal/modules/cjs/loader:868:12)
at Module.require (node:internal/modules/cjs/loader:1057:19)
at require (node:internal/modules/cjs/helpers:103:18)
at Object.<anonymous> (~/src/app.module.ts:22:1)
at Module._compile (node:internal/modules/cjs/loader:1155:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
Which makes sense. DataSource is never defined in your ormconfig file
Once I remove the DataSource({}) and just use a regular object {} I get a connection error because I don't have mysql running. Everything works as expected otherwise
D:\Apps\Wejhat-back\src\app.module.ts:31
TypeOrmModule.forRoot(),
^
ReferenceError: db is not defined
I'm still getting the same error after remove DataSource and Whether inserting dbConfig into forRoot or not
What do you want me to do?
I don't have that error after cloning your repo
So what would you like me to do?
nothing, a guess would suffice for me
I don't have a guess. You don't have a db variable, so I don't know what it's complaining about there. Maybe a stack trace could be helpful, but I doubt it
ok, thanks
@rare ridge just a small question, can you please let me know how to connect the datasource to the database?
I would be grateful if you can be as specific as possible
like where should I keep the inilization file?
Have you looked at TypeORM's docs?
I don't use TypeORM personally, and don't have a strong preference on how to set it all up.
yes, but when I follow it, I get an undefined driver error