Skip to content

Commit e5cdab5

Browse files
committed
feat: added support for ESM config files
1 parent fcea7a3 commit e5cdab5

5 files changed

Lines changed: 83 additions & 10 deletions

File tree

.babelrc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,13 @@
1010
],
1111
"ignore": [
1212
"src/assets"
13-
]
13+
],
14+
"overrides": [{
15+
"test": "./src/helpers/import-helper.js",
16+
"presets": [
17+
["@babel/env", {
18+
"exclude": ["proposal-dynamic-import"],
19+
}]
20+
],
21+
}]
1422
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"pg": "latest",
3939
"pg-hstore": "latest",
4040
"prettier": "^2.4.1",
41+
"semver": "^7.3.5",
4142
"sequelize": "^6.9.0",
4243
"sqlite3": "latest",
4344
"through2": "^4.0.2"

src/helpers/config-helper.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import _ from 'lodash';
55
import { promisify } from 'util';
66
import helpers from './index';
77
import getYArgs from '../core/yargs';
8+
import importHelper from './import-helper';
89

910
const args = getYArgs().argv;
1011

@@ -15,18 +16,19 @@ const api = {
1516
init() {
1617
return Promise.resolve()
1718
.then(() => {
18-
let config;
19-
2019
if (args.url) {
21-
config = api.parseDbUrl(args.url);
20+
return api.parseDbUrl(args.url);
2221
} else {
23-
try {
24-
config = require(api.getConfigFile());
25-
} catch (e) {
26-
api.error = e;
27-
}
22+
return importHelper.importModule(api.getConfigFile());
23+
}
24+
})
25+
.then((module) => module.default)
26+
.catch(() => {
27+
try {
28+
return require(api.getConfigFile());
29+
} catch (e) {
30+
api.error = e;
2831
}
29-
return config;
3032
})
3133
.then((config) => {
3234
if (typeof config === 'object' || config === undefined) {

src/helpers/import-helper.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
importModule: function (module) {
3+
return import(module);
4+
},
5+
};

test/db/migrate.test.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const expect = require('expect.js');
33
const Support = require(__dirname + '/../support');
44
const helpers = require(__dirname + '/../support/helpers');
55
const gulp = require('gulp');
6+
const semver = require('semver');
67
const _ = require('lodash');
78

89
[
@@ -293,6 +294,54 @@ describe(Support.getTestDialectTeaser('db:migrate'), () => {
293294
});
294295
});
295296

297+
describeOnlyForESM(Support.getTestDialectTeaser('db:migrate'), () => {
298+
describe('with config.mjs', () => {
299+
const prepare = function (callback) {
300+
const config = helpers.getTestConfig();
301+
const configContent = 'export default ' + JSON.stringify(config);
302+
let result = '';
303+
304+
return gulp
305+
.src(Support.resolveSupportPath('tmp'))
306+
.pipe(helpers.clearDirectory())
307+
.pipe(helpers.runCli('init'))
308+
.pipe(helpers.removeFile('config/config.json'))
309+
.pipe(helpers.copyMigration('createPerson.js'))
310+
.pipe(helpers.overwriteFile(configContent, 'config/config.mjs'))
311+
.pipe(helpers.runCli('db:migrate --config config/config.mjs'))
312+
.on('error', (e) => {
313+
callback(e);
314+
})
315+
.on('data', (data) => {
316+
result += data.toString();
317+
})
318+
.on('end', () => {
319+
callback(null, result);
320+
});
321+
};
322+
323+
it('creates a SequelizeMeta table', function (done) {
324+
prepare(() => {
325+
helpers.readTables(this.sequelize, (tables) => {
326+
expect(tables).to.have.length(2);
327+
expect(tables).to.contain('SequelizeMeta');
328+
done();
329+
});
330+
});
331+
});
332+
333+
it('creates the respective table', function (done) {
334+
prepare(() => {
335+
helpers.readTables(this.sequelize, (tables) => {
336+
expect(tables).to.have.length(2);
337+
expect(tables).to.contain('Person');
338+
done();
339+
});
340+
});
341+
});
342+
});
343+
});
344+
296345
describe(Support.getTestDialectTeaser('db:migrate'), () => {
297346
describe('with config.json and url option', () => {
298347
const prepare = function (callback) {
@@ -525,3 +574,11 @@ describe(Support.getTestDialectTeaser('db:migrate'), () => {
525574
});
526575
});
527576
});
577+
578+
function describeOnlyForESM(title, fn) {
579+
if (semver.satisfies(process.version, '^12.20.0 || ^14.13.1 || >=16.0.0')) {
580+
describe(title, fn);
581+
} else {
582+
describe.skip(title, fn);
583+
}
584+
}

0 commit comments

Comments
 (0)