dto-mapping

A nice greeThe dto-mapping is a mapping library to facilitate the conversion of an entity bean to a similar DTO (Data Transfer Object) bean or reverseter

Usage no npm install needed!

<script type="module">
  import dtoMapping from 'https://cdn.skypack.dev/dto-mapping';
</script>

README

Welcome to dto-mapping 👋

Version Documentation License: ISC Twitter: hoangnd_58

A nice greeThe dto-mapping is a mapping library to facilitate the conversion of an entity bean to a similar DTO (Data Transfer Object) bean or reverseter

🏠 Homepage

Demo

Install

npm install dto-mapping

Usage

Create some DTO class:

  @Entity()
  class Info {
    @SafeType({ type: Number })
    age?: Number
    @SafeType({ type: Number })
    numbers?: Number[]
    showAge() {
      console.log(`i'm ${this.age}`)
    }
  }

  @Entity()
  class User {
    @SafeType({ type: String })
    name?: String
    @SafeType({ type: Info })
    info?: Info
    constructor(obj: any) {}
  }

Create an model is implemented by the User class:

  const model = { 
    name: "1", 
    info: { 
        age: '1', 
        numbers: [1, 2, "3"] 
      } 
  }

Create DTO instance:

  const user = new User(model)

Then you can use method of info entity:

  user.info.showAge()

Feature

  • Convert model to DTO "@SafeType"
  • Transform data "Transform"

Sample:

  @Entity()
    class Location {
        code: string = ''
        constructor(model: any) {}
        getLocation = () => `My location is ${this.code}`
    }
    @Entity()
    class User {
        @Transform({fn: (model: any, field: string) => model[field] 
            && model[field]
            .split(',')
            .map((code: string) => ({
                code
            }))})
        @SafeType({type: Location})
        locations: Location[] = []
        constructor(model: any) {}
    }

Read more : https://github.com/hoangdevelopers/dto-mapping/blob/main/src/tests/transform.test.ts

The feature will be implemented in the next version

  • omit properties
  • rewrite properties name

sample

Run tests

npm run test

Author

👤 Hoang Nguyen

Show your support

Give a ⭐️ if this project helped you!


This README was generated with ❤️ by readme-md-generator