Fetching many to many along with some aggregation using select

I am trying to fetch many to many relation with drizzle select(). I need to use it because I also want to aggregate a value in nested relation ship Here is the schema

  const results = await db
    .select({
    })
    .from(BatchModel)
    .fullJoin(BatchToCustomer, eq(BatchModel.id, BatchToCustomer.batch_id))
    .fullJoin(CustomerModel, eq(BatchToCustomer.customer_id, CustomerModel.id))
    .groupBy(BatchModel.id)
    .all();


Tried several things but could not wrap my head around how to return something like below

[
  {
    name: "batch name",
    customers: [
      {
        name: "customer name",
        total: "item's total",
        items: [
          {
            name: "",
            price: "",
          },
        ],
      },
    ],
  },
];
Was this page helpful?