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

Commit 6c781d66 by Alejandro Corredor Committed by Sushant

fix(typings): transaction locking (#11621)

1 parent 2083c9a2
...@@ -18,7 +18,7 @@ import Op = require('./operators'); ...@@ -18,7 +18,7 @@ import Op = require('./operators');
import { Promise } from './promise'; import { Promise } from './promise';
import { QueryOptions, IndexesOptions } from './query-interface'; import { QueryOptions, IndexesOptions } from './query-interface';
import { Config, Options, Sequelize, SyncOptions } from './sequelize'; import { Config, Options, Sequelize, SyncOptions } from './sequelize';
import { Transaction } from './transaction'; import { Transaction, LOCK } from './transaction';
import { Col, Fn, Literal, Where } from './utils'; import { Col, Fn, Literal, Where } from './utils';
import { IndexHints } from '..'; import { IndexHints } from '..';
...@@ -548,8 +548,10 @@ export interface FindOptions extends QueryOptions, Filterable, Projectable, Para ...@@ -548,8 +548,10 @@ export interface FindOptions extends QueryOptions, Filterable, Projectable, Para
* Postgres also supports transaction.LOCK.KEY_SHARE, transaction.LOCK.NO_KEY_UPDATE and specific model * Postgres also supports transaction.LOCK.KEY_SHARE, transaction.LOCK.NO_KEY_UPDATE and specific model
* locks with joins. See [transaction.LOCK for an example](transaction#lock) * locks with joins. See [transaction.LOCK for an example](transaction#lock)
*/ */
lock?: Transaction.LOCK | { level: Transaction.LOCK; of: typeof Model }; lock?:
| LOCK
| { level: LOCK; of: typeof Model }
| boolean;
/** /**
* Skip locked rows. Only supported in Postgres. * Skip locked rows. Only supported in Postgres.
*/ */
......
...@@ -26,6 +26,18 @@ export class Transaction { ...@@ -26,6 +26,18 @@ export class Transaction {
* Adds hook that is run after a transaction is committed * Adds hook that is run after a transaction is committed
*/ */
public afterCommit(fn: (transaction: this) => void | Promise<void>): void; public afterCommit(fn: (transaction: this) => void | Promise<void>): void;
/**
* Returns possible options for row locking
*/
static get LOCK(): LOCK;
/**
* Same as its static version, but can also be called on instances of
* transactions to get possible options for row locking directly from the
* instance.
*/
get LOCK(): LOCK;
} }
// tslint:disable-next-line no-namespace // tslint:disable-next-line no-namespace
...@@ -71,8 +83,9 @@ export namespace Transaction { ...@@ -71,8 +83,9 @@ export namespace Transaction {
IMMEDIATE = 'IMMEDIATE', IMMEDIATE = 'IMMEDIATE',
EXCLUSIVE = 'EXCLUSIVE', EXCLUSIVE = 'EXCLUSIVE',
} }
}
/** /**
* Possible options for row locking. Used in conjunction with `find` calls: * Possible options for row locking. Used in conjunction with `find` calls:
* *
* ```js * ```js
...@@ -107,7 +120,7 @@ export namespace Transaction { ...@@ -107,7 +120,7 @@ export namespace Transaction {
* ``` * ```
* UserModel will be locked but TaskModel won't! * UserModel will be locked but TaskModel won't!
*/ */
enum LOCK { export enum LOCK {
UPDATE = 'UPDATE', UPDATE = 'UPDATE',
SHARE = 'SHARE', SHARE = 'SHARE',
/** /**
...@@ -118,7 +131,13 @@ export namespace Transaction { ...@@ -118,7 +131,13 @@ export namespace Transaction {
* Postgres 9.3+ only * Postgres 9.3+ only
*/ */
NO_KEY_UPDATE = 'NO KEY UPDATE', NO_KEY_UPDATE = 'NO KEY UPDATE',
} }
interface LOCK {
UPDATE: LOCK.UPDATE;
SHARE: LOCK.SHARE;
KEY_SHARE: LOCK.KEY_SHARE;
NO_KEY_UPDATE: LOCK.NO_KEY_UPDATE;
} }
/** /**
......
...@@ -18,6 +18,48 @@ async function trans() { ...@@ -18,6 +18,48 @@ async function trans() {
}); });
} }
async function trans2() {
return await sequelize.transaction(async transaction => {
transaction.afterCommit(() => console.log('transaction complete'));
User.findAll(
{
transaction,
lock: transaction.LOCK.UPDATE,
}
);
return 1;
});
}
async function trans3() {
return await sequelize.transaction(async transaction => {
transaction.afterCommit(() => console.log('transaction complete'));
User.findAll(
{
transaction,
lock: true,
}
);
return 1;
});
}
async function trans4() {
return await sequelize.transaction(async transaction => {
transaction.afterCommit(() => console.log('transaction complete'));
User.findAll(
{
transaction,
lock: {
level: transaction.LOCK.UPDATE,
of: User,
},
}
);
return 1;
});
}
async function transact() { async function transact() {
const t = await sequelize.transaction({ const t = await sequelize.transaction({
deferrable: Deferrable.SET_DEFERRED(['test']), deferrable: Deferrable.SET_DEFERRED(['test']),
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!