Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NestJS : Inject Service into Models / Entities

i am currently stuck on a problem, which i don't know how to propery solve:

In my NestJS application, I would like to make all my TypeORM Entities extend a BaseEntity class that provide some general features. For example, i would like to provide an additional getHashedID() method that hashed (and therefore hides) the internal ID from my API customers.

Hashing is done by a HashIdService, which provides an encode() and decode() method.

My setup looks like this (removed the Decorators for readability!):

export class User extends BaseEntity {
  id: int;
  email: string;
  name: string;
  // ...
}

export class BaseEntity {
  @Inject(HashIdService) private readonly hashids: HashIdService;

  getHashedId() {
    return this.hashids.encode(this.id);
  }
}

However, if i call the this.hashids.encode() method, it throws an exception with:

Cannot read property 'encode' of undefined

How can i inject a service into a entity/model class? Is this even possible?

UPDATE #1 In particular, i would like to "inject" the HashIdService into my Entities. Further, the Entities should have a getHashedId() method that returns their hashed ID.. As i don't want to do this "over and over again", i would like to "hide" this method in the BaseEntity as described above..

My current NestJS version is as follows:

Nest version:
+-- @nestjs/[email protected]
+-- @nestjs/[email protected]
+-- @nestjs/[email protected]
+-- @nestjs/[email protected]
+-- @nestjs/[email protected]

Thank you very much for your help!

like image 405
zorn Avatar asked Nov 23 '18 09:11

zorn


People also ask

What is inject model in NestJS?

Dependency injection is an inversion of control (IoC) technique wherein you delegate instantiation of dependencies to the IoC container (in our case, the NestJS runtime system), instead of doing it in your own code imperatively.

What is the use of injectable in NestJS?

With Nest. js' injector system, you can manage your objects without thinking about the instantiation of them, because that is already managed by the injector, which is there to resolve the dependencies of every dependent object. With dependency injection, we connect different classes inside a module.

What is service in NestJS?

Service. In enterprise applications, we follow the SOLID principle, where S stands for Single Responsibility . The controllers are responsible for accepting HTTP requests from the client and providing a response. For providing the response, you may need to connect to some external source for data.

What is NEST IoC container?

IoC Container (a.k.a. DI Container) is a framework for implementing automatic dependency injection. It manages object creation and it's life-time, and also injects dependencies to the class.


1 Answers

If you don't need to inject the HashIdService or mock it in a unit test you can simply do this:

BaseEntity.ts

import { HashIdService } from './HashIdService.ts';

export class BaseEntity {

    public id: number;

    public get hasedId() : string|null {
        const hashIdService = new HashIdService();
        return this.id ? hashIdService.encode(this.id) : null;
    }
}

User.ts

export class User extends BaseEntity {
    public email: string;
    public name: string;
    // ...
}

Then create your user:

const user = new User();
user.id = 1234;
user.name = 'Tony Stark';
user.email = '[email protected]';

console.log(user.hashedId);
//a1b2c3d4e5f6g7h8i9j0...
like image 184
nerdy beast Avatar answered Nov 05 '22 17:11

nerdy beast