Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#36: Adding ES6 style module support #61

Merged
merged 1 commit into from
Oct 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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