Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: DataSource with name "default" has already added #53

Open
piszczu4 opened this issue Aug 28, 2024 · 3 comments
Open

Error: DataSource with name "default" has already added #53

piszczu4 opened this issue Aug 28, 2024 · 3 comments

Comments

@piszczu4
Copy link

piszczu4 commented Aug 28, 2024

I've got an error from title when trying to deploy app with some migrations. This is my app.module:

 TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: (configService: ConfigService<IConfiguration>) => {
        const databaseConfig = configService.get('database', { infer: true });
        return {
          type: databaseConfig.type as any,
          host: databaseConfig.host,
          port: databaseConfig.port,
          username: databaseConfig.username,
          password: databaseConfig.password,
          database: databaseConfig.name,
          synchronize: false,
          subscribers: [BoughtCourseSubscriber, CourseNodeSubscriber, BoughtHomeworkSubscriber],
          entities: [__dirname + '/modules/**/*.entity.{js,ts}'],
          migrations: [__dirname + '/migrations/*.{js,ts}'],
          migrationsRun: true,
          cli: {
            migrationsDir: __dirname + '/migrations',
          },
          ...(!databaseConfig.sslRejectUnathorized
            ? {
                ssl: {
                  rejectUnauthorized: false,
                },
              }
            : {}),
        };
      },
      async dataSourceFactory(options) {
        if (!options) {
          throw new Error('Invalid options passed');
        }

        const dataSource = new DataSource(options);
        return addTransactionalDataSource(dataSource);
      },
      inject: [ConfigService],
    }),

Any ideas why is that a case? Here is full error stack:

[2024-08-28 21:13:56] Error: DataSource with name "default" has already added.
[2024-08-28 21:13:56]     at addTransactionalDataSource (/workspace/node_modules/typeorm-transactional/dist/common/index.js:156:15)
[2024-08-28 21:13:56]     at dataSourceFactory (/workspace/dist/src/app.module.js:99:83)
[2024-08-28 21:13:56]     at /workspace/node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:169:36
[2024-08-28 21:13:56]     at Observable._subscribe (/workspace/node_modules/rxjs/dist/cjs/internal/observable/defer.js:8:31)
[2024-08-28 21:13:56]     at Observable._trySubscribe (/workspace/node_modules/rxjs/dist/cjs/internal/Observable.js:41:25)
[2024-08-28 21:13:56]     at /workspace/node_modules/rxjs/dist/cjs/internal/Observable.js:35:31
[2024-08-28 21:13:56]     at Object.errorContext (/workspace/node_modules/rxjs/dist/cjs/internal/util/errorContext.js:22:9)
[2024-08-28 21:13:56]     at Observable.subscribe (/workspace/node_modules/rxjs/dist/cjs/internal/Observable.js:26:24)
[2024-08-28 21:13:56]     at subscribeForRetryWhen (/workspace/node_modules/rxjs/dist/cjs/internal/operators/retryWhen.js:14:31)
[2024-08-28 21:13:56]     at /workspace/node_modules/rxjs/dist/cjs/internal/operators/retryWhen.js:18:43
@piszczu4
Copy link
Author

piszczu4 commented Sep 3, 2024

Any ideas? It works only when there is no migrations

@asf-loft
Copy link

Same problem here.

@piszczu4
Copy link
Author

Same problem here.

I did the following and it seems that it works:

async dataSourceFactory(options) {
    const dataSource = await new DataSource(options).initialize();
    return addTransactionalDataSource(dataSource);
  },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants