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

Commit b603ebcc by Mick Hansen

Merge pull request #3547 from BridgeAR/return-daos-after-sync

Return sequelize instance / dao after .sync and use promise.each instead...
2 parents d732d0cf b560b2cb
'use strict'; 'use strict';
var Utils = require('../../utils'); var Utils = require('../../utils')
, Promise = require('../../promise');
/** /**
Returns an object that treats SQLite's inabilities to do certain queries. Returns an object that treats SQLite's inabilities to do certain queries.
...@@ -32,9 +33,9 @@ module.exports = { ...@@ -32,9 +33,9 @@ module.exports = {
var sql = self.QueryGenerator.removeColumnQuery(tableName, fields) var sql = self.QueryGenerator.removeColumnQuery(tableName, fields)
, subQueries = sql.split(';').filter(function(q) { return q !== ''; }); , subQueries = sql.split(';').filter(function(q) { return q !== ''; });
return Utils.Promise.reduce(subQueries, function(total, subQuery) { return Promise.each(subQueries, function(subQuery) {
return self.sequelize.query(subQuery + ';', null, { raw: true}); return self.sequelize.query(subQuery + ';', null, { raw: true});
}, null); });
}); });
}, },
...@@ -63,9 +64,9 @@ module.exports = { ...@@ -63,9 +64,9 @@ module.exports = {
var sql = self.QueryGenerator.removeColumnQuery(tableName, fields) var sql = self.QueryGenerator.removeColumnQuery(tableName, fields)
, subQueries = sql.split(';').filter(function(q) { return q !== ''; }); , subQueries = sql.split(';').filter(function(q) { return q !== ''; });
return Utils.Promise.reduce(subQueries, function(total, subQuery) { return Promise.each(subQueries, function(subQuery) {
return self.sequelize.query(subQuery + ';', null, { raw: true}); return self.sequelize.query(subQuery + ';', null, { raw: true});
}, null); });
}); });
}, },
...@@ -94,9 +95,9 @@ module.exports = { ...@@ -94,9 +95,9 @@ module.exports = {
var sql = self.QueryGenerator.renameColumnQuery(tableName, attrNameBefore, attrNameAfter, fields) var sql = self.QueryGenerator.renameColumnQuery(tableName, attrNameBefore, attrNameAfter, fields)
, subQueries = sql.split(';').filter(function(q) { return q !== ''; }); , subQueries = sql.split(';').filter(function(q) { return q !== ''; });
return Utils.Promise.reduce(subQueries, function(total, subQuery) { return Promise.each(subQueries, function(subQuery) {
return self.sequelize.query(subQuery + ';', null, { raw: true}); return self.sequelize.query(subQuery + ';', null, { raw: true});
}, null); });
}); });
} }
}; };
...@@ -233,12 +233,12 @@ module.exports = (function() { ...@@ -233,12 +233,12 @@ module.exports = (function() {
options = options || {}; options = options || {};
var dropAllTables = function(tableNames) { var dropAllTables = function(tableNames) {
return Utils.Promise.reduce(tableNames, function(total, tableName) { return Promise.each(tableNames, function(tableName) {
// if tableName is not in the Array of tables names then dont drop it // if tableName is not in the Array of tables names then dont drop it
if (skip.indexOf(tableName.tableName || tableName) === -1) { if (skip.indexOf(tableName.tableName || tableName) === -1) {
return self.dropTable(tableName, { cascade: true }); return self.dropTable(tableName, { cascade: true });
} }
}, null); });
}; };
var skip = options.skip || []; var skip = options.skip || [];
...@@ -273,7 +273,7 @@ module.exports = (function() { ...@@ -273,7 +273,7 @@ module.exports = (function() {
}); });
}); });
return Utils.Promise.all(promises).then(function() { return Promise.all(promises).then(function() {
return dropAllTables(tableNames); return dropAllTables(tableNames);
}); });
}); });
...@@ -283,7 +283,7 @@ module.exports = (function() { ...@@ -283,7 +283,7 @@ module.exports = (function() {
QueryInterface.prototype.dropAllEnums = function(options) { QueryInterface.prototype.dropAllEnums = function(options) {
if (this.sequelize.getDialect() !== 'postgres') { if (this.sequelize.getDialect() !== 'postgres') {
return Utils.Promise.resolve(); return Promise.resolve();
} }
options = options || {}; options = options || {};
...@@ -461,10 +461,10 @@ module.exports = (function() { ...@@ -461,10 +461,10 @@ module.exports = (function() {
var self = this; var self = this;
if (tableNames.length === 0) { if (tableNames.length === 0) {
return Utils.Promise.resolve({}); return Promise.resolve({});
} }
return Utils.Promise.map(tableNames, function(tableName) { return Promise.map(tableNames, function(tableName) {
return self.sequelize.query(self.QueryGenerator.getForeignKeysQuery(tableName, self.sequelize.config.database)).get(0); return self.sequelize.query(self.QueryGenerator.getForeignKeysQuery(tableName, self.sequelize.config.database)).get(0);
}).then(function(results) { }).then(function(results) {
var result = {}; var result = {};
...@@ -630,19 +630,19 @@ module.exports = (function() { ...@@ -630,19 +630,19 @@ module.exports = (function() {
} }
} }
return Promise.reduce(cascades, function (memo, cascade) { return Promise.each(cascades, function (cascade) {
return dao[cascade]({ return dao[cascade]({
transaction: options.transaction transaction: options.transaction
}).then(function (instances) { }).then(function (instances) {
if (!Array.isArray(instances)) instances = [instances]; if (!Array.isArray(instances)) instances = [instances];
return Promise.reduce(instances, function (memo, instance) { return Promise.each(instances, function (instance) {
return instance.destroy({ return instance.destroy({
transaction: options.transaction transaction: options.transaction
}); });
}, []); });
}); });
}, []).then(function () { }).then(function () {
return self.sequelize.query(sql, dao, options); return self.sequelize.query(sql, dao, options);
}); });
}; };
...@@ -747,7 +747,7 @@ module.exports = (function() { ...@@ -747,7 +747,7 @@ module.exports = (function() {
if (sql) { if (sql) {
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging}); return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging});
} else { } else {
return Utils.Promise.resolve(); return Promise.resolve();
} }
}; };
...@@ -756,7 +756,7 @@ module.exports = (function() { ...@@ -756,7 +756,7 @@ module.exports = (function() {
if (sql) { if (sql) {
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging}); return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging});
} else { } else {
return Utils.Promise.resolve(); return Promise.resolve();
} }
}; };
...@@ -765,7 +765,7 @@ module.exports = (function() { ...@@ -765,7 +765,7 @@ module.exports = (function() {
if (sql) { if (sql) {
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging}); return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging});
} else { } else {
return Utils.Promise.resolve(); return Promise.resolve();
} }
}; };
...@@ -774,7 +774,7 @@ module.exports = (function() { ...@@ -774,7 +774,7 @@ module.exports = (function() {
if (sql) { if (sql) {
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging}); return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging});
} else { } else {
return Utils.Promise.resolve(); return Promise.resolve();
} }
}; };
...@@ -783,7 +783,7 @@ module.exports = (function() { ...@@ -783,7 +783,7 @@ module.exports = (function() {
if (sql) { if (sql) {
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging}); return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging});
} else { } else {
return Utils.Promise.resolve(); return Promise.resolve();
} }
}; };
...@@ -792,7 +792,7 @@ module.exports = (function() { ...@@ -792,7 +792,7 @@ module.exports = (function() {
if (sql) { if (sql) {
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging}); return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging});
} else { } else {
return Utils.Promise.resolve(); return Promise.resolve();
} }
}; };
...@@ -840,7 +840,7 @@ module.exports = (function() { ...@@ -840,7 +840,7 @@ module.exports = (function() {
if (sql) { if (sql) {
return this.sequelize.query(sql, null, { transaction: transaction }); return this.sequelize.query(sql, null, { transaction: transaction });
} else { } else {
return Utils.Promise.resolve(); return Promise.resolve();
} }
}; };
...@@ -858,7 +858,7 @@ module.exports = (function() { ...@@ -858,7 +858,7 @@ module.exports = (function() {
if (sql) { if (sql) {
return this.sequelize.query(sql, null, { transaction: transaction }); return this.sequelize.query(sql, null, { transaction: transaction });
} else { } else {
return Utils.Promise.resolve(); return Promise.resolve();
} }
}; };
...@@ -891,7 +891,7 @@ module.exports = (function() { ...@@ -891,7 +891,7 @@ module.exports = (function() {
if (sql) { if (sql) {
return this.sequelize.query(sql, null, options); return this.sequelize.query(sql, null, options);
} else { } else {
return Utils.Promise.resolve(); return Promise.resolve();
} }
}; };
......
...@@ -892,9 +892,10 @@ module.exports = (function() { ...@@ -892,9 +892,10 @@ module.exports = (function() {
} }
}); });
return Promise.reduce(daos, function(total, dao) { return Promise.each(daos, function(dao) {
return dao.sync(options); return dao.sync(options);
}, null); // return the sequelize instance
}).return(self);
}); });
}; };
...@@ -912,11 +913,11 @@ module.exports = (function() { ...@@ -912,11 +913,11 @@ module.exports = (function() {
if (dao) { if (dao) {
daos.push(dao); daos.push(dao);
} }
}, { reverse: false}); }, { reverse: false });
return Promise.reduce(daos, function(total, dao) { return Promise.each(daos, function(dao) {
return dao.drop(options); return dao.drop(options);
}, null); });
}; };
/** /**
......
...@@ -128,7 +128,7 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -128,7 +128,7 @@ describe(Support.getTestDialectTeaser('Include'), function() {
, tags = results.tags , tags = results.tags
, companies = results.companies; , companies = results.companies;
return Promise.resolve([0, 1, 2, 3, 4]).each(function (i) { return Promise.each([0, 1, 2, 3, 4], function (i) {
return Promise.props({ return Promise.props({
user: User.create(), user: User.create(),
products: Product.bulkCreate([ products: Product.bulkCreate([
...@@ -398,7 +398,7 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -398,7 +398,7 @@ describe(Support.getTestDialectTeaser('Include'), function() {
return Tag.findAll(); return Tag.findAll();
}) })
]).spread(function(groups, ranks, tags) { ]).spread(function(groups, ranks, tags) {
return Promise.resolve([0, 1, 2, 3, 4]).each(function (i) { return Promise.each([0, 1, 2, 3, 4], function (i) {
return Promise.all([ return Promise.all([
User.create(), User.create(),
Product.bulkCreate([ Product.bulkCreate([
...@@ -1267,7 +1267,7 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1267,7 +1267,7 @@ describe(Support.getTestDialectTeaser('Include'), function() {
return Tag.findAll(); return Tag.findAll();
}) })
]).spread(function (groups, ranks, tags) { ]).spread(function (groups, ranks, tags) {
return Promise.resolve([0, 1, 2, 3, 4]).each(function (i) { return Promise.each([0, 1, 2, 3, 4], function (i) {
return Promise.props({ return Promise.props({
user: User.create({name: 'FooBarzz'}), user: User.create({name: 'FooBarzz'}),
products: Product.bulkCreate([ products: Product.bulkCreate([
......
...@@ -126,7 +126,7 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() { ...@@ -126,7 +126,7 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
Tag.findAll() Tag.findAll()
]); ]);
}).spread(function (groups, companies, ranks, tags) { }).spread(function (groups, companies, ranks, tags) {
return Promise.resolve([0, 1, 2, 3, 4]).each(function (i) { return Promise.each([0, 1, 2, 3, 4], function (i) {
return Promise.all([ return Promise.all([
AccUser.create(), AccUser.create(),
Product.bulkCreate([ Product.bulkCreate([
...@@ -268,7 +268,7 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() { ...@@ -268,7 +268,7 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
return Tag.findAll(); return Tag.findAll();
}) })
]).spread(function(groups, ranks, tags) { ]).spread(function(groups, ranks, tags) {
return Promise.resolve([0, 1, 2, 3, 4]).each(function (i) { return Promise.each([0, 1, 2, 3, 4], function (i) {
return Promise.all([ return Promise.all([
AccUser.create(), AccUser.create(),
Product.bulkCreate([ Product.bulkCreate([
...@@ -383,7 +383,7 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() { ...@@ -383,7 +383,7 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
{}, {}, {}, {}, {}, {}, {}, {} {}, {}, {}, {}, {}, {}, {}, {}
]).then(function() { ]).then(function() {
var previousInstance; var previousInstance;
return Promise.resolve(singles).each(function(model) { return Promise.each(singles, function(model) {
return model.create({}).then(function(instance) { return model.create({}).then(function(instance) {
if (previousInstance) { if (previousInstance) {
return previousInstance['set'+ Sequelize.Utils.uppercaseFirst(model.name)](instance).then(function() { return previousInstance['set'+ Sequelize.Utils.uppercaseFirst(model.name)](instance).then(function() {
......
...@@ -774,6 +774,28 @@ describe(Support.getTestDialectTeaser('Sequelize'), function() { ...@@ -774,6 +774,28 @@ describe(Support.getTestDialectTeaser('Sequelize'), function() {
return this.sequelize.sync(); return this.sequelize.sync();
}); });
it('return the sequelize instance after syncing', function() {
var self = this;
return this.sequelize.sync().then(function(sequelize) {
expect(sequelize).to.deep.equal(self.sequelize);
});
});
it('return the single dao after syncing', function() {
var block = this.sequelize.define('block', {
id: { type: DataTypes.INTEGER, primaryKey: true },
name: DataTypes.STRING
}, {
tableName: 'block',
timestamps: false,
paranoid: false
});
return block.sync().then(function(result) {
expect(result).to.deep.equal(block);
});
});
} }
describe("doesn't emit logging when explicitly saying not to", function() { describe("doesn't emit logging when explicitly saying not to", function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!