week5-node-express-knex-pg/models/subscribers.js

52 lines
1.1 KiB
JavaScript
Raw Normal View History

2024-10-21 17:09:40 +01:00
import db from "../db/db.js";
const getAllSubscribers = async () => {
const results = await db
.select("*")
.from("subscribers")
.orderBy([{ column: "lastname", order: "asc" }]);
console.log(results);
return results;
};
const addOneSubscriber = async (data) => {
console.log(data);
const { firstname, lastname, email } = data;
const [id] = await db("subscribers")
.insert({
firstname: firstname,
lastname: lastname,
email: email
})
.returning("id");
return id;
};
const selectOneSubscriber = async (data) => {
console.log(data);
const { id } = data;
const result = await db.select("*").from("subscribers").where("id", id);
console.log(`..resuts .. ${Object.keys(result)}`);
return result;
};
const deleteOneSubscriber = async (data) => {
console.log(data);
const { id } = data;
const [result] = await db("subscribers")
.where("id", id)
.del()
.returning(["id", "firstname", "lastname", "email"]);
return result;
};
export {
getAllSubscribers,
addOneSubscriber,
selectOneSubscriber,
deleteOneSubscriber
};