Skip to content

Instantly share code, notes, and snippets.

@kasramp
Created May 28, 2020 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kasramp/81071bb486e7f586eba93b2beb55aee8 to your computer and use it in GitHub Desktop.
Save kasramp/81071bb486e7f586eba93b2beb55aee8 to your computer and use it in GitHub Desktop.
import { Client } from "https://deno.land/x/mysql/mod.ts";
import config from "../config.js";
const client = await new Client().connect({
hostname: config.database.host,
username: config.database.username,
password: config.database.password,
db: config.database.name,
poolSize: 10
});
const getUsers = async () => {
return await client.query("SELECT id, first_name AS firstName, last_name as lastName, age FROM users");
}
const getUserById = async (id) => {
return await client.query(`SELECT id, first_name AS firstName, last_name AS lastName, age ` +
`FROM users WHERE id = ?`, [id]);
}
const addUser = async (firstName, lastName, age) => {
const result = await client.execute(`INSERT INTO users(first_name, last_name, age) VALUES(?, ?, ?)`, [firstName, lastName, age]);
return getUserById(result.lastInsertId);
}
const deleteUser = async (id) => {
return (await client.execute("DELETE FROM users WHERE id = ?", [id])).affectedRows > 0 ? true : false;
}
const updateUser = async (id, firstName, lastName, age) => {
const result = await client.execute("UPDATE users SET first_name = ?, last_name = ?, age = ? WHERE id = ?", [firstName, lastName, age, id]);
return getUserById(id);
}
export default {
getUsers,
getUserById,
addUser,
deleteUser,
updateUser
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment