2.2.0 • Published 9 months ago

@aegenet/belt-rows-inflator v2.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

npm version

@aegenet/belt-rows-inflator

Rows Inflator, transforms SQL results into nested objects

💾 Installation

yarn add @aegenet/belt-rows-inflator@^2.0.0
# or
npm i @aegenet/belt-rows-inflator@^2.0.0

📝 Usage

import { rowsInflator, ERowsInflatorAssociation } from '@aegenet/belt-rows-inflator';

const raws = [
  { id: 1, code: 'Luna', users__id: 1, users__code: 'wismerhill', users__subordinates__id: 3, users__subordinates__code: 'maurel' },
  { id: 2, code: 'Sun', users__id: 2, users__code: 'pileouface', users__subordinates__id: null, users__subordinates__code: null },
  { id: 1, code: 'Luna', users__id: 3, users__code: 'maurel', users__subordinates__id: null, users__subordinates__code: null },
];

const results = rowsInflator(raws, {
  as: 'company',
  fields: {
    code: {},
    id: {},
  },
  associations: {
    users: {
      as: 'users',
      associationType: ERowsInflatorAssociation.hasMany,
      fields: {
        code: {},
        id: {},
      },
      associations: {
        subordinates: {
          as: 'subordinates',
          associationType: ERowsInflatorAssociation.hasMany,
          fields: {
            code: {},
            id: {},
          },
        },
      },
    },
  },
});

Results

[
  {
    "code": "Luna",
    "id": 1,
    "users": [
      {
        "code": "wismerhill",
        "id": 1,
        "subordinates": [
          {
            "code": "maurel",
            "id": 3
          }
        ]
      },
      {
        "code": "maurel",
        "id": 3
      }
    ]
  },
  {
    "code": "Sun",
    "id": 2,
    "users": [
      {
        "code": "pileouface",
        "id": 2
      }
    ]
  }
]
2.2.0

9 months ago

2.1.0

11 months ago

1.6.2

12 months ago

1.7.0

12 months ago

2.0.0

12 months ago

1.5.0

1 year ago

1.4.2

1 year ago

1.4.1

1 year ago

1.4.0

1 year ago

1.3.2

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

1.2.0

2 years ago