nim/src/db.ts
2025-04-16 22:28:11 +08:00

57 lines
No EOL
1.2 KiB
TypeScript

import { main } from "bun";
import { sql } from "bun";
import { MIMEType } from "util"
export interface Attachment {
type: MIMEType;
urls: string[];
}
export interface Post {
content: string,
hash: string,
}
export async function setup_func() {
const rows = await sql`
CREATE OR REPLACE FUNCTION fetch_post(
OUT p_text VARCHAR(1000),
OUT p_hash CHAR(32),
OUT p_date TIMESTAMP,
OUT p_pics INT[]
)
RETURNS SETOF RECORD AS
$$
DECLARE
post_cursor CURSOR FOR
SELECT content, hash, date
FROM posts;
post_record RECORD;
BEGIN
-- Open cursor
OPEN post_cursor;
-- Fetch rows and return
LOOP
FETCH NEXT FROM post_cursor INTO post_record;
EXIT WHEN NOT FOUND;
p_text = post_record.content;
p_date = post_record.date;
p_hash = post_record.hash;
RETURN NEXT;
END LOOP;
-- Close cursor
CLOSE post_cursor;
END;
$$
LANGUAGE PLPGSQL;`;
}
/* retrieve the latest post with posts table */
export async function retrieve_post(offset: Number) {
const res = await sql`SELECT * FROM fetch_post();`
console.log(res)
}
setup_func()
retrieve_post(0)