Posts.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. const db = require('../db/connection');
  2. class Posts {
  3. static fectchFirst = (columns, callback) => {
  4. columns = columns.join(', ')
  5. const query = `SELECT ${columns} FROM posts ORDER by id LIMIT 1`
  6. const stmt = db.prepare(query)
  7. stmt.get({}, callback);
  8. }
  9. static fetchOne = (query, params, callback) => {
  10. const stmt = db.prepare(query);
  11. stmt.get(params, callback);
  12. }
  13. static fetchByPK = (columns, pkValue, callback) => {
  14. columns = columns.join(', ')
  15. const query = `SELECT ${columns} FROM posts WHERE id = $id`
  16. const stmt = db.prepare(query)
  17. stmt.get({ $id: pkValue }, callback)
  18. }
  19. static fetchAll = (query, params, callback) => {
  20. const stmt = db.prepare(query);
  21. stmt.all(params, callback);
  22. }
  23. static update = (post) => {
  24. let columns = new Array()
  25. Object.entries(post).forEach(([key, value]) => {
  26. columns.push(`\n${key} = "${value}"`)
  27. })
  28. const sql = `UPDATE posts\nSET ${columns.join(',')}\nWHERE id = ${post.id}`
  29. }
  30. }
  31. module.exports = Posts;