transformInputsToPrisma

Transform relation input data to handle it in prisma syntax using connect, disconnect methods

Usage

import { transformInputsToPrisma } from '@ra-libs/nestjs'; 

@Injectable()
export class Service {

  private relations: string[] = ["clients"]

  async update(id: string, newSale: UpdateSaleDto) {
    const oldSale = await this.findOne(id)
    const updateData = transformInputsToPrisma(newSale, oldSale, this.relations) as any
    return this.prisma.sale.update({
      where: { id },
      data: updateData,
    });
  }

}

In the example above it will transform the clients relations in the sale object from

{
  "clients": ["<id>"]
}

to

{
  "clients": {
    "connect": [
      {"id": "<id>"}
    ]
  }
}

If the oldSale has more than one clients and the newSale does not have (the user deleted some users from the sale) then the transformInputsToPrisma will add the removed clients to the disconnect field

{
  "clients": {
    "disconnect": [
      { "id": "<id_1>" }
    ],
    "connect": [
      { "id": "id_2" }
    ]
  }
}

Last updated