This is our sequel to the databases and promises workshops :)
- Fork this repository and create a new Cloud9 project by cloning your fork
- Each exercise should be done in a separate branch, branched off of master, with a descriptive name of your choice
- Completed exercises should be submitted as pull requests. The pull request name should be "Exercise #: " followed by the exercise's title
- All the exercises require the
bluebird
NPM package. Apackage.json
file is already initialized in this repo with this dependency. You'll have to do annpm install
upon cloning in order to get all the dependencies. - Different exercises may also require different NPM packages. This will require you 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 and Promisify the MySQL library, make a first query, extract the rows as well as close the connection. You may keep this file in your different commits, and copy its content to get started fresh on a new exercise :)
- Write a program that fetches all the databases in MySQL and prints them in a nice way 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 all the databases, and for each database, all the tables in that database.
- Use the
.map
function of bluebird to end up with an array of objects{databaseName, tableNames}
. - Don't hesitate to use
colors
orcli-table
or any other module of your choice to make the output nicer.
For this exercise you have nothing to submit. This is going to be useful for the following exercises
- Using the instructions from https://github.com/DecodeMTL/node-mysql-demo/blob/master/README.md, create the addressbook database, associated tables and data
- Write a program that fetches the first 10 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 - Bonus points: find a way to align the
#XX
part
- Write a program that fetches the first 10 accounts AND their address books, using the "billion queries method" from https://github.com/DecodeMTL/node-mysql-demo/blob/master/related-data.sql
- 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
- Write a program that fetches the first 10 accounts AND their address books, using the "best of both worlds method" from https://github.com/DecodeMTL/node-mysql-demo/blob/master/related-data.sql
- 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
- Write a program that fetches all the accounts and their address books, using a single MySQL
JOIN
query console.log
the data that you get back.- Notice that some fields are missing. When you did
SELECT *
in your query, MySQL output some fields that have the same names! For example:
*************************** 10. row ***************************
id: 1
email: [email protected]
password: LUW56VCP9SK
createdOn: 2016-05-16 12:35:09
modifiedOn: 2014-11-15 06:06:53
id: 1052
accountId: 1
name: Non Hendrerit Id Inc.
createdOn: 2014-11-10 23:49:48
modifiedOn: 2015-02-23 08:08:36
10 rows in set (0.00 sec)
- This is nobody's fault: a JavaScript object can only have one property with the same name... The proper way to fix this is to point out to your
SELECT
statement each column you want to fetch, and give it an alias. For example:
SELECT Account.id AS accountId, Account.email as accountEmail, AddressBook.id as addressBookId, AddressBook.name as addressBookName --...
- Fix the query, and output the list of all accounts with all their address books!
- Using whichever method you prefer, output a list of the first 10 accounts AND their first address book AND the first 10 entries for each address book.
- Make the output look nice using whichever method you prefer
- As you know, our database currently has 1000 accounts imported into it. Displaying them all together gives for a looong output!
- In this exercise, we're going to display the list of accounts 10 at a time, and fetch a new "page" after the user is ready
- Using a similar process as exercise #4, list the first 10 accounts in the database
- Using the
prompt
library, ask the user if they want to fetch the next page - If they say no, end the program
- If they say yes, fetch the next page of results
- Make sure your program detects when there are no more results, and display that to the user
- We're going to learn about a new MySQL functionality called
LAST_INSERT_ID()
. This is rarely used directly on the command line, so that's why you haven't seen it before.
- Basically, when you
INSERT
data in a MySQL table that has anAUTO_INCREMENT
id field, you have no idea what the new ID will be. - If you're on the command line, you could perhaps just do a
SELECT * FROM Account
and look at the last row. But what happens if someone inserted a new row just after you, before you have the time to look again? - The
LAST_INSERT_ID()
function will give you the latest inserted ID in your own connection. - Let's see this for ourselves:
- Run
INSERT INTO Account (email,password,createdOn,modifiedOn) VALUES ("[email protected]","Hunter2","2016-05-16 12:35:09","2014-11-15 06:06:53");
- Open a new terminal window, login to mysql, and run
INSERT INTO Account (email,password,createdOn,modifiedOn) VALUES ("[email protected]","Hunter42","2016-05-16 12:35:09","2014-11-15 06:06:53");
- In both windows, run
SELECT LAST_INSERT_ID()
- Notice that the query will return a different ID in each case, proving to you that you can use this function even if many queries are being done at the same time
- Run
- Using the
prompt
library, ask the user for an email, password and confirm password - After checking that they put the same password twice, create an account for them in MySQL
- Output back to the user that their account was created, along with their account ID