From the course: Master Next.js by Building Scalable Apps with Routing, Databases, and Performance

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Inserting data using createMany()

Inserting data using createMany()

With the DB instance, we have another method that can be used for the data insertion. This method is called the createMany, and it can be used when you want to insert multiple records at once. Let's see the syntax. It is very similar to create method. So you have the db.tableName.createMany to call it and we pass the object with data property as an argument. Now with the create method, we passed an object in the data property, but here since we can insert multiple values, you actually can have an array of objects like this. Let's try this method as well. Right now we don't have the proper use case for inserting multiple values. So I'll manually enter three records into the products table. So in the code, I'll change the method name to createMany. Now we already have the product data assigned to the data property. So we will assign the array of objects to product data itself. I will comment this code and say const product data is equal to opening the array, opening the object literal…

Contents