Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jude Pierre | Friday 6:30 | Instructor Donato, TA Edgardo #41

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 9 additions & 8 deletions config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"username": "ctp_user",
"password": "ctp_pass",
"database": "learn_sequelize",
"host": "127.0.0.1",
"dialect": "postgres",
"logging": false
}
"test": {
"use_env_variable": "DATABASE_URL",
"dialect": "postgres",
"dialectOptions": {
"ssl": {
"rejectUnauthorized": false
}
}
}
}
87 changes: 72 additions & 15 deletions learn-sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@ const { Genre, Movie, Actor } = require("./models");
- add one more Genre of your choice
- duplicate entries are not allowed (try it to learn about errors)
*/
function insertNewGenre() {
// Add code here
async function insertNewGenre() {
try {
// Adding a new genre
await Genre.create({
id: 4,
name: 'SciFi',
});
}catch(e){
console.log("Error creating genre", e)
}
}

/*
Expand All @@ -16,36 +24,73 @@ function insertNewGenre() {
- add one more Movie of your choice.
- the movie CANNOT be from year 2008 (try it to learn about errors)
*/
function insertNewMovie() {
// Add code here
async function insertNewMovie() {
await Movie.create({
id: 6,
title: 'Blue Beetle',
year: 2023
})

}

/*
Write a function that returns the title of the movie with ID=2
*/
function getMovieWithId2() {
// Add code here
async function getMovieWithId2() {
const movieWithId2 = await Movie.findAll({
where:{
id: 2
}
})
return movieWithId2[0].title;

}

/*
Write a function that returns an array of all the actor names
*/
function getAllActors() {
// Add code here
async function getAllActors() {
const actors = await Actor.findAll();

const actorNames = []

for(let i = 0; i < actors.length; i++){
actorNames.push(actors[i].name)
}



return actorNames;
}

/*
Write a function that returns an array of all the movie titles from 2008
*/
function getAllMoviesFrom2008() {
// Add code here
async function getAllMoviesFrom2008() {

const moviesFrom2008 = await Movie.findAll({
where:{ year: 2008}
})


const movieNamesFrom2008 = [];

for(let i = 0; i <moviesFrom2008.length; i++ ){
movieNamesFrom2008.push(moviesFrom2008[i].title)
}

return movieNamesFrom2008;
}

/*
Write a function that deletes the genre you added in the first function: insertNewGenre()
*/
function deleteGenreYouAdded() {
// Add code here
async function deleteGenreYouAdded() {
await Genre.destroy({
where:{
id: 4
}
})
}

/*
Expand All @@ -54,8 +99,16 @@ function deleteGenreYouAdded() {
- the actor and movie record already exist in the database
- add the association record to the database
*/
function associateRosarioToEagleEye() {
// Add code here
async function associateRosarioToEagleEye() {
Actor.belongsToMany(Movie, { through: "ActorMovies" });
Movie.belongsToMany(Actor, { through: "ActorMovies" });

const actor = await Actor.findOne({where:{
name: 'Rosario Dawson'
}})

const movie = await Movie.findOne({where:{title: 'Eagle Eye'}});
await actor.addMovie(movie)
}

/*
Expand All @@ -65,7 +118,11 @@ function associateRosarioToEagleEye() {
- add the association record to the database
*/
async function associateRobertToTropicThunder() {
// Add code here
const actor = await Actor.findOne({where:{name: 'Robert Downey Jr.'}})

const movie = await Movie.findOne({where:{title: 'Tropic Thunder'}});

await actor.addMovie(movie)
}

module.exports = {
Expand Down
7 changes: 6 additions & 1 deletion models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ const db = {};

let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);

sequelize = new Sequelize(
"postgresql://postgres.sgeqliohbvztbqllylji:[email protected]:6543/postgres",
config
);

} else {
sequelize = new Sequelize(
config.database,
Expand Down
27 changes: 16 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.