restore Method - Data Model Repository Reference
The restore method of a data model repository restores one or more records of the data model that were previously soft-deleted.
This means that the deleted_at property of the records is set to null.
Note: This reference assumes you've already resolved the data model repository, as explained in the Data Model Repository Reference documentation.
restore Parameters#
Loading...
Restore Record by ID#
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.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 restoredPosts, 22 restoredEntities,23 ] = await this.postRepository_.restore(id)24 25 return restoredPosts26 }27 28 @InjectManager()29 async doSomething(30 id: string,31 @MedusaContext() sharedContext?: Context<EntityManager>32 ): Promise<any> {33 return await this.doSomething_(id, sharedContext)34 }35}
Parameters#
The method can accept the ID of a record to restore as a string.
Returns#
The method returns a tuple with two elements:
- The first element is an array of the restored records.
- The second element is an object whose keys are the names of restored data models, and values an array of restored record objects.
- This is useful if records from other data models were previously soft-deleted due to cascading soft-deletes, and are now restored.
For example:
1[2 [3 {4 "id": "01JSHAW6Z7KW4X6E8MFPGNEKHC",5 "title": "My first post",6 "content": "This is my first post",7 "metadata": null,8 "product_id": null,9 "product_ids": [10 "prod_01JP4M5T5K55P2JN1F17EY7B2T"11 ],12 "author_id": "01JSHAW6YZNKR65TJ242GCMD2S",13 "author": "01JSHAW6YZNKR65TJ242GCMD2S",14 "created_at": "2025-04-23T12:44:59.751Z",15 "updated_at": "2025-04-23T12:44:59.751Z",16 "deleted_at": "2025-04-23T12:45:08.750Z"17 }18 ],19 {20 "Post": [21 {22 "id": "01JSHAW6Z7KW4X6E8MFPGNEKHC",23 "title": "My first post",24 "content": "This is my first post",25 "metadata": null,26 "product_id": null,27 "product_ids": [28 "prod_01JP4M5T5K55P2JN1F17EY7B2T"29 ],30 "author_id": "01JSHAW6YZNKR65TJ242GCMD2S",31 "author": "01JSHAW6YZNKR65TJ242GCMD2S",32 "created_at": "2025-04-23T12:44:59.751Z",33 "updated_at": "2025-04-23T12:44:59.751Z",34 "deleted_at": "2025-04-23T12:45:08.750Z"35 }36 ]37 }38]
Restore Multiple Records By IDs#
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 ids: string[],18 @MedusaContext() sharedContext?: Context<EntityManager>19 ): Promise<any> {20 const [21 restoredPosts, 22 restoredEntities,23 ] = await this.postRepository_.restore(ids)24 25 return restoredPosts26 }27 28 @InjectManager()29 async doSomething(30 ids: string[],31 @MedusaContext() sharedContext?: Context<EntityManager>32 ): Promise<any> {33 return await this.doSomething_(ids, sharedContext)34 }35}
Parameters#
The restore method can accept an array of record IDs to restore.
Returns#
The method returns a tuple with two elements:
- The first element is an array of the restored records.
- The second element is an object whose keys are the names of restored data models, and values an array of restored record objects.
- This is useful if records from other data models were previously soft-deleted due to cascading soft-deletes, and are now restored.
For example:
1[2 [3 {4 "id": "01JSHAW6Z7KW4X6E8MFPGNEKHC",5 "title": "My first post",6 "content": "This is my first post",7 "metadata": null,8 "product_id": null,9 "product_ids": [10 "prod_01JP4M5T5K55P2JN1F17EY7B2T"11 ],12 "author_id": "01JSHAW6YZNKR65TJ242GCMD2S",13 "author": "01JSHAW6YZNKR65TJ242GCMD2S",14 "created_at": "2025-04-23T12:44:59.751Z",15 "updated_at": "2025-04-23T12:44:59.751Z",16 "deleted_at": "2025-04-23T12:45:08.750Z"17 }18 ],19 {20 "Post": [21 {22 "id": "01JSHAW6Z7KW4X6E8MFPGNEKHC",23 "title": "My first post",24 "content": "This is my first post",25 "metadata": null,26 "product_id": null,27 "product_ids": [28 "prod_01JP4M5T5K55P2JN1F17EY7B2T"29 ],30 "author_id": "01JSHAW6YZNKR65TJ242GCMD2S",31 "author": "01JSHAW6YZNKR65TJ242GCMD2S",32 "created_at": "2025-04-23T12:44:59.751Z",33 "updated_at": "2025-04-23T12:44:59.751Z",34 "deleted_at": "2025-04-23T12:45:08.750Z"35 }36 ]37 }38]
Restore Records by Filters#
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 restoredPosts, 22 restoredEntities,23 ] = await this.postRepository_.restore({24 title: "My Post",25 })26 27 return restoredPosts28 }29 30 @InjectManager()31 async doSomething(32 ids: string[],33 @MedusaContext() sharedContext?: Context<EntityManager>34 ): Promise<any> {35 return await this.doSomething_(ids, sharedContext)36 }37}
Parameters#
The restore method can accept an object of filters to select the records to restore.
In the example above, you pass an object with the title property to restore all records with that title.
Note: Find examples of different filters in the Filter Records documentation.
Returns#
The method returns a tuple with two elements:
- The first element is an array of the restored records.
- The second element is an object whose keys are the names of restored data models, and values an array of restored record objects.
- This is useful if records from other data models were previously soft-deleted due to cascading soft-deletes, and are now restored.
For example:
1[2 [3 {4 "id": "01JSHAW6Z7KW4X6E8MFPGNEKHC",5 "title": "My first post",6 "content": "This is my first post",7 "metadata": null,8 "product_id": null,9 "product_ids": [10 "prod_01JP4M5T5K55P2JN1F17EY7B2T"11 ],12 "author_id": "01JSHAW6YZNKR65TJ242GCMD2S",13 "author": "01JSHAW6YZNKR65TJ242GCMD2S",14 "created_at": "2025-04-23T12:44:59.751Z",15 "updated_at": "2025-04-23T12:44:59.751Z",16 "deleted_at": "2025-04-23T12:45:08.750Z"17 }18 ],19 {20 "Post": [21 {22 "id": "01JSHAW6Z7KW4X6E8MFPGNEKHC",23 "title": "My first post",24 "content": "This is my first post",25 "metadata": null,26 "product_id": null,27 "product_ids": [28 "prod_01JP4M5T5K55P2JN1F17EY7B2T"29 ],30 "author_id": "01JSHAW6YZNKR65TJ242GCMD2S",31 "author": "01JSHAW6YZNKR65TJ242GCMD2S",32 "created_at": "2025-04-23T12:44:59.751Z",33 "updated_at": "2025-04-23T12:44:59.751Z",34 "deleted_at": "2025-04-23T12:45:08.750Z"35 }36 ]37 }38]
Was this page helpful?