diff --git a/types/lib/errors.d.ts b/types/lib/errors.d.ts index 91e3f2eea98f..83fbee9edc58 100644 --- a/types/lib/errors.d.ts +++ b/types/lib/errors.d.ts @@ -129,6 +129,13 @@ export class ExclusionConstraintError extends DatabaseError { constructor(options: { parent?: Error; message?: string; constraint?: string; fields?: string[]; table?: string }); } +/** + * Thrown when attempting to update a stale model instance + */ +export class OptimisticLockError extends BaseError { + constructor(options: { message?: string, modelName?: string, values?: { [key: string]: any }, where?: { [key: string]: any } }); +} + /** * A base class for all connection related errors. */ diff --git a/types/test/errors.ts b/types/test/errors.ts index f0d9b9b7d2d8..3a58ba296232 100644 --- a/types/test/errors.ts +++ b/types/test/errors.ts @@ -1,6 +1,7 @@ // Error === BaseError import { BaseError, EmptyResultError, Error, UniqueConstraintError } from 'sequelize'; import { User } from './models/User'; +import { OptimisticLockError } from '../lib/errors'; async function test() { try { @@ -23,4 +24,16 @@ async function test() { console.error('should return emptyresulterror'); } } + + try { + const user: User | null = await User.findByPk(1); + if (user != null) { + user.username = 'foo'; + user.save(); + } + } catch (e) { + if (!(e instanceof OptimisticLockError)) { + console.log('should return OptimisticLockError'); + } + } }