indexes Method - API Reference
This method defines indices on the data model. An index can be on multiple columns
and have conditions.
Example#
An example of a simple index:
1 import { model } from "@medusajs/utils" 2
3 const MyCustom = model . define ( "my_custom" , { 4 id : model . id ( ) , 5 name : model . text ( ) , 6 age : model . number ( ) 7 } ) . indexes ( [ 8 { 9 on : [ "name" , "age" ] , 10 } , 11 ] ) 12
13 export default MyCustom
To add a condition on the index, use the where
option:
1 import { model } from "@medusajs/utils" 2
3 const MyCustom = model . define ( "my_custom" , { 4 id : model . id ( ) , 5 name : model . text ( ) , 6 age : model . number ( ) 7 } ) . indexes ( [ 8 { 9 on : [ "name" , "age" ] , 10 where : { 11 age : 30 12 } 13 } , 14 ] ) 15
16 export default MyCustom
The condition can also be a negation. For example:
1 import { model } from "@medusajs/utils" 2
3 const MyCustom = model . define ( "my_custom" , { 4 id : model . id ( ) , 5 name : model . text ( ) , 6 age : model . number ( ) 7 } ) . indexes ( [ 8 { 9 on : [ "name" , "age" ] , 10 where : { 11 age : { 12 $ne : 30 13 } 14 } 15 } , 16 ] ) 17
18 export default MyCustom
In this example, the index is created when the value of age
doesn't equal 30
.
Parameters#
The index's configuration.
Was this page helpful? It was helpful It wasn't helpful Report Issue