@aegenet/belt-rows-inflator
v2.3.0
Published
Rows Inflator, transforms SQL results into nested objects
Downloads
88
Readme
@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
}
]
}
]