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" }, ], });
GitHub
github.com › prisma › prisma › issues › 24294
`createManyAndReturn()` not getting recognized/available · Issue #24294 · prisma/prisma
May 26, 2024 - 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 references to it but its not available to me. I have tried to wipe my node_modules and restarted my vscode and everything and still not available. All the other methods work. Setup a nodejs serverless project · Install prisma 5.14.0 ·
Author prisma
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
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
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
createManyAndReturn for Microsoft SQL Server
Add createManyAndReturn for SQL Server connectors, using OUPUT Inserted.* in the generated query (instead of RETURNING * in other connectors). Homebrew solutions suggested in #8131 have a noticeable performance hit when inserting and querying many rows. 👍React with 👍7jkomyno, MarkusLund, anPalmer, YaakovR, LRRetoura and 2 more ... domain/clientIssue in the "Client" domain: Prisma ... More on github.com
07:50
Prisma Tutorial for Beginners #3 - CRUD - Creating Records - YouTube
38:48
Prisma ORM Tutorial for Beginners | CRUD, CreateMany, Associations...
05:07
Prisma Tutorial for Beginners #4 - CRUD - Reading Records - YouTube
06:24
Implementing Bulk Inserts | Express API & Prisma ORM Query ...
Working With Prisma 2: Many-to-Many Relations
Prisma
prisma.io › home › prisma client api › prisma client api › prisma client api
Prisma Client API | Prisma Documentation
You cannot create or connect relations by using nested create, createMany, connect, connectOrCreate queries inside a top-level createManyAndReturn() query. See here for a workaround. When relations are included via include, a separate query is generated per relation. ... const users = await prisma.user.createManyAndReturn({ data: [ { name: "Sonali", email: "sonali@prisma.io" }, { name: "Alex", email: "alex@prisma.io" }, ], });
GitHub
github.com › prisma › prisma › issues › 24892
createManyAndReturn() return order · Issue #24892 · prisma/prisma
July 26, 2024 - 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 ...
Author prisma
GitHub
github.com › prisma › prisma › issues › 24727
createManyAndReturn for Microsoft SQL Server · Issue #24727 · prisma/prisma
July 8, 2024 - Add createManyAndReturn for SQL Server connectors, using OUPUT Inserted.* in the generated query (instead of RETURNING * in other connectors). Homebrew solutions suggested in #8131 have a noticeable performance hit when inserting and querying many rows. 👍React with 👍7jkomyno, MarkusLund, anPalmer, YaakovR, LRRetoura and 2 more ... domain/clientIssue in the "Client" domain: Prisma ...
Author prisma
Prisma
prisma.io › home › relation queries › relation queries › relation queries › relation queries
Relation queries (Concepts) | Prisma Documentation
You cannot access relations in a createMany() or createManyAndReturn() query, which means that you cannot create multiple users and multiple posts in a single nested write. The following is not possible: const createMany = await prisma.user.createMany({ data: [ { name: "Yewande", email: "yewande@prisma.io", posts: { // Not possible to create posts!
GitHub
github.com › prisma › prisma › issues › 8131
`createMany()` should return the created records · Issue #8131 · prisma/prisma
July 7, 2021 - 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 in some circumstances, meaning we must f...
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 moments in 2024", categoryId: categories.filter(category => category.name === 'Fun')!.id }, { title: "Linux or macOS — what's better?", categoryId: categories.filter(category => category.name === 'Technology')!.id }, { title: "Who will win the next soccer championship?", categoryId: categories.filter(category => category.name === 'Sports')!.id }] });
Author prisma
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 › 1326276619085545502
When using createManyAndReturn, am I guaranteed the results order to be the same as of the input? - Prisma
January 7, 2025 - Prisma•10mo agoDH · When using createManyAndReturn, am I guaranteed the results order to be the same as of the input? 3 Replies · Nurul•10mo ago · Hey 👋 no, because databases do not explicitly guarantee that themselves. The order of the returned data from createManyAndReturn() is not guaranteed to match the order of the input data.
Prisma
prisma.io › home › transactions and batch queries › transactions and batch queries › transactions and batch queries › transactions and batch queries
Transactions and batch queries (Reference) | Prisma Documentation
// Create user with posts in a single transaction const user = await prisma.user.create({ data: { email: "alice@prisma.io", posts: { create: [{ title: "Post 1" }, { title: "Post 2" }], }, }, }); These bulk operations run as transactions: createMany() / createManyAndReturn() updateMany() / updateManyAndReturn() deleteMany() Pass an array of queries to execute sequentially in a transaction: const [posts, totalPosts] = await prisma.$transaction([ prisma.post.findMany({ where: { title: { contains: "prisma" } } }), prisma.post.count(), ]); With options: await prisma.$transaction( [prisma.resource.d
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
Top answer 1 of 2
4
Ok I found that this was actually quite simple... just do it as usual insert and include the forign key id.
const result = await prisma.posts.createMany([
{
...postData,
userId,
},
]);
2 of 2
0
This better works on my end. https://www.prisma.io/docs/orm/reference/prisma-client-reference#create
async function main() {
let users: Prisma.UserCreateInput[] = [
{
email: '[email protected]',
userId: { connect: { id }
},
{
email: '[email protected]',
userId: { connect: { id }
},
]
await Promise.all(
users.map(async (user) => {
await prisma.user.create({
data: user,
})
})
)
}