Skip to content
This repository has been archived by the owner on Oct 9, 2020. It is now read-only.

Setup backend DB and add example service #8

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
fbb38f3
add checkin routes file
ashwin1104 Sep 26, 2020
d71af3a
Merge branch 'master' of https://github.com/hack4impact-uiuc/closegap…
ashwin1104 Sep 29, 2020
3c79fe4
create API readme
ashwin1104 Sep 29, 2020
bbce007
Install dotenv
Qrtn Sep 29, 2020
0eb8cfe
Add default config for db
Qrtn Sep 29, 2020
505f018
Install pg
Qrtn Sep 30, 2020
28686d5
Install pg-promise
Qrtn Sep 30, 2020
6ef6486
Add ruby db schema
Qrtn Sep 30, 2020
a0cbc74
Add db module
Qrtn Sep 30, 2020
fdc0033
Add schema-only and seeded db dumps
Qrtn Oct 1, 2020
628e68a
Change var to const in app.js
Qrtn Oct 1, 2020
5b69ecc
Add example route for accessing db via service
Qrtn Oct 1, 2020
2dd6309
Add example service for student
Qrtn Oct 1, 2020
a2d11fc
Update example route with 404
Qrtn Oct 1, 2020
578104c
Add setup script for dev db
Qrtn Oct 1, 2020
3a6cb73
Add db setup instructions in backend readme
Qrtn Oct 1, 2020
4865289
Update default dev.env config
Qrtn Oct 1, 2020
c69a39b
Differentiate dev environment in yarn start
Qrtn Oct 1, 2020
56df922
Update dev.env.default config
Qrtn Oct 1, 2020
a94ae50
Fix linting
Qrtn Oct 1, 2020
2f1e0b5
Fix setup db script not seeding data
Qrtn Oct 1, 2020
6800a25
Export router from checkin route
Qrtn Oct 1, 2020
d59f041
Run prettier
Qrtn Oct 1, 2020
d93a304
Use http-status-codes
Qrtn Oct 1, 2020
8b5de95
Add comments to db setup script
Qrtn Oct 1, 2020
869c1d7
Remove checkin route comments
Qrtn Oct 1, 2020
6fc2ec2
Remove return from route
Qrtn Oct 1, 2020
d7d9706
Fix statusCodes import
Qrtn Oct 1, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Closegap API

This folder contains the backend api of the application.

## Setup DB

### Install PostgreSQL

#### macOS

1. Install [Homebrew](https://brew.sh).

2. Install postgresql via Homebrew.

`$ brew install postgresql`

3. Start postgresql and register it to launch on system start.

`$ brew services start postgresql`

#### Windows Subsystem for Linux (WSL)

1. [Install postgresql.](https://github.com/michaeltreat/Windows-Subsystem-For-Linux-Setup-Guide/blob/master/readmes/installs/PostgreSQL.md)

2. Start postgresql.

`$ sudo service postgresql start `

### Create database

Run setup script. Enter `n` if you do not wish to seed with initial data.

```
$ infra/setup_dev_db.sh
Seed database with initial data? [Y/n] y
...
```

## Configure App

```
$ cp config/dev.env.default config/dev.env
```

## Install & Run

```bash
yarn && yarn start
```

## Test DB access

```
$ curl http://localhost:5000/students/1
{"data":{"id":"1","user_id":"1","first_name":"Ipsum1","middle_name":"Lorem1","last_name":"Dolor1","birthdate":"2010-11-14T06:00:00.000Z","homeroom_teacher":"Amet1","grade":"1st","gender":"male","created_at":"2020-10-01T09:31:50.910Z","updated_at":"2020-10-01T09:31:50.976Z","school_name":"some school name"}}%
```
10 changes: 10 additions & 0 deletions api/config/dev.env.default
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# PostgreSQL DB Configuration

PGDATABASE=closegap-api_development

PGHOST=localhost

PGUSER=closegap-api
PGPASSWORD=closegap-api

PGPORT=5432
27 changes: 27 additions & 0 deletions api/infra/setup_dev_db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash

# Get root path of Git repo
ROOT=$(git rev-parse --show-toplevel)

# Ask user if they want to seed the database with initial data
while true; do
read -p "Seed database with initial data? [Y/n] " yn
case $yn in
"" | [Yy]* ) SUFFIX="_seeded"; break;;
[Nn]* ) SUFFIX=""; break;;
* ) echo "Please answer y or n.";;
esac
done

# Create user closegap-api, which will be the API's DB user
createuser -d closegap-api

# Create development and test DBs
createdb -U closegap-api closegap-api_development
createdb -U closegap-api closegap-api_test

# Run the SQL schemas.
# schema/db/development_seeded.sql for schema of seeded DB
# schema/db/development.sql for schema of bare DB
psql closegap-api_development < ${ROOT}/api/schema/db/development${SUFFIX}.sql
psql closegap-api_test < ${ROOT}/api/schema/db/test${SUFFIX}.sql
9 changes: 7 additions & 2 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@
"format:check": "prettier --check \"./**/*.{js,jsx,json,md}\"",
"lint": "eslint \"./**/*.js\"",
"lint:fix": "eslint --fix \"./**/*.js\"",
"start": "nodemon ./src/bin/www"
"start": "yarn start:dev",
"start:dev": "NODE_ENV=dev nodemon ./src/bin/www"
},
"dependencies": {
"cookie-parser": "~1.4.4",
"cors": "^2.8.5",
"debug": "~2.6.9",
"dotenv": "^8.2.0",
"express": "~4.16.1",
"http-errors": "~1.6.3",
"http-status-codes": "^2.1.4",
"morgan": "~1.9.1",
"nodemon": "^2.0.4"
"nodemon": "^2.0.4",
"pg": "^8.3.3",
"pg-promise": "^10.6.1"
},
"devDependencies": {
"@h4iuiuc/eslint-plugin": "^1.0.12",
Expand Down
Loading