try to use Shopify CLI (ES6) with Sequelize(common js) database

umairakram
Shopify Partner
17 1 5

I'm going to use Shopify CLI with Sequelize database but it's not compatible with ES6.

 

 I tried to change the Sequelize (ES5, common js) files to the ES6 module but it's giving me problems. If you have any suggestions or you did this type of work ES5/commonjs to ES6 module. Please help. 

 

'bundle.js'  Sequelize model file.

'use strict';
import { Model } from 'sequelize';
export default (sequelize, DataTypes) => {
  class bundle extends Model {
    static associate(models) {
      // define association here
    }
  };
  bundle.init({
    storeId: DataTypes.BIGINT,
    name: DataTypes.STRING,
    qty: DataTypes.INTEGER,
    price: DataTypes.STRING,
  }, {
    sequelize,
    modelName: 'bundle',
  });
  return bundle;
};

'index.js'  Sequelize configuration file.

'use strict';

import fs from 'fs';
import path from 'path';
import Sequelize from 'sequelize';
import dbConfig from '../config/config.js';
import * as url from 'url';

const __filename = url.fileURLToPath(import.meta.url);
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

const env = process.env.NODE_ENV || 'development';
const config = dbConfig[env]
const basename = path.basename(__filename);
const models = {};

let sequelize;
if (config.use_env_variable) {
  sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
  .readdirSync(__dirname)
  .filter(file => {
    return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
  })
  .forEach(async file => {
    const model = await import(path.join('file://', __dirname, file))
    models[model.name] = model;
  });

Object.keys(models).forEach(modelName => {
  if (models[modelName].associate) {
    models[modelName].associate(models);
  }
});

models.sequelize = sequelize;
export default models; 

 

Reply 1 (1)

AndyHung
Shopify Partner
60 0 16

Hello @umairakram  ,

I'm facing the same your issue. Did you have any solution? Please help me

Best Regards