diff --git a/db/Client.js b/db/Client.js deleted file mode 100644 index 350e3cf..0000000 --- a/db/Client.js +++ /dev/null @@ -1,9 +0,0 @@ -const { Client } = require('pg'); -require('dotenv').config({ path: './config.env' }); - -const connectionString = process.env.CONNECTION; -const client = () => { - return new Client(connectionString); -} - -module.exports = client; diff --git a/db/Pool.js b/db/Pool.js index f7e3969..2e96ffa 100644 --- a/db/Pool.js +++ b/db/Pool.js @@ -5,7 +5,7 @@ const pool = new Pool({ connectionString: process.env.CONNECTION }); module.exports = { // text = SQL query; params = array of values to inject - connect: async () => await pool.connect(), + connect: async () => await pool.connect().then(console.log("Connection successful.")), query: (text, params) => pool.query(text, params), end: async () => await pool.end() } \ No newline at end of file diff --git a/loaders/passport.js b/loaders/passport.js index 04718a8..aa274b7 100644 --- a/loaders/passport.js +++ b/loaders/passport.js @@ -1,6 +1,5 @@ const passport = require('passport'); const LocalStrategy = require('passport-local'); -const client = require('../db/Client'); module.exports = (app) => { app.use(passport.initialize()); diff --git a/routes/_experimental.js b/routes/_experimental.js index 070c8eb..c015d4a 100644 --- a/routes/_experimental.js +++ b/routes/_experimental.js @@ -37,6 +37,7 @@ experimentRouter.route('/').put(async (req, res) => { throw new Error(e); } finally { client.release(); + console.log("Client disconnected."); } } }) diff --git a/routes/login.js b/routes/login.js index 49aa497..fb80be5 100644 --- a/routes/login.js +++ b/routes/login.js @@ -1,15 +1,13 @@ +const loginRouter = require('express').Router(); +const { connect } = require('../db/Pool'); const bcrypt = require('bcrypt'); -const loginRouter = require('express').Router(); -const client = require('../db/Client'); - loginRouter.route('/').post(async (req, res) => { - const newClient = client(); const { email, password } = req.body; + const client = await connect(); try { - newClient.connect().then(console.log("Connection successful.")); - let hash = await newClient.query("SELECT password FROM users WHERE email = ($1)", [email]); + let hash = await client.query("SELECT password FROM users WHERE email = ($1)", [email]); hash = hash.rows[0].password; const match = bcrypt.compare(password, hash); @@ -19,7 +17,7 @@ loginRouter.route('/').post(async (req, res) => { req.session.authenticated = true; req.session.user = { email: email, password: password } - let fullUserProfile = await newClient.query("SELECT * FROM users WHERE email = ($1)", [email]); + let fullUserProfile = await client.query("SELECT * FROM users WHERE email = ($1)", [email]); res.send({ session: req.session, @@ -27,10 +25,11 @@ loginRouter.route('/').post(async (req, res) => { }); } } catch(e) { - console.log(e); + await client.query("ROLLBACK"); + throw new Error(e); } finally { - await newClient.end() - .then(console.log("Client disconnected.")); + client.release() + console.log("Client disconnected."); } }); diff --git a/routes/products.js b/routes/products.js index a93f5cf..d801d0c 100644 --- a/routes/products.js +++ b/routes/products.js @@ -1,62 +1,64 @@ const express = require('express'); const productsRouter = express.Router(); - -const client = require('../db/Client'); +const { connect } = require('../db/Pool'); // route to get all products productsRouter.route('/').get(async (req, res) => { - const newClient = client(); + const client = await connect(); try { - newClient.connect() - .then(console.log('Success')); - const result = await newClient.query("SELECT * FROM products"); - res.send(result.rows); + await client.query("BEGIN"); + const result = await client.query("SELECT * FROM products"); + await client.query("COMMIT"); + if (result) res.send(result.rows); } catch(e) { - console.log(e); + await client.query("ROLLBACK"); + throw new Error(e); } finally { - newClient.end() - .then(console.log("Client disconnected.")); + client.release(); + console.log("Client disconnected."); } }); // route to get a product by id productsRouter.route('/:id').get(async (req, res) => { const { id } = req.params; - const newClient = client(); + const client = await connect(); try { - newClient.connect().then(console.log("Connection successful.")); - const result = await newClient.query(("SELECT * FROM products WHERE id = ($1)"), [id]); - res.send(result.rows[0]); + await client.query("BEGIN"); + const result = await client.query(("SELECT * FROM products WHERE id = ($1)"), [id]); + await client.query("COMMIT"); + if (result) res.send(result.rows[0]); } catch(e) { - console.log(e); + await client.query("ROLLBACK"); + throw new Error(e); } finally { - await newClient.end() - .then(console.log("Client disconnected.")); + client.release() + console.log("Client disconnected."); } }); // post a product from req.body productsRouter.route('/').post(async (req, res) => { - const newClient = client(); const { name, description, category, categoryID, price } = req.body; + const input = ` + INSERT INTO products (name, description, category, category_id, price) + VALUES ($1, $2, $3, $4, $5) + ` + + const client = await connect(); try { - newClient.connect((err) => { - if (err) { - throw err; - } else { - console.log("Connection successful.") - } - }); - - await newClient.query(("INSERT INTO products (name, description, category, category_id, price) VALUES ($1, $2, $3, $4, $5)"), [name, description, category, categoryID, price]); + await client.query("BEGIN"); + await client.query(input, [name, description, category, categoryID, price]); + await client.query("COMMIT"); res.sendStatus(204); } catch(e) { - console.log(e); + await client.query("ROLLBACK"); + throw new Error(e); } finally { - await newClient.end() + await client.release() .then(console.log("Client disconnected.")); } }); diff --git a/routes/register.js b/routes/register.js index 2e6a852..eac7e6f 100644 --- a/routes/register.js +++ b/routes/register.js @@ -1,26 +1,27 @@ +const registerRouter = require('express').Router(); +const { connect } = require('../db/Pool'); const bcrypt = require('bcrypt'); -const registerRouter = require('express').Router(); -const client = require('../db/Client'); - registerRouter.route('/').post(async (req, res) => { - const newClient = client(); const { firstName, lastName, email, password } = req.body; + const input = "INSERT INTO users (first_name, last_name, email, password) values ($1, $2, $3, $4)"; const salt = await bcrypt.genSalt(10); const hash = await bcrypt.hash(password, salt); + const client = await connect(); + try { - newClient.connect().then(console.log("Connection successful.")); - await newClient.query( - "INSERT INTO users (first_name, last_name, email, password) values ($1, $2, $3, $4)", - [firstName, lastName, email, hash]); + await client.query("BEGIN"); + await client.query(input, [firstName, lastName, email, hash]); + await client.query("COMMIT"); res.sendStatus(200); } catch(e) { - console.log(e); + await client.query("ROLLBACK"); + throw new Error(e); } finally { - await newClient.end() - .then(console.log("Client disconnected.")); + client.release(); + console.log("Client disconnected."); } }); diff --git a/routes/user.js b/routes/user.js index adaee6b..939bbd1 100644 --- a/routes/user.js +++ b/routes/user.js @@ -1,38 +1,41 @@ const express = require('express'); const userRouter = express.Router(); -const client = require('../db/Client'); +// const client = require('../db/Client'); +const { connect } = require('../db/Pool'); // get a list of all users, or a single user matching an email passed in as a query param userRouter.route('/').get(async (req, res) => { - const newClient = client(); const { email } = req.query; + const client = await connect() + .then(console.log('Connection successful.')) + .catch(e => console.log(e)); if (!email) { try { - await newClient.connect() - .then(console.log('Connection successful.')); - - const results = await newClient.query("SELECT * FROM users"); - res.send(results.rows); + await client.query("BEGIN"); + const results = await client.query("SELECT * FROM users"); + await client.query("COMMIT"); + if (results) res.send(results.rows); } catch(e) { - console.log(e); + await client.query('ROLLBACK'); + throw new Error(e); } finally { - await newClient.end(); + await client.release(); console.log("Client disconnected."); } } else { try { - await newClient.connect() - .then(console.log("Connection successful.")); - - const result = await newClient.query(("SELECT * FROM users WHERE email = ($1)"), [email]) - res.send(result.rows); + await client.query("BEGIN"); + const result = await client.query(("SELECT * FROM users WHERE email = ($1)"), [email]) + await client.query("COMMIT"); + if (result) res.send(result.rows); } catch(e) { - console.log(e); + await client.query('ROLLBACK'); + throw new Error(e); } finally { - await newClient.end() - .then(console.log("Client disconnected.")); + await client.release(); + console.log("Client disconnected."); } } }); @@ -40,19 +43,21 @@ userRouter.route('/').get(async (req, res) => { // post a new user to the database userRouter.route('/').post(async (req, res) => { const { name, email } = req.body; - const newClient = client(); + const client = await connect() + .then(console.log('Connection successful.')); + const input = "INSERT INTO users (name, email) VALUES ($1, $2)"; try { - await newClient.connect() - .then(console.log("Connection successful.")); - - await newClient.query(("INSERT INTO users (name, email) VALUES ($1, $2)"), [name, email]) - .then(res.sendStatus(204)); + await client.query("BEGIN"); + await client.query(input, [name, email]); + await client.query("COMMIT"); + res.sendStatus(200); } catch(e) { - console.log(e); + await client.query('ROLLBACK'); + throw new Error(e); } finally { - await newClient.end() - .then(console.log("Client disconnected.")); + await client.release(); + console.log("Client disconnected."); } });