0.0.4 • Published 2 years ago

prisma-extensions v0.0.4

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

My YouTube: Fedevcoding 🫂

Installation

To use Prisma Kit in your project, follow these steps:

  1. Install prisma extensions using npm or yarn:
npm i prisma-extensions
  1. Import the function you need and add $extends(function) when you are creating the prisma client:
import { updateManyArray } from "prisma-extensions";
export const DB = new PrismaClient().$extends(updateManyArray);

Function: updateManyArray

The updateManyArray function allows you to update multiple rows in a Prisma model using an array of conditions and data to be updated. It is particularly useful when you need to perform bulk updates on arrays and handle complex conditional cases.

Supported datasources

  • postgresql
  • mysql
  • sqlserver
  • sqlite
  • cockroachdb

Parameters

  • conditions: An array of objects with the following structure:

    • where: An object representing the unique condition to select rows for update. The condition is specified based on the model's unique fields.
    • data: An object containing the fields to be updated and their new values.
  • nameIdentifier: A string used for logging purposes to identify the update operation.

Usage

import { dbClient } from "@/config/db";

const conditions = [
  {
    where: { id: 1 },
    data: { name: "new name", status: "active" },
  },
  {
    where: { age: 22 },
    data: { items: "new name 2", status: "inactive" },
  },
  // Add more update conditions here...
];

dbClient.profile.updateManyArray({
  conditions,
  nameIdentifier: "profiles",
});

Note: in where and data you CAN'T pass objects to properties like in prisma :

THIS WON'T WORK

  {
    where: { age: {in: [10, 12]} }, // ERROR: can't pass objects
    data: { items: {set: "hello" } }, // ERROR: can't pass objects
  },

Function: updateManyArray

This function is currently supported but not documented here yet. Docs coming soon.

Authors