Skip to content

Instantly share code, notes, and snippets.

@Adurtxi
Last active August 10, 2021 12:10
Show Gist options
  • Select an option

  • Save Adurtxi/a9701d36928c54339f0a342af4eeadc2 to your computer and use it in GitHub Desktop.

Select an option

Save Adurtxi/a9701d36928c54339f0a342af4eeadc2 to your computer and use it in GitHub Desktop.
NodeJS, Express and MySQL -- document.model.js
'use strict';
const db = require('@config/db');
let Document = function (document) {
this.id = document.id;
this.title = document.title;
this.description = document.description;
this.created_at = document.created_at ?? new Date();
this.updated_at = new Date();
};
Document.getById = ({id}, result) => {
const callback = (error, response) => (error)
? result(error, null)
: result(null, response[0]);
db.query('SELECT * FROM documents WHERE id = ?', id, callback);
}
Document.getAll = (result) => {
const callback = (error, response) => (error)
? result(error, null)
: result(null, response);
db.query('SELECT * FROM documents', callback);
}
Document.store = (newDocument, result) => {
const callback = (error, response) => (error)
? result(error, null)
: result(null, response.insertId);
db.query('INSERT INTO documents set ?', newDocument, callback);
}
Document.update = ({ id, title, description }, result) => {
const callback = (error, response) => (error)
? result(error, null)
: result(null, null);
db.query(
'UPDATE documents SET title = ?, description = ? WHERE id = ?',
[title, description, id],
callback
);
}
Document.delete = (id, result) => {
const callback = (error, response) => (error)
? result(error, null)
: result(null, response);
db.query('DELETE FROM documents WHERE id = ?', [id], callback);
}
module.exports = Document;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment