Giter Club home page Giter Club logo

dto-mapping's Introduction

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

dto-mapping's People

Contributors

hoangdevelopers avatar thaibuiminhncc avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.