Indexes
An example of adding an index to my ORM system
Indexes help speed up data retrieval in a table, especially when working with large datasets. In my ORM system, indexes can be added using the @Index
decorator.
An example of adding an index to the user table
import {Index, Column, PrimaryGeneratedColumn, Table} from "@myroslavshymon/orm";
@Index({indexName: 'user_index', columns: ['username', 'email'], options: {isUnique: true}})
@Table({name: 'users'})
export class Users {
@PrimaryGeneratedColumn({type: 'BIGINT'})
user_id: number;
@Column({options: {dataType: "VARCHAR", length: 255}})
username: string;
@Column({options: {dataType: "VARCHAR", length: 255}})
email: string;
@Column({options: {dataType: "VARCHAR", length: 255}})
password: string;
@Column({options: {dataType: 'BOOLEAN', defaultValue: false}})
is_active: boolean;
}
In this example, we create a unique index named user_index
on the username
and email
columns in the users
table. The index ensures that each combination of these fields is unique, helping to prevent duplicates.
Last updated