question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom db logger service with TypeOrmModule.forRootAsync()

See original GitHub issue

[ ] Regression 
[ ] Bug report
[ ] Feature request
[X] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

I have a custom DB module to initialize TypeOrm (= Nestjs’s TypeOrmModule). I want to pass an TypeormLoggerService (which relies on an app-wide LoggerService). This is the code:

db.module.ts:

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TypeormLoggerService } from './typeorm-logger.service';

@Module({
  controllers: [],
  providers: [],
  imports: [
    TypeOrmModule.forRootAsync({
      inject: [TypeormLoggerService],
      useFactory: async (typeormLoggerService: TypeormLoggerService): Promise<any> => ({
        type: 'sqlite',
        database: `var/nest.db`,
        entities: [`src/**/**.entity{.ts,.js}`],
        logger: typeormLoggerService,
        logging: true
      }),
    })
  ]
})
export class DbModule {}

Node crashes with the following error:

1: node::Abort() [/usr/local/bin/node]
 2: node::Chdir(v8::FunctionCallbackInfo<v8::Value> const&) [/usr/local/bin/node]
 3: v8::internal::FunctionCallbackArguments::Call(void (*)(v8::FunctionCallbackInfo<v8::Value> const&)) [/usr/local/bin/node]
 4: v8::internal::MaybeHandle<v8::internal::Object> v8::internal::(anonymous namespace)::HandleApiCallHelper<false>(v8::internal::Isolate*, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::FunctionTemplateInfo>, v8::internal::Handle<v8::internal::Object>, v8::internal::BuiltinArguments) [/usr/local/bin/node]
 5: v8::internal::Builtin_Impl_HandleApiCall(v8::internal::BuiltinArguments, v8::internal::Isolate*) [/usr/local/bin/node]
 6: 0x1776425042fd

Environment


nest/typeorm version: 5.2.2
nest/common version: 5.3.5
nest/core version: 5.3.4

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
kamilmysliwieccommented, Dec 6, 2018
TypeOrmModule.forRootAsync({
   inject: [TypeormLoggerService],
   useFactory: ...,
})

You are trying to inject TypeormLoggerService which is unavailable in the TypeOrmModule. In order to provide it, you would have to import a module that exports TypeormLoggerService, for example:

TypeOrmModule.forRootAsync({
   imports: [TypeOrmLoggerModule], <------ THIS
   inject: [TypeormLoggerService],
   useFactory: ...,
})
1reaction
vrilcodecommented, Dec 13, 2018

I had to export TypeormLoggerService from DbModule and import it in TypeOrmModule although TypeOrmModule is included in the Module of TypeormLoggerService (DbModule). That was a bit confusing. But now I understand and it’s working. Thanks a lot!

Read more comments on GitHub >

github_iconTop Results From Across the Web

NestJS Using ConfigService with TypeOrmModule
How can I use this service with the the TypeOrmModule? TypeOrmModule.forRoot({ type: 'mysql', host: 'localhost', ...
Read more >
API with NestJS #50. Introduction to logging with the built-in ...
We need a way to troubleshoot the application. This article serves as an introduction to how we can keep logs with the built-in...
Read more >
Database | NestJS - A progressive Node.js framework
The forRoot() method supports all the configuration properties exposed by the DataSource constructor from the TypeORM package. In addition, there are several ...
Read more >
Creating Dynamic Modules in Nest JS Part-1
Lets say i have created sendGrid Module, AzureBlobModule or Database Module, These Module will be used by other Modules and sometime
Read more >
Multiple Databases | Nestjs-query - Blog
Then setup multiple database connections. ... TypeOrmModule.forRootAsync({ name: SECRET_DB_CONNECTION, // you need to set the name here! imports: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found