Your First Fullstack App!
Up until now, the app structure has looked like this:
The model is the layer of the application that directly manipulates the data. It provides methods that the controllers can use to execute those changes whenever the frontend makes the appropriate request.
Currently, the model uses an in-memory array to store data. The issue is that the data is not persistent — each time we restart the server, all data created during that "session" is lost.
Now that we have a database and can connect to it using Knex, we can refactor our model to use that database and have our data persist.
Part 1 - Adding a DB for the fellows
- create a database called - fellows_tracker_f23
- Create the table structure - CREATE TABLE fellows ( id SERIAL PRIMARY KEY, name TEXT NOT NULL )
- cdinto- server/
- npm i pg knex
- npx knex initto create- knexfile.jsin the root of the- server/directory
- modify the file to use - pgand the- fellow-tracker-f23database with a valid username and password
- create a - knex.jsfile in the- model/directory- make sure the import statement for - knexfile.jsis accurate
 - // Set the deployment environment variable // Depending on where it is deployed, this could be "staging" or "production" const env = process.env.NODE_ENV || 'development'; // Grab the corresponding knex configuration object from knexfile.js const knexConfig = require('../knexfile.js')[env]; // Create the knex connection object using that config const knex = require('knex')(knexConfig); module.exports = knex;
- Refactor - Fellow.js!- Import - knexinto the- Fellowmodel
- Remove the import of the - getIdfunction
- Remove the - Fellow.#allarray
- Remove the - constructorentirely and create a- static async createmethod (we'll use the constructor for another purpose)
- All functions need to be - asyncto use- await knex.raw()
- Every function needs to return something (use - RETURNING *)- Create should return the new fellow 
- Get ALL should return an array of all fellows 
- Get One should return a single fellow object (the first object in the - rowsarray)
- Update should return the updated fellow object 
- Delete should return the deleted fellow object 
 
 
- Refactor - fellowControllers.jsto use the new model methods and to- awaiteverything
Part 2 - Adding posts
- Create a posts table that references the fellows table 
- Create a - Postmodel with methods:- create(content, fellowId) 
- list() (not really used other than for testing) 
- findById(id) (not really used other than for testing) 
- findPostsByFellowId(fellowId) 
- delete(id) 
- deleteAllPostsForFellow(fellowId) 
 
- make the - postControllers
- define endpoints in - index.js
- test with postman 
- add frontend components + fetching on the FellowDetails page - components: - list of posts with delete buttons 
- form for adding a new post 
 
- event handlers / fetching: - useEffect now also fetches fellow posts -> - GET /api/fellows/:fellowId/posts
- list item delete button on click -> - DELETE /api/posts/:id
- new post form on submit -> - POST /api/fellows/:fellowId/posts/:id
 
 
- when deleting a fellow, we need to delete posts first 
Part 3 - Adapters
Create an "adapter" file for fetching fellows and posts
Last updated