不要怂,就是干,撸起袖子干!

Commit feeff9ef by Mick Hansen

Merge pull request #4888 from alexschneider/fix-es6-imports

Fix #4881 and add tests for es6 imports
2 parents 8d752082 7c40faae
# Future
- [FIXED] Add support for Babel/ES6 imports [#4881](https://github.com/sequelize/sequelize/issues/4881)
# 3.14.2 # 3.14.2
- [FIXED] Model.aggregate methods now support attributes and where conditions with fields. [#4935](https://github.com/sequelize/sequelize/issues/4935) - [FIXED] Model.aggregate methods now support attributes and where conditions with fields. [#4935](https://github.com/sequelize/sequelize/issues/4935)
- [FIXED] Don't overwrite options.foreignKey in associations [#4927](https://github.com/sequelize/sequelize/pull/4927) - [FIXED] Don't overwrite options.foreignKey in associations [#4927](https://github.com/sequelize/sequelize/pull/4927)
......
...@@ -638,6 +638,10 @@ Sequelize.prototype.import = function(path) { ...@@ -638,6 +638,10 @@ Sequelize.prototype.import = function(path) {
if (!this.importCache[path]) { if (!this.importCache[path]) {
var defineCall = (arguments.length > 1 ? arguments[1] : require(path)); var defineCall = (arguments.length > 1 ? arguments[1] : require(path));
if (typeof defineCall === 'object' && defineCall.__esModule) {
// Babel/ES6 module compatability
defineCall = defineCall['default'];
}
this.importCache[path] = defineCall(this, DataTypes); this.importCache[path] = defineCall(this, DataTypes);
} }
......
...@@ -49,6 +49,8 @@ ...@@ -49,6 +49,8 @@
"wkx": "0.1.0" "wkx": "0.1.0"
}, },
"devDependencies": { "devDependencies": {
"babel-core": "^6.2.0",
"babel-preset-es2015": "^6.1.18",
"chai": "^3.0.0", "chai": "^3.0.0",
"chai-as-promised": "^5.1.0", "chai-as-promised": "^5.1.0",
"chai-datetime": "^1.4.0", "chai-datetime": "^1.4.0",
......
export default function(sequelize, DataTypes) {
return sequelize.define('Project' + parseInt(Math.random() * 9999999999999999), {
name: DataTypes.STRING
});
};
...@@ -14,6 +14,8 @@ var chai = require('chai') ...@@ -14,6 +14,8 @@ var chai = require('chai')
, moment = require('moment') , moment = require('moment')
, Transaction = require(__dirname + '/../../lib/transaction') , Transaction = require(__dirname + '/../../lib/transaction')
, sinon = require('sinon') , sinon = require('sinon')
, babel = require('babel-core')
, fs = require('fs')
, current = Support.sequelize; , current = Support.sequelize;
...@@ -545,14 +547,14 @@ describe(Support.getTestDialectTeaser('Sequelize'), function() { ...@@ -545,14 +547,14 @@ describe(Support.getTestDialectTeaser('Sequelize'), function() {
expect(result[0]).to.deep.equal([{ foo: 1, bar: '$ / $one' }]); expect(result[0]).to.deep.equal([{ foo: 1, bar: '$ / $one' }]);
}); });
}); });
it('throw an exception when binds passed with object and numeric $1 is also present', function() { it('throw an exception when binds passed with object and numeric $1 is also present', function() {
var self = this; var self = this;
var typeCast = (dialect === 'postgres') ? '::int' : ''; var typeCast = (dialect === 'postgres') ? '::int' : '';
var logSql; var logSql;
expect(function() { expect(function() {
self.sequelize.query('select $one'+typeCast+' as foo, $two'+typeCast+' as bar, \'$1\' as baz', { raw: true, bind: { one: 1, two: 2 }, logging: function(s) { logSql = s; } }); self.sequelize.query('select $one'+typeCast+' as foo, $two'+typeCast+' as bar, \'$1\' as baz', { raw: true, bind: { one: 1, two: 2 }, logging: function(s) { logSql = s; } });
}).to.throw(Error, /Named bind parameter "\$\w+" has no value in the given object\./g); }).to.throw(Error, /Named bind parameter "\$\w+" has no value in the given object\./g);
}); });
it('throw an exception when binds passed as array and $alpha is also present', function() { it('throw an exception when binds passed as array and $alpha is also present', function() {
...@@ -619,7 +621,7 @@ describe(Support.getTestDialectTeaser('Sequelize'), function() { ...@@ -619,7 +621,7 @@ describe(Support.getTestDialectTeaser('Sequelize'), function() {
self.sequelize.query('select $one as foo, $two as bar', { raw: true, bind: new Date()}); self.sequelize.query('select $one as foo, $two as bar', { raw: true, bind: new Date()});
}).to.throw(Error, /Named bind parameter "\$\w+" has no value in the given object\./g); }).to.throw(Error, /Named bind parameter "\$\w+" has no value in the given object\./g);
}); });
it('handles AS in conjunction with functions just fine', function() { it('handles AS in conjunction with functions just fine', function() {
var datetime = (dialect === 'sqlite' ? 'date(\'now\')' : 'NOW()'); var datetime = (dialect === 'sqlite' ? 'date(\'now\')' : 'NOW()');
if (dialect === 'mssql') { if (dialect === 'mssql') {
...@@ -1132,8 +1134,21 @@ describe(Support.getTestDialectTeaser('Sequelize'), function() { ...@@ -1132,8 +1134,21 @@ describe(Support.getTestDialectTeaser('Sequelize'), function() {
describe('import', function() { describe('import', function() {
it('imports a dao definition from a file absolute path', function() { it('imports a dao definition from a file absolute path', function() {
var Project = this.sequelize.import(__dirname + '/assets/project'); var Project = this.sequelize.import(__dirname + '/assets/project');
expect(Project).to.exist;
});
it('imports a dao definition from a file compiled with babel', function () {
var es6project = babel.transformFileSync(__dirname + '/assets/es6project.es6', {
presets: ['es2015']
}).code;
fs.writeFileSync(__dirname + '/assets/es6project.js', es6project);
var Project = this.sequelize.import(__dirname + '/assets/es6project');
expect(Project).to.exist; expect(Project).to.exist;
});
after(function(){
fs.unlink(__dirname + '/assets/es6project.js');
}); });
it('imports a dao definition from a function', function() { it('imports a dao definition from a function', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!