Skip to content

Commit

Permalink
#36: Adding ES6 style module support
Browse files Browse the repository at this point in the history
  • Loading branch information
icer9955 committed Aug 17, 2018
1 parent 6478e15 commit 2f8840b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,13 @@ Sequelize.prototype.import = function (importPath) {
importPath = path.resolve(callLoc, importPath);
}

if(this.importCache[importPath] === 'string' || !this.importCache[importPath]) {
this.importCache[importPath] = require(importPath)(this, DataTypes);
if(this.importCache[importPath] === 'string' || !this.importCache[importPath]) {
var defineCall = arguments.length > 1 ? arguments[1] : require(importPath);
if(typeof defineCall === 'object') {
// ES6 module compatibility
defineCall = defineCall.default;
}
this.importCache[importPath] = defineCall(this, DataTypes);
}

return this.importCache[importPath];
Expand Down
13 changes: 13 additions & 0 deletions test/sequelize.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var Sequelize = proxyquire('../src/sequelize', {
'./data-types' : function () {},

'import-test' : importTestFunc,
'import-test-es6' : { default: importTestFunc },
});

describe('Sequelize', function () {
Expand Down Expand Up @@ -272,6 +273,18 @@ describe('Sequelize', function () {
};
seq.import('foo').should.be.exactly(findItem);
});

it('should import an es6 model from the given path', function () {
seq.import('import-test-es6');
should(lastImportTestCall).not.be.Null();
lastImportTestCall[0].should.be.exactly(seq);
});

it('should import a model function as the second argument (for meteor compatibility)', function () {
seq.import('import-test', importTestFunc);
should(lastImportTestCall).not.be.Null();
lastImportTestCall[0].should.be.exactly(seq);
});
});

describe('#model', function() {
Expand Down

0 comments on commit 2f8840b

Please sign in to comment.