nestjs / typeorm

TypeORM module for Nest framework (node.js) 🍇
https://nestjs.com
MIT License
1.91k stars 206 forks source link

TypeOrmModule.forRootAsync can't read name property correctly #156

Closed ddehghan closed 5 years ago

ddehghan commented 5 years ago

I'm submitting a...


[ ] Regression 
[X] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { join } from 'path';
import { PhotoModule } from './photo/photo.module';

@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      // name: 'connection2',  <- uncommenting this line causes error
      useFactory: () => ({
        type: 'sqlite',
        database: ':memory:',
        entities: [join(__dirname, '**/**.entity{.ts,.js}')],
        synchronize: true,
        logging: true,
      }),
    }),

    TypeOrmModule.forRootAsync({
      name: 'connection1',
      useFactory: () => ({
        type: 'sqlite',
        database: ':memory:',
        entities: [join(__dirname, '**/**.entity{.ts,.js}')],
        synchronize: true,
        logging: true,
      }),
    }),

    PhotoModule,
  ],
})
export class ApplicationModule {}

it seems that you cant set 2 ORM models and name both of them. Uncommenting the name for the first one causes the error below.

setting the name property for one ORM also cause the same error.

[Nest] 83610 - 07/12/2019, 3:51 PM [NestFactory] Starting Nest application... [Nest] 83610 - 07/12/2019, 3:51 PM [InstanceLoader] ApplicationModule dependencies initialized +42ms [Nest] 83610 - 07/12/2019, 3:51 PM [InstanceLoader] TypeOrmModule dependencies initialized +0ms [Nest] 83610 - 07/12/2019, 3:51 PM [InstanceLoader] TypeOrmModule dependencies initialized +0ms [Nest] 83610 - 07/12/2019, 3:51 PM [ExceptionHandler] Nest can't resolve dependencies of the PhotoRepository (?). Please make sure that the argument at index [0] is available in the TypeOrmModule context. +2ms Error: Nest can't resolve dependencies of the PhotoRepository (?). Please make sure that the argument at index [0] is available in the TypeOrmModule context. at Injector.lookupComponentInExports (/Users/david/gitroot/test/examples/sample/05-sql-typeorm/node_modules/@nestjs/core/injector/injector.js:183:19) at processTicksAndRejections (internal/process/task_queues.js:89:5) at Injector.resolveComponentInstance (/Users/david/gitroot/test/examples/sample/05-sql-typeorm/node_modules/@nestjs/core/injector/injector.js:143:33) at resolveParam (/Users/david/gitroot/test/examples/sample/05-sql-typeorm/node_modules/@nestjs/core/injector/injector.js:96:38) at async Promise.all (index 0) at Injector.resolveConstructorParams (/Users/david/gitroot/test/examples/sample/05-sql-typeorm/node_modules/@nestjs/core/injector/injector.js:112:27) at Injector.loadInstance (/Users/david/gitroot/test/examples/sample/05-sql-typeorm/node_modules/@nestjs/core/injector/injector.js:78:9) at Injector.loadProvider (/Users/david/gitroot/test/examples/sample/05-sql-typeorm/node_modules/@nestjs/core/injector/injector.js:35:9) at Injector.lookupComponentInImports (/Users/david/gitroot/test/examples/sample/05-sql-typeorm/node_modules/@nestjs/core/injector/injector.js:213:17) at Injector.lookupComponentInExports (/Users/david/gitroot/test/examples/sample/05-sql-typeorm/node_modules/@nestjs/core/injector/injector.js:181:33) npm ERR! code ELIFECYCLE

Expected behavior

you should be able to have 2 database connection to sqlite.

Minimal reproduction of the problem with instructions

use use the 05-sql-typeorm example code and change the app module class to above.

Environment


{
  "name": "nest-typescript-starter",
  "version": "1.0.0",
  "description": "Nest TypeScript starter repository",
  "license": "MIT",
  "scripts": {
    "build": "tsc -p tsconfig.build.json",
    "start": "ts-node src/main",
    "prestart:prod": "npm run build",
    "start:prod": "node dist/main.js"
  },
  "dependencies": {
    "@nestjs/common": "6.5.2",
    "@nestjs/core": "6.5.2",
    "@nestjs/platform-express": "6.5.2",
    "@nestjs/typeorm": "6.1.2",
    "mysql": "2.17.1",
    "reflect-metadata": "0.1.13",
    "rxjs": "6.5.2",
    "sqlite3": "^4.0.9",
    "typeorm": "0.2.18",
    "typescript": "3.5.3"
  },
  "devDependencies": {
    "@types/node": "^12.6.2",
    "ts-node": "8.3.0",
    "tslint": "5.18.0"
  }

Nest version: 6.5.2


For Tooling issues:
- Node version: 12
- Platform:  osx

Others:

kamilmysliwiec commented 5 years ago

[Nest] 83610 - 07/12/2019, 3:51 PM [ExceptionHandler] Nest can't resolve dependencies of the PhotoRepository (?). Please make sure that the argument at index [0] is available in the TypeOrmModule context. +2ms

@InjectRepository() takes 2 arguments in which the second one is the name of the connection.

@InjectRepository(Photo, 'connection2')
rafalmaciejewski commented 5 years ago

I have the same case and i also had this issue using nest@6.5.2, nestjs/typeorm@6.1.3. Unfortunately solution by Kamil doesn't work for me.

redamsy commented 3 years ago

any one knows how can i make the connections of typeorm, and knex the same? i.e when i use entities to get info from a table it return empty result