# compact-sequelize

> Compacts SEQUELIZE results to make them easier to manipulate and more readable

Latest version **1.0.1** (published 2022-02-10) · ISC license · 0 weekly downloads

## Install

```sh
npm install compact-sequelize
pnpm add compact-sequelize
yarn add compact-sequelize
bun add compact-sequelize
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2022-02-10 |
| First published | 2022-02-07 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 95.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Pablo Merener |
| Maintainers | pablo.merener.ok |
| Keywords | sequelize, compact, readable |

## Links

- npm: https://www.npmjs.com/package/compact-sequelize
- Repository: https://github.com/PabloMerener/compact-sequelize
- Homepage: https://github.com/PabloMerener/compact-sequelize#readme
- Issues: https://github.com/PabloMerener/compact-sequelize/issues
- npm.io page: https://npm.io/package/compact-sequelize

## Alternatives

- [byte-size](https://npm.io/package/byte-size.md) — 2.1M weekly downloads
- [speed-limiter](https://npm.io/package/speed-limiter.md) — 16.0K weekly downloads
- [@powersync/node](https://npm.io/package/@powersync/node.md) — 10.9K weekly downloads
- [@ledgerhq/coin-cardano](https://npm.io/package/@ledgerhq/coin-cardano.md) — 1.0K weekly downloads
- [@jayesol/jayeson.lib.streamfinder](https://npm.io/package/@jayesol/jayeson.lib.streamfinder.md) — 1.0K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2022-02-10
- 1.0.0 — 2022-02-07

## README

# compact-sequelize

```sh
npm install compact-sequelize
```
## Overview

<p> The goal of compact-sequelize is to make the objects returned by the sequelize ORM more readable and manageable. And secondly apply the toJSON method recursively.<p>

<p> We often want to get a single field from linked tables. Although sequelize allows us to achieve this, the resulting structure has unnecessary complexity.</p>

<p>As an example we will get orders and their details. For each order, the user (email), product detail, brand (name), gender (name), and product categories (name).</p>

<p align='center'>
    <img src='./erd.jpg'>
</p>

<p>The corresponging sequelize code</p>

```js
Order.findAll({
  include: [
    { model: User, attributes: ["email"] },
    {
      model: OrderDetail,
      include: [
        {
          model: Product,
          include: { model: Brand, attributes: ["name"] },
          include: { model: Gender, attributes: ["name"] },
          include: { model: Category, attributes: ["name"] },
        },
        { model: Size, attributes: ["name"] }
      ]
    }
  ],
})
```

and we get a json like this:

```js
[{
  "id": 42,
  "date": "2022-01-27T07:54:02.571Z",
  "status": "carrito",
  "userId": 12,
  "user": {
    "email": "andres@sportbase.com"
  },
  "OrderDetails": [
    {
      "id": 197,
      "price": 10999,
      "quantity": 2,
      "orderId": 42,
      "productId": "c2fcdddd-2ea4-465a-939a-63d91bb60aa0",
      "sizeId": 12,
      "product": {
        "id": "c2fcdddd-2ea4-465a-939a-63d91bb60aa0",
        "name": "Adidas jacket Firebird",
        "price": 10999,
        "genderId": 2,
        "brandId": 1,
        "brand": {
          "name": "Adidas"
        },
        "gender": {
          "name": "Woman"
        },
        "categories": [
          {
            "name": "jacket",
            "product_category": {
              "createdAt": "2022-01-20T02:05:12.939Z",
              "updatedAt": "2022-01-20T02:05:12.939Z",
              "productId": "c2fcdddd-2ea4-465a-939a-63d91bb60aa0",
              "categoryId": 5
            }
          },
          {
            "name": "sportswear",
            "product_category": {
              "createdAt": "2022-01-20T02:05:12.939Z",
              "updatedAt": "2022-01-20T02:05:12.939Z",
              "productId": "c2fcdddd-2ea4-465a-939a-63d91bb60aa0",
              "categoryId": 9
            }
          }
        ]
      },
      "size": {
        "name": "XL"
      }
    }
  ]
}]
```
<p>compact-sequelize is particularly useful in many-to-many relationships such as product and category relationships.</p>

<p>Finally we apply the compact function to the sequelize object/array.<p>

```js
const { compact } = require('compact-sequelize');
const { Order, User, OrderDetail, Size, Product, Gender, Brand, Category } = require("./src/db.js");

const getOrders = async () => {

  const orders = compact(await Order.findAll({
    include: [
      { model: User, attributes: ["email"] },
      {
        model: OrderDetail,
        include: [
          {
            model: Product,
            include: { model: Gender, attributes: ["name"] },
            include: { model: Brand, attributes: ["name"] },
            include: { model: Category, attributes: ["name"] }
          },
          { model: Size, attributes: ["name"] }
        ]
      }
    ],
  }));

  return orders;
}
```

<p>Next we get a more concise object</p>

```js
{
  "id": 42,
  "date": "2022-01-27T07:54:02.571Z",
  "userId": 12,
  "user": "andres@sportbase.com",
  "OrderDetails": [
    {
      "id": 197,
      "price": 10999,
      "quantity": 2,
      "orderId": 42,
      "productId": "c2fcdddd-2ea4-465a-939a-63d91bb60aa0",
      "sizeId": 12,
      "product": {
        "id": "c2fcdddd-2ea4-465a-939a-63d91bb60aa0",
        "name": "Adidas jacket Firebird",
        "price": 10999,
        "genderId": 2,
        "brandId": 1,
        "brand": "Adidas",
        "gender": "Woman",
        "categories": [
          "jacket",
          "sportswear"
        ]
      },
      "size": "XL"
    }
  ]
}
```

---
_Source: https://npm.io/package/compact-sequelize · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
