Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add summary for locks #278

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions docs/other-topics/transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,25 +290,57 @@ User.afterSave((instance, options) => {
});
```

## Locks
## Row Locking

A `lock` option allows row locking.

Queries within a `transaction` can be performed with locks:

```js
```typescript
return User.findAll({
limit: 1,
lock: true,
transaction: t1
});
```

### Implement locked transactions

You can also implement it with a `transaction` object to wait the action will either be committed or rolled back when the lock is released.

```typescript
return User.findAll({
limit: 1,
lock: true,
transaction: t1
});
```

### Skip locked rows (PostgreSQL Only)

With `skipLocked` option sets true, any selected rows that cannot be immediately locked are skipped.

Queries within a transaction can skip locked rows:

```js
```typescript
return User.findAll({
limit: 1,
lock: true,
skipLocked: true,
transaction: t2
});
```

### Using enumeration LOCK

You can also use enumeration `LOCK` to implement possible options for row locking.

```typescript
import { LOCK } from '@sequelize/core';

Model.findAll({
transaction,
lock: LOCK.UPDATE,
});
```

Read more about the options [here](https://sequelize.org/api/v7/enums/lock).