@b8/model-transformer
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

Model Transformer

Motivation

Transform Model Objects to Response JSON.

Cases

  1. Pass key to value mapping
  2. Pass function to transform data independently
  3. Pass function to transform based on the data in model itself

How to install package

npm install --save @b8/model-transformer

Example

Model Value

let model = {
  _id: 1,
  cognito_id: "asdasf",
  username: "mango",
  sensitiveData: "helloWorld",
  permissions: "not_admin",
  wallets: [
    {
      id: "21",
      wallet: "asd"
    }
  ],
  partners: ["2"],
  time: new Date()
};

To transform to suppose API response use it as:

import {Transform} from "@b8/model-transformer"

const result = Transform(
  [
    ["_id", "id"],
    ["cognito_id", "cognitoId"],
    [
      "sensitiveData",
      "sensitiveData",
      model => (model["permissions"] == "admin" ? model["sensitiveData"] : null)
    ],
    ["time", "lastSeen"],
    ["partners", "partner", () => "The Partner"],
    ["wallets.0.id", "wallets.0.k"]
  ],
  model
);

The resulting api response will be

{
  id: 1,
  cognitoId: 'asdasf',
  sensitiveData: null,
  lastSeen: 2019-05-22T11:48:56.014Z,
  partner: 'The Partner',
  wallets: [ { k: '21' } ]
}

Cases handled:

  1. Key transformed ( "_id" to "id" )
  2. Nested keys transformed ("wallets.0.id" to "wallets.0.k")
  3. Functions used to transform value. See permissions

One last case which isn't seen above is adding true as last parameter which will add all remaining values from model to api object. It's set by default as false and omits all the remaining values (which in our hope should be the desired behaviour)

Package Sidebar

Install

npm i @b8/model-transformer

Weekly Downloads

2

Version

1.0.3

License

none

Unpacked Size

5.23 kB

Total Files

7

Last publish

Collaborators

  • funktrust
  • mrjackdavis
  • rastogiabhijeet