refactored for pg pool
This commit is contained in:
@@ -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;
|
|
||||||
@@ -5,7 +5,7 @@ const pool = new Pool({ connectionString: process.env.CONNECTION });
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
// text = SQL query; params = array of values to inject
|
// 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),
|
query: (text, params) => pool.query(text, params),
|
||||||
end: async () => await pool.end()
|
end: async () => await pool.end()
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
const passport = require('passport');
|
const passport = require('passport');
|
||||||
const LocalStrategy = require('passport-local');
|
const LocalStrategy = require('passport-local');
|
||||||
const client = require('../db/Client');
|
|
||||||
|
|
||||||
module.exports = (app) => {
|
module.exports = (app) => {
|
||||||
app.use(passport.initialize());
|
app.use(passport.initialize());
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ experimentRouter.route('/').put(async (req, res) => {
|
|||||||
throw new Error(e);
|
throw new Error(e);
|
||||||
} finally {
|
} finally {
|
||||||
client.release();
|
client.release();
|
||||||
|
console.log("Client disconnected.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,15 +1,13 @@
|
|||||||
|
const loginRouter = require('express').Router();
|
||||||
|
const { connect } = require('../db/Pool');
|
||||||
const bcrypt = require('bcrypt');
|
const bcrypt = require('bcrypt');
|
||||||
|
|
||||||
const loginRouter = require('express').Router();
|
|
||||||
const client = require('../db/Client');
|
|
||||||
|
|
||||||
loginRouter.route('/').post(async (req, res) => {
|
loginRouter.route('/').post(async (req, res) => {
|
||||||
const newClient = client();
|
|
||||||
const { email, password } = req.body;
|
const { email, password } = req.body;
|
||||||
|
const client = await connect();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
newClient.connect().then(console.log("Connection successful."));
|
let hash = await client.query("SELECT password FROM users WHERE email = ($1)", [email]);
|
||||||
let hash = await newClient.query("SELECT password FROM users WHERE email = ($1)", [email]);
|
|
||||||
hash = hash.rows[0].password;
|
hash = hash.rows[0].password;
|
||||||
|
|
||||||
const match = bcrypt.compare(password, hash);
|
const match = bcrypt.compare(password, hash);
|
||||||
@@ -19,7 +17,7 @@ loginRouter.route('/').post(async (req, res) => {
|
|||||||
req.session.authenticated = true;
|
req.session.authenticated = true;
|
||||||
req.session.user = { email: email, password: password }
|
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({
|
res.send({
|
||||||
session: req.session,
|
session: req.session,
|
||||||
@@ -27,10 +25,11 @@ loginRouter.route('/').post(async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.log(e);
|
await client.query("ROLLBACK");
|
||||||
|
throw new Error(e);
|
||||||
} finally {
|
} finally {
|
||||||
await newClient.end()
|
client.release()
|
||||||
.then(console.log("Client disconnected."));
|
console.log("Client disconnected.");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,62 +1,64 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const productsRouter = express.Router();
|
const productsRouter = express.Router();
|
||||||
|
const { connect } = require('../db/Pool');
|
||||||
const client = require('../db/Client');
|
|
||||||
|
|
||||||
// route to get all products
|
// route to get all products
|
||||||
productsRouter.route('/').get(async (req, res) => {
|
productsRouter.route('/').get(async (req, res) => {
|
||||||
const newClient = client();
|
const client = await connect();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
newClient.connect()
|
await client.query("BEGIN");
|
||||||
.then(console.log('Success'));
|
const result = await client.query("SELECT * FROM products");
|
||||||
const result = await newClient.query("SELECT * FROM products");
|
await client.query("COMMIT");
|
||||||
res.send(result.rows);
|
if (result) res.send(result.rows);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.log(e);
|
await client.query("ROLLBACK");
|
||||||
|
throw new Error(e);
|
||||||
} finally {
|
} finally {
|
||||||
newClient.end()
|
client.release();
|
||||||
.then(console.log("Client disconnected."));
|
console.log("Client disconnected.");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// route to get a product by id
|
// route to get a product by id
|
||||||
productsRouter.route('/:id').get(async (req, res) => {
|
productsRouter.route('/:id').get(async (req, res) => {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
const newClient = client();
|
const client = await connect();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
newClient.connect().then(console.log("Connection successful."));
|
await client.query("BEGIN");
|
||||||
const result = await newClient.query(("SELECT * FROM products WHERE id = ($1)"), [id]);
|
const result = await client.query(("SELECT * FROM products WHERE id = ($1)"), [id]);
|
||||||
res.send(result.rows[0]);
|
await client.query("COMMIT");
|
||||||
|
if (result) res.send(result.rows[0]);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.log(e);
|
await client.query("ROLLBACK");
|
||||||
|
throw new Error(e);
|
||||||
} finally {
|
} finally {
|
||||||
await newClient.end()
|
client.release()
|
||||||
.then(console.log("Client disconnected."));
|
console.log("Client disconnected.");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// post a product from req.body
|
// post a product from req.body
|
||||||
productsRouter.route('/').post(async (req, res) => {
|
productsRouter.route('/').post(async (req, res) => {
|
||||||
const newClient = client();
|
|
||||||
const { name, description, category, categoryID, price } = req.body;
|
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 {
|
try {
|
||||||
newClient.connect((err) => {
|
await client.query("BEGIN");
|
||||||
if (err) {
|
await client.query(input, [name, description, category, categoryID, price]);
|
||||||
throw err;
|
await client.query("COMMIT");
|
||||||
} 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]);
|
|
||||||
res.sendStatus(204);
|
res.sendStatus(204);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.log(e);
|
await client.query("ROLLBACK");
|
||||||
|
throw new Error(e);
|
||||||
} finally {
|
} finally {
|
||||||
await newClient.end()
|
await client.release()
|
||||||
.then(console.log("Client disconnected."));
|
.then(console.log("Client disconnected."));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,26 +1,27 @@
|
|||||||
|
const registerRouter = require('express').Router();
|
||||||
|
const { connect } = require('../db/Pool');
|
||||||
const bcrypt = require('bcrypt');
|
const bcrypt = require('bcrypt');
|
||||||
|
|
||||||
const registerRouter = require('express').Router();
|
|
||||||
const client = require('../db/Client');
|
|
||||||
|
|
||||||
registerRouter.route('/').post(async (req, res) => {
|
registerRouter.route('/').post(async (req, res) => {
|
||||||
const newClient = client();
|
|
||||||
const { firstName, lastName, email, password } = req.body;
|
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 salt = await bcrypt.genSalt(10);
|
||||||
const hash = await bcrypt.hash(password, salt);
|
const hash = await bcrypt.hash(password, salt);
|
||||||
|
|
||||||
|
const client = await connect();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
newClient.connect().then(console.log("Connection successful."));
|
await client.query("BEGIN");
|
||||||
await newClient.query(
|
await client.query(input, [firstName, lastName, email, hash]);
|
||||||
"INSERT INTO users (first_name, last_name, email, password) values ($1, $2, $3, $4)",
|
await client.query("COMMIT");
|
||||||
[firstName, lastName, email, hash]);
|
|
||||||
res.sendStatus(200);
|
res.sendStatus(200);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.log(e);
|
await client.query("ROLLBACK");
|
||||||
|
throw new Error(e);
|
||||||
} finally {
|
} finally {
|
||||||
await newClient.end()
|
client.release();
|
||||||
.then(console.log("Client disconnected."));
|
console.log("Client disconnected.");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,38 +1,41 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const userRouter = express.Router();
|
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
|
// 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) => {
|
userRouter.route('/').get(async (req, res) => {
|
||||||
const newClient = client();
|
|
||||||
const { email } = req.query;
|
const { email } = req.query;
|
||||||
|
const client = await connect()
|
||||||
|
.then(console.log('Connection successful.'))
|
||||||
|
.catch(e => console.log(e));
|
||||||
|
|
||||||
if (!email) {
|
if (!email) {
|
||||||
try {
|
try {
|
||||||
await newClient.connect()
|
await client.query("BEGIN");
|
||||||
.then(console.log('Connection successful.'));
|
const results = await client.query("SELECT * FROM users");
|
||||||
|
await client.query("COMMIT");
|
||||||
const results = await newClient.query("SELECT * FROM users");
|
if (results) res.send(results.rows);
|
||||||
res.send(results.rows);
|
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.log(e);
|
await client.query('ROLLBACK');
|
||||||
|
throw new Error(e);
|
||||||
} finally {
|
} finally {
|
||||||
await newClient.end();
|
await client.release();
|
||||||
console.log("Client disconnected.");
|
console.log("Client disconnected.");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
await newClient.connect()
|
await client.query("BEGIN");
|
||||||
.then(console.log("Connection successful."));
|
const result = await client.query(("SELECT * FROM users WHERE email = ($1)"), [email])
|
||||||
|
await client.query("COMMIT");
|
||||||
const result = await newClient.query(("SELECT * FROM users WHERE email = ($1)"), [email])
|
if (result) res.send(result.rows);
|
||||||
res.send(result.rows);
|
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.log(e);
|
await client.query('ROLLBACK');
|
||||||
|
throw new Error(e);
|
||||||
} finally {
|
} finally {
|
||||||
await newClient.end()
|
await client.release();
|
||||||
.then(console.log("Client disconnected."));
|
console.log("Client disconnected.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -40,19 +43,21 @@ userRouter.route('/').get(async (req, res) => {
|
|||||||
// post a new user to the database
|
// post a new user to the database
|
||||||
userRouter.route('/').post(async (req, res) => {
|
userRouter.route('/').post(async (req, res) => {
|
||||||
const { name, email } = req.body;
|
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 {
|
try {
|
||||||
await newClient.connect()
|
await client.query("BEGIN");
|
||||||
.then(console.log("Connection successful."));
|
await client.query(input, [name, email]);
|
||||||
|
await client.query("COMMIT");
|
||||||
await newClient.query(("INSERT INTO users (name, email) VALUES ($1, $2)"), [name, email])
|
res.sendStatus(200);
|
||||||
.then(res.sendStatus(204));
|
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.log(e);
|
await client.query('ROLLBACK');
|
||||||
|
throw new Error(e);
|
||||||
} finally {
|
} finally {
|
||||||
await newClient.end()
|
await client.release();
|
||||||
.then(console.log("Client disconnected."));
|
console.log("Client disconnected.");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user