prisma1 icon indicating copy to clipboard operation
prisma1 copied to clipboard

add createMany mutation

Open marktani opened this issue 7 years ago • 32 comments

Feature Request

A createManyXs mutation to create many nodes in a batched fashion.

Suggested API:

mutation {
  createManyItems(data: [{
    name: "Tesla"
    price: "1000"
  }, {
    name: "Audi"
    price: "2"
  }]) {
    ids # list of ids for newly created nodes 
    count
  }
}

marktani avatar Mar 27 '18 08:03 marktani

Where would i look in order to implement this? After all, most of the groundwork for this seems done and things like TypeCreateManyInput already exist when you create nested resources.

I tried to find "CreateManyInput" via code search, but turned up empty-handed. Not sure if i was even searching the right repo.

codepunkt avatar Jul 03 '18 08:07 codepunkt

any updates on this feature?

marcosfede avatar Oct 12 '18 18:10 marcosfede

Is hard to implement? I think this will be very useful.

dortamiguel avatar Oct 19 '18 14:10 dortamiguel

Any progress?

FluorescentHallucinogen avatar Dec 13 '18 11:12 FluorescentHallucinogen

Are there plans to tackle this any time soon? Or at least some help from the prima team as to how it should be tackled for contributors to work on this?

marcosfede avatar Jan 11 '19 14:01 marcosfede

I'm missing not having this after 3 hours of trying out Prisma.

devuxer avatar Feb 17 '19 21:02 devuxer

Hey guys! This looks pretty simple to implement since Prisma already have updateMany. What are the blockers?

lnmunhoz avatar Feb 26 '19 02:02 lnmunhoz

any updates on this?

andrejunges avatar Mar 19 '19 16:03 andrejunges

It would be very handy

MaksimKlepikov avatar May 05 '19 07:05 MaksimKlepikov

one of the first thing I am using prisma is the bulk import of csv rows, so bit saddened by unpresence of this feature

Swoorup avatar May 05 '19 16:05 Swoorup

I need this. Please hurry up

george1028 avatar May 08 '19 15:05 george1028

I need it tooooo :(

rollrodrig avatar May 10 '19 00:05 rollrodrig

bump

gotexis avatar Jun 15 '19 00:06 gotexis

any good workaround for this?

vladimirvolek avatar Jun 29 '19 00:06 vladimirvolek

I feel like this is such a must-have. Shame it wasn't added way sooner

mpcen avatar Jul 22 '19 04:07 mpcen

any updates on this?

HugoLiconV avatar Jul 28 '19 22:07 HugoLiconV

does anyone have a good workaround for this?

aarshaw avatar Aug 01 '19 07:08 aarshaw

does anyone have a good workaround for this?

It isn't pretty but I'm temporarily getting around it by building a dynamic template string that prepends an arbitrary unique ID to each data object getting inserted and calling the mutation method. Using axios for the POST, It looks something like this:

const players = [{id: 'a', name: 'playerA'}, {id: 'b', name: 'playerB'}]
let playerMutations = ``;

players.forEach(player => {
    playerMutations += `
        p_${player.id}: createPlayer (
            data: {
                name: "${player.name}"
            }
        ) {
            name
        }
    `;
});

const mutation = `
    mutation {
        ${playerMutations}
    }
`;

try {
    await axios({
        url: 'http://localhost:4000/default/dev',
        method: 'post',
        data: { query: mutation }
    });
} catch (e) {
    console.error('Error in PlayerServices run:', e.message);
    return [];
}

mpcen avatar Aug 02 '19 23:08 mpcen

@aarshaw check out my comment above

mpcen avatar Aug 02 '19 23:08 mpcen

@vladimirvolek @HugoLiconV @gotexis @george1028 @rollrodrig check out my comment above. temp solution that im currently using

mpcen avatar Aug 07 '19 19:08 mpcen

You can use raw access (edit prisma config rawAccess: true). This option provides $graphql function where you can insert many values in a single request.

docker-compose.yml

PRISMA_CONFIG: |
        port: 4466
        # uncomment the next line and provide the env var PRISMA_MANAGEMENT_API_SECRET=my-secret to activate cluster security
        # managementApiSecret: my-secret
        databases:
          default:
            connector: mysql
            host: mysql
            user: root
            password: prisma
            port: 3306
            rawAccess: true // HERE
            migrations: true

generated/prisma-client/index

export interface Prisma {
  $exists: Exists;
  $graphql: <T = any>(
    query: string,
    variables?: { [key: string]: any }
  ) => Promise<T>;

example

await prisma.$graphql(`
    mutation {
        executeRaw(
            query: "insert into \"default$default\".\"Address\" (\"address\", \"createdAt\", \"id\", \"updatedAt\") values ('00:01:cc:00:00:02', '2018-10-26T08:51:04.242Z', 'MDA6MDE6Y2M6MDA6MDA6MDIwMD', '2018-10-26T08:51:04.242Z')"
        )
    }
`);

more info https://www.prisma.io/forum/t/how-i-can-create-many-records-at-the-same-time-with-the-prisma-client/4723/5 https://github.com/prisma/prisma/issues/3300#issuecomment-430696015

vladimirvolek avatar Aug 25 '19 00:08 vladimirvolek

@vladimirvolek What about SQL injection while using the raw queries? Is prisma already handling it or should we implement it by ourself?

sapkra avatar Aug 25 '19 14:08 sapkra

@sapkra In this case you probably need to handle it yourself since Prisma is not aware of the semantics of the query you are dynamically building.

biels avatar Aug 30 '19 11:08 biels

It has been well over a year at this point, is there any dialogue on this?

bradleykhan avatar Oct 03 '19 14:10 bradleykhan

code u can do something like this 😉

ahmedB7r avatar Oct 16 '19 13:10 ahmedB7r

@ahmedB7r , that works when it's only a few items. But if someone is attempting to add > 1000 items, Prisma will begin rejecting the requests after the queue reaches 1000 to protect the database. A feature like createMany would allow Prisma to internally queue these requests and handle them as it sees fit with the possibility of providing updates as the queue is worked to completion.

technicallynick avatar Jan 06 '20 20:01 technicallynick

any update on this ?

younes200 avatar Jan 25 '20 17:01 younes200

Also waiting on this. Will it be a feature of Prisma2?

hatchli avatar Feb 26 '20 21:02 hatchli

Does this mean the team won't work on Prisma anymore? Now that Prisma2 is taking all of their attention?

Khareta avatar Mar 18 '20 08:03 Khareta

Exactly. That's how I understood it too.

sapkra avatar Mar 22 '20 05:03 sapkra