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

Commit 2924eba1 by Andy Edwards Committed by GitHub

refactor(sequelize): asyncify methods (#12125)

1 parent f1d08764
...@@ -538,7 +538,7 @@ class Sequelize { ...@@ -538,7 +538,7 @@ class Sequelize {
* @see {@link Model.build} for more information about instance option. * @see {@link Model.build} for more information about instance option.
*/ */
query(sql, options) { async query(sql, options) {
options = Object.assign({}, this.options.query, options); options = Object.assign({}, this.options.query, options);
if (options.instance && !options.model) { if (options.instance && !options.model) {
...@@ -583,7 +583,6 @@ class Sequelize { ...@@ -583,7 +583,6 @@ class Sequelize {
options.searchPath = 'DEFAULT'; options.searchPath = 'DEFAULT';
} }
return Promise.resolve().then(() => {
if (typeof sql === 'object') { if (typeof sql === 'object') {
if (sql.values !== undefined) { if (sql.values !== undefined) {
if (options.replacements !== undefined) { if (options.replacements !== undefined) {
...@@ -634,29 +633,27 @@ class Sequelize { ...@@ -634,29 +633,27 @@ class Sequelize {
const retryOptions = Object.assign({}, this.options.retry, options.retry || {}); const retryOptions = Object.assign({}, this.options.retry, options.retry || {});
return Promise.resolve(retry(() => Promise.resolve().then(() => { return retry(async () => {
if (options.transaction === undefined && Sequelize._cls) { if (options.transaction === undefined && Sequelize._cls) {
options.transaction = Sequelize._cls.get('transaction'); options.transaction = Sequelize._cls.get('transaction');
} }
checkTransaction(); checkTransaction();
return options.transaction const connection = await (options.transaction ? options.transaction.connection : this.connectionManager.getConnection(options));
? options.transaction.connection
: this.connectionManager.getConnection(options);
}).then(connection => {
const query = new this.dialect.Query(connection, this, options); const query = new this.dialect.Query(connection, this, options);
return this.runHooks('beforeQuery', options, query)
.then(() => checkTransaction()) try {
.then(() => query.run(sql, bindParameters)) await this.runHooks('beforeQuery', options, query);
.finally(() => this.runHooks('afterQuery', options, query)) await checkTransaction();
.finally(() => { return await query.run(sql, bindParameters);
} finally {
await this.runHooks('afterQuery', options, query);
if (!options.transaction) { if (!options.transaction) {
return this.connectionManager.releaseConnection(connection); await this.connectionManager.releaseConnection(connection);
} }
}); }
}), retryOptions)); }, retryOptions);
});
} }
/** /**
...@@ -671,7 +668,7 @@ class Sequelize { ...@@ -671,7 +668,7 @@ class Sequelize {
* *
* @returns {Promise} * @returns {Promise}
*/ */
set(variables, options) { async set(variables, options) {
// Prepare options // Prepare options
options = Object.assign({}, this.options.set, typeof options === 'object' && options); options = Object.assign({}, this.options.set, typeof options === 'object' && options);
...@@ -693,7 +690,7 @@ class Sequelize { ...@@ -693,7 +690,7 @@ class Sequelize {
`SET ${ `SET ${
_.map(variables, (v, k) => `@${k} := ${typeof v === 'string' ? `"${v}"` : v}`).join(', ')}`; _.map(variables, (v, k) => `@${k} := ${typeof v === 'string' ? `"${v}"` : v}`).join(', ')}`;
return this.query(query, options); return await this.query(query, options);
} }
/** /**
...@@ -722,8 +719,8 @@ class Sequelize { ...@@ -722,8 +719,8 @@ class Sequelize {
* *
* @returns {Promise} * @returns {Promise}
*/ */
createSchema(schema, options) { async createSchema(schema, options) {
return this.getQueryInterface().createSchema(schema, options); return await this.getQueryInterface().createSchema(schema, options);
} }
/** /**
...@@ -737,8 +734,8 @@ class Sequelize { ...@@ -737,8 +734,8 @@ class Sequelize {
* *
* @returns {Promise} * @returns {Promise}
*/ */
showAllSchemas(options) { async showAllSchemas(options) {
return this.getQueryInterface().showAllSchemas(options); return await this.getQueryInterface().showAllSchemas(options);
} }
/** /**
...@@ -753,8 +750,8 @@ class Sequelize { ...@@ -753,8 +750,8 @@ class Sequelize {
* *
* @returns {Promise} * @returns {Promise}
*/ */
dropSchema(schema, options) { async dropSchema(schema, options) {
return this.getQueryInterface().dropSchema(schema, options); return await this.getQueryInterface().dropSchema(schema, options);
} }
/** /**
...@@ -768,8 +765,8 @@ class Sequelize { ...@@ -768,8 +765,8 @@ class Sequelize {
* *
* @returns {Promise} * @returns {Promise}
*/ */
dropAllSchemas(options) { async dropAllSchemas(options) {
return this.getQueryInterface().dropAllSchemas(options); return await this.getQueryInterface().dropAllSchemas(options);
} }
/** /**
...@@ -886,18 +883,20 @@ class Sequelize { ...@@ -886,18 +883,20 @@ class Sequelize {
* *
* @returns {Promise} * @returns {Promise}
*/ */
authenticate(options) { async authenticate(options) {
options = Object.assign({ options = Object.assign({
raw: true, raw: true,
plain: true, plain: true,
type: QueryTypes.SELECT type: QueryTypes.SELECT
}, options); }, options);
return this.query('SELECT 1+1 AS result', options).then(() => undefined); await this.query('SELECT 1+1 AS result', options);
return;
} }
databaseVersion(options) { async databaseVersion(options) {
return this.getQueryInterface().databaseVersion(options); return await this.getQueryInterface().databaseVersion(options);
} }
/** /**
...@@ -1094,7 +1093,7 @@ class Sequelize { ...@@ -1094,7 +1093,7 @@ class Sequelize {
* *
* @returns {Promise} * @returns {Promise}
*/ */
transaction(options, autoCallback) { async transaction(options, autoCallback) {
if (typeof options === 'function') { if (typeof options === 'function') {
autoCallback = options; autoCallback = options;
options = undefined; options = undefined;
...@@ -1102,20 +1101,28 @@ class Sequelize { ...@@ -1102,20 +1101,28 @@ class Sequelize {
const transaction = new Transaction(this, options); const transaction = new Transaction(this, options);
if (!autoCallback) return transaction.prepareEnvironment(false).then(() => transaction); if (!autoCallback) {
await transaction.prepareEnvironment(false);
return transaction;
}
// autoCallback provided // autoCallback provided
return Sequelize._clsRun(() => { return Sequelize._clsRun(async () => {
return transaction.prepareEnvironment() try {
.then(() => autoCallback(transaction)) await transaction.prepareEnvironment();
.then(result => Promise.resolve(transaction.commit()).then(() => result)) const result = await autoCallback(transaction);
.catch(err => { await transaction.commit();
// Rollback transaction if not already finished (commit, rollback, etc) return await result;
// and reject with original error (ignore any error in rollback) } catch (err) {
return Promise.resolve().then(() => { if (!transaction.finished) {
if (!transaction.finished) return transaction.rollback().catch(() => {}); try {
}).then(() => { throw err; }); await transaction.rollback();
}); } catch (err0) {
// ignore
}
}
throw err;
}
}); });
} }
......
...@@ -880,10 +880,9 @@ describe(Support.getTestDialectTeaser('Sequelize'), () => { ...@@ -880,10 +880,9 @@ describe(Support.getTestDialectTeaser('Sequelize'), () => {
if (dialect === 'mysql') { if (dialect === 'mysql') {
describe('set', () => { describe('set', () => {
it("should return an promised error if transaction isn't defined", function() { it("should return an promised error if transaction isn't defined", async function() {
expect(() => { await expect(this.sequelize.set({ foo: 'bar' }))
this.sequelize.set({ foo: 'bar' }); .to.be.rejectedWith(TypeError, 'options.transaction is required');
}).to.throw(TypeError, 'options.transaction is required');
}); });
it('one value', function() { it('one value', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!