3.5.2. Data Model Properties
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.
Data Model's Default Properties#
By default, Medusa creates the following properties for every data model:
created_at: A dateTime property that stores when a record of the data model was created.updated_at: A dateTime property that stores when a record of the data model was updated.deleted_at: A dateTime property that stores when a record of the data model was deleted. When you soft-delete a record, Medusa sets thedeleted_atproperty to the current date.
Property Types#
This section covers the different property types you can define in a data model's schema using the model methods.
id#
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.
For example:
text#
The text method defines a string property.
For example:
Limit Text Length
To limit the allowed length of a text property, use the checks method.
For example, to limit the name property to a maximum of 50 characters:
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.
number#
The number method defines a number property.
For example:
float#
The float method defines a number property that allows for values with decimal places.
For example:
bigNumber#
The bigNumber method defines a number property that expects large numbers, such as prices.
For example:
boolean#
The boolean method defines a boolean property.
For example:
enum#
The enum method defines a property whose value can only be one of the specified values.
For example:
The enum method accepts an array of possible string values.
dateTime#
The dateTime method defines a timestamp property.
For example:
json#
The json method defines a property whose value is stored as a stringified JSON object in the database.
For example:
Learn more in the JSON Properties chapter.
array#
The array method defines an array of strings property.
For example:
Properties Reference#
Refer to the Data Model Language (DML) reference for a full reference of the properties.
Set Primary Key Property#
To set any id, text, or number property as a primary key, use the primaryKey method.
For example:
In the example above, the id property is defined as the data model's primary key.
Property Default Value#
Use the default method on a property's definition to specify the default value of a property.
For example:
In this example, you set the default value of the color enum property to black, and that of the age number property to 0.
Make Property Optional#
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.
For example:
In the example above, the price property is configured to allow null values, making it optional.
Unique Property#
The unique method indicates that a property’s value must be unique in the database through a unique index.
For example:
In this example, multiple users can’t have the same email.
Define Database Index on Property#
Use the index method on a property's definition to define a database index.
For example:
The index method optionally accepts the name of the index as a parameter.
In this example, you define an index on the name property.
Define a Searchable Property#
Methods generated by the service factory that accept filters, such as list{ModelName}s, accept a q property as part of the filters.
When the q filter is passed, the data model's searchable properties are queried to find matching records.
Use the searchable method on a text property to indicate that it's searchable.
For example:
In this example, the title property is searchable.
Search Example#
If you pass a q filter to the listPosts method:
This retrieves records that include New Products in their title property.