nest icon indicating copy to clipboard operation
nest copied to clipboard

mongoose - acsessing data of one collection from anther module

Open roeehershko opened this issue 8 years ago • 15 comments

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?

roeehershko avatar Feb 14 '18 12:02 roeehershko

No you dont need register again. Only user @InjectModel decorator to inject the schema.

silvelo avatar Feb 14 '18 13:02 silvelo

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;

shekohex avatar Feb 14 '18 14:02 shekohex

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>) { }

silvelo avatar Feb 14 '18 14:02 silvelo

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 avatar Feb 14 '18 15:02 roeehershko

@roeehershko in the docs you find this https://docs.nestjs.com/modules, Modules re-exporting explain about it.

silvelo avatar Feb 14 '18 15:02 silvelo

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

roeehershko avatar Feb 14 '18 16:02 roeehershko

I have 8 collections and [Nest] 19344 - 2018-2-14 17:16:02 [InstanceLoader] MongooseCoreModule dependencies initialized +1044ms

It should depend on schemas complexity

silvelo avatar Feb 14 '18 16:02 silvelo

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

roeehershko avatar Feb 14 '18 16:02 roeehershko

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

silvelo avatar Feb 14 '18 16:02 silvelo

are you using windows?

roeehershko avatar Feb 14 '18 16:02 roeehershko

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>

silvelo avatar Feb 14 '18 16:02 silvelo

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

roeehershko avatar Feb 14 '18 16:02 roeehershko

Perfect then. you can close the issue.

silvelo avatar Feb 14 '18 16:02 silvelo

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

roeehershko avatar Feb 14 '18 20:02 roeehershko

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.

lock[bot] avatar Sep 25 '19 06:09 lock[bot]