Comments (13)
@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.
The forRoot
provides the Connection
and the forFeature
provides the Repositories
. There's nothing weird in that ;)
from typeorm.
What about people who:
- uses
forRoot
in aDatabaseModule
which exports the instanciated connection - uses
forFeature([ User ])
in aUserModule
which imports theDatabaseModule
- uses
forFeature([ Car ])
in aCarModule
which imports theDatabaseModule
- doesn't want the
Repository<User>
available in theCarModule
- doesn't want the
Repository<Car>
available in theUserModule
I don't think that what you're asking for would allow that design.
from typeorm.
Hi @FriOne,
Take a look here: https://docs.nestjs.com/techniques/sql
from typeorm.
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.
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.
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.
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.
@fwoelffel saw your pull request, will verify soon :)
from typeorm.
it's still wierd to pass entities twice, will it be posible to omit the TypeOrmModule.forFeature
call?
from typeorm.
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.
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.
@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)
- Nest could not find DataSource element HOT 3
- Result of query with comment is inconsistent and random HOT 20
- TypeOrmModule.forRoot() report errors: ConnectionNotFoundError: Connection "default" was not found. HOT 4
- '"typeorm"' has no exported member named 'InjectRepository'. Did you mean 'getRepository'? HOT 3
- setting synchronize to true gives me a typeorm error HOT 1
- When a custom converter is used on a column, migration fails.(nestjs+typeorm) HOT 1
- Testing mocker issues HOT 1
- Option to not initialize DataSource in createDataSourceFactory HOT 6
- Many to many relation is finding a column that doesn't exists HOT 1
- Polymorphic association #12750 HOT 1
- createTypeOrmProviders should inject EntityManager instead of Connection HOT 1
- Bring back keepConnectionAlive HOT 1
- auto db connection close problem When Execute Graceful shtudown with pm2 HOT 1
- Every migration:generate creates DROP/CREATES constraints and keys HOT 1
- Inject ConfigService into an Entity or Subscriber HOT 4
- Nestjs warns about "TypeOrmModule" taking a long time to serialize HOT 8
- TypeOrmModule makes @Request() unusable HOT 1
- When connectionstring param used, Hostname and database name are not correctly extracted HOT 2
- perhaps Typeorm module didn't passing request response to controller HOT 2
- Check whether the object passed is an entity HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typeorm.