1234567891011121314151617181920212223242526272829303132333435363738 |
- const db = require('../db/connection');
- class Posts {
- static fectchFirst = (columns, callback) => {
- columns = columns.join(', ')
- const query = `SELECT ${columns} FROM posts ORDER by id LIMIT 1`
- const stmt = db.prepare(query)
- stmt.get({}, callback);
- }
- static fetchOne = (query, params, callback) => {
- const stmt = db.prepare(query);
- stmt.get(params, callback);
- }
- static fetchByPK = (columns, pkValue, callback) => {
- columns = columns.join(', ')
- const query = `SELECT ${columns} FROM posts WHERE id = $id`
- const stmt = db.prepare(query)
- stmt.get({ $id: pkValue }, callback)
- }
- static fetchAll = (query, params, callback) => {
- const stmt = db.prepare(query);
- stmt.all(params, callback);
- }
- static update = (post) => {
- let columns = new Array()
- Object.entries(post).forEach(([key, value]) => {
- columns.push(`\n${key} = "${value}"`)
- })
- const sql = `UPDATE posts\nSET ${columns.join(',')}\nWHERE id = ${post.id}`
- }
- }
- module.exports = Posts;
|