ORM
  • Introduction
  • Basics of Usage
    • Connecting to the Database
    • Creating a Model
    • Simple Queries
  • Configuration
  • Core Features
    • Models
    • CRUD Operations (Create, Read, Update, Delete): examples
    • Relationships
    • Transactions
  • Advanced Usage
    • Data Definition Language
    • Indexes
    • Triggers
    • Caching
  • Examples and templates
  • Security
    • Query parameterization and protection against SQL injections
    • Logging
    • Monitoring
    • Sanitization
  • ORM-CLI
    • Installing
    • Commands and usage
  • System architecture and implementation
    • Support of various databases
      • Implementation using a pattern strategy
      • Implementation using generics
      • Implementation using metaprogramming
    • Decorators
    • Migrations
      • Assignment of id in Tables and Columns
      • Assigning IDs to indexes and triggers
      • Working with migrations in ORM-CLI
        • Implementation of a pattern command for migration
    • QueryBuilder
    • Caching
    • File structure
  • API
    • Decorators
      • Column
      • ComputedColumn
      • Index
      • ForeignKey
      • PrimaryGeneratedColumn
      • Relations
      • Table
      • Trigger
      • Types decorators
        • Integer
        • Numeric
        • Float
        • Boolean
        • String
    • DDL
    • QueryBuilder
    • Additional API
Powered by GitBook
On this page
  1. Advanced Usage

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.

PreviousData Definition LanguageNextTriggers

Last updated 9 months ago