Search code examples
node.jsexpressfeathersjs

Get a reference to knex client in feathers app


How can I get a reference to knexClient that the app uses, when I have a test.js file that exports multiple functions? For instance, test.js could be used to perform scheduled operations to the database and it's not part of a service.

    module.exports = {

      const knex = app.get('knexClient');

      doThings: function (params) {  
       //I NEED KNEX HERE
       return something;
      },

      doThings2: function () {
        return somethingElse
      }
    };

Solution

  • In a generated application src/app.js (see here) exports the app object:

    const app = require('./app');
    const knex = app.get('knexClient');
    
    module.exports = {
      doThings: function (params) {  
        //I NEED KNEX HERE
        return something;
      },
    
      doThings2: function () {
        return somethingElse
      }
    };