Skip to content

Commit

Permalink
chore(database): setup and configure postgres database
Browse files Browse the repository at this point in the history
- install and configure pg to export two pools
- add normal database and test database urls to .env
- create database schema
- setup npm scripts to manage database

[Finishes #160819060]
  • Loading branch information
akhilome committed Sep 30, 2018
1 parent 27ce24a commit 4258c71
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PORT=[server_port_number]
DATABASE_URL=postgres://[username]@[server]:[port]/[database_name]
TEST_DATABASE_URL=postgres://[username]@[server]:[port]/[database_name]
101 changes: 99 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
"scripts": {
"start": "npm run build && node ./dist/index.js",
"build": "babel ./server -d dist",
"test": "nyc --reporter=lcov mocha --recursive ./tests/* --require babel-register --exit",
"test": "export NODE_ENV=test && nyc --reporter=lcov mocha --recursive ./tests/* --require babel-register --exit",
"cover": "nyc report --reporter=text-lcov | coveralls",
"dev": "nodemon --exec babel-node ./server/index.js"
"dev": "nodemon --exec babel-node ./server/index.js",
"purge-db": "echo 'DROP DATABASE IF EXISTS fastfoodfast;' | psql -U postgres && echo 'CREATE DATABASE fastfoodfast;' | psql -U postgres",
"setup-schema": "psql -U postgres fastfoodfast < ./server/db/schema.sql",
"config-db": "npm run purge-db && npm run setup-schema",
"setup-testdb": "echo 'DROP DATABASE IF EXISTS fastfoodfast_test;' | psql -U postgres && echo 'CREATE DATABASE fastfoodfast_test;' | psql -U postgres"
},
"engines": {
"node": "8.12.0"
Expand All @@ -24,12 +28,13 @@
},
"homepage": "https://github.com/akhilome/fast-food-fast#readme",
"dependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-stage-2": "^6.24.1",
"body-parser": "^1.18.3",
"dotenv": "^6.0.0",
"express": "^4.16.3",
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-stage-2": "^6.24.1"
"pg": "^7.4.3"
},
"devDependencies": {
"chai": "^4.1.2",
Expand Down
16 changes: 16 additions & 0 deletions server/db/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import dotenv from 'dotenv';
import { Pool } from 'pg';

dotenv.config();

const env = process.env.NODE_ENV || 'development';
let pool;
let testPool;

if (env === 'test') {
testPool = new Pool({ connectionString: process.env.TEST_DATABASE_URL });
} else {
pool = new Pool({ connectionString: process.env.DATABASE_URL });
}

export default { pool, testPool };
21 changes: 21 additions & 0 deletions server/db/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
CREATE TABLE IF NOT EXISTS users (
id serial PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL,
is_admin BOOLEAN NOT NULL
);

CREATE TABLE IF NOT EXISTS menu (
id serial PRIMARY KEY,
food_name VARCHAR(255) NOT NULL,
price REAL NOT NULL
);

CREATE TABLE IF NOT EXISTS orders (
id serial PRIMARY KEY,
item INTEGER REFERENCES menu(id),
author INTEGER REFERENCES users(id),
date DATE NOT NULL DEFAULT CURRENT_DATE,
status VARCHAR(50) NOT NULL DEFAULT 'new'
);

0 comments on commit 4258c71

Please sign in to comment.