Search code examples
knex.jsstrapi

Can I get configs outside strapi runtime?


I'm implementing knex migrations on my api and I plan on running them on Heroku release phase (before the new version starts).

Is there any way I can get Strapi's parsed configs from my knexfile.js?

Something like this would be great:

const strapi = require('strapi');
strapi.config.database;

The problem is that I don't wanna duplicate my configs, and requiring the config/environments/xxx/database.json won't give me the "parsed" results. Meaning "${process.env.DATABASE_HOST}" won't be interpolated yet.


Solution

  • TLDR;

    You can get Strapi's config this way:

    const strapi = require('strapi')({ serveAdminPanel: false });
    
    strapi.load()
      .then(() => {
        const config = strapi.config.currentEnvironment;
      });
    

    But you can get Strapi's knex instance directly this way:

    const strapi = require('strapi')({ serveAdminPanel: false });
    
    strapi.load()
      .then(() => {
        const knex = strapi.connections[
          strapi.config.currentEnvironment.database.defaultConnection
        ];
    
        // do anything with knex here
        // like knex.migrate.latest();
      });
    

    --

    I tried to get the config first, but when I did I kept getting connection error so I managed to get the knex connection directly and things started working!