Skip to content

Commit 095754d

Browse files
authored
perf(api): otimiza paginação em fetchChats usando LIMIT/OFFSET no SQL
1 parent b94b452 commit 095754d

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/api/services/channel.service.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,9 @@ export class ChannelStartupService {
697697
AND "Message"."messageTimestamp" <= ${Math.floor(new Date(query.where.messageTimestamp.lte).getTime() / 1000)}`
698698
: Prisma.sql``;
699699

700+
const limit = query?.take ? Prisma.sql`LIMIT ${query.take}` : Prisma.sql``;
701+
const offset = query?.skip ? Prisma.sql`OFFSET ${query.skip}` : Prisma.sql``;
702+
700703
const results = await this.prismaRepository.$queryRaw`
701704
WITH rankedMessages AS (
702705
SELECT DISTINCT ON ("Message"."key"->>'remoteJid')
@@ -732,7 +735,9 @@ export class ChannelStartupService {
732735
ORDER BY "Message"."key"->>'remoteJid', "Message"."messageTimestamp" DESC
733736
)
734737
SELECT * FROM rankedMessages
735-
ORDER BY "updatedAt" DESC NULLS LAST;
738+
ORDER BY "updatedAt" DESC NULLS LAST
739+
${limit}
740+
${offset};
736741
`;
737742

738743
if (results && Array.isArray(results) && results.length > 0) {

0 commit comments

Comments
 (0)