diff --git a/.babelrc b/.babelrc index 9aa643c0a..b88442426 100644 --- a/.babelrc +++ b/.babelrc @@ -10,5 +10,13 @@ ], "ignore": [ "src/assets" - ] + ], + "overrides": [{ + "test": "./src/helpers/import-helper.js", + "presets": [ + ["@babel/env", { + "exclude": ["proposal-dynamic-import"], + }] + ], + }] } diff --git a/package.json b/package.json index db99c8881..487c17a2d 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "pg": "latest", "pg-hstore": "latest", "prettier": "^2.4.1", + "semver": "^7.3.5", "sequelize": "^6.9.0", "sqlite3": "latest", "through2": "^4.0.2" diff --git a/src/helpers/config-helper.js b/src/helpers/config-helper.js index 17b9ac6ea..17296c56c 100644 --- a/src/helpers/config-helper.js +++ b/src/helpers/config-helper.js @@ -5,6 +5,7 @@ import _ from 'lodash'; import { promisify } from 'util'; import helpers from './index'; import getYArgs from '../core/yargs'; +import importHelper from './import-helper'; const args = getYArgs().argv; @@ -15,18 +16,19 @@ const api = { init() { return Promise.resolve() .then(() => { - let config; - if (args.url) { - config = api.parseDbUrl(args.url); + return api.parseDbUrl(args.url); } else { - try { - config = require(api.getConfigFile()); - } catch (e) { - api.error = e; - } + return importHelper.importModule(api.getConfigFile()); + } + }) + .then((module) => module.default) + .catch(() => { + try { + return require(api.getConfigFile()); + } catch (e) { + api.error = e; } - return config; }) .then((config) => { if (typeof config === 'object' || config === undefined) { diff --git a/src/helpers/import-helper.js b/src/helpers/import-helper.js new file mode 100644 index 000000000..3ab0d4dde --- /dev/null +++ b/src/helpers/import-helper.js @@ -0,0 +1,5 @@ +module.exports = { + importModule: function (module) { + return import(module); + }, +}; diff --git a/test/db/migrate.test.js b/test/db/migrate.test.js index 384796629..755d7219a 100644 --- a/test/db/migrate.test.js +++ b/test/db/migrate.test.js @@ -3,6 +3,7 @@ const expect = require('expect.js'); const Support = require(__dirname + '/../support'); const helpers = require(__dirname + '/../support/helpers'); const gulp = require('gulp'); +const semver = require('semver'); const _ = require('lodash'); [ @@ -293,6 +294,54 @@ describe(Support.getTestDialectTeaser('db:migrate'), () => { }); }); +describeOnlyForESM(Support.getTestDialectTeaser('db:migrate'), () => { + describe('with config.mjs', () => { + const prepare = function (callback) { + const config = helpers.getTestConfig(); + const configContent = 'export default ' + JSON.stringify(config); + let result = ''; + + return gulp + .src(Support.resolveSupportPath('tmp')) + .pipe(helpers.clearDirectory()) + .pipe(helpers.runCli('init')) + .pipe(helpers.removeFile('config/config.json')) + .pipe(helpers.copyMigration('createPerson.js')) + .pipe(helpers.overwriteFile(configContent, 'config/config.mjs')) + .pipe(helpers.runCli('db:migrate --config config/config.mjs')) + .on('error', (e) => { + callback(e); + }) + .on('data', (data) => { + result += data.toString(); + }) + .on('end', () => { + callback(null, result); + }); + }; + + it('creates a SequelizeMeta table', function (done) { + prepare(() => { + helpers.readTables(this.sequelize, (tables) => { + expect(tables).to.have.length(2); + expect(tables).to.contain('SequelizeMeta'); + done(); + }); + }); + }); + + it('creates the respective table', function (done) { + prepare(() => { + helpers.readTables(this.sequelize, (tables) => { + expect(tables).to.have.length(2); + expect(tables).to.contain('Person'); + done(); + }); + }); + }); + }); +}); + describe(Support.getTestDialectTeaser('db:migrate'), () => { describe('with config.json and url option', () => { const prepare = function (callback) { @@ -525,3 +574,11 @@ describe(Support.getTestDialectTeaser('db:migrate'), () => { }); }); }); + +function describeOnlyForESM(title, fn) { + if (semver.satisfies(process.version, '^12.20.0 || ^14.13.1 || >=16.0.0')) { + describe(title, fn); + } else { + describe.skip(title, fn); + } +}