1.0.32 ā¢ Published 2 months ago
@tamnt-work/data-mapper v1.0.32
Install
yarn add @tamnt-work/data-mapper
Usage with CLI
Create custom config
npx tw init
We will create a tw-config.json
file in the root of your project.
Default config:
{
"modulePath": "/app",
"modelSuffix": ".model",
"mapperSuffix": ".mapper",
"entitySuffix": ".entity",
"overwrite": false
}
Create schema
npx tw schema init
We will create a schema/schema.tws
file in your project.
Define schema
Example:
user:
id: string <=> id
fullName: string <=> name
username: string
email: string
phoneNumber: string <=> phone
companyName: string <=> company.name
address: string <=> address.street.name
age: number
post:
id: string
title: string <=> name
content: string <=> body
views: number
createdAt: string <=> date
Generate with schema
npx tw schema generate
Output
user.entity.ts
export interface UserEntity {
id: string;
name: string;
username: string;
email: string;
phone: string;
age: number;
company: {
name: string;
};
address: {
street: {
name: string;
};
};
}
user.model.ts
export interface UserModel {
id: string;
fullName: string;
username: string;
email: string;
phoneNumber: string;
companyName: string;
address: string;
age: number;
}
user.mapper.ts
import { Mapper, type TransformationMap } from "@tamnt-work/data-mapper";
import type { UserEntity } from "./user.entity";
import type { UserModel } from "./user.model";
const transformationMap: TransformationMap<UserModel, UserEntity> = {
id: "id",
fullName: "name",
username: "username",
email: "email",
phoneNumber: "phone",
companyName: "company.name",
address: "address.street.name",
age: "age",
};
export const UserMapper = new Mapper<UserEntity, UserModel>(transformationMap);
Example
Auto suggestion with key and value
Usage with code
const data = {
id: "1",
name: "Leanne Graham",
username: "Bret",
email: "Sincere@april.biz",
address: {
street: "Kulas Light",
suite: "Apt. 556",
city: "Gwenborough",
zipcode: "92998-3874",
geo: {
lat: "-37.3159",
lng: "81.1496",
},
},
phone: "1-770-736-8031 x56442",
website: "hildegard.org",
company: {
name: "Romaguera-Crona",
catchPhrase: "Multi-layered client-server neural-net",
bs: "harness real-time e-markets",
},
};
const model = UserMapper.toModel(data);
// Output
{
"id": "1",
"companyName": "Romaguera-Crona",
"email": "Sincere@april.biz",
"fullName": "Leanne Graham",
"phoneNumber": "1-770-736-8031 x56442",
"username": "Bret"
}
const entity = UserMapper.toEntity(model);
// Output
{
"id": "1",
"company": {
"name": "Romaguera-Crona"
},
"email": "Sincere@april.biz",
"name": "Leanne Graham",
"phone": "1-770-736-8031 x56442",
"username": "Bret"
}
Method
Mapper
toArrayModel
toArrayModel(entity: Entity[]): Model[]
toEntityArray
toEntityArray(model: Model[]): Entity[]
toModel
toModel(entity: Entity): Model
toEntity
toEntity(model: Model): Entity
Author
š¤ TamNT
Email: contact@tamnt.work
Show your support
Give a āļø if this project helped you!
1.0.19
2 months ago
1.0.17
2 months ago
1.0.16
2 months ago
1.0.22
2 months ago
1.0.21
2 months ago
1.0.20
2 months ago
1.0.25
2 months ago
1.0.24
2 months ago
1.0.23
2 months ago
1.0.29
2 months ago
1.0.28
2 months ago
1.0.27
2 months ago
1.0.32
2 months ago
1.0.31
2 months ago
1.0.30
2 months ago
1.0.9
2 months ago
1.0.8
2 months ago
1.0.7
2 months ago
1.0.6
2 months ago
1.0.11
2 months ago
1.0.10
2 months ago
1.0.15
2 months ago
1.0.14
2 months ago
1.0.13
2 months ago
1.0.12
2 months ago
1.0.5
2 months ago
1.0.4
2 months ago
1.0.3
2 months ago
1.0.2
2 months ago
1.0.1
2 months ago
1.0.0
2 months ago