Excluding any discussion about performance, I think each type of transaction is a tradeoff of convenience vs. flexibility. So I would approach it like this:
- if you can use one of the high-level
doManyfunctions, use that. Its clear the intent and the code is straightforward. - If you need to create multiple things at once and ensure rollback if any of them fail, but they are not all of the same type, then use
prisma.transaction([]). you simply pass in all the calls you want without awaiting them. - If you need even more complex logic, possibly other checks in between step in the transaction or possibly throwing an error, use what prisma calls "interactive" transactions. This is the more traditional approach where all work is done inside of a callback:
await prisma.$transaction(async (tx) => {
// use tx instead of prisma
await tx.users.create()
await tx.emails.create()
I think which one is "best" is determined by which one you are able to use for your use-case, the more complex the problem you will have to use the option that supports it.
In terms of performance, its not clear from the docs if a batch op (e.g. createMany) is more performant. The docs only state that the batch is ran in a single transaction, not a single query (e.g. COPY)
Prisma
prisma.io › home › crud › crud › crud › crud
CRUD (Reference) | Prisma Documentation
const users = await prisma.user.createManyAndReturn({ data: [ { name: "Alice", email: "alice@prisma.io" }, { name: "Bob", email: "bob@prisma.io" }, ], }); See Nested writes for creating records with relations.
node.js - Prisma CreateMany Vs Transaction - Stack Overflow
As of Prisma version 5.14.0 you can createMany and return all created records directly using createManyAndReturn() More on stackoverflow.com
`createMany()` should return the created records
Initially brought up here: #4998 (comment) Problem The fact that the createMany API does not return the created records (which should be possible for Postgres) makes it difficult/impossible to use ... More on github.com
createManyAndReturn return order
My suggestion is the following, we can update the workaround example like this · const categories = await prisma.category.createManyAndReturn({ data: [ { name: 'Fun', }, { name: 'Technology', }, { name: 'Sports', } ], select: { id: true } }); const posts = await prisma.post.createManyAndReturn({ ... More on github.com
createManyAndReturn() return order
Bug description I looked at the Prisma Client API Reference and noticed there wasn't any information on the order of the entries returned from the createManyAndReturn() function. This could be ... More on github.com
GitHub
github.com › prisma › prisma › issues › 8131
`createMany()` should return the created records · Issue #8131 · prisma/prisma
July 7, 2021 - const inputs: Array<{post: PostData, comments: Array<CommentData>}> = [...]; const posts = prisma.post.createMany({data: [...]}); const commentCreateData = inputs.map((input, index) => input.comments.map((commentData) => ...))).flat(); ...
Author prisma
GitHub
github.com › prisma › docs › issues › 6746
createManyAndReturn return order · Issue #6746 · prisma/docs
March 17, 2025 - const categories = await prisma.category.createManyAndReturn({ data: [ { name: 'Fun', }, { name: 'Technology', }, { name: 'Sports', } ], select: { id: true } }); const posts = await prisma.post.createManyAndReturn({ data: [{ title: "Funniest ...
Author prisma
GitHub
github.com › prisma › prisma › issues › 24294
`createManyAndReturn()` not getting recognized/available · Issue #24294 · prisma/prisma
May 26, 2024 - Bug description When I try to implement createManyAndReturn in my code after updating to 5.14.0 the ts types and autocomplete is not available. When I looked at the compiled code I do see reference...
Author prisma
YouTube
youtube.com › prisma
Bulk Inserting Data with Prisma's createMany Method - YouTube
New at Prisma 2.16 is the createMany method. This method allows developers to bulk insert data instead of looping over a dataset and creating records one at ...
Published February 5, 2021 Views 28K
GitHub
github.com › prisma › prisma › issues › 24892
createManyAndReturn() return order · Issue #24892 · prisma/prisma
July 26, 2024 - I make my prisma call: const res = prisma.favorite_colors.createManyAndReturn({ data: newData })
Author prisma
GitHub
github.com › prisma › prisma › issues › 25290
createManyAndReturn fails with @ignore fields (field must be a known scalar or virtual) · Issue #25290 · prisma/prisma
September 26, 2024 - The elements are successfully created and returned. Included in the reproduction. ... generator client { provider = "prisma-client-js" } model User { id String @id @default(cuid()) email String @unique ignored String?
Author prisma
DEV Community
dev.to › endykaufman › update-to-prisma-5180-in-typegraphql-prisma-nestjs-27co
Update to Prisma 5.18.0 in typegraphql-prisma-nestjs - DEV Community
August 8, 2024 - You can expect breaking changes in the generated schema because of that 🚨 The new createManyAndReturn Prisma operation is fully supported in this release 🎉 The supported Node.js version has been bumped to v20.11.1. Updates to generated code in example: https://github.com/EndyKaufman/typegraphql-prisma-nestjs-example/commit/29523859e0efb585fb7679bb519d588a2a09a4f9 ·
Answer Overflow
answeroverflow.com › m › 1242853891775795372
Using createManyAndReturn - Prisma
May 22, 2024 - Hi! I'm using Prisma with SQLite, and the LibSQL adapter, and I've just updated to the latest version. I see in the docs that I should have access to `createManyAndReturn` since I am using SQLite, but it does not appear to be an option in my editor. Would the LibSQL adapter prevent me from ...
Answer Overflow
answeroverflow.com › m › 1248343778771865611
Help Needed with Nested CreateMany! - Prisma
June 6, 2024 - 3. Creates an AssignmentSet for the classroom. 4. Iterates over the students to: • Create an Assignment for each student. • Create a Portfolio for each student. • Connect the Assignment to the AssignmentSet. I’m encountering issues with Prisma’s lack of support for nested CreateMany.