Skip to content

Instantly share code, notes, and snippets.

@spion
Created February 15, 2020 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spion/2779ae6dc9552c229c1eeacd90c03bf3 to your computer and use it in GitHub Desktop.
Save spion/2779ae6dc9552c229c1eeacd90c03bf3 to your computer and use it in GitHub Desktop.
diff --git a/frameworks/JavaScript/express/express-postgres.dockerfile b/frameworks/JavaScript/express/express-postgres.dockerfile
index f2907bd02..9b2196082 100644
--- a/frameworks/JavaScript/express/express-postgres.dockerfile
+++ b/frameworks/JavaScript/express/express-postgres.dockerfile
@@ -1,9 +1,9 @@
FROM node:12.3.1-slim
-COPY ./ ./
-
+COPY package.json ./
RUN npm install
+COPY ./ ./
ENV NODE_ENV production
CMD ["node", "postgresql-app.js"]
diff --git a/frameworks/JavaScript/express/package.json b/frameworks/JavaScript/express/package.json
index 15a169b14..7c9b36ebe 100644
--- a/frameworks/JavaScript/express/package.json
+++ b/frameworks/JavaScript/express/package.json
@@ -3,18 +3,18 @@
"version": "0.0.1",
"private": true,
"dependencies": {
- "body-parser": "1.18.2",
+ "fast-url-parser": "^1.1.3",
+ "body-parser": "^1.19.0",
"dateformat": "3.0.3",
"escape-html": "1.0.3",
- "express": "4.16.2",
- "express-graphql": "0.6.12",
+ "express": "^4.17.1",
+ "express-graphql": "^0.9.0",
"graphql": "0.13.2",
"graphql-tools": "3.1.1",
"mongoose": "5.0.6",
"mysql": "2.16.0",
"mysql2": "1.6.5",
- "pg": "4.5.7",
- "pg-promise": "8.4.6",
+ "pg": "^7.0.0",
"pug": "2.0.1",
"sequelize": "5.10.0"
}
diff --git a/frameworks/JavaScript/express/postgresql-app.js b/frameworks/JavaScript/express/postgresql-app.js
index 3e3f24aac..1a97d1370 100644
--- a/frameworks/JavaScript/express/postgresql-app.js
+++ b/frameworks/JavaScript/express/postgresql-app.js
@@ -1,7 +1,8 @@
+require('fast-url-parser').replace();
+
const express = require('express'),
app = express(),
- bodyParser = require('body-parser'),
- pgp = require('pg-promise')(),
+ {Pool} = require('pg');
helper = require('./helper');
const connection = {
@@ -12,12 +13,13 @@ const connection = {
dialect: 'postgres'
}
-const db = pgp(`postgres://${connection.username}:${connection.password}@${connection.host}:5432/${connection.db}`);
+const db = new Pool({
+ connectionString: `postgres://${connection.username}:${connection.password}@${connection.host}:5432/${connection.db}`,
+ // max: 20
+})
app.set('view engine', 'pug');
app.set('views', __dirname + '/views');
-app.use(bodyParser.urlencoded({ extended: true }));
-app.use(bodyParser.json());
// Set headers for all routes
app.use((req, res, next) => {
@@ -37,10 +39,10 @@ app.get('/db', async (req, res) => {
app.get('/queries', async (req, res) => {
const results = [],
- queries = Math.min(parseInt(req.query.queries) || 1, 500);
+ queries = Math.min(Number(req.query.queries) || 1, 500);
for (let i = 0; i < queries; i++) {
-
+
results.push(await getRandomWorld());
}
@@ -49,40 +51,42 @@ app.get('/queries', async (req, res) => {
app.get('/fortunes', async (req, res) => {
- let fortunes = await getAllFortunes()
+ let fortunes = (await db.query('select * from fortune', [])).rows;
const newFortune = { id: 0, message: "Additional fortune added at request time." };
fortunes.push(newFortune);
fortunes.sort((a, b) => (a.message < b.message) ? -1 : 1);
- res.render('fortunes/index', { fortunes: fortunes });
+ res.render('fortunes/index', { fortunes });
});
app.get('/updates', async (req, res) => {
const results = [],
- queries = Math.min(parseInt(req.query.queries) || 1, 500);
-
- for (let i = 1; i <= queries; i++) {
-
- results.push(await updateRandomWorld())
- }
-
- res.json(results);
-});
-
-const getRandomWorld = async () => {
+ queries = Math.min(Number(req.query.queries) || 1, 500);
- return await db.one(`select * from world where id = ${helper.randomizeNum()}`, [true])
-};
+ console.log('Doing updates', queries);
+ const client = await db.connect()
+ try {
+ for (let i = 1; i <= queries; i++) {
-const updateRandomWorld = async () => {
+ let res = await client.query(
+ 'update world set randomNumber = $1 where id = $2 returning id, randomNumber',
+ [helper.randomizeNum(), helper.randomizeNum()])
- return await db.oneOrNone(`update world set randomNumber = ${helper.randomizeNum()} where id = ${helper.randomizeNum()} returning id, randomNumber`, [true])
-};
+ results.push(res.rows[0])
+ }
-const getAllFortunes = async () => {
+ res.json(results);
+ } finally {
+ client.release();
+ }
+});
- return await db.many('select * from fortune', [true]);
-};
+async function getOne(text, args) {
+ return (await db.query(text, args)).rows[0];
+}
+function getRandomWorld() {
+ return getOne(`select * from world where id = $1`, [helper.randomizeNum()])
+}
app.listen(8080, () => {
console.log('listening on port 8080');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment