mirror of https://github.com/fantasticit/think.git
sever: fix sql to select recent documents
This commit is contained in:
parent
d1f51707a8
commit
f37820f79f
|
@ -19,21 +19,12 @@ export class ViewService {
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
async create({ userId = 'public', documentId, userAgent }) {
|
async create({ userId = 'public', documentId, userAgent }) {
|
||||||
const old = await this.viewRepo.findOne({ documentId, userId });
|
const data = await this.viewRepo.create({
|
||||||
let data;
|
userId,
|
||||||
|
documentId,
|
||||||
if (old) {
|
originUserAgent: userAgent,
|
||||||
data = await this.viewRepo.merge(old, {
|
parsedUserAgent: parseUserAgent(userAgent).text,
|
||||||
updatedAt: convertDateToMysqlTimestamp(Date.now()),
|
});
|
||||||
});
|
|
||||||
} else {
|
|
||||||
data = await this.viewRepo.create({
|
|
||||||
userId,
|
|
||||||
documentId,
|
|
||||||
originUserAgent: userAgent,
|
|
||||||
parsedUserAgent: parseUserAgent(userAgent).text,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
const ret = await this.viewRepo.save(data);
|
const ret = await this.viewRepo.save(data);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -80,12 +71,17 @@ export class ViewService {
|
||||||
const count = 20;
|
const count = 20;
|
||||||
|
|
||||||
const ret = await this.viewRepo.query(
|
const ret = await this.viewRepo.query(
|
||||||
`SELECT documentId, ANY_VALUE(updated_at) as visitedAt
|
`
|
||||||
FROM view
|
SELECT v.documentId, v.visitedAt FROM (
|
||||||
WHERE view.userId = '${userId}'
|
SELECT ANY_VALUE(documentId) as documentId, ANY_VALUE(created_at) as visitedAt
|
||||||
AND (view.updated_at BETWEEN '${from}' AND '${end}')
|
FROM view
|
||||||
GROUP BY documentId
|
WHERE view.userId = '${userId}'
|
||||||
LIMIT ${count}
|
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());
|
ret.sort((a, b) => -new Date(a.visitedAt).getTime() + new Date(b.visitedAt).getTime());
|
||||||
|
|
Loading…
Reference in New Issue