From 3cfd11f1e679b00d094a369a689e13bdda92b5a2 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Tue, 30 May 2023 09:53:32 -0500 Subject: [PATCH] Add Batch Insert Records With createMany as a Prisma TIL --- README.md | 3 +- .../batch-insert-records-with-create-many.md | 41 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 prisma/batch-insert-records-with-create-many.md diff --git a/README.md b/README.md index 15c61e3..43aa1f1 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1305 TILs and counting..._ +_1306 TILs and counting..._ --- @@ -742,6 +742,7 @@ _1305 TILs and counting..._ ### Prisma - [Apply Separate Formatting With A Blank Line](prisma/apply-separate-formatting-with-a-blank-line.md) +- [Batch Insert Records With createMany](prisma/batch-insert-records-with-create-many.md) - [Execute A Raw SQL Query](prisma/execute-a-raw-sql-query.md) - [Grab A Limited Set Of Records](prisma/grab-a-limited-set-of-records.md) - [Specify Alternate Location For Prisma Schema](prisma/specify-alternate-location-for-prisma-schema.md) diff --git a/prisma/batch-insert-records-with-create-many.md b/prisma/batch-insert-records-with-create-many.md new file mode 100644 index 0000000..81f966f --- /dev/null +++ b/prisma/batch-insert-records-with-create-many.md @@ -0,0 +1,41 @@ +# Batch Insert Records With createMany + +As part of its suite of CRUD functionality, [Prisma has a `createMany` +function](https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#createmany) +that allows you to `insert` many records at once with your target database. +This will perform one large insert statement which will generally be faster +than an equivalent series of individual insert statements. + +```javascript +const createResult = await prisma.books.createMany({ + data: [ + { isbn: '123', title: 'The Goldfinch' }, + { isbn: '345', title: 'Piranesi' }, + { isbn: '987', title: 'The Fifth Season' }, + ], + skipDuplicates: true +}) +``` + +With the `skipDuplicates` option, any inserts that would result in a duplicate +record (`isbn` is my unique key in this example) will be skipped. + +The result of the query will include a `count` key to let you know how many +records were actually inserted. + +If I'm bulk inserting a _ton_ of data, I like to chunk it up so that I'm not +creating queries that are too big. For a recent script, I found that `1000` was +a good chunking number. + +```javascript +import 'chunk' from 'lodash/chunk' + +const chunkedBatchInsert = async (records) => { + for(const batch of chunk(records, 1000)) { + await prisma.books.createMany({ + data: batch, + skipDuplicates: true + }) + } +} +```