Skip to content

Commit

Permalink
fix: add indexes for faster query
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiin committed Apr 6, 2024
1 parent e29df3b commit eae7e27
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 64 deletions.
13 changes: 9 additions & 4 deletions src/entities/category.entity.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { Collection, Entity, ManyToMany, Property } from "@mikro-orm/postgresql";
import {
Collection,
Entity,
ManyToMany,
Property,
} from "@mikro-orm/postgresql";
import { BaseEntity } from "@common/database";
import { Post } from "./index";

@Entity()
export class Category extends BaseEntity {
@Property({ index: true })
name!: string;
name!: string;

@Property()
description!: string;
description!: string;

@ManyToMany(() => Post, post => post.categories)
posts = new Collection<Post>(this);
posts = new Collection<Post>(this);

constructor(partial?: Partial<Category>) {
super();
Expand Down
9 changes: 4 additions & 5 deletions src/entities/comment.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ import type { Post, User } from "./index";
@Entity()
export class Comment extends BaseEntity {
@Property()
body!: string;
body!: string;

@ManyToOne({
eager: false,
index: true,
})
post!: Rel<Ref<Post>>;
post!: Rel<Ref<Post>>;

@ManyToOne({
eager: false,
index: true,
})
author!: Rel<Ref<User>>;
author!: Rel<Ref<User>>;

constructor(partial?: Partial<Comment>) {
super();
Expand Down
14 changes: 10 additions & 4 deletions src/entities/conversation.entity.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import { BaseEntity } from "@common/database";
import { Collection, Entity, ManyToMany, OneToMany, Property } from "@mikro-orm/postgresql";
import {
Collection,
Entity,
ManyToMany,
OneToMany,
Property,
} from "@mikro-orm/postgresql";
import { Message, User } from "./index";

@Entity()
export class Conversation extends BaseEntity {
@Property({ index: true })
chatName!: string;
chatName!: string;

@ManyToMany(() => User, user => user.conversations, { index: true })
users = new Collection<User>(this);
users = new Collection<User>(this);

@OneToMany(() => Message, message => message.conversation, {
orphanRemoval: true,
})
messages = new Collection<Message>(this);
messages = new Collection<Message>(this);

constructor(partial?: Partial<Conversation>) {
super();
Expand Down
14 changes: 6 additions & 8 deletions src/entities/message.entity.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
import { BaseEntity } from "@common/database";
import type { Opt , Ref } from "@mikro-orm/postgresql";
import type { Opt, Ref } from "@mikro-orm/postgresql";
import { Entity, ManyToOne, Property, Rel } from "@mikro-orm/postgresql";
import type { Conversation, User } from "./index";

@Entity()
export class Message extends BaseEntity {
@Property()
body!: string;
body!: string;

@ManyToOne({
eager: false,
index: true,
})
sender!: Rel<Ref<User>>;
sender!: Rel<Ref<User>>;

@ManyToOne({
eager: false,
index: true,
})
conversation!: Rel<Ref<Conversation>>;
conversation!: Rel<Ref<Conversation>>;

@Property()
isRead: boolean & Opt = false;
isRead: boolean & Opt = false;

@Property()
readAt?: Date;
readAt?: Date;

constructor(partial?: Partial<Message>) {
super();
Expand Down
8 changes: 4 additions & 4 deletions src/entities/news-letter.entity.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Entity ,Property} from "@mikro-orm/postgresql";
import { Entity, Property } from "@mikro-orm/postgresql";
import { BaseEntity } from "@common/database";

@Entity()
export class NewsLetter extends BaseEntity {
@Property({ index: true, unique: true })
name!: string;
name!: string;

@Property({ columnType: "text" })
content!: string;
content!: string;

@Property()
sentAt?: Date;
sentAt?: Date;

constructor(partial?: Partial<NewsLetter>) {
super();
Expand Down
11 changes: 5 additions & 6 deletions src/entities/otp-log.entity.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import type { Opt ,Ref } from "@mikro-orm/postgresql";
import type { Opt, Ref } from "@mikro-orm/postgresql";
import { Entity, ManyToOne, Property, Rel } from "@mikro-orm/postgresql";
import { BaseEntity } from "@common/database";
import type { User } from "./user.entity";

@Entity()
export class OtpLog extends BaseEntity {
@Property()
expiresIn!: Date;
expiresIn!: Date;

@Property({
length: 20,
index: true,
})
otpCode?: string;
otpCode?: string;

@ManyToOne({
eager: false,
index: true,
})
user!: Rel<Ref<User>>;
user!: Rel<Ref<User>>;

@Property()
isUsed: boolean & Opt = false;
isUsed: boolean & Opt = false;

constructor(partial?: Partial<OtpLog>) {
super();
Expand Down
6 changes: 3 additions & 3 deletions src/entities/points-redemption-log.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import type { User } from "./user.entity";
@Entity()
export class PointRedemptionLog extends BaseEntity {
@Property()
points!: number;
points!: number;

@Property({ columnType: "numeric(9,2)" })
amount!: string;
amount!: string;

@ManyToOne()
user!: Rel<Ref<User>>;
user!: Rel<Ref<User>>;

constructor(partial?: Partial<PointRedemptionLog>) {
super();
Expand Down
10 changes: 4 additions & 6 deletions src/entities/post.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,18 @@ export class Post extends BaseEntity {
@Property()
readCount: number & Opt = 0;

@Property()
@Property({ index: true })
published: boolean & Opt = false;

@Property()
favoritesCount: number & Opt = 0;

@ManyToOne({
eager: false,
index: true,
})
author!: Rel<Ref<User>>;

@OneToMany(() => Comment, comment => comment.post, {
eager: false,
orphanRemoval: true,
})
comments = new Collection<Comment>(this);
Expand All @@ -76,9 +74,9 @@ export class Post extends BaseEntity {
@BeforeUpdate()
async generateSlug(arguments_: EventArgs<this>) {
if (arguments_.changeSet?.payload?.title) {
this.slug
= `${slugify(this.title)
}-${Math.trunc(Math.random() * 36 ** 6).toString(36)}`;
this.slug = `${slugify(
this.title,
)}-${Math.trunc(Math.random() * 36 ** 6).toString(36)}`;
}
this.readingTime = this.getReadingTime(this.content);
}
Expand Down
8 changes: 4 additions & 4 deletions src/entities/protocol.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import { BaseEntity } from "@common/database";
@Entity()
export class Protocol extends BaseEntity {
@Property()
loginAttemptnumbererval!: number;
loginAttemptnumbererval!: number;

@Property()
loginnumberervalUnit!: string;
loginnumberervalUnit!: string;

@Property()
loginMaxRetry!: number;
loginMaxRetry!: number;

@Property()
otpExpiryInMinutes!: number;
otpExpiryInMinutes!: number;

constructor(partial?: Partial<Protocol>) {
super();
Expand Down
13 changes: 10 additions & 3 deletions src/entities/referral.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import type { Opt, Ref } from "@mikro-orm/postgresql";
import { Entity, Enum, Index, ManyToOne, Property, Rel } from "@mikro-orm/postgresql";
import {
Entity,
Enum,
Index,
ManyToOne,
Property,
Rel,
} from "@mikro-orm/postgresql";
import { ReferralStatus } from "@common/@types";
import { BaseEntity } from "@common/database";
import type { User } from "./user.entity";
Expand All @@ -9,12 +16,12 @@ export class Referral extends BaseEntity {
@ManyToOne({
index: true,
})
referrer!: Rel<Ref<User>>;
referrer!: Rel<Ref<User>>;

@Property({
index: true,
})
mobileNumber!: string;
mobileNumber!: string;

@Index()
@Enum(() => ReferralStatus)
Expand Down
8 changes: 4 additions & 4 deletions src/entities/refresh-token.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import type { User } from "./user.entity";
@Entity()
export class RefreshToken extends BaseEntity {
@Property()
expiresIn!: Date;
expiresIn!: Date;

@ManyToOne({
eager: false,
index: true,
})
user!: Rel<Ref<User>>;
user!: Rel<Ref<User>>;

@Property()
isRevoked: boolean & Opt = false;
isRevoked: boolean & Opt = false;

constructor(partial?: Partial<RefreshToken>) {
super();
Expand Down
2 changes: 1 addition & 1 deletion src/entities/subscribers.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BaseEntity } from "@common/database";
@Entity()
export class Subscriber extends BaseEntity {
@Property({ index: true, unique: true })
email!: string;
email!: string;

constructor(partial?: Partial<Subscriber>) {
super();
Expand Down
10 changes: 4 additions & 6 deletions src/entities/tag.entity.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {
BeforeCreate,

BeforeUpdate,

BeforeUpsert,
Collection,
Entity,
Expand All @@ -21,16 +19,16 @@ export class Tag extends BaseEntity {
index: true,
unique: true,
})
title!: string;
title!: string;

@Property({ columnType: "text" })
description!: string;
description!: string;

@Property({ index: true })
slug?: string;
slug?: string;

@ManyToMany(() => Post, post => post.tags)
posts = new Collection<Post>(this);
posts = new Collection<Post>(this);

constructor(partial?: Partial<Tag>) {
super();
Expand Down
9 changes: 3 additions & 6 deletions src/entities/user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import type {
FilterQuery,
} from "@mikro-orm/postgresql";
import type { FilterQuery } from "@mikro-orm/postgresql";
import {
BeforeCreate,

BeforeUpdate,

BeforeUpsert,
Collection,
Embeddable,
Embedded,
Entity,
Enum,
EventArgs,
Index,
ManyToMany,
OneToMany,
Property,
Expand Down Expand Up @@ -67,6 +64,7 @@ export class User extends BaseEntity {
@Property()
isTwoFactorEnabled? = false;

@Index()
@Enum({ items: () => Roles, array: true })
roles?: Roles[] = [Roles.AUTHOR];

Expand All @@ -78,7 +76,6 @@ export class User extends BaseEntity {

@OneToMany(() => Post, post => post.author, {
orphanRemoval: true,
eager: false
})
posts = new Collection<Post>(this);

Expand Down

0 comments on commit eae7e27

Please sign in to comment.