From d9d5fc55147276e995b6512de096689489cb8d31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98?= Date: Sun, 2 Oct 2022 13:15:45 +0700 Subject: [PATCH 1/2] docs: add summary for locks --- docs/other-topics/transactions.md | 38 ++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/docs/other-topics/transactions.md b/docs/other-topics/transactions.md index 054b8f6c..b27b2b92 100644 --- a/docs/other-topics/transactions.md +++ b/docs/other-topics/transactions.md @@ -292,9 +292,23 @@ User.afterSave((instance, options) => { ## Locks +A `lock` option alows 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, @@ -302,13 +316,31 @@ return User.findAll({ }); ``` +### 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). From c370d59c22efdccd9cb6e9e18feb5ebd2f7af095 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98?= Date: Sun, 2 Oct 2022 13:27:09 +0700 Subject: [PATCH 2/2] docs: better SEO with Row Locking --- docs/other-topics/transactions.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/other-topics/transactions.md b/docs/other-topics/transactions.md index b27b2b92..e0cdbe51 100644 --- a/docs/other-topics/transactions.md +++ b/docs/other-topics/transactions.md @@ -290,9 +290,9 @@ User.afterSave((instance, options) => { }); ``` -## Locks +## Row Locking -A `lock` option alows row locking. +A `lock` option allows row locking. Queries within a `transaction` can be performed with locks: