Skip to main content

Indices

This allows configuring indices in the database.

The easiest way to add an index on a single column is to use the index modifier on the field.

However, to add a multi-column index if we're querying a lot on the price of items:

src/schema/product_item_schema.ts
import {  FloatType, EntSchema } from "@snowtop/ent"; 

const ProductItemSchema = new EntSchema({
fields: {
price: FloatType(),
discount_price: FloatType(),
},

indices: [
{
name: "product_items_idx",
columns: ["price", "discount_price"],
},
],
});
export default ProductItemSchema;

which leads to

ent-test=# \d+ product_items
Table "public.product_items"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
----------------+-----------------------------+-----------+----------+---------+---------+--------------+-------------
id | uuid | | not null | | plain | |
created_at | timestamp without time zone | | not null | | plain | |
updated_at | timestamp without time zone | | not null | | plain | |
price | double precision | | not null | | plain | |
discount_price | double precision | | not null | | plain | |
Indexes:
"product_items_id_pkey" PRIMARY KEY, btree (id)
"product_items_idx" btree (price, discount_price)