Home > Back-end >  How to add multiple ids to a connect on Prisma
How to add multiple ids to a connect on Prisma

Time:09-06

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;

CodePudding user response:

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 }),
        },
...
  • Related