1 export const metadata = {
2 title: `${pageNumber} Data Model Database Index`,
3 }
4
5 # {metadata.title}
6
7 In this chapter, you’ll learn how to define a database index on a data model.
8
9 <Note>
10
11 You can also define an index on a property as explained in the [Properties chapter](../properties/page.mdx#define-database-index-on-property).
12
13 </Note>
14
15 ## Define Database Index on Data Model
16
17 A data model has an `indexes` method that defines database indices on its properties.
18
19 The index can be on multiple columns (composite index). For example:
20
21 export const dataModelIndexHighlights = [
22 ["7", "indexes", "Define indices on the data model's properties."],
23 ["9", "on", "Specify the properties to define the index on."]
24 ]
25
26 ```ts highlights={dataModelIndexHighlights}
27 import { model } from "@medusajs/framework/utils"
28
29 const MyCustom = model.define("my_custom", {
30 id: model.id().primaryKey(),
31 name: model.text(),
32 age: model.number(),
33 }).indexes([
34 {
35 on: ["name", "age"],
36 },
37 ])
38
39 export default MyCustom
40 ```
41
42 The `indexes` method receives an array of indices as a parameter. Each index is an object with a required `on` property indicating the properties to apply the index on.
43
44 In the above example, you define a composite index on the `name` and `age` properties.
45
46 ### Index Conditions
47
48 An index can have conditions. For example:
49
50 export const conditionHighlights = [
51 ["10", "where", "Specify conditions on properties."],
52 ["11", "age", "Create the index when `age` is `30`."]
53 ]
54
55 ```ts highlights={conditionHighlights}
56 import { model } from "@medusajs/framework/utils"
57
58 const MyCustom = model.define("my_custom", {
59 id: model.id().primaryKey(),
60 name: model.text(),
61 age: model.number(),
62 }).indexes([
63 {
64 on: ["name", "age"],
65 where: {
66 age: 30,
67 },
68 },
69 ])
70
71 export default MyCustom
72 ```
73
74 The index object passed to `indexes` accepts a `where` property whose value is an object of conditions. The object's key is a property's name, and its value is the condition on that property.
75
76 In the example above, the composite index is created on the `name` and `age` properties when the `age`'s value is `30`.
77
78 A property's condition can be a negation. For example:
79
80 export const negationHighlights = [
81 ["12", "$ne", "Create the index when `age` is not `null`."]
82 ]
83
84 ```ts highlights={negationHighlights}
85 import { model } from "@medusajs/framework/utils"
86
87 const MyCustom = model.define("my_custom", {
88 id: model.id().primaryKey(),
89 name: model.text(),
90 age: model.number().nullable(),
91 }).indexes([
92 {
93 on: ["name", "age"],
94 where: {
95 age: {
96 $ne: null,
97 },
98 },
99 },
100 ])
101
102 export default MyCustom
103 ```
104
105 A property's value in `where` can be an object having a `$ne` property. `$ne`'s value indicates what the specified property's value shouldn't be.
106
107 In the example above, the composite index is created on the `name` and `age` properties when `age`'s value is not `null`.
108
109 ### Unique Database Index
110
111 The object passed to `indexes` accepts a `unique` property indicating that the created index must be a unique index.
112
113 For example:
114
115 export const uniqueHighlights = [
116 ["10", "unique", "Specify if the index is a unique index."]
117 ]
118
119 ```ts highlights={uniqueHighlights}
120 import { model } from "@medusajs/framework/utils"
121
122 const MyCustom = model.define("my_custom", {
123 id: model.id().primaryKey(),
124 name: model.text(),
125 age: model.number(),
126 }).indexes([
127 {
128 on: ["name", "age"],
129 unique: true,
130 },
131 ])
132
133 export default MyCustom
134 ```
135
136 This creates a unique composite index on the `name` and `age` properties.