This is our sequel to the databases workshops :)
- Fork this repository and create a new Cloud9 project by cloning your fork
- All exercises to be done on master, in a file called
exercise-#.js
- Some exercises may require NPM packages. You will need to use
npm install --save
to get these packages. Thepackage.json
should be part of the same commit where you start using the required package(s). For example, if one of the exercises requires the use of therequest
library, then thepackage.json
for that submission should contain the dependency, and it should be committed at the same time as theexercise-#.js
thatrequire
s it. - A
boilerplate.js
file is made available to you. It contains the base code to load the MySQL library, make a first query, extract the rows as well as close the connection. You may copy this file for each exercise so that you have a base to get started from.
Before starting the exercises, import the file called data.sql
in MySQL. This will create a database called addressbook
. The database will contain a few tables. Explore all the tables using describe
to get a feel for this address book schema. Don't come back here until you know the table structure well! It'll make it easier to do the exercises.
- Write a program that fetches all the databases in MySQL and prints them nicely on the screen.
- Don't hesitate to use
colors
orcli-table
or any other module of your choice to make the output nicer.
- Write a program that fetches the first 5 accounts in the addressbook database
- For each account,
console.log
a line with the account's ID and email, like this:#1:
[email protected]
- Use the
colors
NPM module with the.bold
option to achieve this effect
- Write a program that fetches all the accounts and their addressbooks.
- Output one line for each account as in Exercise 4, followed by a listing of all the address book names for that account, one per line
- Make the output look nice in any way you like
- Here is an example:
#1: [email protected]
business contacts
friends
#2: [email protected]
...
- Notice that for the previous exercise, Account #5 did not appear in the listing. Don't come back here until you have re-checked the previous exercise and noticed for yourself that Account #5 is missing.
- The reason for this is because Account #5 does not have any AddressBook, so doing the JOIN left it out.
- Read and understand this article on SQL JOINs, more specifically about the
LEFT JOIN
. - Based on your new understanding, create a similar program to Exercise #4.
- The only difference, if an account does not have any address book, print it like this:
#3: [email protected]
--no address books--
- Write a program that fetches all accounts, their addressbooks, and entries.
- Hint #1: you will have two
JOIN
s in your query. - Hint #2: you will need to use
AS
to give each column a unique name - Once you receive your results in JavaScript land, transform them into an array of accounts with nested relations.
- Here is an example of the end result we are looking for. Note you will probably need to use some of these array methods we saw together in week 1. This is a good time to remind yourself of them, specifically
map
andreduce
.
[
{
id: 1,
email: '[email protected]',
addressBooks: [
{
id: 1,
name: 'business contacts',
entries: [
{
id: 1,
firstName: 'John',
lastName: 'Connor'
},
{
id: 10,
firstName: 'Sarah',
lastName: 'Connor'
}
]
},
{
// another address book for account 1...
}
]
},
{
// another account...
}
]
- This exercise expands on #6. Here we want to get all accounts with all address books, all entries, AND all emails/phones/addresses
- We want to get an array of accounts as the output
- The only difference is that each entry will now contain arrays for
emails
,phones
andaddresses
, basically one more level of nesting
- Using
inquirer
,prompt
or a similar library, ask the user for their email address and use it as a "login" to the address book system in the following way. - Find the account that corresponds to this email, and show the user a list of
firstName lastName
entries, listed in alphabetical order of last name. - When the user chooses an entry, display a
cli-table
showing all the data for that entry. - Then loop back to show them all the entries again.
- Hint #1: if a person (account) has multiple address books, we want to see all entries from all address books, no matter what.
- Hint #2: you will need to join a few tables together to get your result.
- This exercise builds up on Exercise #8.
- The only difference is, when showing the list of entries, we will show them 5 by 5.
- Below the list of 5 entries, we will have two extra options for "Previous Page" and "Next Page"
- These options should only be enabled if there is a previous/next page
- Selecting one of them should show the appropriate entries
- The rest of the program works the same way
- This exercise builds up on Exercise #8. It does NOT require Exercise #9, but can go well with it :)
- The only difference is, in addition to the list of entries, the user will have a choice to "Search entries"
- Choosing "search entries" will make the program ask for an input search
- Following the search, the program should show only the entries that match the search
- Matching the search means: firstName contains search term OR lastName contains search term OR any email address contains search term OR any address line1/city/state/country match the search term.
- The rest of the program works the same way
For this challenge, you will link up your address book project with MySQL:
- Your app will NOT be holding the full address book data as an array anymore!
- All the data ("source of truth") will be in your MySQL database, as tables
- When you choose "Create", the app will ask you all the same info, but at the end will put it in the table structure account/addressbook/entry/emails+addresses+phones.
- When someone does a search, do the search using a MySQL query instead
- When somemone deletes an entry, delete it from MySQL
- All the rest of the application stays the same
If you worked on the Slack Bot workshop, do the following challenge. For this one, we will make the bot keep track of how many messages each user sends per channel. At the end of the day we want to be able to produce a report with the top messengers per channel and per day/week/month. Weeks start on Monday and months start on the 1st of the month.
For this challenge, you will have to create the data schema yourself in MySQL, create the bot code that will compile the message counts and put them in MySQL, as well as create a separate NodeJS command-line app that does the reporting.
Have fun :)