Giter VIP home page Giter VIP logo

Comments (13)

FriOne avatar FriOne commented on May 22, 2024 4

@obedm503 yeah, I have the same thought about it. It will be great if forRoot method creates repositories for entities if they was provided as classes array. We can have both methods implemented.
Also forFeature name doesn't explain what the function actually do. It is better to call it provideRepositories or smth like that.

from typeorm.

fwoelffel avatar fwoelffel commented on May 22, 2024 3

The forRoot provides the Connection and the forFeature provides the Repositories. There's nothing weird in that ;)

from typeorm.

fwoelffel avatar fwoelffel commented on May 22, 2024 3

What about people who:

  • uses forRoot in a DatabaseModule which exports the instanciated connection
  • uses forFeature([ User ]) in a UserModule which imports the DatabaseModule
  • uses forFeature([ Car ]) in a CarModule which imports the DatabaseModule
  • doesn't want the Repository<User> available in the CarModule
  • doesn't want the Repository<Car> available in the UserModule

I don't think that what you're asking for would allow that design.

from typeorm.

kamilmysliwiec avatar kamilmysliwiec commented on May 22, 2024

Hi @FriOne,
Take a look here: https://docs.nestjs.com/techniques/sql

from typeorm.

FriOne avatar FriOne commented on May 22, 2024

Hi, @kamilmysliwiec, well, if I have one root module, I should import the orm module twice :(
Actually I don't want use config file because I prefer to control it from code.

from typeorm.

fwoelffel avatar fwoelffel commented on May 22, 2024

Is your code sample even working? I'm trying to do something very similar (btw, I'm okay with the TypeORM module design; perhaps should I post this in a new issue).

import {Module} from '@nestjs/common';
import {TypeOrmModule} from "@nestjs/typeorm";

import {AnimalsService} from './services/animals/animals.service';
import {AnimalsController} from './controllers/animals/animals.controller';
import {Animal} from "./entities/animal.entity";

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'postgres',
      host: process.env.hasOwnProperty('POSTGRES_HOST') ? process.env.POSTGRES_HOST : '127.0.0.1',
      port: process.env.hasOwnProperty('POSTGRES_PORT') ? Number(process.env.POSTGRES_PORT) : 5432,
      username: process.env.hasOwnProperty('POSTGRES_USERNAME') ? process.env.POSTGRES_USERNAME : 'basicuser',
      password: process.env.hasOwnProperty('POSTGRES_PASSWORD') ? process.env.POSTGRES_PASSWORD : 'ultrastrongpassword',
      database: process.env.hasOwnProperty('POSTGRES_DB') ? process.env.POSTGRES_DB : 'ris',
      entities: [ Animal ],
      synchronize: true
    }),
    TypeOrmModule.forFeature([ Animal ])
  ],
  components: [
    AnimalsService
  ],
  controllers: [
    AnimalsController
  ]
})
export class AnimalsModule {}

When I start my app, I get the following error:

[Nest] 26187   - 2018-1-17 10:06:49   [NestFactory] Starting Nest application...
[Nest] 26187   - 2018-1-17 10:06:49   [InstanceLoader] ApplicationModule dependencies initialized +9ms
[Nest] 26187   - 2018-1-17 10:06:50   [InstanceLoader] ProjectsModule dependencies initialized +173ms
[Nest] 26187   - 2018-1-17 10:06:50   [InstanceLoader] TypeOrmModule dependencies initialized +1ms
[Nest] 26187   - 2018-1-17 10:06:50   [InstanceLoader] TypeOrmCoreModule dependencies initialized +2ms
[Nest] 26187   - 2018-1-17 10:06:50   [InstanceLoader] TypeOrmModule dependencies initialized +1ms
[Nest] 26187   - 2018-1-17 10:06:50   [InstanceLoader] ProtocolsModule dependencies initialized +3ms
[Nest] 26187   - 2018-1-17 10:06:50   [ExceptionHandler] No repository for "Animal" was found. Looks like this entity is not registered in current "default" connection?

It looks like the TypeOrmModule.forFeature call is done too soon. Is there any way to make sure that the Connection and Repositories are initialized before the forFeature call?

from typeorm.

kamilmysliwiec avatar kamilmysliwiec commented on May 22, 2024

It looks like the TypeOrmModule.forFeature call is done too soon
That's not possible. To create repositories typeorm need connection instance that is provided by forRoot(). If forFeature is called before forRoot you would receive an exception that connection is not available yet.

from typeorm.

fwoelffel avatar fwoelffel commented on May 22, 2024

I found my issue ;)
It is because, in another module, I also use TypeOrmModule.forRoot to get a connection to another database. I've looked through the code and I understand that there is currently no way to handle multiple connections with this TypeORM module. Is it planned? (would you like me to create a new issue for this?)

from typeorm.

kamilmysliwiec avatar kamilmysliwiec commented on May 22, 2024

@fwoelffel saw your pull request, will verify soon :)

from typeorm.

obedm503 avatar obedm503 commented on May 22, 2024

it's still wierd to pass entities twice, will it be posible to omit the TypeOrmModule.forFeature call?

from typeorm.

obedm503 avatar obedm503 commented on May 22, 2024

But I would guess that if I'm already passing the entities to forRoot that that also registers the entities and provides the repositories

from typeorm.

obedm503 avatar obedm503 commented on May 22, 2024

I'm not saying we should get rid of forFeature, I'm just saying that if the entities are already registered in the root module using forRoot, then forFeature is unnecessary in the child modules. on the other hand, if it's registered in a child module but not in the root module, then it would only be available for the child module, and not the entire app

from typeorm.

FriOne avatar FriOne commented on May 22, 2024

@fwoelffel In case which you described, entities will be passed to options as regexp string, not by importing entities classes. I mean why should we import them as classes into forRoot in this case? So no repositories will not be provided without forFeature calls.

from typeorm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.