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

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