add index

This commit is contained in:
2025-12-11 18:37:50 +03:30
parent a54e8eb9f3
commit 49ec05b410
12 changed files with 42 additions and 11 deletions
+3 -1
View File
@@ -1,7 +1,9 @@
import { PrimaryKey, Property, Filter, OptionalProps } from '@mikro-orm/core'; import { Index, PrimaryKey, Property, Filter, OptionalProps } from '@mikro-orm/core';
import { ulid } from 'ulid'; import { ulid } from 'ulid';
@Filter({ name: 'notDeleted', cond: { deletedAt: null }, default: true }) @Filter({ name: 'notDeleted', cond: { deletedAt: null }, default: true })
@Index({ properties: ['deletedAt'] })
@Index({ properties: ['createdAt'] })
export abstract class BaseEntity { export abstract class BaseEntity {
[OptionalProps]?: 'id' | 'createdAt' | 'updatedAt' | 'deletedAt'; [OptionalProps]?: 'id' | 'createdAt' | 'updatedAt' | 'deletedAt';
@@ -1,4 +1,4 @@
import { Entity, ManyToOne, Unique } from '@mikro-orm/core'; import { Entity, Index, ManyToOne, Unique } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity'; import { BaseEntity } from '../../../common/entities/base.entity';
import { Role } from '../../roles/entities/role.entity'; import { Role } from '../../roles/entities/role.entity';
import { Restaurant } from 'src/modules/restaurants/entities/restaurant.entity'; import { Restaurant } from 'src/modules/restaurants/entities/restaurant.entity';
@@ -6,6 +6,9 @@ import { Admin } from './admin.entity';
@Entity({ tableName: 'admin_roles' }) @Entity({ tableName: 'admin_roles' })
@Unique({ properties: ['admin', 'restaurant'] }) @Unique({ properties: ['admin', 'restaurant'] })
@Index({ properties: ['admin', 'restaurant'] })
@Index({ properties: ['admin'] })
@Index({ properties: ['restaurant'] })
export class AdminRole extends BaseEntity { export class AdminRole extends BaseEntity {
@ManyToOne(() => Admin) @ManyToOne(() => Admin)
admin!: Admin; admin!: Admin;
@@ -1,4 +1,4 @@
import { Entity, Enum, Property } from '@mikro-orm/core'; import { Entity, Enum, Index, Property } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity'; import { BaseEntity } from '../../../common/entities/base.entity';
@@ -8,6 +8,9 @@ export enum RefreshTokenType {
} }
@Entity({ tableName: 'refreshtokens' }) @Entity({ tableName: 'refreshtokens' })
@Index({ properties: ['ownerId', 'restId', 'type'] })
@Index({ properties: ['hashedToken'] })
@Index({ properties: ['expiresAt'] })
export class RefreshToken extends BaseEntity { export class RefreshToken extends BaseEntity {
@Property({ type: 'varchar', length: 255 }) @Property({ type: 'varchar', length: 255 })
hashedToken!: string; hashedToken!: string;
@@ -1,4 +1,4 @@
import { Entity, ManyToOne, Property, Enum, Unique } from '@mikro-orm/core'; import { Entity, Index, ManyToOne, Property, Enum, Unique } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity'; import { BaseEntity } from '../../../common/entities/base.entity';
import { Restaurant } from '../../restaurants/entities/restaurant.entity'; import { Restaurant } from '../../restaurants/entities/restaurant.entity';
import { normalizePhone } from '../../utils/phone.util'; import { normalizePhone } from '../../utils/phone.util';
@@ -6,6 +6,9 @@ import { CouponType } from '../interface/coupon';
@Entity({ tableName: 'coupons' }) @Entity({ tableName: 'coupons' })
@Unique({ properties: ['code', 'restaurant'] }) @Unique({ properties: ['code', 'restaurant'] })
@Index({ properties: ['restaurant', 'code', 'isActive'] })
@Index({ properties: ['restaurant', 'isActive'] })
@Index({ properties: ['code'] })
export class Coupon extends BaseEntity { export class Coupon extends BaseEntity {
@ManyToOne(() => Restaurant) @ManyToOne(() => Restaurant)
restaurant!: Restaurant; restaurant!: Restaurant;
@@ -1,9 +1,11 @@
import { Entity, Property, Collection, OneToMany, ManyToOne } from '@mikro-orm/core'; import { Entity, Index, Property, Collection, OneToMany, ManyToOne } from '@mikro-orm/core';
import { Food } from './food.entity'; import { Food } from './food.entity';
import { BaseEntity } from '../../../common/entities/base.entity'; import { BaseEntity } from '../../../common/entities/base.entity';
import { Restaurant } from 'src/modules/restaurants/entities/restaurant.entity'; import { Restaurant } from 'src/modules/restaurants/entities/restaurant.entity';
@Entity({ tableName: 'categories' }) @Entity({ tableName: 'categories' })
@Index({ properties: ['restaurant', 'isActive'] })
@Index({ properties: ['isActive'] })
export class Category extends BaseEntity { export class Category extends BaseEntity {
@Property() @Property()
title!: string; title!: string;
+4 -1
View File
@@ -1,10 +1,13 @@
import { Cascade, Collection, Entity, ManyToOne, OneToMany, Property } from '@mikro-orm/core'; import { Cascade, Collection, Entity, Index, ManyToOne, OneToMany, Property } from '@mikro-orm/core';
import { Category } from './category.entity'; import { Category } from './category.entity';
import { BaseEntity } from '../../../common/entities/base.entity'; import { BaseEntity } from '../../../common/entities/base.entity';
import { Restaurant } from '../../../modules/restaurants/entities/restaurant.entity'; import { Restaurant } from '../../../modules/restaurants/entities/restaurant.entity';
import { Review } from 'src/modules/review/entities/review.entity'; import { Review } from 'src/modules/review/entities/review.entity';
@Entity({ tableName: 'foods' }) @Entity({ tableName: 'foods' })
@Index({ properties: ['restaurant', 'isActive'] })
@Index({ properties: ['category', 'isActive'] })
@Index({ properties: ['isActive'] })
export class Food extends BaseEntity { export class Food extends BaseEntity {
@ManyToOne(() => Restaurant) @ManyToOne(() => Restaurant)
restaurant: Restaurant; restaurant: Restaurant;
@@ -1,9 +1,11 @@
import { Entity, ManyToOne, Property } from '@mikro-orm/core'; import { Entity, Index, ManyToOne, Property } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity'; import { BaseEntity } from '../../../common/entities/base.entity';
import { Order } from './order.entity'; import { Order } from './order.entity';
import { Food } from '../../foods/entities/food.entity'; import { Food } from '../../foods/entities/food.entity';
@Entity({ tableName: 'order_items' }) @Entity({ tableName: 'order_items' })
@Index({ properties: ['order'] })
@Index({ properties: ['food'] })
export class OrderItem extends BaseEntity { export class OrderItem extends BaseEntity {
@ManyToOne(() => Order) @ManyToOne(() => Order)
order!: Order; order!: Order;
@@ -1,5 +1,6 @@
import { import {
Entity, Entity,
Index,
ManyToOne, ManyToOne,
OneToMany, OneToMany,
Property, Property,
@@ -22,6 +23,11 @@ import { Delivery } from '../../delivery/entities/delivery.entity';
@Entity({ tableName: 'orders' }) @Entity({ tableName: 'orders' })
@Unique({ properties: ['restaurant', 'orderNumber'] }) @Unique({ properties: ['restaurant', 'orderNumber'] })
@Index({ properties: ['restaurant', 'status'] })
@Index({ properties: ['user', 'status'] })
@Index({ properties: ['restaurant', 'orderNumber'] })
@Index({ properties: ['status'] })
@Index({ properties: ['paymentStatus'] })
export class Order extends BaseEntity { export class Order extends BaseEntity {
@ManyToOne(() => User) @ManyToOne(() => User)
user!: User; user!: User;
@@ -1,8 +1,10 @@
import { Collection, Entity, OneToMany, Property } from '@mikro-orm/core'; import { Collection, Entity, Index, OneToMany, Property } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity'; import { BaseEntity } from '../../../common/entities/base.entity';
import { Delivery } from '../../delivery/entities/delivery.entity'; import { Delivery } from '../../delivery/entities/delivery.entity';
@Entity({ tableName: 'restaurants' }) @Entity({ tableName: 'restaurants' })
@Index({ properties: ['isActive'] })
@Index({ properties: ['slug', 'isActive'] })
export class Restaurant extends BaseEntity { export class Restaurant extends BaseEntity {
// --- اطلاعات پایه --- // --- اطلاعات پایه ---
@Property() @Property()
+4 -1
View File
@@ -1,4 +1,4 @@
import { Entity, ManyToOne, Property, Unique, Enum } from '@mikro-orm/core'; import { Entity, Index, ManyToOne, Property, Unique, Enum } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity'; import { BaseEntity } from '../../../common/entities/base.entity';
import { Food } from '../../foods/entities/food.entity'; import { Food } from '../../foods/entities/food.entity';
import { User } from '../../users/entities/user.entity'; import { User } from '../../users/entities/user.entity';
@@ -8,6 +8,9 @@ import { ReviewStatus } from '../enums/review-status.enum';
@Entity({ tableName: 'reviews' }) @Entity({ tableName: 'reviews' })
@Unique({ properties: ['order', 'food', 'user'] }) @Unique({ properties: ['order', 'food', 'user'] })
@Index({ properties: ['food', 'status'] })
@Index({ properties: ['user'] })
@Index({ properties: ['order'] })
export class Review extends BaseEntity { export class Review extends BaseEntity {
@ManyToOne(() => Order) @ManyToOne(() => Order)
order: Order; order: Order;
+2 -1
View File
@@ -1,4 +1,4 @@
import { Collection, Entity, ManyToMany, OneToMany, ManyToOne, Property } from '@mikro-orm/core'; import { Collection, Entity, Index, ManyToMany, OneToMany, ManyToOne, Property } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity'; import { BaseEntity } from '../../../common/entities/base.entity';
import { Permission } from './permission.entity'; import { Permission } from './permission.entity';
import { Restaurant } from '../../restaurants/entities/restaurant.entity'; import { Restaurant } from '../../restaurants/entities/restaurant.entity';
@@ -6,6 +6,7 @@ import { RolePermission } from './rolePermission.entity';
import { AdminRole } from 'src/modules/admin/entities/adminRole.entity'; import { AdminRole } from 'src/modules/admin/entities/adminRole.entity';
@Entity({ tableName: 'roles' }) @Entity({ tableName: 'roles' })
@Index({ properties: ['restaurant'] })
export class Role extends BaseEntity { export class Role extends BaseEntity {
@Property() @Property()
name!: string; name!: string;
+2 -1
View File
@@ -1,10 +1,11 @@
import { Entity, Property, OneToMany, Collection, Cascade } from '@mikro-orm/core'; import { Entity, Index, Property, OneToMany, Collection, Cascade } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity'; import { BaseEntity } from '../../../common/entities/base.entity';
import { UserAddress } from './user-address.entity'; import { UserAddress } from './user-address.entity';
import { Order } from 'src/modules/orders/entities/order.entity'; import { Order } from 'src/modules/orders/entities/order.entity';
import { normalizePhone } from '../../utils/phone.util'; import { normalizePhone } from '../../utils/phone.util';
@Entity({ tableName: 'users' }) @Entity({ tableName: 'users' })
@Index({ properties: ['isActive'] })
export class User extends BaseEntity { export class User extends BaseEntity {
@OneToMany(() => Order, order => order.user) @OneToMany(() => Order, order => order.user)
orders = new Collection<Order>(this); orders = new Collection<Order>(this);