1.1.1 • Published 2 years ago

dto-mapping v1.1.1

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

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

1.1.1

2 years ago

1.0.23

2 years ago

1.1.0

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago