New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

nested-response

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nested-response

Make nested response from flat records

latest
Source
npmnpm
Version
1.1.0
Version published
Maintainers
1
Created
Source

Nested Response

Make nested response from flat records

Installation

$ npm install nested-response

Usage

const nestedResponse = require('nested-response');

// This is a sample of flat records that we're going to make it nested
// You might get these records from anywhere (i.e. SQL databases)
const records = [
  {
    storeId: 1,
    storeName: 'store A',
    productId: 1,
    productName: 'product A',
  }
  {
    storeId: 1,
    storeName: 'store A',
    productId: 2,
    productName: 'product B',
  }
  {
    storeId: 2,
    storeName: 'store B',
    productId: 3,
    productName: 'product C',
  }
];

// You need to define a pattern for nesting
// Also have some options for customizing the output
const definition = [{
  storeId: 'storeId',
  storeName: 'storeName',
  products: [{
    id: 'productId',
    name: 'productName',
  }]
}];

const nestedResult = nestedResponse(records, definition);

// nestedResult: [
//   {
//     storeId: 1,
//     storeName: 'store A',
//     products: [
//       {
//         id: 1,
//         name: 'product A',
//       },
//       {
//         id: 2,
//         name: 'product B',
//       }
//     ]
//   },
//   {
//     storeId: 2,
//     storeName: 'store B',
//     products: [
//       {
//         id: 3,
//         name: 'product C',
//       }
//     ]
//   }
// ];

The primary key is defined the first property in each level of nesting (by default)

Options

You also have some options to customize the output

For example:

You can change the primary key field:

  storeName: { $column: 'storeName', $pk: true }

You can also change the type of value or define a default value if the field doesn't exist on the record:

  prodSerial: { $column: 'productSerialNumber', $type: 'number', $default: 123456 }

If you're going to use these options, the $column property is mandatory

Keywords

nested

FAQs

Package last updated on 16 Nov 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts