TrendsDB: fix parameterized queries

This commit is contained in:
Alex Gleason 2023-07-25 12:10:37 -05:00
parent 9ebf83af5d
commit 3bdde98f8f
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 9 additions and 6 deletions

View File

@ -17,15 +17,18 @@ class TrendsDB {
`); `);
} }
getTrendingTags(): string[] { getTrendingTags(since: Date, until: Date): string[] {
return this.#db.query<string[]>(` return this.#db.query<string[]>(
`
SELECT tag, COUNT(DISTINCT pubkey8) SELECT tag, COUNT(DISTINCT pubkey8)
FROM tag_usages FROM tag_usages
WHERE inserted_at >= $1 AND inserted_at < $2 WHERE inserted_at >= ? AND inserted_at < ?
GROUP BY tag GROUP BY tag
ORDER BY COUNT(DISTINCT pubkey8) ORDER BY COUNT(DISTINCT pubkey8)
DESC LIMIT 10; DESC LIMIT 10;
`).map((row) => row[0]); `,
[since, until],
).map((row) => row[0]);
} }
addTagUsage(tag: string, pubkey8: string): void { addTagUsage(tag: string, pubkey8: string): void {
@ -35,10 +38,10 @@ class TrendsDB {
); );
} }
cleanupTagUsages(): void { cleanupTagUsages(until: Date): void {
this.#db.query( this.#db.query(
'DELETE FROM tag_usages WHERE inserted_at < ?', 'DELETE FROM tag_usages WHERE inserted_at < ?',
[new Date(Date.now() - 1000 * 60 * 60 * 24 * 7)], [until],
); );
} }
} }