Map entities right into your Ts.ED controllers!
Find a file
semantic-release-bot 1311514cd1 chore(release): 1.0.0 [skip ci]
# 1.0.0 (2022-12-07)

### Bug Fixes

* **release:** Update releaserc ([30a2b28](30a2b28af5))

### Features

* **docs:** Update README.md ([98a0fa0](98a0fa0cd1))
2022-12-07 18:18:26 +00:00
.yarn/releases Initial Commit 2022-12-05 19:10:52 +01:00
src Testing 2022-12-07 18:24:10 +01:00
test Testing 2022-12-07 18:24:10 +01:00
.barrelsby.json Initial Commit 2022-12-05 19:10:52 +01:00
.gitignore Testing 2022-12-07 18:24:10 +01:00
.gitlab-ci.yml Initial Commit 2022-12-05 19:10:52 +01:00
.mocharc.json Initial Commit 2022-12-05 19:10:52 +01:00
.npmignore Initial Commit 2022-12-05 19:10:52 +01:00
.nycrc.json Initial Commit 2022-12-05 19:10:52 +01:00
.releaserc fix(release): Update releaserc 2022-12-07 18:44:37 +01:00
.yarnrc.yml Initial Commit 2022-12-05 19:10:52 +01:00
CHANGELOG.md chore(release): 1.0.0 [skip ci] 2022-12-07 18:18:26 +00:00
package.json chore(release): 1.0.0 [skip ci] 2022-12-07 18:18:26 +00:00
README.md feat(docs): Update README.md 2022-12-07 19:09:28 +01:00
renovate.json Initial Commit 2022-12-05 19:10:52 +01:00
tsconfig.json Initial Commit 2022-12-05 19:10:52 +01:00
tsconfig.spec.json Initial Commit 2022-12-05 19:10:52 +01:00
yarn.lock Update yarn.lock 2022-12-07 18:32:42 +01:00

@jojoxd/tsed-entity-mapper

status coverage Latest Release

See on NPM

Map Ts.ED request parameters to any object!

Installation

You can get the latest release and the type definitions using npm:

$ npm install @jojoxd/tsed-entity-mapper reflect-metadata
// Server.ts
import "reflect-metadata";
import "@jojoxd/tsed-entity-mapper";

// <snip>

@Configuration({
    // No configuration required
})
export class Server {}

⚠️ Important: Don't skip the reflect-metadata package. It is required to extract the types from your controller methods.
You also need "emitDecoratorMetadata": true in your tsconfig.

Examples

Creating a Mapper

The Entity Mapper is the core of translating parameters to an object.

import { Inject } from "@tsed/di";
import { EntityMapper } from "@jojoxd/tsed-entity-mapper";
import { MyEntity } from "../entity/my-entity";
import { MyEntityRepository } from "../entity/repository/my-entity-repository";

@EntityMapper(MyEntity)
export class MyEntityMapper implements EntityMapperMethods<MyEntity>
{
    // EntityMapper is an injectable, so Inject works here

    @Inject()
    protected entityRepository: MyEntityRepository;

    async map(value: unknown, ctx: EntityMapperMapContext<MyEntity>): Promise<MyEntity>
    {
        return this.entityRepository.find({ id: value });
    }
}

Using an Entity Mapper in a controller

Entity mappers are used when you specify a parameter decorator:

// Using the above defined Entity Mapper

import { Get, Post } from "@tsed/schema";
import { Controller } from "@tsed/common";
import { BodyParamEntity, QueryParamEntity, PathParamEntity } from "@jojoxd/tsed-entity-mapper";
import { MyEntity } from "../entity/my-entity";

@Controller('/')
export class MyController
{
    @Get('/test')
    getMyEntity(@QueryParamEntity('id') entity: MyEntity)
    {
        // entity will be resolved as a MyEntity
    }

    @Post('/test')
    async postMyEntity(@BodyParamEntity() entity: MyEntity)
    {
        // entity will be resolved as a MyEntity
    }
    
    @Get('/by-id/:id')
    async getById(@PathParamEntity('id') entity: MyEntity)
    {
        // entity will be resolved as a MyEntity
    }
}

Using the Entity Mapper itself:

It currently is not possible to use the Entity Mapper by itself.

Roadmap

  • Better Exception Handling
  • Extract mapping functionality from EntityMapperPipe into a usable service