Skip to content

Commit 85ba828

Browse files
committed
add optimistic locking option
1 parent 9d04ead commit 85ba828

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

lib/interfaces/IDefineOptions.ts

+5
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@ import {DefineOptions} from "sequelize";
22

33
export interface IDefineOptions extends DefineOptions<any> {
44
modelName?: string;
5+
6+
/**
7+
* To enable optimistic locking.
8+
*/
9+
version?: boolean;
510
}

lib/models/Model.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,11 @@ export declare class Model<T> extends Hooks {
466466
*/
467467
deletedAt?: Date|any;
468468

469+
/**
470+
* version number automatically created by sequelize if table options.version is true
471+
*/
472+
version?: number;
473+
469474
/**
470475
* Returns true if this instance has not yet been persisted to the database
471476
*/

test/models/UserWithVersion.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {Column, Model, Table} from "../../index";
2+
3+
@Table({version: true})
4+
export class UserWithVersion extends Model<UserWithVersion> {
5+
6+
@Column
7+
name: string;
8+
9+
}

test/specs/instance.spec.ts

+24
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {UserWithValidation} from "../models/UserWithValidation";
2020
import {UserWithNoAutoIncrementation} from "../models/UserWithNoAutoIncrementation";
2121
import {UserWithCustomUpdatedAt} from "../models/UserWithCustomUpdatedAt";
2222
import {UserWithCreatedAtButWithoutUpdatedAt} from "../models/UserWithCreatedAtButWithoutUpdatedAt";
23+
import {UserWithVersion} from "../models/UserWithVersion";
2324
import chaiDatetime = require('chai-datetime');
2425
import {IDefineOptions} from "../../lib/interfaces/IDefineOptions";
2526
// import {UserWithSwag} from "../models/UserWithSwag";
@@ -1481,6 +1482,29 @@ describe('instance', () => {
14811482
});
14821483
});
14831484

1485+
1486+
describe('with version option', () => {
1487+
1488+
it("version column is updated by sequelize", () => {
1489+
let version = undefined;
1490+
UserWithCustomUpdatedAt
1491+
.sync()
1492+
.then(() => UserWithVersion.create<UserWithVersion>({ name: 'john doe' }))
1493+
.then((johnDoe: UserWithVersion) => {
1494+
expect(johnDoe.version).not.to.be.undefined;
1495+
version = johnDoe.version;
1496+
return johnDoe.update({name: 'doe john'});
1497+
})
1498+
.then((johnDoe: UserWithVersion) => {
1499+
expect(johnDoe.name).not.equals('doe john');
1500+
expect(johnDoe.version).not.equals(version);
1501+
return johnDoe.update({});
1502+
})
1503+
}
1504+
)
1505+
});
1506+
1507+
14841508
it('should fail a validation upon creating', () =>
14851509
User
14861510
.create({aNumber: 0, validateTest: 'hello'})

0 commit comments

Comments
 (0)