Author: @shiryz
In this workshop we'll be building on what we learnt in the pg walkthrough. This app currently contains static data on users' name and location. We'll be setting up our own database connection so that the data can be retrieved from a table of "users" instead. We'll also want to write code that enables us to add new users via a form.
- Clone this repo
- Install the node_modules by typing
npm i
in your terminal. - Run
npm run dev
in your terminal and checkout the result at http://localhost:5000. This is the starting template for what you'll be building. - In
src/handlers.js
you'll find a function that deals with calls to the/users
endpoint. The data is currently coming from thestatic.js
file. Add your own name and location withinstatic.js
. Refresh the page & check the results.
We are currently hard-coding the data in to the application (static.js
) because we don't have a database. Now we want to replace static.js
with an actual database. Let's start by setting up the database we will connect to.
- Connect to postgres, by typing
psql
orpgcli
in the terminal. - Create the database by typing
CREATE DATABASE [the name of the database];
. - Create a superuser with a password by typing
CREATE USER [the new username] WITH SUPERUSER PASSWORD '[the password of the database]';
(the password needs to be in quotes, otherwise you get an error). - Change ownership of the database to the new user by typing
ALTER DATABASE [name of the database] OWNER TO [the new username];
- Add a
config.env
file and add the database's url in this format:DB_URL = postgres://[username]:[password]@localhost:5432/[database]
. The database name needs to be in lower case.
Your job now is to add to database/db_connection.js
and queries/getData.js
and refactor handlers.js
so that the response data comes from the users table in your database instead of from static.js
.
- In the terminal, connect to your database using
psql postgres://[username]:[password]@localhost:5432/[database]
. - Create a table called 'users' with three columns: 'id', 'name' and 'location' and add a couple of rows of dummy data. Hint: don't hard code the ids
- Inside
queries/getData.js
, write a SQL query that pulls the necessary data from your database. - Change the
/users
handler inhandlers.js
to call thegetData
query.
So far, we've only been dealing with GET
ting data from a server. But what if we want users to be able to add their details to our database?
As we still don't have a visible form in the front end, only a developer can add to the database, either through psql
in the command line, or by adding .sql
files to this repo. Let's change that.
In the commented out form in index.html
we have this HTML attribute: action="create-user"
. When a user clicks 'submit' on this form, the details they have entered will be sent in the payload to a 'create-user' endpoint.
- Open up
index.html
and uncomment the form - Check that you can see an empty input box when you refresh your browser
- Add a new endpoint to
router.js
(/create-user
) - Create a new handler function for the endpoint
create-user
inhandlers.js
that uses the data from your form to INSERT a new row into your users table. What kind of method do you think you'll need (check the HTML!)? How will you access the data from your form?
Style it up!
- Try and get the design looking as close as possible to the below:
Customise your app!
You could:
- Add some new columns to your
users
table - Try and handle different types on input on the client side (e.g. boolean values or longer text input)
- Validate input to check that the user does not already exist