Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple ids to a connect on Prisma

Reading the prisma docs i find that is possible to create a connection where i make the plan have one item connected (as i did below). but i want to dinamic pass an array of strings (that items prop) that have ids of items to connect when creating my plan.

The code below works well, but i dont know how to pass that array n connect every item that match one of the ids on the array

const plan = await this.connection.create({
      data: {
        name,
        description,
        type,
        picture,
        productionPrice,
        price,
        items: {
          connect: [
            {
              id: items,
            },
          ],
        },
      },
      include: {
        items: true,
      },
    });

    return plan;
like image 749
Fox-Carlinhos Avatar asked Sep 06 '25 03:09

Fox-Carlinhos


1 Answers

I think, you have to provide an array like this:

const plan = await this.connection.create({
      data: {
        name,
        description,
        type,
        picture,
        productionPrice,
        price,
        items: {
          connect: [
            {
              id: 1,
            },
            {
              id: 2,
            },
            {
              id: 3,
            },
          ],
        },
      },
      include: {
        items: true,
      },
    });

If items contains a list of IDs, you can use:

...
        items: {
          connect: items.map(id => ({ id }),
        },
...
like image 163
some-user Avatar answered Sep 07 '25 19:09

some-user