|
1
|
import { Prerequisites } from "docs-ui" |
|
2
|
|
|
3
|
export const metadata = { |
|
4
|
title: `${pageNumber} Data Model Properties`, |
|
5
|
} |
|
6
|
|
|
7
|
# {metadata.title} |
|
8
|
|
|
9
|
In this chapter, you'll learn about the different property types you can use in a data model and how to configure a data model's properties. |
|
10
|
|
|
11
|
## Data Model's Default Properties |
|
12
|
|
|
13
|
By default, Medusa creates the following properties for every data model: |
|
14
|
|
|
15
|
- `created_at`: A [dateTime](#dateTime) property that stores when a record of the data model was created. |
|
16
|
- `updated_at`: A [dateTime](#dateTime) property that stores when a record of the data model was updated. |
|
17
|
- `deleted_at`: A [dateTime](#dateTime) property that stores when a record of the data model was deleted. When you soft-delete a record, Medusa sets the `deleted_at` property to the current date. |
|
18
|
|
|
19
|
### Precision of Default Timestamp Properties |
|
20
|
|
|
21
|
The `created_at` and `updated_at` properties are stored in `timestamptz` columns, and their precision depends on how you write the record: |
|
22
|
|
|
23
|
- When you create a record through a module's service or the ORM, Medusa sets the value from a JavaScript `Date`, which has millisecond precision. |
|
24
|
- When you insert a record with raw SQL that omits the column, such as in seed scripts, backfills, or imports, PostgreSQL applies the column's `now()` default, which has microsecond precision. |
|
25
|
|
|
26
|
When you retrieve a record, the database driver returns these properties as JavaScript `Date` objects, which drop any sub-millisecond digits. So, a retrieved value may be earlier than the value stored in the database. |
|
27
|
|
|
28
|
<Note title="Warning" type="warning"> |
|
29
|
|
|
30
|
Don't use a retrieved `created_at` or `updated_at` value directly in a cursor for keyset pagination or in a `>` comparison against the database column. If the stored value has microsecond precision, the comparison can match the same record again, causing duplicate results or infinite pagination loops. Instead, select the column as text in raw SQL, such as `created_at::text`, or truncate the column to milliseconds with `date_trunc('milliseconds', created_at)` in the comparison. |
|
31
|
|
|
32
|
</Note> |
|
33
|
|
|
34
|
--- |
|
35
|
|
|
36
|
## Property Types |
|
37
|
|
|
38
|
This section covers the different property types you can define in a data model's schema using the `model` methods. |
|
39
|
|
|
40
|
### id |
|
41
|
|
|
42
|
The `id` method defines an automatically generated string ID property. The generated ID is a unique string that has a mix of letters and numbers. |
|
43
|
|
|
44
|
For example: |
|
45
|
|
|
46
|
export const idHighlights = [["4", ".id()", "Define an `id` property."]] |
|
47
|
|
|
48
|
```ts highlights={idHighlights} |
|
49
|
import { model } from "@medusajs/framework/utils" |
|
50
|
|
|
51
|
const Post = model.define("post", { |
|
52
|
id: model.id(), |
|
53
|
// ... |
|
54
|
}) |
|
55
|
|
|
56
|
export default Post |
|
57
|
``` |
|
58
|
|
|
59
|
### text |
|
60
|
|
|
61
|
The `text` method defines a string property. |
|
62
|
|
|
63
|
For example: |
|
64
|
|
|
65
|
export const textHighlights = [["4", "text", "Define a `text` property."]] |
|
66
|
|
|
67
|
```ts highlights={textHighlights} |
|
68
|
import { model } from "@medusajs/framework/utils" |
|
69
|
|
|
70
|
const Post = model.define("post", { |
|
71
|
name: model.text(), |
|
72
|
// ... |
|
73
|
}) |
|
74
|
|
|
75
|
export default Post |
|
76
|
``` |
|
77
|
|
|
78
|
#### Limit Text Length |
|
79
|
|
|
80
|
To limit the allowed length of a `text` property, use the [checks method](../check-constraints/page.mdx). |
|
81
|
|
|
82
|
For example, to limit the `name` property to a maximum of 50 characters: |
|
83
|
|
|
84
|
export const textLengthHighlights = [ |
|
85
|
["7", "checks", "Add check constraints to the data model."], |
|
86
|
] |
|
87
|
|
|
88
|
```ts highlights={textLengthHighlights} |
|
89
|
import { model } from "@medusajs/framework/utils" |
|
90
|
|
|
91
|
const Post = model.define("post", { |
|
92
|
name: model.text(), |
|
93
|
// ... |
|
94
|
}) |
|
95
|
.checks([ |
|
96
|
{ |
|
97
|
name: "limit_name_length", |
|
98
|
expression: (columns) => `LENGTH(${columns.name}) <= 50`, |
|
99
|
}, |
|
100
|
]) |
|
101
|
|
|
102
|
export default Post |
|
103
|
``` |
|
104
|
|
|
105
|
This will add a database check constraint that ensures the `name` property of a record does not exceed 50 characters. If a record with a longer `name` is attempted to be inserted, an error will be thrown. |
|
106
|
|
|
107
|
### number |
|
108
|
|
|
109
|
The `number` method defines a number property. |
|
110
|
|
|
111
|
For example: |
|
112
|
|
|
113
|
export const numberHighlights = [["4", "number", "Define a `number` property."]] |
|
114
|
|
|
115
|
```ts highlights={numberHighlights} |
|
116
|
import { model } from "@medusajs/framework/utils" |
|
117
|
|
|
118
|
const Post = model.define("post", { |
|
119
|
age: model.number(), |
|
120
|
// ... |
|
121
|
}) |
|
122
|
|
|
123
|
export default Post |
|
124
|
``` |
|
125
|
|
|
126
|
### float |
|
127
|
|
|
128
|
<Note> |
|
129
|
|
|
130
|
This property is only available after [Medusa v2.1.2](https://github.com/medusajs/medusa/releases/tag/v2.1.2). |
|
131
|
|
|
132
|
</Note> |
|
133
|
|
|
134
|
The `float` method defines a number property that allows for values with decimal places. |
|
135
|
|
|
136
|
<Note title="Tip"> |
|
137
|
|
|
138
|
Use this property type when it's less important to have high precision for numbers with large decimal places. Alternatively, for higher precision, use the [bigNumber property](#bignumber). |
|
139
|
|
|
140
|
</Note> |
|
141
|
|
|
142
|
For example: |
|
143
|
|
|
144
|
export const floatHighlights = [["4", "float", "Define a `float` property."]] |
|
145
|
|
|
146
|
```ts highlights={floatHighlights} |
|
147
|
import { model } from "@medusajs/framework/utils" |
|
148
|
|
|
149
|
const Post = model.define("post", { |
|
150
|
rating: model.float(), |
|
151
|
// ... |
|
152
|
}) |
|
153
|
|
|
154
|
export default Post |
|
155
|
``` |
|
156
|
|
|
157
|
### bigNumber |
|
158
|
|
|
159
|
The `bigNumber` method defines a number property that expects large numbers, such as prices. |
|
160
|
|
|
161
|
<Note title="Tip"> |
|
162
|
|
|
163
|
Use this property type when it's important to have high precision for numbers with large decimal places. Alternatively, for less precision, use the [float property](#float). |
|
164
|
|
|
165
|
</Note> |
|
166
|
|
|
167
|
For example: |
|
168
|
|
|
169
|
export const bigNumberHighlights = [["4", "bigNumber", "Define a `bigNumber` property."]] |
|
170
|
|
|
171
|
```ts highlights={bigNumberHighlights} |
|
172
|
import { model } from "@medusajs/framework/utils" |
|
173
|
|
|
174
|
const Post = model.define("post", { |
|
175
|
price: model.bigNumber(), |
|
176
|
// ... |
|
177
|
}) |
|
178
|
|
|
179
|
export default Post |
|
180
|
``` |
|
181
|
|
|
182
|
<Note> |
|
183
|
|
|
184
|
Medusa stores along the `bigNumber` property a `raw_{property_name}` property that contains its raw value. Learn more in the [Big Number documentation](../big-numbers/page.mdx). |
|
185
|
|
|
186
|
</Note> |
|
187
|
|
|
188
|
### boolean |
|
189
|
|
|
190
|
The `boolean` method defines a boolean property. |
|
191
|
|
|
192
|
For example: |
|
193
|
|
|
194
|
export const booleanHighlights = [["4", "boolean", "Define a `boolean` property."]] |
|
195
|
|
|
196
|
```ts highlights={booleanHighlights} |
|
197
|
import { model } from "@medusajs/framework/utils" |
|
198
|
|
|
199
|
const Post = model.define("post", { |
|
200
|
hasAccount: model.boolean(), |
|
201
|
// ... |
|
202
|
}) |
|
203
|
|
|
204
|
export default Post |
|
205
|
``` |
|
206
|
|
|
207
|
### enum |
|
208
|
|
|
209
|
The `enum` method defines a property whose value can only be one of the specified values. |
|
210
|
|
|
211
|
For example: |
|
212
|
|
|
213
|
export const enumHighlights = [["4", "enum", "Define a `enum` property."]] |
|
214
|
|
|
215
|
```ts highlights={enumHighlights} |
|
216
|
import { model } from "@medusajs/framework/utils" |
|
217
|
|
|
218
|
const Post = model.define("post", { |
|
219
|
color: model.enum(["black", "white"]), |
|
220
|
// ... |
|
221
|
}) |
|
222
|
|
|
223
|
export default Post |
|
224
|
``` |
|
225
|
|
|
226
|
The `enum` method accepts an array of possible string values. |
|
227
|
|
|
228
|
### dateTime |
|
229
|
|
|
230
|
The `dateTime` method defines a timestamp property. |
|
231
|
|
|
232
|
For example: |
|
233
|
|
|
234
|
export const dateTimeHighlights = [["4", "dateTime", "Define a `dateTime` property."]] |
|
235
|
|
|
236
|
```ts highlights={dateTimeHighlights} |
|
237
|
import { model } from "@medusajs/framework/utils" |
|
238
|
|
|
239
|
const Post = model.define("post", { |
|
240
|
date_of_birth: model.dateTime(), |
|
241
|
// ... |
|
242
|
}) |
|
243
|
|
|
244
|
export default Post |
|
245
|
``` |
|
246
|
|
|
247
|
### json |
|
248
|
|
|
249
|
The `json` method defines a property whose value is stored as a `jsonb` column in the database, supporting any JSON-serializable value. |
|
250
|
|
|
251
|
By default, the property's TypeScript type is `Record<string, unknown>`. Pass a type parameter to use a different shape, such as an array of objects. |
|
252
|
|
|
253
|
For example: |
|
254
|
|
|
255
|
export const jsonHighlights = [["4", "json", "Define a `json` property."]] |
|
256
|
|
|
257
|
```ts highlights={jsonHighlights} |
|
258
|
import { model } from "@medusajs/framework/utils" |
|
259
|
|
|
260
|
const Post = model.define("post", { |
|
261
|
metadata: model.json(), |
|
262
|
// ... |
|
263
|
}) |
|
264
|
|
|
265
|
export default Post |
|
266
|
``` |
|
267
|
|
|
268
|
To store an array of typed objects, pass a type argument: |
|
269
|
|
|
270
|
<Note> |
|
271
|
|
|
272
|
The type argument is supported since [Medusa v2.19.0](https://github.com/medusajs/medusa/releases/tag/v2.19.0). |
|
273
|
|
|
274
|
</Note> |
|
275
|
|
|
276
|
```ts |
|
277
|
import { model } from "@medusajs/framework/utils" |
|
278
|
|
|
279
|
const Post = model.define("post", { |
|
280
|
warnings: model.json<{ code: string; message: string }[]>(), |
|
281
|
// ... |
|
282
|
}) |
|
283
|
|
|
284
|
export default Post |
|
285
|
``` |
|
286
|
|
|
287
|
<Note title="Tip"> |
|
288
|
|
|
289
|
To store an array of strings, use the [array](#array) property type instead, which stores the value in a `text[]` column. |
|
290
|
|
|
291
|
</Note> |
|
292
|
|
|
293
|
Learn more in the [JSON Properties](../json-properties/page.mdx) chapter. |
|
294
|
|
|
295
|
### array |
|
296
|
|
|
297
|
The `array` method defines an array of strings property. |
|
298
|
|
|
299
|
For example: |
|
300
|
|
|
301
|
export const arrHightlights = [["4", "array", "Define an `array` property."]] |
|
302
|
|
|
303
|
```ts highlights={arrHightlights} |
|
304
|
import { model } from "@medusajs/framework/utils" |
|
305
|
|
|
306
|
const Post = model.define("post", { |
|
307
|
names: model.array(), |
|
308
|
// ... |
|
309
|
}) |
|
310
|
|
|
311
|
export default Post |
|
312
|
``` |
|
313
|
|
|
314
|
### Properties Reference |
|
315
|
|
|
316
|
Refer to the [Data Model Language (DML) reference](!resources!/references/data-model) for a full reference of the properties. |
|
317
|
|
|
318
|
--- |
|
319
|
|
|
320
|
## Set Primary Key Property |
|
321
|
|
|
322
|
To set any `id`, `text`, or `number` property as a primary key, use the `primaryKey` method. |
|
323
|
|
|
324
|
For example: |
|
325
|
|
|
326
|
export const highlights = [ |
|
327
|
["4", "primaryKey", "Define the `id` property to be the data model's primary key."] |
|
328
|
] |
|
329
|
|
|
330
|
```ts highlights={highlights} |
|
331
|
import { model } from "@medusajs/framework/utils" |
|
332
|
|
|
333
|
const Post = model.define("post", { |
|
334
|
id: model.id().primaryKey(), |
|
335
|
// ... |
|
336
|
}) |
|
337
|
|
|
338
|
export default Post |
|
339
|
``` |
|
340
|
|
|
341
|
In the example above, the `id` property is defined as the data model's primary key. |
|
342
|
|
|
343
|
--- |
|
344
|
|
|
345
|
## Property Default Value |
|
346
|
|
|
347
|
Use the `default` method on a property's definition to specify the default value of a property. |
|
348
|
|
|
349
|
For example: |
|
350
|
|
|
351
|
export const defaultHighlights = [ |
|
352
|
["6", "default", "Set the default value to `black`."], |
|
353
|
["9", "default", "Set the default value to `0`."] |
|
354
|
] |
|
355
|
|
|
356
|
```ts highlights={defaultHighlights} |
|
357
|
import { model } from "@medusajs/framework/utils" |
|
358
|
|
|
359
|
const Post = model.define("post", { |
|
360
|
color: model |
|
361
|
.enum(["black", "white"]) |
|
362
|
.default("black"), |
|
363
|
age: model |
|
364
|
.number() |
|
365
|
.default(0), |
|
366
|
// ... |
|
367
|
}) |
|
368
|
|
|
369
|
export default Post |
|
370
|
``` |
|
371
|
|
|
372
|
In this example, you set the default value of the `color` enum property to `black`, and that of the `age` number property to `0`. |
|
373
|
|
|
374
|
--- |
|
375
|
|
|
376
|
## Make Property Optional |
|
377
|
|
|
378
|
Use the `nullable` method to indicate that a property’s value can be `null`. This is useful when you want a property to be optional. |
|
379
|
|
|
380
|
For example: |
|
381
|
|
|
382
|
export const nullableHighlights = [ |
|
383
|
["4", "nullable", "Configure the `price` property to allow `null` values."] |
|
384
|
] |
|
385
|
|
|
386
|
```ts highlights={nullableHighlights} |
|
387
|
import { model } from "@medusajs/framework/utils" |
|
388
|
|
|
389
|
const Post = model.define("post", { |
|
390
|
price: model.bigNumber().nullable(), |
|
391
|
// ... |
|
392
|
}) |
|
393
|
|
|
394
|
export default Post |
|
395
|
``` |
|
396
|
|
|
397
|
In the example above, the `price` property is configured to allow `null` values, making it optional. |
|
398
|
|
|
399
|
--- |
|
400
|
|
|
401
|
## Unique Property |
|
402
|
|
|
403
|
The `unique` method indicates that a property’s value must be unique in the database through a unique index. |
|
404
|
|
|
405
|
For example: |
|
406
|
|
|
407
|
export const uniqueHighlights = [ |
|
408
|
["4", "unique", "Configure the `email` property to allow unique values only."] |
|
409
|
] |
|
410
|
|
|
411
|
```ts highlights={uniqueHighlights} |
|
412
|
import { model } from "@medusajs/framework/utils" |
|
413
|
|
|
414
|
const User = model.define("user", { |
|
415
|
email: model.text().unique(), |
|
416
|
// ... |
|
417
|
}) |
|
418
|
|
|
419
|
export default User |
|
420
|
``` |
|
421
|
|
|
422
|
In this example, multiple users can’t have the same email. |
|
423
|
|
|
424
|
--- |
|
425
|
|
|
426
|
## Define Database Index on Property |
|
427
|
|
|
428
|
Use the `index` method on a property's definition to define a database index. |
|
429
|
|
|
430
|
For example: |
|
431
|
|
|
432
|
export const dbIndexHighlights = [ |
|
433
|
["5", "index", "Define an index on the `name` property."], |
|
434
|
["6", '"IDX_MY_CUSTOM_NAME"', "Index name is optional."] |
|
435
|
] |
|
436
|
|
|
437
|
```ts highlights={dbIndexHighlights} |
|
438
|
import { model } from "@medusajs/framework/utils" |
|
439
|
|
|
440
|
const Post = model.define("post", { |
|
441
|
id: model.id().primaryKey(), |
|
442
|
name: model.text().index( |
|
443
|
"IDX_MY_CUSTOM_NAME" |
|
444
|
), |
|
445
|
}) |
|
446
|
|
|
447
|
export default Post |
|
448
|
``` |
|
449
|
|
|
450
|
The `index` method optionally accepts the name of the index as a parameter. |
|
451
|
|
|
452
|
In this example, you define an index on the `name` property. |
|
453
|
|
|
454
|
--- |
|
455
|
|
|
456
|
## Define a Searchable Property |
|
457
|
|
|
458
|
Methods generated by the [service factory](../../modules/service-factory/page.mdx) that accept filters, such as `list{ModelName}s`, accept a `q` property as part of the filters. |
|
459
|
|
|
460
|
When the `q` filter is passed, the data model's searchable properties are queried to find matching records. |
|
461
|
|
|
462
|
Use the `searchable` method on a `text` property to indicate that it's searchable. |
|
463
|
|
|
464
|
For example: |
|
465
|
|
|
466
|
export const searchableHighlights = [ |
|
467
|
["4", "searchable", "Define the `title` property as searchable."] |
|
468
|
] |
|
469
|
|
|
470
|
```ts highlights={searchableHighlights} |
|
471
|
import { model } from "@medusajs/framework/utils" |
|
472
|
|
|
473
|
const Post = model.define("post", { |
|
474
|
title: model.text().searchable(), |
|
475
|
// ... |
|
476
|
}) |
|
477
|
|
|
478
|
export default Post |
|
479
|
``` |
|
480
|
|
|
481
|
In this example, the `title` property is searchable. |
|
482
|
|
|
483
|
### Searchable Relationship Properties |
|
484
|
|
|
485
|
You can also make relationship properties searchable by adding the `searchable` method to relationship definitions like `hasMany`, `hasOne`, `belongsTo`, or `manyToMany`. |
|
486
|
|
|
487
|
For example: |
|
488
|
|
|
489
|
export const relationshipSearchableHighlights = [ |
|
490
|
["6", "searchable", "Make the `variants` relationship searchable."] |
|
491
|
] |
|
492
|
|
|
493
|
```ts highlights={relationshipSearchableHighlights} |
|
494
|
import { model } from "@medusajs/framework/utils" |
|
495
|
|
|
496
|
const Product = model.define("product", { |
|
497
|
variants: model.hasMany(() => ProductVariant, { |
|
498
|
mappedBy: "product", |
|
499
|
}).searchable(), |
|
500
|
// ... |
|
501
|
}) |
|
502
|
|
|
503
|
export default Product |
|
504
|
``` |
|
505
|
|
|
506
|
When a relationship property is marked as searchable, the related records' searchable text properties can be queried when using the `q` filter. |
|
507
|
|
|
508
|
### Search Example |
|
509
|
|
|
510
|
If you pass a `q` filter to the `listPosts` method: |
|
511
|
|
|
512
|
```ts |
|
513
|
const posts = await blogModuleService.listPosts({ |
|
514
|
q: "New Products", |
|
515
|
}) |
|
516
|
``` |
|
517
|
|
|
518
|
This retrieves records that include `New Products` in their `title` property. |
|
519
|
|
|
520
|
--- |
|
521
|
|
|
522
|
## Make Property Translatable |
|
523
|
|
|
524
|
<Prerequisites |
|
525
|
items={[ |
|
526
|
{ |
|
527
|
text: "Medusa v2.13.0+", |
|
528
|
link: "https://github.com/medusajs/medusa/releases/tag/v2.13.0" |
|
529
|
}, |
|
530
|
{ |
|
531
|
text: "Translation Module Configured", |
|
532
|
link: "!resources!/commerce-modules/translation#configure-translation-module", |
|
533
|
}, |
|
534
|
]} |
|
535
|
/> |
|
536
|
|
|
537
|
Use the `translatable` method on a `text` property to indicate that the property can have different values per locale. This only works if the [Translation Module](!resources!/commerce-modules/translation) is configured in your application. |
|
538
|
|
|
539
|
When a property is marked as translatable, merchants can manage translations for these fields through the [Medusa Admin](!user-guide!/settings/translations). |
|
540
|
|
|
541
|
For example: |
|
542
|
|
|
543
|
export const translatableHighlights = [ |
|
544
|
["4", "translatable", "Mark the `title` property as translatable."], |
|
545
|
["5", "translatable", "Mark the `description` property as translatable."] |
|
546
|
] |
|
547
|
|
|
548
|
```ts highlights={translatableHighlights} |
|
549
|
import { model } from "@medusajs/framework/utils" |
|
550
|
|
|
551
|
const Post = model.define("post", { |
|
552
|
title: model.text().translatable(), |
|
553
|
description: model.text().translatable(), |
|
554
|
// ... |
|
555
|
}) |
|
556
|
|
|
557
|
export default Post |
|
558
|
``` |
|
559
|
|
|
560
|
In this example, the `title` and `description` properties are translatable. So, admin users can translate the values of these properties into different locales. |
|
561
|
|
|
562
|
Learn more in the [Translate Custom Data Models](!resources!/commerce-modules/translation/custom-data-models) guide. |