#How to connect to the database with DataSource?

42 messages · Page 1 of 1 (latest)

hearty pilot
#

Since Ormconfig is deprecated and replaced with DataSource, how can I use it to connect to the database?

rare ridge
#

What do you mean?

hearty pilot
#

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!
rare ridge
#

What does your TypeOrmModule.forRoot/Async() look like?

hearty pilot
rare ridge
#

You have to pass configuration to the module

hearty pilot
rare ridge
#

Seriously?

#
@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: 'localhost',
      port: 3306,
      username: 'root',
      password: 'root',
      database: 'test',
      entities: [],
      synchronize: true,
    }),
  ],
})
export class AppModule {}
hearty pilot
#
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

rare ridge
#

TypeOrmModule.forRoot(dbConfig) should be fine then

hearty pilot
rare ridge
hearty pilot
# rare ridge 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)
rare ridge
rare ridge
#

Okay, but why? If you just pass the options to TypeOrmModule.forRoot/Async() Nest will initialize the datasource for you

hearty pilot
#

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
rare ridge
#

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

hearty pilot
#

I'm still getting the same error after remove DataSource and Whether inserting dbConfig into forRoot or not

rare ridge
#

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?

hearty pilot
#

nothing, a guess would suffice for me

rare ridge
#

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

hearty pilot
#

ok, thanks

hearty pilot
#

@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?

rare ridge
#

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.

hearty pilot
#

yes, but when I follow it, I get an undefined driver error