|
1
|
export const metadata = { |
|
2
|
title: `${pageNumber} Add Data Model Check Constraints`, |
|
3
|
} |
|
4
|
|
|
5
|
# {metadata.title} |
|
6
|
|
|
7
|
In this chapter, you'll learn how to add check constraints to your data model. |
|
8
|
|
|
9
|
## What is a Check Constraint? |
|
10
|
|
|
11
|
A check constraint is a condition that must be satisfied by records inserted into a database table, otherwise an error is thrown. |
|
12
|
|
|
13
|
For example, if you have a data model with a `price` property, you want to only allow positive number values. So, you add a check constraint that fails when inserting a record with a negative price value. |
|
14
|
|
|
15
|
--- |
|
16
|
|
|
17
|
## How to Set a Check Constraint? |
|
18
|
|
|
19
|
To set check constraints on a data model, use the `checks` method. This method accepts an array of check constraints to apply on the data model. |
|
20
|
|
|
21
|
For example, to set a check constraint on a `price` property that ensures its value can only be a positive number: |
|
22
|
|
|
23
|
export const checks1Highlights = [ |
|
24
|
["7", "checks", "Add check constraints to the data model."], |
|
25
|
["8", "columns", "An object of column names for each property."], |
|
26
|
["8", "`${columns.price} >= 0`", "Return a SQL check constraint expression."] |
|
27
|
] |
|
28
|
|
|
29
|
```ts highlights={checks1Highlights} |
|
30
|
import { model } from "@medusajs/framework/utils" |
|
31
|
|
|
32
|
const CustomProduct = model.define("custom_product", { |
|
33
|
// ... |
|
34
|
price: model.bigNumber(), |
|
35
|
}) |
|
36
|
.checks([ |
|
37
|
(columns) => `${columns.price} >= 0`, |
|
38
|
]) |
|
39
|
``` |
|
40
|
|
|
41
|
The item passed in the array parameter of `checks` can be a callback function that accepts as a parameter an object whose keys are the names of the properties in the data model schema, and values the respective column name in the database. |
|
42
|
|
|
43
|
The function returns a string indicating the [SQL check constraint expression](https://www.postgresql.org/docs/current/ddl-constraints.html#DDL-CONSTRAINTS-CHECK-CONSTRAINTS). In the expression, use the `columns` parameter to access a property's column name. |
|
44
|
|
|
45
|
You can also pass an object to the `checks` method: |
|
46
|
|
|
47
|
export const checks2Highlights = [ |
|
48
|
["7", "checks", "Add check constraints to the data model."], |
|
49
|
["9", "name", "The check constraint's name."], |
|
50
|
["10", "expression", "A function that returns the SQL check constraint expression."] |
|
51
|
] |
|
52
|
|
|
53
|
```ts highlights={checks2Highlights} |
|
54
|
import { model } from "@medusajs/framework/utils" |
|
55
|
|
|
56
|
const CustomProduct = model.define("custom_product", { |
|
57
|
// ... |
|
58
|
price: model.bigNumber(), |
|
59
|
}) |
|
60
|
.checks([ |
|
61
|
{ |
|
62
|
name: "custom_product_price_check", |
|
63
|
expression: (columns) => `${columns.price} >= 0`, |
|
64
|
}, |
|
65
|
]) |
|
66
|
``` |
|
67
|
|
|
68
|
The object accepts the following properties: |
|
69
|
|
|
70
|
- `name`: The check constraint's name. |
|
71
|
- `expression`: A function similar to the one that can be passed to the array. It accepts an object of columns and returns an [SQL check constraint expression](https://www.postgresql.org/docs/current/ddl-constraints.html#DDL-CONSTRAINTS-CHECK-CONSTRAINTS). |
|
72
|
|
|
73
|
--- |
|
74
|
|
|
75
|
## Apply in Migrations |
|
76
|
|
|
77
|
After adding the check constraint, make sure to generate and run migrations if you already have the table in the database. Otherwise, the check constraint won't be reflected. |
|
78
|
|
|
79
|
To generate a migration for the data model's module then reflect it on the database, run the following command: |
|
80
|
|
|
81
|
```bash npx2yarn |
|
82
|
npx medusa db:generate custom_module |
|
83
|
npx medusa db:migrate |
|
84
|
``` |
|
85
|
|
|
86
|
The first command generates the migration under the `migrations` directory of your module's directory, and the second reflects it on the database. |
|
87
|
|
|
88
|
--- |
|
89
|
|
|
90
|
## Examples |
|
91
|
|
|
92
|
This section covers common use cases where check constraints are particularly useful. |
|
93
|
|
|
94
|
### 1. Enforce Text Length |
|
95
|
|
|
96
|
Ensure that text properties meet minimum or maximum length requirements: |
|
97
|
|
|
98
|
```ts |
|
99
|
const User = model.define("user", { |
|
100
|
username: model.text(), |
|
101
|
password: model.text(), |
|
102
|
}) |
|
103
|
.checks([ |
|
104
|
{ |
|
105
|
name: "password_length_check", |
|
106
|
expression: (columns) => `LENGTH(${columns.password}) >= 8`, |
|
107
|
}, |
|
108
|
]) |
|
109
|
``` |
|
110
|
|
|
111
|
In the above example, the check constraint fails if the `password` property is less than 8 characters long. |
|
112
|
|
|
113
|
### 2. Validate Email Format |
|
114
|
|
|
115
|
Ensure email addresses contain the `@` symbol: |
|
116
|
|
|
117
|
```ts |
|
118
|
const Customer = model.define("customer", { |
|
119
|
email: model.text(), |
|
120
|
}) |
|
121
|
.checks([ |
|
122
|
{ |
|
123
|
name: "email_format_check", |
|
124
|
expression: (columns) => `${columns.email} LIKE '%@%'`, |
|
125
|
}, |
|
126
|
]) |
|
127
|
``` |
|
128
|
|
|
129
|
In the above example, the check constraint fails if the `email` property does not contain the `@` symbol. |
|
130
|
|
|
131
|
### 3. Enforce Date Ranges |
|
132
|
|
|
133
|
Ensure dates fall within valid ranges: |
|
134
|
|
|
135
|
```ts |
|
136
|
const Event = model.define("event", { |
|
137
|
start_date: model.dateTime(), |
|
138
|
end_date: model.dateTime(), |
|
139
|
}) |
|
140
|
.checks([ |
|
141
|
{ |
|
142
|
name: "date_order_check", |
|
143
|
expression: (columns) => `${columns.end_date} >= ${columns.start_date}`, |
|
144
|
}, |
|
145
|
]) |
|
146
|
``` |
|
147
|
|
|
148
|
In the above example, the check constraint fails if the `end_date` is earlier than the `start_date`. |