Back to Sequelize

Optimistic Locking

docs/models/optimistic-locking.mdx

latest1.3 KB
Original Source

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';

Sequelize has built-in support for optimistic locking through a model instance version count.

Whenever Sequelize needs to modify the model, it will make sure that the version count on the model instance is equal to the version count in the database. If the counts are different, an OptimisticLockError will be thrown.

Enabling optimistic locking

Optimistic locking is disabled by default and can be enabled by using the @Version decorator on the attribute of your model that should be used as the version count.

<Tabs groupId="ts-js"> <TabItem value="ts" label="TypeScript">
typescript
import { InferCreationAttributes, InferAttributes, Model, CreationOptional } from '@sequelize/core';
import { Version } from '@sequelize/core/decorators-legacy';

class User extends Model<InferAttributes<User>, InferCreationAttributes<User>> {
  // highlight-next-line
  @Version
  declare version: CreationOptional<number>;
}
</TabItem> <TabItem value="js" label="JavaScript">
javascript
import { Model } from '@sequelize/core';
import { Version } from '@sequelize/core/decorators-legacy';

class User extends Model {
  // highlight-next-line
  @Version
  version;
}
</TabItem> </Tabs>