create Method - Service Factory Reference

This method of a module's service creates one or more records of a data model.

The method's name is of the format createDataModel, where DataModel is the plural pascal-case name of the data model.

Create One Record#

Code
1const post = await postModuleService.createPosts({2  name: "My Post",3  published_at: new Date(),4  metadata: {5     external_id: "1234",6  },7})

If an object is passed to the method, the created record object is returned.


Create Multiple Records#

Code
1const posts = await postModuleService.createPosts([2  {3    name: "My Post",4    published_at: new Date(),5  },6  {7    name: "My Other Post",8    published_at: new Date(),9  },10])

If an array of objects is passed to the method, an array containing the created records is returned.

Was this page helpful?
Ask Anything
Ask any questions about Medusa. Get help with your development.
You can also use the Medusa MCP server in Cursor, VSCode, etc...
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break