indexes Method - DML 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:

Code
1import { model } from "@medusajs/framework/utils"2
3const 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
13export default MyCustom

To add a condition on the index, use the where option:

Code
1import { model } from "@medusajs/framework/utils"2
3const 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: 3012    }13  },14])15
16export default MyCustom

The condition can also be a negation. For example:

Code
1import { model } from "@medusajs/framework/utils"2
3const 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: 3013      }14    }15  },16])17
18export default MyCustom

In this example, the index is created when the value of age doesn't equal 30.

Parameters#

indexesEntityIndex<Schema, string | QueryCondition<Schema>>[]
The index's configuration.
Was this page helpful?