Back

Version 6

4/23/2024
import { sqlite } from "https://esm.town/v/std/sqlite";

export async function getUserByUsername(username: string) {
const userResult = await sqlite.execute({
sql: `SELECT id, name, bio, username, email FROM users WHERE username = ?`,
args: [username],
});

if (userResult.rows.length === 0) {
throw new Error("User not found");
}

const [id, name, bio, usernameRetrieved, email] = userResult.rows[0];

const linksResult = await sqlite.execute({
sql: `SELECT id, label, url FROM user_links WHERE user_id = ?`,
args: [id],
});

const links = linksResult.rows.map(([id, label, url]) => ({ id, label, url }));

return { id, name, bio, username: usernameRetrieved, email, links };
}

export async function updateUser(userId, name, bio) {
if (typeof userId !== "number") {
throw new Error("userId must be a number.");
}
if (typeof name !== "string" || typeof bio !== "string") {
throw new Error("name and bio must be strings.");
}

await sqlite.execute({
sql: `UPDATE users SET name = ?, bio = ? WHERE id = ?`,
args: [name, bio, userId],
});
Updated: May 23, 2024