list Method - Service Factory Reference

This method retrieves a list of records.

Retrieve List of Records#

Code
const posts = await postModuleService.listPosts()

If no parameters are passed, the method returns an array of the first 15 records.


Filter Records#

Code
1const posts = await postModuleService.listPosts({2  id: ["123", "321"],3})

Parameters#

To retrieve records matching a set of filters, pass an object of the filters as a first parameter.

NoteLearn more about accepted filters in this documentation.

Returns#

The method returns an array of the first 15 records matching the filters.


Retrieve Relations#

NoteThis applies to relations between data models of the same module. To retrieve linked records of different modules, use Query.
Code
1const posts = await postModuleService.listPosts({}, {2  relations: ["author"],3})

Parameters#

To retrieve records with their relations, pass as a second parameter an object having a relations property. relations's value is an array of relation names.

Returns#

The method returns an array of the first 15 records matching the filters.


Select Properties#

Code
1const posts = await postModuleService.listPosts({}, {2  select: ["id", "name"],3})

Parameters#

By default, retrieved records have all their properties. To select specific properties to retrieve, pass in the second object parameter a select property.

select's value is an array of property names to retrieve.

Returns#

The method returns an array of the first 15 records matching the filters.


Paginate Relations#

Code
1const posts = await postModuleService.listPosts({}, {2  take: 20,3  skip: 10,4})

Parameters#

To paginate the returned records, the second object parameter accepts the following properties:

  • take: a number indicating how many records to retrieve. By default, it's 15.
  • skip: a number indicating how many records to skip before the retrieved records. By default, it's 0.

Returns#

The method returns an array of records. The number of records is less than or equal to take's value.


Sort Records#

Code
1const posts = await postModuleService.listPosts({}, {2  order: {3    name: "ASC",4  },5})

Parameters#

To sort records by one or more properties, pass to the second object parameter the order property. Its value is an object whose keys are the property names, and values can either be:

  • ASC to sort by this property in the ascending order.
  • DESC to sort by this property in the descending order.

Returns#

The method returns an array of the first 15 records matching the filters.

Was this page helpful?
Edit this page
Ask Anything
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