Skip to content

Commit

Permalink
perf: enhance perf when query
Browse files Browse the repository at this point in the history
  • Loading branch information
conganhhcmus committed Mar 23, 2024
1 parent b7d0f55 commit bfd61e8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/models/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import mongoose, { Types } from 'mongoose';
const TransactionSchema = new mongoose.Schema({
amount: { type: Number, required: true },
sourceId: { type: Types.ObjectId, required: false },
targetId: { type: Types.ObjectId, required: true },
targetId: { type: Types.ObjectId, required: true, index: true },
type: { type: Number, required: true, index: true },
status: { type: Number, required: true, index: true },
createTime: { type: Date, required: true },
updateTime: { type: Date, required: false, default: moment().utc().toDate() },
updateTime: { type: Date, required: false, default: moment().utc().toDate(), index: true },
updateBy: { type: Types.ObjectId, required: false },
});

Expand Down
12 changes: 6 additions & 6 deletions src/repositories/comics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { ComicStatus } from '@/constants/comic';
const getComicByPageAndQuery = async (page: number, query: {}, sort: {}, pageSize: number = DEFAULT_COMIC_PAGE_SIZE) => {
const total = await ComicsModel.countDocuments(query).exec();
const comics = await ComicsModel.aggregate([
{ $match: query },
{ $sort: sort },
{ $skip: pageSize * page - pageSize },
{ $limit: pageSize },
{
$lookup: {
from: 'chapters',
Expand All @@ -30,10 +34,6 @@ const getComicByPageAndQuery = async (page: number, query: {}, sort: {}, pageSiz
as: 'genres',
},
},
{ $match: query },
{ $sort: sort },
{ $skip: pageSize * page - pageSize },
{ $limit: pageSize },
]);

return { data: comics, totalPage: Math.ceil(total / pageSize), currentPage: page };
Expand All @@ -54,7 +54,7 @@ export const getAllComics = async (page: number, q: string) => {
};

export const getComicsByGenres = async (type: string, page: number) => {
const query = type == 'all' ? {} : { 'genres._id': new Types.ObjectId(type) };
const query = type == 'all' ? {} : { genres: { $in: [new Types.ObjectId(type)] } };
const sort = { createTime: -1, updateTime: -1 };

return getComicByPageAndQuery(page, query, sort);
Expand Down Expand Up @@ -84,6 +84,7 @@ export const getTopComics = async (type: string, page: number, status: string) =
export const getComicById = async (id: string) => {
const query = { _id: new Types.ObjectId(id) };
const comics = await ComicsModel.aggregate([
{ $match: query },
{
$lookup: {
from: 'chapters',
Expand All @@ -107,7 +108,6 @@ export const getComicById = async (id: string) => {
as: 'genres',
},
},
{ $match: query },
]);

return comics[0] || undefined;
Expand Down
16 changes: 8 additions & 8 deletions src/repositories/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ export const createTransaction = (values: Record<string, any>): Promise<Transact

export const getAllTransaction = async (option: number, status: number[], page: number, q: string, sort: {}) => {
const date = moment().utc().endOf('day').subtract(option, 'days').toDate();
const query =
option !== 0
? { status: { $in: status }, updateTime: { $gt: date }, 'users.username': { $regex: '.*' + q + '.*', $options: 'i' } }
: { status: { $in: status }, 'users.username': { $regex: '.*' + q + '.*', $options: 'i' } };
const queryOption = option !== 0 ? { status: { $in: status }, updateTime: { $gt: date } } : { status: { $in: status } };
const querySearch = !!q ? { 'users.username': { $regex: '.*' + q + '.*', $options: 'i' } } : {};
const total = await TransactionModel.aggregate([
{ $match: queryOption },
{
$lookup: {
from: 'users',
Expand All @@ -23,6 +22,7 @@ export const getAllTransaction = async (option: number, status: number[], page:
as: 'users',
},
},
{ $match: querySearch },
{
$lookup: {
from: 'users',
Expand All @@ -31,10 +31,10 @@ export const getAllTransaction = async (option: number, status: number[], page:
as: 'updateUsers',
},
},
{ $match: query },
]);

let transaction = await TransactionModel.aggregate([
{ $match: queryOption },
{
$lookup: {
from: 'users',
Expand All @@ -43,6 +43,9 @@ export const getAllTransaction = async (option: number, status: number[], page:
as: 'users',
},
},
{ $match: querySearch },
{ $sort: sort },
{ $skip: DEFAULT_PAGE_SIZE * page - DEFAULT_PAGE_SIZE },
{
$lookup: {
from: 'users',
Expand All @@ -51,9 +54,6 @@ export const getAllTransaction = async (option: number, status: number[], page:
as: 'updateUsers',
},
},
{ $match: query },
{ $sort: sort },
{ $skip: DEFAULT_PAGE_SIZE * page - DEFAULT_PAGE_SIZE },
]);
const limit = Math.min(DEFAULT_PAGE_SIZE, transaction.length);

Expand Down
8 changes: 5 additions & 3 deletions src/repositories/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { Types } from 'mongoose';

export const getUsers = async (page: number, q: string, type: string) => {
const queryType = type === '0' ? {} : { 'transactions.0': { $exists: type === '1' } };
const query = !!q ? { username: { $regex: '.*' + q + '.*', $options: 'i' }, ...queryType } : { ...queryType };
const queryUsername = !!q ? { username: { $regex: '.*' + q + '.*', $options: 'i' } } : {};
const total = await UserModel.aggregate([
{ $match: queryUsername },
{
$lookup: {
from: 'transactions',
Expand All @@ -23,9 +24,10 @@ export const getUsers = async (page: number, q: string, type: string) => {
as: 'transactions',
},
},
{ $match: query },
{ $match: queryType },
]);
const users = await UserModel.aggregate([
{ $match: queryUsername },
{
$lookup: {
from: 'transactions',
Expand All @@ -41,7 +43,7 @@ export const getUsers = async (page: number, q: string, type: string) => {
as: 'transactions',
},
},
{ $match: query },
{ $match: queryType },
{ $sort: { createTime: -1 } },
{ $skip: DEFAULT_PAGE_SIZE * page - DEFAULT_PAGE_SIZE },
{ $limit: DEFAULT_PAGE_SIZE },
Expand Down

0 comments on commit bfd61e8

Please sign in to comment.