fix: avoid connection leaks in frontpage code
This commit is contained in:
parent
340dd3ac19
commit
afeca1e796
|
@ -2,22 +2,18 @@ import { pgRead, readWritePool } from "./database/pg-wrapper";
|
|||
import { Forecast } from "./platforms";
|
||||
|
||||
export async function getFrontpage(): Promise<Forecast[]> {
|
||||
const client = await readWritePool.connect();
|
||||
const res = await client.query(
|
||||
const res = await readWritePool.query(
|
||||
"SELECT frontpage_sliced FROM frontpage ORDER BY id DESC LIMIT 1"
|
||||
);
|
||||
if (!res.rows.length) return [];
|
||||
console.log(res.rows[0].frontpage_sliced);
|
||||
return res.rows[0].frontpage_sliced;
|
||||
}
|
||||
|
||||
export async function getFrontpageFull(): Promise<Forecast[]> {
|
||||
const client = await readWritePool.connect();
|
||||
const res = await client.query(
|
||||
const res = await readWritePool.query(
|
||||
"SELECT frontpage_full FROM frontpage ORDER BY id DESC LIMIT 1"
|
||||
);
|
||||
if (!res.rows.length) return [];
|
||||
console.log(res.rows[0]);
|
||||
return res.rows[0].frontpage_full;
|
||||
}
|
||||
|
||||
|
@ -26,9 +22,8 @@ export async function rebuildFrontpage() {
|
|||
tableName: "questions",
|
||||
});
|
||||
|
||||
const client = await readWritePool.connect();
|
||||
const frontpageSliced = (
|
||||
await client.query(`
|
||||
await readWritePool.query(`
|
||||
SELECT * FROM questions
|
||||
WHERE
|
||||
(qualityindicators->>'stars')::int >= 3
|
||||
|
@ -39,7 +34,7 @@ export async function rebuildFrontpage() {
|
|||
).rows;
|
||||
|
||||
const start = Date.now();
|
||||
await client.query(
|
||||
await readWritePool.query(
|
||||
"INSERT INTO frontpage(frontpage_full, frontpage_sliced) VALUES($1, $2)",
|
||||
[JSON.stringify(frontpageFull), JSON.stringify(frontpageSliced)]
|
||||
);
|
||||
|
|
|
@ -7,7 +7,5 @@ export default async function handler(
|
|||
res: NextApiResponse
|
||||
) {
|
||||
let frontpageElements = await getFrontpage();
|
||||
console.log(frontpageElements.map((element) => element.title).slice(0, 5));
|
||||
console.log("...");
|
||||
res.status(200).json(frontpageElements);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user