From f37820f79f0a9a94194049deff27af5ae585497b Mon Sep 17 00:00:00 2001 From: fantasticit Date: Wed, 25 May 2022 12:39:44 +0800 Subject: [PATCH] sever: fix sql to select recent documents --- packages/server/src/services/view.service.ts | 38 +++++++++----------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/packages/server/src/services/view.service.ts b/packages/server/src/services/view.service.ts index f9b8936..2f0de57 100644 --- a/packages/server/src/services/view.service.ts +++ b/packages/server/src/services/view.service.ts @@ -19,21 +19,12 @@ export class ViewService { * @returns */ async create({ userId = 'public', documentId, userAgent }) { - const old = await this.viewRepo.findOne({ documentId, userId }); - let data; - - if (old) { - data = await this.viewRepo.merge(old, { - updatedAt: convertDateToMysqlTimestamp(Date.now()), - }); - } else { - data = await this.viewRepo.create({ - userId, - documentId, - originUserAgent: userAgent, - parsedUserAgent: parseUserAgent(userAgent).text, - }); - } + const data = await this.viewRepo.create({ + userId, + documentId, + originUserAgent: userAgent, + parsedUserAgent: parseUserAgent(userAgent).text, + }); const ret = await this.viewRepo.save(data); return ret; } @@ -80,12 +71,17 @@ export class ViewService { const count = 20; const ret = await this.viewRepo.query( - `SELECT documentId, ANY_VALUE(updated_at) as visitedAt - FROM view - WHERE view.userId = '${userId}' - AND (view.updated_at BETWEEN '${from}' AND '${end}') - GROUP BY documentId - LIMIT ${count} + ` + SELECT v.documentId, v.visitedAt FROM ( + SELECT ANY_VALUE(documentId) as documentId, ANY_VALUE(created_at) as visitedAt + FROM view + WHERE view.userId = '${userId}' + AND (view.created_at BETWEEN '${from}' AND '${end}') + GROUP BY visitedAt + ORDER BY visitedAt DESC + ) v + GROUP BY v.documentId + LIMIT ${count} ` ); ret.sort((a, b) => -new Date(a.visitedAt).getTime() + new Date(b.visitedAt).getTime());