Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit 6c781d66
authored
Oct 29, 2019
by
Alejandro Corredor
Committed by
Sushant
Oct 30, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(typings): transaction locking (#11621)
1 parent
2083c9a2
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
111 additions
and
48 deletions
types/lib/model.d.ts
types/lib/transaction.d.ts
types/test/transaction.ts
types/lib/model.d.ts
View file @
6c781d6
...
...
@@ -18,7 +18,7 @@ import Op = require('./operators');
import
{
Promise
}
from
'./promise'
;
import
{
QueryOptions
,
IndexesOptions
}
from
'./query-interface'
;
import
{
Config
,
Options
,
Sequelize
,
SyncOptions
}
from
'./sequelize'
;
import
{
Transaction
}
from
'./transaction'
;
import
{
Transaction
,
LOCK
}
from
'./transaction'
;
import
{
Col
,
Fn
,
Literal
,
Where
}
from
'./utils'
;
import
{
IndexHints
}
from
'..'
;
...
...
@@ -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
* 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.
*/
...
...
types/lib/transaction.d.ts
View file @
6c781d6
...
...
@@ -26,6 +26,18 @@ export class Transaction {
* Adds hook that is run after a transaction is committed
*/
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
...
...
@@ -71,54 +83,61 @@ export namespace Transaction {
IMMEDIATE
=
'IMMEDIATE'
,
EXCLUSIVE
=
'EXCLUSIVE'
,
}
}
/**
* Possible options for row locking. Used in conjunction with `find` calls:
*
* ```js
* t1 // is a transaction
* t1.LOCK.UPDATE,
* t1.LOCK.SHARE,
* t1.LOCK.KEY_SHARE, // Postgres 9.3+ only
* t1.LOCK.NO_KEY_UPDATE // Postgres 9.3+ only
* ```
*
* Usage:
* ```js
* t1 // is a transaction
* Model.findAll({
* where: ...,
* transaction: t1,
* lock: t1.LOCK...
* });
* ```
*
* Postgres also supports specific locks while eager loading by using OF:
* ```js
* UserModel.findAll({
* where: ...,
* include: [TaskModel, ...],
* transaction: t1,
* lock: {
* level: t1.LOCK...,
* of: UserModel
* }
* });
* ```
* UserModel will be locked but TaskModel won't!
*/
export
enum
LOCK
{
UPDATE
=
'UPDATE'
,
SHARE
=
'SHARE'
,
/**
* Possible options for row locking. Used in conjunction with `find` calls:
*
* ```js
* t1 // is a transaction
* t1.LOCK.UPDATE,
* t1.LOCK.SHARE,
* t1.LOCK.KEY_SHARE, // Postgres 9.3+ only
* t1.LOCK.NO_KEY_UPDATE // Postgres 9.3+ only
* ```
*
* Usage:
* ```js
* t1 // is a transaction
* Model.findAll({
* where: ...,
* transaction: t1,
* lock: t1.LOCK...
* });
* ```
*
* Postgres also supports specific locks while eager loading by using OF:
* ```js
* UserModel.findAll({
* where: ...,
* include: [TaskModel, ...],
* transaction: t1,
* lock: {
* level: t1.LOCK...,
* of: UserModel
* }
* });
* ```
* UserModel will be locked but TaskModel won't!
* Postgres 9.3+ only
*/
enum
LOCK
{
UPDATE
=
'UPDATE'
,
SHARE
=
'SHARE'
,
/**
* Postgres 9.3+ only
*/
KEY_SHARE
=
'KEY SHARE'
,
/**
* Postgres 9.3+ only
*/
NO_KEY_UPDATE
=
'NO KEY UPDATE'
,
}
KEY_SHARE
=
'KEY SHARE'
,
/**
* Postgres 9.3+ only
*/
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
;
}
/**
...
...
types/test/transaction.ts
View file @
6c781d6
...
...
@@ -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
()
{
const
t
=
await
sequelize
.
transaction
({
deferrable
:
Deferrable
.
SET_DEFERRED
([
'test'
]),
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment