5. Examples
A base repository for use with these examples (contains some simple code to help get started) can be found here
Example 1: CLI-Based Item Manager
You are tasked with building a Command Line Interface (CLI) application that connects to a PostgreSQL database and allows the user to manage a simple list of items. Your app should be able to:
- Create a table in the database for storing item information if it doesn’t already exist.
- Insert a new item into the table by passing the item name as a command-line argument.
- Display all items stored in the table.
Functional Requirements:
The database should have a table called
items
with two columns:id
(integer): The primary key, which auto-increments.name
(text): The name of the item.
The CLI should support two commands:
insert <item_name>
: Inserts an item with the provided name into the database.show
: Displays all the items currently stored in the table.
Detailed Breakdown
Setting up the database connection:
- Use the
pg
npm package to connect to your PostgreSQL database. - The connection details (username, password, etc.) should be set up in the script.
- Use the
Creating the
items
table:- Before any data can be inserted, the script should ensure the table exists.
- Use the SQL statement
CREATE TABLE IF NOT EXISTS
to create the table if it doesn’t already exist.
Inserting data:
- When the user runs the command
insert <item_name>
, the script should take the item name from the command-line argument and insert it into theitems
table. - Use parameterized queries to avoid SQL injection.
- When the user runs the command
Displaying all data:
- When the user runs the command
show
, the script should query theitems
table and display all the rows (showing theid
andname
of each item).
- When the user runs the command
Input/Output Examples
Inserting an item:
- Command:
node index.js insert "Apple"
- Output:
Inserted item: Apple
- Command:
Displaying all items:
- Command:
node index.js show
- Output:
Items in the table: 1: Apple 2: Orange
- Command:
Additional Instructions
- Ensure that the database connection is properly closed after each command to avoid hanging processes.
- Handle any errors that might occur during the database connection or query execution and provide meaningful error messages to the user.
- Use parameterized queries when inserting data to prevent SQL injection attacks.
Hint
- For PostgreSQL interaction, the
pg
library will be used. Make sure to install it using:npm install pg
- You’ll need to pass arguments from the command line, which can be accessed via
process.argv
in Node.js. - Make sure to test both commands (
insert
andshow
) to verify that your application works correctly.
Bonus Task
- Add a command to delete all items from the
items
table, giving users a way to clear the list. - Command:
node index.js clear
Example 2: CLI-Based To-Do List Manager
You are tasked with building a Command Line Interface (CLI) application that connects to a PostgreSQL database and allows users to manage a to-do list. Your app should be able to:
- Create a table in the database for storing to-do items if it doesn’t already exist.
- Insert a new to-do item into the table via command-line arguments.
- Display all to-do items stored in the table.
- Mark a to-do item as completed by its ID.
- Delete a to-do item by its ID.
Functional Requirements:
The database should have a table called
todos
with the following columns:id
(serial primary key): The unique identifier for each to-do item.task
(text): The description of the to-do item.completed
(boolean): Indicates whether the item has been completed (default isfalse
).
The CLI should support the following commands:
add <task>
: Adds a new to-do item with the provided task description.show
: Displays all to-do items along with their IDs and completion status.complete <id>
: Marks the to-do item with the given ID as completed.delete <id>
: Deletes the to-do item with the specified ID.
Detailed Breakdown
Setting up the database connection:
- Use the
pg
npm package to connect to your PostgreSQL database. - Set up the connection details (username, password, etc.) in the script.
- Use the
Creating the
todos
table:- Ensure the
todos
table exists before any data manipulation occurs. - Use the SQL statement
CREATE TABLE IF NOT EXISTS
to create the table.
- Ensure the
Inserting a to-do item:
- When the user runs the command
add <task>
, the script should take the task description from the command-line argument and insert it into thetodos
table.
- When the user runs the command
Displaying all to-do items:
- When the user runs the command
show
, the script should query thetodos
table and display all rows (showing theid
,task
, andcompleted
status).
- When the user runs the command
Completing a to-do item:
- When the user runs the command
complete <id>
, the script should update thecompleted
status of the to-do item with the given ID totrue
.
- When the user runs the command
Deleting a to-do item:
- When the user runs the command
delete <id>
, the script should remove the to-do item with the specified ID from thetodos
table.
- When the user runs the command
Input/Output Examples
Adding a to-do item:
- Command:
node index.js add "Finish homework"
- Output:
Added to-do item: Finish homework
- Command:
Displaying all to-do items:
- Command:
node index.js show
- Output:
To-Do List: 1: Finish homework [Not completed] 2: Buy groceries [Not completed]
- Command:
Completing a to-do item:
- Command:
node index.js complete 1
- Output:
Marked item 1 as completed.
- Command:
Deleting a to-do item:
- Command:
node index.js delete 2
- Output:
Deleted item 2 from the list.
- Command:
Additional Instructions
- Ensure that the database connection is properly closed after each command to avoid hanging processes.
- Handle any errors that might occur during database connection or query execution, providing meaningful error messages.
- Use parameterized queries to prevent SQL injection attacks when inserting or updating data.
Hint
- For PostgreSQL interaction, the
pg
library will be used. Make sure to install it using:npm install pg
- Access command-line arguments through
process.argv
in Node.js. - Test all commands to verify the application works correctly.
Bonus Task
- Implement a command to clear all completed to-do items from the
todos
table. - Command:
node index.js clear
Example 3: CLI-Based Library Book Manager
You are tasked with building a Command Line Interface (CLI) application that interacts with a PostgreSQL database and allows users to manage a library’s book collection. Your app should be able to:
- Create a table in the database for storing book information if it doesn’t already exist.
- Insert a new book record into the table via command-line arguments.
- Display all books stored in the table.
- Update book information (like title or author) by the book’s ID.
- Mark a book as checked out by its ID.
- Delete a book by its ID.
- List all available books (i.e., books that haven’t been checked out).
Functional Requirements:
The database should have a table called
books
with the following columns:id
(serial primary key): The unique identifier for each book.title
(text): The title of the book.author
(text): The author of the book.checked_out
(boolean): Indicates whether the book has been checked out (default isfalse
).
The CLI should support the following commands:
add <title> <author>
: Adds a new book with the given title and author to the collection.show
: Displays all books along with their IDs, titles, authors, and check-out status.update <id> <field> <new_value>
: Updates the specified field (title
orauthor
) of the book with the given ID.checkout <id>
: Marks the book with the given ID as checked out.delete <id>
: Deletes the book with the specified ID.available
: Displays all books that have not been checked out.
Detailed Breakdown
Setting up the database connection:
- Use the
pg
npm package to connect to your PostgreSQL database. - Set up the connection details (username, password, etc.) in the script.
- Use the
Creating the
books
table:- Ensure the
books
table exists before any data manipulation occurs. - Use the SQL statement
CREATE TABLE IF NOT EXISTS
to create the table.
- Ensure the
Inserting a new book:
- When the user runs the command
add <title> <author>
, the script should take the title and author from the command-line arguments and insert them into thebooks
table.
- When the user runs the command
Displaying all books:
- When the user runs the command
show
, the script should query thebooks
table and display all rows (showing theid
,title
,author
, andchecked_out
status).
- When the user runs the command
Updating book information:
- When the user runs the command
update <id> <field> <new_value>
, the script should update the specified field (eithertitle
orauthor
) of the book with the given ID.
- When the user runs the command
Marking a book as checked out:
- When the user runs the command
checkout <id>
, the script should update thechecked_out
status of the book with the given ID totrue
.
- When the user runs the command
Deleting a book:
- When the user runs the command
delete <id>
, the script should remove the book with the specified ID from thebooks
table.
- When the user runs the command
Listing available books:
- When the user runs the command
available
, the script should display all books where thechecked_out
status isfalse
.
- When the user runs the command
Input/Output Examples
Adding a new book:
- Command:
node index.js add "The Hobbit" "J.R.R. Tolkien"
- Output:
Added book: The Hobbit by J.R.R. Tolkien
- Command:
Displaying all books:
- Command:
node index.js show
- Output:
Library Books: 1: The Hobbit by J.R.R. Tolkien [Available] 2: 1984 by George Orwell [Checked out]
- Command:
Updating book information:
- Command:
node index.js update 1 title "The Hobbit: Revised Edition"
- Output:
Updated book 1: Title changed to "The Hobbit: Revised Edition"
- Command:
Marking a book as checked out:
- Command:
node index.js checkout 1
- Output:
Marked book 1 as checked out.
- Command:
Deleting a book:
- Command:
node index.js delete 2
- Output:
Deleted book 2 from the collection.
- Command:
Listing available books:
- Command:
node index.js available
- Output:
Available Books: 1: The Hobbit by J.R.R. Tolkien
- Command:
Additional Instructions
- Ensure that the database connection is properly closed after each command to avoid hanging processes.
- Handle any errors that might occur during database connection or query execution, providing meaningful error messages.
- Use parameterized queries to prevent SQL injection attacks when inserting, updating, or deleting data.
Hint
- For PostgreSQL interaction, the
pg
library will be used. Make sure to install it using:npm install pg
- Access command-line arguments through
process.argv
in Node.js. - Test all commands to verify the application works correctly.
Bonus Task
- Implement a command to return a checked-out book by updating its
checked_out
status tofalse
. - Command:
node index.js return <id>