Is there a way way to request all entries of a table?

In old versions of Strapi, we could use ?limit=-1 to get all entries of a table. However, this apparently does not work anymore, although I’ve not really tried it yet.

Right now, to get all data, I just have the following in my config/api.js

module.exports = {
  rest: {
    defaultLimit: 10000000,
    maxLimit: 10000000,
    withCount: true
  }
};

Now, in my case, I don’t have more than 2k entries in any of the tables and I don’t expect to get to 1 million any time soon, but still would like to be able to get all entries, even if I have more entries than the defaultLimit or maxLimit.

Here REST API | Strapi Documentation I couldn’t find a solution. After having read this Parameters | Strapi Documentation, it seems that I may be able to use the pagination parameter to get all data, but I am not sure if this pagination parameter can really be used for that or is used only to break the maximum number of entries we may get (as specified in config/api.js) into pages.

What’s the correct way of getting all entries from a table in Stapi 4.16.2 (preferably, with one request, but it’s ok if I also need to make more)?

Could this maybe be done with a custom route, which, on the backend side, takes care of getting all data? I suppose so. If anyone has a concrete code example, it would be great.

Note I can’t update the Strapi version because newer versions removed a feature that I need (i.e. there’s yet another bug in Strapi).

Yes, I’ve already gone through Get all records from collection. I’d like to do this without the help of third-party tools or plugins

I attended the Discord meetup today with this exact question!

  1. Loop through multiple requests until all of the data is returned back. (Discord meetup suggested solution)
  2. Increase the limit in the ./config/api.js Discord question

Example for #1:

When we make a request to a given table we get back the following:

Response object: Has a “data” property with an array of objects, and a “meta” property object with some information example: { pagination: { page: 1, pageSize: 25, pageCount: 2, total: 27 } }

One of the ways of solving this would be to create a loop over requests & responses and store the page we are on here is a javascript example:


const axios = require('axios');
const qs = require('qs');

// API token for authentication
const apiToken = abc123

// Base URL for the API
const baseURL = 'http://localhost:1337/api';

// Headers for API requests, including authorization
const headers = {
  'Authorization': `Bearer ${apiToken}`
};

// Function to fetch a single page of data
const fetchPage = async (endpoint, page) => {
    try {
        const url = new URL(`${baseURL}/${endpoint}`);
        url.search = qs.stringify({
            pagination: { page: page }
        });

        console.log(`Fetching ${url}`);

        const response = await axios.get(url.toString(), { headers });
        return response.data;
    } catch (error) {
        console.error(`Error fetching ${endpoint} page ${page}:`, error.message);
        return null;
    }
};

// Function to fetch all data from a specific endpoint
const fetchAllData = async (endpoint) => {
    let allData = [];
    let currentPage = 1;
    let hasNextPage = true;

    while (hasNextPage) {
        const pageData = await fetchPage(endpoint, currentPage);
        
        if (!pageData) {
            break;
        }

        allData = [...allData, ...pageData.data];
        
        hasNextPage = currentPage < pageData.meta.pagination.pageCount;
        currentPage++;
    }

    return allData;
};

// Function to fetch data from both tables and create JSON files
const fetchAllDataAndDoSomethingWithIt = async () => {
    // Fetch data from your desired table *remember it should be the plural name*
    const yourTableName = await fetchAllData('yourTableName');
    
    console.log('All operations completed.');
};
fetchAllDataAndDoSomethingWithIt()

I would love to hear if anyone else has found another way to retrieve all of the entries of a table!

1 Like