-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.js
99 lines (89 loc) · 4.06 KB
/
queries.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { Sequelize, Model, DataTypes, QueryTypes, sql } from '@sequelize/core';
//imports dontenv module and allows us to access stored environment variables stored in .env file
import 'dotenv/config';
import { Listing } from './ListingModel.js';
/* Connect to your database */
const sequelize = new Sequelize(process.env.API_URL);
// For this starter code I will use async-await and regular async functional notation.
try {
/* Testing the Connection */
await sequelize.authenticate();
console.log('Connection has been established successfully.');
}
catch (error) {
// error if unable to connect to database
console.error('Unable to connect to the database:', error);
}
/* Retrieve all listings in the database, and log them to the console */
async function retrieveAllListings() {
console.log('Retrieving all listings');
// the findAll() function gets all entries in the Listing table
const listing = await Listing.findAll();
// prints all listings to the console
console.log("All listings:", JSON.stringify(listing, null, 2));
}
/* Find the document that contains data corresponding to Library West, then log it to the console */
async function findLibraryWest() {
console.log('Finding Library West');
// findOne() gets one entry with the specified parameters
const listing = await Listing.findOne( {where: { code: 'LBW', name: 'Library West' } });
if (listing === null) {
console.log('Not found!');
} else {
// print the code and name once found
console.log("Listing found:", JSON.stringify(listing, null, 2));
}
}
/* Find the document with the code 'CABL' and remove this listing */
async function removeCable() {
console.log('Removing Cable BLDG');
// find the entry to remove
const listing = await Listing.findOne( {where: { code: 'CABL', name: 'Course viewed only on cable TV' } });
if (listing === null) {
console.log('Not found!');
}
else {
// print the entry's code and name
console.log("Deleted listing:", JSON.stringify(listing, null, 2));
// destroy() removes the entry with the specified parameters
await Listing.destroy( {where: { code: 'CABL', name: 'Course viewed only on cable TV' } });
}
}
/* Create a listing for the new Data Science and IT (DSIT) Building. Add the code and name to the database */
async function addDSIT() {
console.log('Adding the new DSIT BLDG that will be across from Reitz union. Bye Bye CSE, Hub, and French Fries.');
// findOrCreate() finds the entry if it exists or creates the specified entry if it does not exist already
const [listing, created] = await Listing.findOrCreate({
where: { code: 'DSIT', name: 'Data Science and IT Building' } });
if (created) {
// print the new entry
console.log("New listing:", JSON.stringify(listing, null, 2));
}
else if (listing) {
// print the existing entry
console.log("Listing found:", JSON.stringify(listing, null, 2));
}
}
/* The Phelps Lab address is incorrect. Find the listing, update it with the correct address.
Then log the updated listing in the database and use console.log to inspect it. */
async function updatePhelpsLab() {
console.log('UpdatingPhelpsLab.');
// update() revises the entry's specified attribute(s)
await Listing.update({ address: '1953 Museum Rd, Gainesville, FL 32603'},
{ where: { code: 'PHL', name: 'Phelps Laboratory' } });
const newListing = await Listing.findOne( {where: { code: 'PHL', name: 'Phelps Laboratory' } });
if (newListing === null) {
console.log('Not found!');
}
else {
// print the revised entry to the console
console.log("Revised listing:", JSON.stringify(newListing, null, 2));
}
}
console.log("Use these calls to test that your functions work. Use console.log statements in each so you can look at the terminal window to see what is executing. Also check the database.")
// Calling all the functions to test them
// retrieveAllListings()
// removeCable();
// addDSIT();
// updatePhelpsLab();
// findLibraryWest();