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!
HashIdService
so my entites are able to hash their own ID.. How would you solve this issue? What do you mean with a "utility function"? – Roselleroselyn