Skip to content

Commit 4008ceb

Browse files
committed
UPDATE: added check to schema attributes
1 parent 3732025 commit 4008ceb

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

db.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ export const works = new Schema<{
4949
rating: {
5050
type: "INTEGER",
5151
default() {
52-
return 1;
52+
return "1";
5353
},
54+
check: ["1", "1.5", "2", "2.5", "3", "3.5", "4", "4.5", "5"],
5455
},
5556
createdAt: {
5657
type: "DATETIME",

src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ interface SchemaColumnOptions {
1818
unique?: boolean;
1919
default?: () => string;
2020
target?: string;
21+
check?: string[];
2122
}
2223

2324
type Params = string | number | null | boolean;
@@ -674,6 +675,13 @@ export class Schema<Model extends Record<string, any> = {}> {
674675
(this.columns[col].target
675676
? " REFERENCES " + this.columns[col].target + "(id)"
676677
: "") +
678+
(this.columns[col].check?.length
679+
? " CHECK (" +
680+
col +
681+
" IN (" +
682+
this.columns[col].check.map((c) => "'" + c + "'").join(",") +
683+
")) "
684+
: "") +
677685
(this.columns[col].default
678686
? " DEFAULT " + this.columns[col].default()
679687
: "") +

0 commit comments

Comments
 (0)