dto-mapping
    TypeScript icon, indicating that this package has built-in type declarations

    1.1.0 • Public • Published

    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

    Install

    npm i dto-mapping

    DownloadsWeekly Downloads

    111

    Version

    1.1.0

    License

    ISC

    Unpacked Size

    12.9 kB

    Total Files

    20

    Last publish

    Collaborators

    • hoangdevelopers
    • thaibm