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

Commit 2924eba1 by Andy Edwards Committed by GitHub

refactor(sequelize): asyncify methods (#12125)

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