#NestJS + TypeORM

17 messages · Page 1 of 1 (latest)

raven chasm
#

How do I inject my NestJS service, that has a TypeORM entity injected, into another module, outside the module it lives?

export class XPTOService implements XPTORepository {
  constructor(
    @InjectRepository(XPTOEntity)
    private repository: Repository<XPTOEntity>,
  ) {}

This guy is declared as a provider of XPTOModule
And i would like to use it inside the FooBarModule

How to do that?

short turret
#

Add XPTOService to XPTOModule's exports, then add XPTOModule to the imports of FooBarModule.

DO NOT add XPTOService to the providers of FooBarModule

raven chasm
#

Got it! Lemme try

#

I'm getting the following error:

Potential solutions:

  • Is SharedModule a valid NestJS module?
  • If XPTOService is a provider, is it part of the current SharedModule?
  • If XPTOService is exported from a separate @Module, is that module imported within SharedModule?
    @Module({
    imports: [ /* the Module containing XPTOService */ ]
    })
#

My SharedModuleimports the XPTOModule that i want to import the XPTOService

short turret
#

So what does SharedModule look like?

raven chasm
#
📦shared
 ┣ 📂module-a
 ┃ ┣ 📜module-a.module.ts
 ┃ ┣ 📜module-a.service.spec.ts
 ┃ ┗ 📜module-a.service.ts
 ┣ 📂module-b
 ┃ ┣ 📜module-b.module.ts
 ┃ ┗ 📜module-b.service.spec.ts
 ┣ 📂module-c
 ┃ ┣ 📜module-c.module.ts
 ┃ ┣ 📜module-c.service.spec.ts
 ┃ ┗ 📜module-c.service.ts
 ┣ 📂module-d
 ┃ ┣ 📂interfaces
 ┃ ┃ ┗ 📜module-d.ts
 ┃ ┣ 📂use-cases
 ┃ ┃ ┗ 📂foobar
 ┃ ┃ ┃ ┣ 📜foobar.ts
 ┃ ┃ ┃ ┗ 📜index.ts
 ┃ ┗ 📜module-d.module.ts
 ┣ 📂types
 ┃ ┣ 📜FooBar.ts
 ┃ ┗ 📜BarFoo.ts
 ┗ 📜shared.module.ts

#

Something like this. All modules inside shared/ are declared inside SharedModule providers/imports, etc

short turret
#

I could care less about the directory. It's the class that's important here

#

And show the XPTOMoudle class as well, just to be sure. Possibly include the file imports too. Might make a difference

raven chasm
#
// My SharedModule
@Global()
@Module({
  imports: [ModuleA, ModuleB, ModuleC, ModuleD],
  providers: [XPTOService, FooBar],
  exports: [ModuleA, ModuleB, ModuleC, ModuleD],
})

// My XPTOModule

@Module({
  imports: [XPTOModule] // <- This is the one that i want to use,
  providers: [
    {
      provide: XPTOSOAPClient,
      useFactory: async () => {
        const xptoClient = new XPTOSOAPClient()
        await xptoClient.build()
        return xptoClient
      },
    },
    XPTOService,
  ],
  exports: [XPTOService, XPTOSOAPClient],
})
#

Just to give context, my SharedModulestores all external integrations that i have, but i'm not sure if this is the best pattern when using NestJS

short turret
#

Why is XPTOService added to the SharedModuke's providers, and why is XPTOModule not in the imports?

raven chasm
#

I have no clue TBH, I'm just doing some maintenance in an existing app :P, but it was working that way, i just don't know how

short turret
#

Okay. Is XPTOModule suppose to be shared? Let'sstart with that

raven chasm
#

XPTOModule declares XPTOService as a provider, and XPTOService access the database through repositories, so yes, right now, it needs to be shared

short turret
#

Okay, good. So SharedModule should have XPTOModule in the imports and exports. It should not have XPTOService in the providers