Api to fetch data from all collections and store as a json file in any frontend project

System Information
  • Strapi Version: 4.5.4
  • Operating System: Windows 11
  • Database: postgres (hosted)
  • Node Version: v16.13.0
  • NPM Version: 8.1.0
  • Yarn Version: 3.3.0 | 3.2.4 (different machines/ docker setup)

We have set up strapi as .ts to be used as BFF (backend for frontend) and all is working good.
Now looking into fault tolerance options for the same.
Was thinking of an api | script that will fetch all the data from all OR specified collections and store as .JSON file in my node server OR frontend side OR Docker where my main backend server is running.

I realize, there will be data size limit in this case. so for a small data size of 5-10 collections.
1-Does Strapi provide anything like this built-in (at least )?
2-I will have to write a script to hit all APIs and store the response?

Reason for doing this
Since our main website is Work In Progress, there are times when collections and relations have to be modified, following which Strapi server restarts. during this time the users get no data text and is left blank. eg: when user visits ‘FAQ page’ there are no question answers list. This exists for few minutes while the strapi server is restarting.
Now imagine this with Dashboard, Main pages/sections that user interacts.

any help | ideas related to this are welcome

1 Like

Since nothing of sort was found in strapi as per my search,
I opted to create a custom script to do the same.
its working well.
Now attempting to modify the strapi response structure, as i do not want response to be as {id:1,attributes: {…xyz…}} and prefer {id:1, …{…xyz…}}.
Will have to modify the default controllers. Have done a poc previously so this should be easier.

eg:
current default controller:
export default factories.createCoreController("api::student-list.student-list);

New controller will be:
filename: student-list/controllers/student-list.js

"use strict";
const { createCoreController } = require("@strapi/strapi").factories;
module.exports = createCoreController(
  "api::student-list.student-list",
  ({ strapi }) => ({
    async customAction(ctx) {
      try {
        ctx.body = "ok";
      } catch (err) {
        ctx.body = err;
      }
    },
    async customActionPost(ctx) {
      try {
        ctx.body = "ook";
      } catch (err) {
        ctx.body = err;
      }
    },
    async findOne(ctx) {
      try {
        const { id } = ctx.params;
        const { query } = ctx;
        const entity = await strapi
          .service("api::student-list.student-list")
          .findOne(id, query);

// probably do not require sanitizedOutput() and/or transformResponse()
        const sanitizedEntity = await this.sanitizeOutput(entity, ctx);
        const resp = this.transformResponse(sanitizedEntity);

        return resp;
      } catch (err) {
        ctx.body = err;
      }
    },
    async find(ctx) {
      try {
        const { query } = ctx;
        const entity = await strapi
          .service("api::student-list.student-list")
          .findOne(query);

        const sanitizedEntity = await this.sanitizeOutput(entity, ctx);

        const resp = this.transformResponse(sanitizedEntity);

        return resp;
      } catch (err) {
        ctx.body = err;
      }
    },
  })
);

New routes will be:
filename: student-list/routes/custom.js

module.exports = {
  routes: [
    {
      method: "GET",
      path: "/custom", // use this instead of /student-list
      handler: "student-list.customAction",
      config: {
        auth: false,
      },
    },
    {
      method: "POST",
      path: "/custom-post", // use this instead of /student-list with type POST
      handler: "student-list.customActionPost",
      config: {
        auth: false,
      },
    },
  ],
};