kongnyc / users-app-assessment-backend

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Backend deployment and database challenge

My deployed API URL is:

https://users-app-assessment-backend-budb.onrender.com

Overview

In this challenge, you will take an Express API that serves hard-coded data from a JSON file and:

  1. deploy this API to render
  2. create a database for the API on ElephantSQL
  3. create a users table in your database
  4. seed the table with the data from usersData.json
  5. refactor the controllers and queries to serve data from your db
  6. deploy your changes so that your API on render calls the db

See the Detailed instructions section below for detailed instructions, tips, and hints.

Getting started

  1. Fork this repo.
  2. Clone your fork to your computer.
  3. Install dependencies: npm install
  4. Start the server: npm start

Submitting

  • As you write code, push or merge it to the main branch of your fork (not the upstream repo)
  • When the instructions, videos, or other resources say to make a PR, make that PR to your fork
  • At the end of class, make a PR of your main branch to the upstream repo main branch (so I can review your code)

Helpful resources

Deploying the API to render

Creating the DB and connecting API to DB

Create and seed data; serve data from DB

Reference code

Routes

After each stage of the challenge, make sure that all of these routes function as expected.

  • /: returns a 200 response: `{ data: 'Service is running' }
  • /users: returns a 200 response in this format: { data: [ {//user 1...}, { // user 2, ...}, ...]}
  • /users/:id: returns a 200 response in this format: { data: { name: ..., age: ..., }}
  • /users/:id: returns a 404 response if the id does not exist: { error: 'User with id ... not found'}

Detailed instructions

1. Deploy to render

  1. Deploy your API to render.
  2. Make sure that all routes work.
  3. Paste your deployed URL in the top of this README.
  4. Commit and merge your changes to the main branch of your fork.

2. Create a database

  1. Create a database on Elephant SQL
  2. Do not commit the connection string / URL to GitHub

3. Create the users table

  1. Create a schema.sql file in this repo that defines the table. The columns should match the keys and data types you see in the usersData.json file.
  2. Run the schema file to create the table in your db.
  3. Commit and merge your changes to the main branch of your fork.

Tips and hints

The data type for latitude and longitude is float.

4. Seed the table with the data from usersData.json

  1. Create a seeds.sql file in this repo that adds at least 3 students from usersData.json to the users table.
  2. The uuids in the table do not need to match the uuids in usersData.json (although it is possible to insert the exact ids from the JSON).
  3. Commit and merge your changes to the main branch of your fork.

Tips and hints

Don't waste time copy-pasting every single user from the JSON file. Just do 3 to start. Do the rest if you have time at the end.

The about texts have apostrophes. (For example: 'Hi there! I'm Park...' ). These will cause problems because SQL statments require single quotes around strings.

You can escape a single quote in a SQL statement by repeating it: 'Hi there! I''m Park...'

5. Serve data from the db

  1. Refactor the queries and controllers to fetch from the database.
  2. You will need to use pg-promise. It is already installed.
  3. Use environment variables to store the connection string (database URL).
  4. Delete the usersData.json file.
  5. Commit and merge your changes to the main branch of your fork.

6. Final deployment

  1. If it works locally, update your environment variables on Render, then push, merge, and deploy your code changes.
  2. By the end of this step, your deployed database should serve data from the db.

About


Languages

Language:JavaScript 100.0%