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
npm install dto-mapping
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()
- 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
- omit properties
- rewrite properties name
npm run test
๐ค Hoang Nguyen
- Website: https://hoangdevelopers.github.io/
- Twitter: @hoangnd_58
- Github: @hoangdevelopers
- LinkedIn: @hoang-nguyen-bb95b8111
Give a โญ๏ธ if this project helped you!
This README was generated with โค๏ธ by readme-md-generator