upsertWithReplace Method - Data Model Repository Reference

The upsertWithReplace method of a data model repository can do three things:

  • Create a new record.
  • Update an existing record.
  • Delete a record related to the record being upserted. If a one-to-many or many-to-many relation is omitted in the new data of a record, the method will delete the related records if they belong to the record being upserted.

This method is useful when you're doing complex upserts with relations, or you want details of the operations performed. Otherwise, use the upsert method.

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

upsertWithReplace Parameters#

Loading...

Upsert 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 {21      entities,22      performedActions,23    } = await this.postRepository_.upsertWithReplace([24      {25        id: "01JSHAW6Z7KW4X6E8MFPGNEKHC",26        title: "My Old Post",27        author_id: null,28      },29      {30        id: "123",31        title: "My New Post",32      },33    ])34
35    return {36      entities,37      performedActions,38    }39  }40
41  @InjectManager()42  async doSomething(43    id: string,44    @MedusaContext() sharedContext?: Context<EntityManager>45  ): Promise<any> {46    return await this.doSomething_(id, sharedContext)47  }48}

Parameters#

The upsertWithReplace method accepts an array of objects, each representing a record to create or update.

If a record object has an id property, the method will try to update a record with that id. Otherwise, the method will create a new record.

If the id property is specified but no record with that id exists, the created record will have the specified id.

Each object has the same shape as the data model.

Returns#

The method returns an object with two properties:

  • entities: An array of the created or updated records.
  • performedActions: An object with three properties:
    • created: An object whose keys are the names of data models, and values are arrays of the records that were created.
    • updated: An object whose keys are the names of data models, and values are arrays of the records that were updated.
    • deleted: An object whose keys are the names of data models, and values are arrays of the records that were deleted.

For example:

Example
1{2  "entities": [3    {4      "id": "01JSHAW6Z7KW4X6E8MFPGNEKHC",5      "title": "My Old Post",6      "author_id": null7    },8    {9      "id": "123",10      "title": "My New Post"11    }12  ],13  "performedActions": {14    "created": {15      "Post": [16        {17          "id": "123"18        }19      ]20    },21    "updated": {22      "Post": [23        {24          "id": "01JSHAW6Z7KW4X6E8MFPGNEKHC"25        }26      ]27    },28    "deleted": {29
30    }31  }32}
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
⇧↵