create Method - Data Model Repository Reference

The create method of a data model repository creates one or more records of the data model.

Note: This reference assumes you've already resolved the data model repository, as explained in the Data Model Repository Reference documentation.

create Parameters#

Loading...

Create Records#

Note: As of Medusa v2.11.0, MikroORM dependencies are included in the @medusajs/framework package. If you're using an older version of Medusa, change the import statement to @mikro-orm/knex.
Code
1import { 2  InjectTransactionManager,3  MedusaContext,4  MedusaService,5  InjectManager,6} from "@medusajs/framework/utils"7import { Context, InferTypeOf, DAL } from "@medusajs/framework/types"8import { EntityManager } from "@medusajs/framework/mikro-orm/knex"9import Post from "./models/post"10
11class BlogModuleService extends MedusaService({12  Post,13}){14  // ...15  @InjectTransactionManager()16  protected async doSomething_(17    id: string,18    @MedusaContext() sharedContext?: Context<EntityManager>19  ): Promise<any> {20    const posts = await this.postRepository_.create([21      {22        title: "My Post",23        content: "This is a post",24        author_id: "01JSGRGQ8S61SR0Y7VCRV8FH66",25      },26    ])27
28    return posts29  }30
31  @InjectManager()32  async doSomething(33    id: string,34    @MedusaContext() sharedContext?: Context<EntityManager>35  ): Promise<any> {36    return await this.doSomething_(id, sharedContext)37  }38}

Parameters#

The create method accepts an array of objects, each representing a record to create. The shape of the objects is the same as the shape of the data model.

Returns#

The method returns an array of the created records.

Was this page helpful?
Ask Bloom
For assistance in your development, use Claude Code Plugins or Docs 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
⇧↵