# @roit/roit-model-mapper

> ROIT model mapper makes it easy to convert any object or JSON to the model

Latest version **0.0.10** (published 2021-09-09) · 0 weekly downloads

## Install

```sh
npm install @roit/roit-model-mapper
pnpm add @roit/roit-model-mapper
yarn add @roit/roit-model-mapper
bun add @roit/roit-model-mapper
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.10 |
| Published | 2021-09-09 |
| First published | 2019-11-14 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 38.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | ROIT |
| Maintainers | jeremias.silva, bertbr, karlaugust1, santanajonathan, yucky4, tiagodev, jenkins.roit, caiof, tiago.gois, evelin.iurko.roit |
| Keywords | typescript model mapper, express model mapper, typescript model convert, json-mapper, typescript-json, json-adapter, json-transformer, api-mapper, api-adapter |

## Links

- npm: https://www.npmjs.com/package/@roit/roit-model-mapper
- Repository: https://github.com/roitinnovation/roit-model-mapper
- Homepage: https://github.com/roitinnovation/roit-model-mapper#readme
- Issues: https://github.com/roitinnovation/roit-model-mapper/issues
- npm.io page: https://npm.io/package/@roit/roit-model-mapper

## Dependencies (3)

- [express](https://npm.io/package/express.md) ^4.17.1
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13
- [normalize-html-whitespace](https://npm.io/package/normalize-html-whitespace.md) ^1.0.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.0.10 (latest) — 2021-09-09
- 0.0.9 — 2020-05-23
- 0.0.8 — 2020-05-23
- 0.0.7 — 2020-02-28
- 0.0.6 — 2020-01-26
- 0.0.5 — 2020-01-26
- 0.0.4 — 2020-01-26
- 0.0.3 — 2019-12-03
- 0.0.2 — 2019-11-14
- 0.0.1 — 2019-11-14

## README

# ROIT model mapper
ROIT model mapper makes it easy to convert any object or JSON to the model

## Configure tsconfig

Add in file tsconfig.json attributes "experimentalDecorators" and "emitDecoratorMetadata"

```JSON
{
  "compilerOptions": {
    [...]
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    [...]
}
```

## Convert any object to model

```javascript

import { ModelMapper, JsonProperty } from "@roit/roit-model-mapper";

/**
 * Important: in the class all attributes must be initialized either with value or undefined, otherwise Mapper will not fill the attribute 
 */

// Models
export class Company {

    name: string = undefined

    identity: string = undefined

    @JsonProperty({ clazz: Address })
    address: Address = undefined
}

export class Address {

    country: string = undefined

    city: string = undefined

    @JsonProperty("street_address")
    streetAddress: string = undefined
}

let anyCompany = {
    name: "Company 1 SM inc",
    identity: "58.413.609/0001-72",
    address: {
        'street_address': 'R Argentina',
        city: 'Curitiba',
        country: 'Brasil'
    }
}

/**
 * ModelMapper
 * 1. Accept a simple object
 * 2. Accept array and return array
 * 3. Accept JSON string
*/

// Param 1: Model class, Param 2: any object, list or JSON string
const company = ModelMapper.deserialize(Company, anyCompany)

// Output
/**
  Company {
  name: 'Company 1 SM inc',
  identity: '58.413.609/0001-72',
  address:
   Address {
     country: 'Brasil',
     city: 'Curitiba',
     streetAddress: 'R Argentina' } }
 */
```

## Decorator JsonProperty

It has the purpose of informing some specific configuration for the attribute

```javascript
import { JsonProperty } from '@roit/roit-model-mapper';

// Mapper find in JSON or any object the property with name "street_address" and set value in "streetAddress"
@JsonProperty("street_address")
streetAddress: string = undefined

// Indicates the model class for Mapper initialize
@JsonProperty({ clazz: Address })
address: Address = undefined

// Finding attributes in class root for inicialize address
@JsonProperty({ linear: true })
address: Address = undefined

// Example

// Attributes linear
let anyCompany = {
    name: "Company 1 SM inc",
    identity: "58.413.609/0001-72",
    street_address: 'R Argentina',
    city: 'Curitiba',
    country: 'Brasil'
}

// Mark property linear
@JsonProperty({ linear: true })
address: Address = undefined

const company = ModelMapper.deserialize(Company, anyCompany)

// Output
/**
  Company {
  name: 'Company 1 SM inc',
  identity: '58.413.609/0001-72',
  address:
   Address {
     country: 'Brasil',
     city: 'Curitiba',
     streetAddress: 'R Argentina' } }
 */

```

## ObjectMapperOptions

Options for mapping the model

```javascript
import { ObjectMapperOptions } from '@roit/roit-model-mapper';

// SingleResult: return alwaeys a object
const company = ModelMapper.deserialize(Company, jsonObjectList, { singleResult: true })

// CompareWithAttributesLowerCase: compare attributes the JSON and Model in LowerCase
const company = ModelMapper.deserialize(Company, jsonObjectList, { compareWithAttributesLowerCase: true })

// NormalizeString: remove white spaces in strings
const company = ModelMapper.deserialize(Company, jsonObjectList, { normalizeString: true })

// IgnoreJsonPropertyName: ignore the name in @JsonProperty and uses name in class
const company = ModelMapper.deserialize(Company, jsonObjectList, { ignoreJsonPropertyName: true })

```

## Express integration

ModelMapper converts req.body to model using express middleware

```javascript
import { modelMapperMiddleware, ModelMapperRequest } from '@roit/roit-model-mapper';

// Step by Step

// Step 1: Import middleware controller and bodyParser.json()
var app = express();
app.use(bodyParser.json())
app.use(modelMapperMiddleware)

//  Step 2: Import ModelMapperRequest
app.post('mapper', function (req: ModelMapperRequest, res, next) {
  
  // Invoke bodyToObject method for mapping the object
  const company = req.mapper.bodyToObject(Company)

  console.log(company)

  res.send(company);
});

/**
 Company {
  name: 'Company 1 SM inc',
  identity: '58.413.609/0001-72',
  address:
   Address {
     country: 'Brasil',
     city: 'Curitiba',
     streetAddress: 'R Argentina' } }
*/
```

---
_Source: https://npm.io/package/@roit/roit-model-mapper · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
