Skip to content

Webpack and require(sequelize.config.dialectModulePath) #9608

Closed

Description

@catamphetamine

We've enabled AWS X-Ray SDK in our project to benchmark and trace SQL calls.

It requires this:

var AWSXRay = require('aws-xray-sdk');
var mysql = AWSXRay.captureMySQL(require('mysql2'));

So you do have sequelize.config.dialectModulePath for a custom (wrapped) mysql2 instance but it is fully dynamic:

if (sequelize.config.dialectModulePath) {
  require(sequelize.config.dialectModulePath)
}

It won't work in Webpack because Webpack won't bundle anything for such require and will only throw "Module not found: mysql2".

Now, we've worked around it in a hacky way by using string-replace-loader which is set up to patch the library:

const sequelizeMySQLDialectStuffPath = path.resolve(basePath, 'node_modules/sequelize/lib/dialects/mysql/connection-manager.js');
if (!fs.existsSync(sequelizeMySQLDialectStuffPath)) {
  throw new Error(`[build error] Sequelize MySQL dialect file not found: ${sequelizeMySQLDialectStuffPath}`);
}
const sequelizeMySQLDialectStuff = fs.readFileSync(sequelizeMySQLDialectStuffPath, 'utf-8');
const sequelizeMySQLDialectStuffRequire = 'require(sequelize.config.dialectModulePath)';
if (sequelizeMySQLDialectStuff.indexOf(sequelizeMySQLDialectStuffRequire) < 0) {
  throw new Error(`[build error] Sequelize MySQL dialect file doesn't match.`);
}

{
  test: /\.js$/,
  include: sequelizeMySQLDialectStuffPath,
  use: [{
    loader: 'string-replace-loader',
    options: {
      search: sequelizeMySQLDialectStuffRequire,
      replace: `require("${path.resolve(basePath, '/mysql2-wrapped-with-aws-xray.js').replace(/\\/g, '\\\\')}")`,
    }
  }]
}

So as you can see it's hacky and a bit fragile.

You could support something like this instead:

if (sequelize.config.dialectModulePath) {
  require(sequelize.config.dialectModulePath)
} else if (sequelize.config.dialectModule) {
  // use the `sequelize.config.dialectModule` directly.
}

And the config would be:

{
  options: {
    dialectModule: require('path/to/custom/dialect.js')
  }
}

Activity

  1. added
    type: feature
    on Jun 28, 2018
  2. sushantdhiman commented on Jun 28, 2018

    @sushantdhiman
    Contributor

    Will accept PR

  3. catamphetamine commented on Jun 28, 2018

    @catamphetamine
    ContributorAuthor

    @sushantdhiman Created a PR: #9609

  4. yvele commented on Aug 29, 2018

    @yvele

    Why not passing directly the module with a new option sequelize.config.dialectModule? 🤔

    Then instead of requiring the module it self, Sequelize will also be able to directly use the module.

    I'm also struggling setting Sequelize with Webpack (and AWS X-Ray)

    Edit: Oops that EXACTLY what @catamphetamine is proposing 😄

  5. sushantdhiman commented on Aug 29, 2018

    @sushantdhiman
    Contributor

    Anyone want this feature can takeover #9609 and complete tests #9609 (comment) , I'll merge it

  6. sallyzoughaib commented on Dec 1, 2023

    @sallyzoughaib

    @catamphetamine @pellejacobs @sushantdhiman @jonmagic @sushantdhiman I am minifying and bundling a Node.js TypeScript backend project that uses Sequelize to connect with a database. I used Webpack for this task and successfully generated the 'bundle.js' file. However, when I ran 'node dist/bundle.js,' the server started, but any request requiring a database connection resulted in an error. After debugging and investigating, I discovered that I am missing Sequelize configurations in my 'web.config.js' file. Could you please help me identify what's missing?
    this is my webpack.config.js:

    var path = require('path');
    var fs = require('fs');
    var nodeModules = {};
    fs.readdirSync('node_modules')
      .filter(function (x) {
        return ['.bin'].indexOf(x) === -1;
      })
      .forEach(function (mod) {
        nodeModules[mod] = 'commonjs ' + mod;
      });
    
    module.exports = {
      mode: 'development',
      target: 'node',
      entry: './src/server.ts',
      output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist'),
      },
      resolve: {
        extensions: ['.ts', '.js'],
        alias: {
          '@': path.resolve(__dirname, 'src'),
          '@config': path.resolve(__dirname, 'src/config'),
          '@controllers': path.resolve(__dirname, 'src/controllers'),
          '@dtos': path.resolve(__dirname, 'src/dtos'),
          '@exceptions': path.resolve(__dirname, 'src/exceptions'),
          '@interfaces': path.resolve(__dirname, 'src/interfaces'),
          '@middlewares': path.resolve(__dirname, 'src/middlewares'),
          '@models': path.resolve(__dirname, 'src/models'),
          '@routes': path.resolve(__dirname, 'src/routes'),
          '@services': path.resolve(__dirname, 'src/services'),
          '@utils': path.resolve(__dirname, 'src/utils'),
          '@sentry/profiling-node': path.resolve(__dirname, 'node_modules/@sentry/profiling-node'),
        },
      },
      module: {
        rules: [
          {
            test: /\.ts$/,
            use: 'ts-loader',
            exclude: /node_modules/,
          },
          {
            test: /\.node$/,
            loader: 'node-loader',
          },
        ],
      },
      externals: [nodeModules],
      devtool: 'source-map',
    };
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions