npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@roit/roit-model-mapper

v0.0.10

Published

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

Downloads

692

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"

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

Convert any object to model


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

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

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

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' } }
*/