Skip to content

Instantly share code, notes, and snippets.

@danizavtz
Created June 1, 2020 19:17
Show Gist options
  • Save danizavtz/7acb7f6edc4d07e7b8dc2fc75e39455e to your computer and use it in GitHub Desktop.
Save danizavtz/7acb7f6edc4d07e7b8dc2fc75e39455e to your computer and use it in GitHub Desktop.
image
CREATE TABLE IF NOT EXISTS media_file(
id SERIAL PRIMARY KEY,
img BYTEA,
mimetype VARCHAR(15),
media INT REFERENCES media(id) ON DELETE CASCADE
);
exports.addFile = async(req, res) => {
let sql = 'INSERT INTO media_file(media, mimetype, img) VALUES ($1, $2, $3) returning *';
try {
const result = await postgres.client.query(sql, [req.body.media, req.file.mimetype, req.file.buffer]);
res.status(201).json(result.rows[0]);
res.end();
} catch (err) {
return res.status(500).json({ errors: [{ param: undefined, msg: err.detail, value: undefined }] });
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment