mongoose - acsessing data of one collection from anther module
Current behavior
i am using the mongoose module, lets say i have User collection which configured in the User module, Now i need this collection data in the Accounts module, i need to register it again in the Accounts module ? this is how my modules extract schemas:
Accounts Module
MongooseModule.forFeature([
{ name: 'Account', schema: AccountsSchema},
{ name: 'User', schema: UserSchema},
])
Users Module
MongooseModule.forFeature([
{ name: 'User', schema: UserSchema},
])
Is there a better way of doing it?
No you dont need register again. Only user @InjectModel decorator to inject the schema.
MongooseModule is a Global Scope Module. i.e has a @Global() decorator , so you don't need to Register the Model Again, just inject it;
Remember to export, import the modules correctly
@Module({
imports: [MongooseModule.forFeature([{ name: GAME_COLLECTION_NAME, schema: GameSchema }])],
controllers: [GamesController],
components: [GamesService],
exports: [GamesService, MongooseModule],
})
export class GamesModule implements NestModule {}
@Module({
imports: [MongooseModule.forFeature([{ name: USER_COLLECTION_NAME, schema: UserSchema }]), GamesModule, LinksModule],
controllers: [UsersController],
components: [UsersService],
exports: [UsersService],
})
export class UsersModule implements NestModule {}
@Component()
export class UsersService {
constructor(
@InjectModel(GAME_COLLECTION_NAME) private readonly gameModel: Model<Game>,
@InjectModel(USER_COLLECTION_NAME) private readonly userModel: Model<User>) { }
i am getting out of context error, but after exporting and importing it as @silvelo said it works now, couldnt find it anywhere in the docs, maybe worth adding (mongoose docs)
@roeehershko in the docs you find this https://docs.nestjs.com/modules, Modules re-exporting explain about it.
ok thanks, one more question, is it normal that it takes the application 10 seconds to initialize mongoose module ?
[Nest] 350 - 2018-2-14 16:13:06 [InstanceLoader] MongooseCoreModule dependencies initialized +10082ms
Also after the initalize it takes approx 20 seconds for the connection to establish
I have 8 collections and [Nest] 19344 - 2018-2-14 17:16:02 [InstanceLoader] MongooseCoreModule dependencies initialized +1044ms
It should depend on schemas complexity
weird, i tried to down it to 1 schema and its still 10 seconds, i just figured the connection delay was due to using the auto-increment plugin, i will try to investigate more
Yes, its weird. MongooseModule only wrapper the old way to make the connection and schemas.
Update: I am not using the mongooseModule version, because a problem with modelToken, and refactor the module to use my own version. Maybe there is the problem
are you using windows?
Yes, i am using windows.
You can try this because the way to generate token could make the problem.
Go to this repo, download it and compile with npm run build. https://github.com/silvelo/mongoose Then copy the content of dist folder to node_module/@nestjs/mongoose/ of your main project. Create a collection at the same way
MongooseModule.forFeature([{ name: USER_COLLECTION_NAME, schema: UserSchema }]),
The next step is correct the break change in your services:
@InjectModel(UserSchema) private readonly userModel: Model<User>
to:
@InjectModel(USER_COLLECTION_NAME) private readonly userModel: Model<User>
updated the comment: issue with docker, i tried to run it without docker and it took less then a second, i guess its gonna be harder to figure this one
i will try to get the repo and see if the same issue persists with docker too
Perfect then. you can close the issue.
well i was able to solve this eventually, i used the machine IP instead of docker container name so instead of:
mongodb://mongo/admin
i did:
mongodb://192.168.99.100/admin
just if anyone is breaking his head on it, probably some docker internal DNS crap it also solved the 20 seconds loading autoincrement plugin, crappy docker for windows
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.