Starting post load

This commit is contained in:
2024-05-09 16:06:43 +03:00
parent ec5769f70e
commit e2d2599a95
18 changed files with 274 additions and 3 deletions
+41
View File
@@ -0,0 +1,41 @@
/**
* @param {import('postgres').Sql} sql
* @param {number | undefined} category
* @param {number} limit
* @param {number} offset
* @returns {Promise<import('$types/base').Post[]>}
*/
export async function getPosts(sql, category = undefined, limit = 10, offset = 0) {
let query;
if (category === undefined) {
query = sql`
SELECT name, latest_content
FROM doki8902.message_post
FETCH FIRST ${limit} ROWS ONLY
OFFSET ${offset};`;
} else {
query = sql`
SELECT name, latest_content
FROM doki8902.message_post
WHERE category_id = ${category}
FETCH FIRST ${limit} ROWS ONLY
OFFSET ${offset};`;
}
let posts = await query;
/**
* @type {import('$types/base').Post[]}
*/
let result = [];
posts.forEach(row => {
result.push({
name: row['name'],
content: row['latest_content']
});
});
return result;
}
+6
View File
@@ -0,0 +1,6 @@
// export function runQuery(
// /** @type {import('postgres').Sql} */ sql,
// /** @type {string} */ query,
// /** @type {any[]} */ args = []) {
// sql`${query}`
// }
+8
View File
@@ -0,0 +1,8 @@
import dotenv from 'dotenv';
dotenv.config();
/**
* @type {import('$types/env').Env}
*/
export const env = process.env;