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

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';
var Utils = require('../../utils');
var Utils = require('../../utils')
, Promise = require('../../promise');
/**
Returns an object that treats SQLite's inabilities to do certain queries.
......@@ -32,9 +33,9 @@ module.exports = {
var sql = self.QueryGenerator.removeColumnQuery(tableName, fields)
, 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});
}, null);
});
});
},
......@@ -63,9 +64,9 @@ module.exports = {
var sql = self.QueryGenerator.removeColumnQuery(tableName, fields)
, 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});
}, null);
});
});
},
......@@ -94,9 +95,9 @@ module.exports = {
var sql = self.QueryGenerator.renameColumnQuery(tableName, attrNameBefore, attrNameAfter, fields)
, 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});
}, null);
});
});
}
};
......@@ -233,12 +233,12 @@ module.exports = (function() {
options = options || {};
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 (skip.indexOf(tableName.tableName || tableName) === -1) {
return self.dropTable(tableName, { cascade: true });
}
}, null);
});
};
var skip = options.skip || [];
......@@ -273,7 +273,7 @@ module.exports = (function() {
});
});
return Utils.Promise.all(promises).then(function() {
return Promise.all(promises).then(function() {
return dropAllTables(tableNames);
});
});
......@@ -283,7 +283,7 @@ module.exports = (function() {
QueryInterface.prototype.dropAllEnums = function(options) {
if (this.sequelize.getDialect() !== 'postgres') {
return Utils.Promise.resolve();
return Promise.resolve();
}
options = options || {};
......@@ -461,10 +461,10 @@ module.exports = (function() {
var self = this;
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);
}).then(function(results) {
var result = {};
......@@ -630,19 +630,19 @@ module.exports = (function() {
}
}
return Promise.reduce(cascades, function (memo, cascade) {
return Promise.each(cascades, function (cascade) {
return dao[cascade]({
transaction: options.transaction
}).then(function (instances) {
if (!Array.isArray(instances)) instances = [instances];
return Promise.reduce(instances, function (memo, instance) {
return Promise.each(instances, function (instance) {
return instance.destroy({
transaction: options.transaction
});
}, []);
});
}, []).then(function () {
});
}).then(function () {
return self.sequelize.query(sql, dao, options);
});
};
......@@ -747,7 +747,7 @@ module.exports = (function() {
if (sql) {
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging});
} else {
return Utils.Promise.resolve();
return Promise.resolve();
}
};
......@@ -756,7 +756,7 @@ module.exports = (function() {
if (sql) {
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging});
} else {
return Utils.Promise.resolve();
return Promise.resolve();
}
};
......@@ -765,7 +765,7 @@ module.exports = (function() {
if (sql) {
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging});
} else {
return Utils.Promise.resolve();
return Promise.resolve();
}
};
......@@ -774,7 +774,7 @@ module.exports = (function() {
if (sql) {
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging});
} else {
return Utils.Promise.resolve();
return Promise.resolve();
}
};
......@@ -783,7 +783,7 @@ module.exports = (function() {
if (sql) {
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging});
} else {
return Utils.Promise.resolve();
return Promise.resolve();
}
};
......@@ -792,7 +792,7 @@ module.exports = (function() {
if (sql) {
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging});
} else {
return Utils.Promise.resolve();
return Promise.resolve();
}
};
......@@ -840,7 +840,7 @@ module.exports = (function() {
if (sql) {
return this.sequelize.query(sql, null, { transaction: transaction });
} else {
return Utils.Promise.resolve();
return Promise.resolve();
}
};
......@@ -858,7 +858,7 @@ module.exports = (function() {
if (sql) {
return this.sequelize.query(sql, null, { transaction: transaction });
} else {
return Utils.Promise.resolve();
return Promise.resolve();
}
};
......@@ -891,7 +891,7 @@ module.exports = (function() {
if (sql) {
return this.sequelize.query(sql, null, options);
} else {
return Utils.Promise.resolve();
return Promise.resolve();
}
};
......
......@@ -892,9 +892,10 @@ module.exports = (function() {
}
});
return Promise.reduce(daos, function(total, dao) {
return Promise.each(daos, function(dao) {
return dao.sync(options);
}, null);
// return the sequelize instance
}).return(self);
});
};
......@@ -912,11 +913,11 @@ module.exports = (function() {
if (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);
}, null);
});
};
/**
......
......@@ -128,7 +128,7 @@ describe(Support.getTestDialectTeaser('Include'), function() {
, tags = results.tags
, 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({
user: User.create(),
products: Product.bulkCreate([
......@@ -398,7 +398,7 @@ describe(Support.getTestDialectTeaser('Include'), function() {
return Tag.findAll();
})
]).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([
User.create(),
Product.bulkCreate([
......@@ -1267,7 +1267,7 @@ describe(Support.getTestDialectTeaser('Include'), function() {
return Tag.findAll();
})
]).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({
user: User.create({name: 'FooBarzz'}),
products: Product.bulkCreate([
......
......@@ -126,7 +126,7 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
Tag.findAll()
]);
}).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([
AccUser.create(),
Product.bulkCreate([
......@@ -268,7 +268,7 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
return Tag.findAll();
})
]).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([
AccUser.create(),
Product.bulkCreate([
......@@ -383,7 +383,7 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
{}, {}, {}, {}, {}, {}, {}, {}
]).then(function() {
var previousInstance;
return Promise.resolve(singles).each(function(model) {
return Promise.each(singles, function(model) {
return model.create({}).then(function(instance) {
if (previousInstance) {
return previousInstance['set'+ Sequelize.Utils.uppercaseFirst(model.name)](instance).then(function() {
......
......@@ -774,6 +774,28 @@ describe(Support.getTestDialectTeaser('Sequelize'), function() {
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() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!