Home > Mobile >  flattening sequelize query result
flattening sequelize query result

Time:08-17

I want to flatten this find query result. here is an example.

"uuid": "5d7c5571-9f39-45ba-93d5-27f91c3322d8",
        "type": "A",
        "number": "00001",
        "code": "978020137962",
        "matches": null,
        "title": "Generate Free Barcodes\n",
        "remark": "",
        "detail": null,
        "minStock": "0.000000",
        "freeStock": "5.000000",
        "realStock": "20.000000",
        "targetStock": "0.000000",
        "price": "156.45",
        "minPrice": "85.37",
        "recommendedPrice": null,
        "latestPrice": "5.00",
        "averagePrice": "51.26",
        "bulkPrices": null,
        "priceUnit": "1.000000",
        "packageUnit": "1.000000",
        "stockManagement": true,
        "createdOn": "2022-07-17T18:23:41.623Z",
        "modifiedOn": null,
        "unit": {
            "uuid": "c5327983-bc02-4350-bb45-cd3abbd2eb7d",
            "group": "Length",
            "name": "Foot",
            "abbreviation": "ft"
        },
        "bundleChildren": [],
        "productionChildren": [],
        "bundleParents": [
            {
                "quantity": "12.000000",
                "remark": "Testing child remark",
                "bundleParent": {
                    "uuid": "80b82a5a-de4b-4798-a4e6-7e3064edae91",
                    "type": "B",
                    "number": "AminB1",
                    "code": "21",
                    "unit": {
                        "name": "Meter",
                        "group": "Length",
                        "abbreviation": "m"
                    }
                }
            }
        ],

to have this as result: "bundleParents": [ { "uuid": "80b82a5a-de4b-4798-a4e6-7e3064edae91", "quantity": "12.000000", "remark": "Testing child remark", "type": "B", "number": "AminB1", "code": "21", "unit": "Meter", "abbreviation": "m" } ],

and here is my query in sequelize:

Product.findAndCountAll({
  where: condition,
  order: [['createdOn', 'DESC']],
  limit: getLimit(req),
  offset: getOffset(req),
  attributes: {exclude: ['unitId']},
  include: [
    {model: Unit, as: 'unit', attributes: {exclude: ['createdOn', 'modifiedOn']}},
    {model: BundleElement, as: 'bundleChildren', attributes: ["quantity", "remark"], },

    {model: ProductionElement, as: 'productionChildren', attributes: ["quantity", "remark"]},
    {model: BundleElement, as: 'bundleParents', attributes: ["quantity", "remark"],
      include: [{
        model: Product, as: "bundleParent", attributes: ['uuid', 'type', 'number', 'code'],
        include: [{model: Unit, as: 'unit', attributes: ['name', 'group', 'abbreviation']}]
      }]
    },
    {model: ProductionElement, as: 'productionParents', attributes: ["quantity", "remark"]},
  ],
  distinct:true
})

Can anyone help me with this?

CodePudding user response:

You should just simplify your query, remove unneeded attributes. I wouldn't try to do the transformation (flattening) inside the query though.

Once you get the result, just extract the fields you need and remap them to new objects.

// e.g. extract the bundleParents element
const bundleParents = [
{
  "quantity": "12.000000",
  "remark": "Testing child remark",
  "bundleParent": {
    "uuid": "80b82a5a-de4b-4798-a4e6-7e3064edae91",
    "type": "B",
    "number": "AminB1",
    "code": "21",
    "unit": {
      "name": "Meter",
      "group": "Length",
      "abbreviation": "m"
    }
  }
}];

// and then just remap it to flat objects
const flat = bundleParents.map((bp) => {
  const { quantity, remark, bundleParent } = bp;
  const { unit: { name: unit, abbreviation }, ...base } = bundleParent;
  return {...base, unit, abbreviation, quantity, remark};
});

console.log(flat);

  • Related