your main file should always be called index (i.e. index.html or index.js)
index.js —> the index file is what you run on your server and stores your data; NOTE: this one can be written in any language, that’s why we have index.html
script.js —> this file runs in the browser of the person who is visiting your website (runs on the user’s computer, not yours); NOTE: this one MUST be written in js
rm -rf deletes your files permanently
SQL Lesson
When deploying something to Heroku —> Heroku is a hosting/deployment platform (i.e. you can push your code to display in the internet) for small and mid-sized projects
heroku create unique-name
git push heroku master //this pushes master branch to heroic in order to deploy project
NOTE: this is a server, and when servers restart your data does not get saved; that’s why we are working in a more permanent solution, which is storing our information in a database…
Unlike servers (APIs), databases don’t get restarted in ways that wipe your data
To have access to express, we use the package manager, typically Yarn:
yarn init -y // -y tells it to answer yes to all questions
Note: package.json holds all the third party dependencies that we need for our project, like express
yarn add express
app.use are middle bits of functionalities that we enable our express server to do
.gitignore file is where you place everything you want your git to ignore (generally we don’t want to commit our node_module folders because they are huge, so we’d put them into .gitignore)
In order to run a javascript file (much like we do with npx serve) use node index.js
Then, use Postman or your web browser to run http://localhost:3000/
Note: much like on real we click play and stop to run our commands, we need to do this manually by killing (control+C) and restarting (yarn start) our server to see the changes
NOTE: when we use our own server we don’t need to use CORS anymore
Setting up connection to SQL database (in Heroku)
Make a new directory named pg_demo i.e. mkdir pg_demo and cd into it
git init // this will make a new repository
git remote add heroic https://git.heroku.com/rbi=postgres-demo.git // connect your git to heroku
heroku login // note: if signing in through the browser fails (i.e. no trust message or “waiting for login…” message), control+c and write heroic login -i to sign in through your local terminal ** This step is important because it will only work if you’re logged into Heroku ***
heroku pg:psql//this enables a direct connection to the database which is shared in Heroku
To exit this use ` or control+d
SQL Queries:
Select —> allows you to “select all” i.e. to pull the table data that is shared use `select from bootcamp_people order by id;`
Select Recipe
Select ____ //the column or columns to be searched
From ___; //the table the data is in - all sql statements must have a `;` at the end to complete the statement
You can filter the data by adding a “WHERE” clause to the end of the recipe statement:
Select ___
From ___
Where____;// “where” filters out other information and will return only the match that is requested (i.e. id = 2)
In sequel, ‘’ are used for whole string character values (i.e. movie title ‘The Kid’)
We can sort the information by using ORDER BY; we can select a category (i.e. ORDER BY duration;) and use commands like DESC (i.e. ORDER BY duration DESC;) to sort by descending order; the default is ascending order
We can also use comparison operators (i.e, >, <,=, <=, >=, <> or != which are used as “do not equal to”)to set parameters (i.e. WHERE duration > 100;)
Adding more filters with “AND” allows us to segment the data further, but the WHERE and AND conditions must be met
i.e.
SELECT title
FROM movies
WHERE id = 1
AND genre = ‘Comedy’;
—> will return movies with the id of 1 that are also comedies
; the WHERE is first located, and those items are then searched for the AND
“OR” in the other hand will allow us to do the same but what is returned may either contained what is signified by the WHERE or the OR
i.e.
SELECT title
FROM movies
WHERE id = 1
OR genre = ‘Comedy’; // will return movies that either have the id of 1 OR the genre of comedy
To add a new item to a SQL table we need an INSERT statement…here’s the recipe:
INSERT INTO ____ (_____) // The first blank indicates the table we want to add the data to and the second blank the column(s) we want to add it to
VALUES (_____); // In this blank we place the actual data values that will be added
Empty cells are considered to have a NULL value
To update values in a table use the UPDATE statement:
UPDATE ____ // The table the data is in
SET ____ = ____ // The first blank sets the column to be changed and the second blank is what you want the new value to be
WHERE _____ // This pinpoints where the change will take place (i.e. id = 1;)
i.e. Changing Pizza to Hamburger:
UPDATE concessions
SET item = 'Hamburger'
WHERE id = 8;
DELETE statement can be used to remove e rows that match specific conditions
DELETE Recipe:
DELETE FROM movies WHERE id = 5; // where is very important because it identifies which rows to remove
To create a new database, use the CREATE DATABASE Recipe:
CREATE DATABASE _____; —> this creates a new empty database // name of database goes in the blank
To remove a database we can use the DROP DATABASE Recipe:
DROP DATABASE ____; —> deleting a database is permanent // name of database goes in the blank
Once the database is created, we can create a new table inside using the CREATE TABLE Recipe:
CREATE TABLE ____ // name of table goes in the blank
(
Column_name 1 datatype,
column_name 2 datatype,
column_name 3 datatype,
…
);
i.e. 1
CREATE TABLE movies
(
Id int,
Title varchar (50),
Genre archer(100),
Duration int // int is used to tell the database that these columns will hold numbers
);
i.e. 2
Create table bootcamp people (
id serial primary key,
first_name varchar(128) not null, // varchar defines the max number of characters
last_name varchar(128) not null, // not null tells it that people are not allowed to leave first_name as an empty value
age integer not null,
current_mood varchar(128),
sql_confidence integer check (sql_confidence > 0), check (sql_confidence <= 10)
);
To remove a table from a database, we can use the DROP TABLE RECIPE:
Drop Table ____ ; // name of database goes in the blank
To add, modify and remove columns with can use the…
ALTER TABLE ADD RECIPE:
ALTER TABLE ____ // the table to make changes to
ADD COLUMN ______ ____; // The first blank is the name of the new column and the second blank is the data type of the new column
To update a table, we need to issue separate update commands
ALTER TABLE DROP:
ALTER TABLE____ // name of table we want to target
DROP COLUMN_____; // name of column we want to drop
A datatype is serial means that the first entry will be 1 and it should auto-increment by 1 each time
Day 13 Lesson
Basics & Refreshers
Checking for equality: we can check if an element is equal to a string, by using the triple equal operator (
===
) and keeping the string within ‘’ i.e.Checking for completion: i.e.
your main file should always be called index (i.e. index.html or index.js) index.js —> the index file is what you run on your server and stores your data; NOTE: this one can be written in any language, that’s why we have index.html script.js —> this file runs in the browser of the person who is visiting your website (runs on the user’s computer, not yours); NOTE: this one MUST be written in js
rm -rf
deletes your files permanentlySQL Lesson When deploying something to Heroku —> Heroku is a hosting/deployment platform (i.e. you can push your code to display in the internet) for small and mid-sized projects
heroku create unique-name
git push heroku master
//this pushes master branch to heroic in order to deploy project NOTE: this is a server, and when servers restart your data does not get saved; that’s why we are working in a more permanent solution, which is storing our information in a database… Unlike servers (APIs), databases don’t get restarted in ways that wipe your dataTo have access to express, we use the package manager, typically Yarn:
yarn init -y
//-y
tells it to answer yes to all questions Note: package.json holds all the third party dependencies that we need for our project, likeexpress
yarn add express
app.use
are middle bits of functionalities that we enable our express server to do.gitignore
file is where you place everything you want your git to ignore (generally we don’t want to commit our node_module folders because they are huge, so we’d put them into .gitignore)In order to run a javascript file (much like we do with
npx serve
) usenode index.js
Then, use Postman or your web browser to run http://localhost:3000/ Note: much like on real we click play and stop to run our commands, we need to do this manually by killing (control+C) and restarting (yarn start
) our server to see the changesSetting up connection to SQL database (in Heroku)
mkdir pg_demo
and cd into itgit init
// this will make a new repositorygit remote add heroic https://git.heroku.com/rbi=postgres-demo.git
// connect your git to herokuheroku login
// note: if signing in through the browser fails (i.e. no trust message or “waiting for login…” message), control+c and writeheroic login -i
to sign in through your local terminal ** This step is important because it will only work if you’re logged into Heroku ***heroku pg:psql
//this enables a direct connection to the database which is shared in HerokuSQL Queries: Select —> allows you to “select all” i.e. to pull the table data that is shared use `select from bootcamp_people order by id;`
ORDER BY
; we can select a category (i.e. ORDER BY duration;) and use commands like DESC (i.e.ORDER BY duration DESC;
) to sort by descending order; the default is ascending order—> will return movies with the id of 1 that are also comedies ; the WHERE is first located, and those items are then searched for the AND
i.e. Changing Pizza to Hamburger:
i.e. 1
i.e. 2