Skip to content

Commit 940174b

Browse files
Robin BuschmannRobin Buschmann
authored andcommitted
unique annotation added #7
1 parent 6507c88 commit 940174b

File tree

5 files changed

+20
-6
lines changed

5 files changed

+20
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ Decorator | Description
150150
--------------------------------------|---------------------
151151
`@AllowNull(allowNull?: boolean)` | sets `attribute.allowNull` (default is `true`)
152152
`@AutoIncrement` | sets `attribute.autoIncrement=true`
153+
`@Unique` | sets `attribute.unique=true`
153154
`@Default(value: any)` | sets `attribute.defaultValue` to specified value
154155
`@PrimaryKey` | sets `attribute.primaryKey=true`
155156
Validate annotations | see [Model valiation](#model-validation)

index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export {Table} from "./lib/annotations/Table";
1515
export {CreatedAt} from "./lib/annotations/CreatedAt";
1616
export {DeletedAt} from "./lib/annotations/DeletedAt";
1717
export {UpdatedAt} from "./lib/annotations/UpdatedAt";
18+
export {Unique} from "./lib/annotations/Unique";
1819

1920
export {Contains} from "./lib/annotations/validation/Contains";
2021
export {Equals} from "./lib/annotations/validation/Equals";

lib/annotations/Unique.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import 'reflect-metadata';
2+
import {addAttributeOptions} from "../services/models";
3+
4+
/**
5+
* Sets unique option true for annotated property.
6+
*/
7+
export function Unique(target: any, propertyName: string): void {
8+
9+
addAttributeOptions(target, propertyName, {
10+
unique: true
11+
});
12+
}

test/models/User.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Table, Model, PrimaryKey, Column, AutoIncrement, DataType, Default, AllowNull} from "../../index";
1+
import {Table, Model, PrimaryKey, Column, AutoIncrement, DataType, Default, AllowNull, Unique} from "../../index";
22

33
@Table
44
export class User extends Model<User> {
@@ -14,10 +14,9 @@ export class User extends Model<User> {
1414
})
1515
uuidv1: string;
1616

17-
@Column({
18-
type: DataType.UUID,
19-
defaultValue: DataType.UUIDV4
20-
})
17+
@Unique
18+
@Default(DataType.UUIDV4)
19+
@Column(DataType.UUID)
2120
uuidv4: string;
2221

2322
@Column
@@ -38,7 +37,7 @@ export class User extends Model<User> {
3837
@Default(false)
3938
@AllowNull(false)
4039
@Column(DataType.BOOLEAN)
41-
isSuperUser: boolean|number;
40+
isSuperUser: boolean | number;
4241

4342
@Column({
4443
defaultValue: DataType.NOW

test/specs/table_column.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ describe('table_column', () => {
2828
},
2929
uuidv4: {
3030
type: DataType.UUID,
31+
unique: true,
3132
defaultValue: DataType.UUIDV4
3233
},
3334
username: {

0 commit comments

Comments
 (0)