Skip to content
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
182 changes: 164 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,182 @@
console.log("works!!", process.argv[2]);






console.log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
console.log("~~~~~~~~~ TO DO LIST PROGRAM ~~~~~~~~");
console.log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");


const pg = require('pg');

const configs = {
user: 'akira',
user: 'shane',
host: '127.0.0.1',
database: 'todo',
port: 5432,
};

const client = new pg.Client(configs);
const consoleCommand = process.argv[2];

// A function that reads what command user inputted
checkCommand = (input) => {
console.log("Checking for command")

const client = new pg.Client(configs);

// create a global status to make sure only correct commands are true
let commandStatus = false

if( consoleCommand == "add"){
commandStatus = true;
console.log("-----------------------");
console.log("INITIALIZE COMMAND: ADD");

let queryDoneCallback = (err, result) => {
if (err) {
console.log("query error", err.message);
} else {
console.log("result", result.rows );
// This function runs if query returns a response
let queryDoneCallback = (err, result) => {

if (err) {
console.log("////////////////////////");
console.log("ADD ERROR");
console.log(err.message);
console.log("////////////////////////");
} else {
// ???
// how do I view what is inside result.rows object?
console.log("result", result.rows );
}
// ???
// why put this code outside of else block?
console.log("-----------------------");
console.log("DISCONNECTING DATABASE")
client.end();
};


// Only activates when connection is successful
let addnewList = (err) => {
// checks if connection has errors
if( err ){
console.log("////////////////////////");
console.log("CONNECTION ERROR :");
console.log( err.message );
console.log("////////////////////////");
}
// specifies where to add the data
let text = "INSERT INTO todoitems (status, instructions) VALUES ($1, $2) RETURNING id";
// retrieve input from process to fill data
const inputData = [process.argv[3], process.argv[4]];

client.query(text, inputData, queryDoneCallback);
console.log("-----------------------");
console.log("finished query...");
};
console.log("-----------------------");
console.log("making the connection to database [todo]...");
client.connect(addnewList);
}
client.end();
};

let clientConnectionCallback = (err) => {

if( err ){
console.log( "error", err.message );
}

let text = "INSERT INTO todo (name) VALUES ($1) RETURNING id";

const values = ["hello"];

client.query(text, values, queryDoneCallback);
// command to edit the list
if( consoleCommand == "edit"){
commandStatus = true;
console.log("-----------------------");
console.log("INITIALIZE COMMAND: EDIT");
console.log();
}





// command to delete one list
if( consoleCommand == "delete"){
commandStatus = true;
console.log("-----------------------");
console.log("INITIALIZE COMMAND: DELETE");

queryDoneCallback = (err, remainder) =>{
if(err){
console.log("////////////////////////");
console.log("CONNECTION ERROR :");
console.log( err.message );
console.log("////////////////////////");
} else {
console.log("-----------------------");
console.log("deleted")
}
client.end();
}

deleteList = () =>{
let text = "DELETE FROM todoitems WHERE id = 10"
client.query(text,queryDoneCallback)
};

client.connect(deleteList);
}





// command to view the list
if( consoleCommand == "view"){
commandStatus = true;
console.log("-----------------------");
console.log("INITIALIZE COMMAND: VIEW");

let queryDoneCallback = (err, result) => {
if (err) {
console.log("////////////////////////");
console.log("ADD ERROR");
console.log(err.message);
console.log("////////////////////////");
} else {
// ???
// how do I view what is inside result.rows object?
console.table( result.rows );
}
// ???
// why put this code outside of else block?
console.log("-----------------------");
console.log("DISCONNECTING DATABASE")
client.end();
};

viewList = (err) => {
if(err){
console.log("////////////////////////");
console.log("CONNECTION ERROR :");
console.log( err.message );
console.log("////////////////////////");
}
let listTable = "SELECT * FROM todoitems";

client.query(listTable, queryDoneCallback);

}
console.log("-----------------------");
console.log("making the connection to database [todo]...");
client.connect(viewList)
}





// checks if command does not exist
if(commandStatus === false){
console.log("////////////////////////");
console.log("ERROR: COMMAND DOES NOT EXIST");
console.log("entered command: " + consoleCommand);
console.log("////////////////////////");
}
};

client.connect(clientConnectionCallback);
checkCommand();
119 changes: 119 additions & 0 deletions package-lock.json

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

22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "cli-todo-sql",
"version": "1.0.0",
"description": "![https://i.giphy.com/media/26ufnwz3wDUli7GU0/giphy.webp](https://i.giphy.com/media/26ufnwz3wDUli7GU0/giphy.webp)",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/wdi-sg/cli-todo-sql.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/wdi-sg/cli-todo-sql/issues"
},
"homepage": "https://github.com/wdi-sg/cli-todo-sql#readme",
"dependencies": {
"pg": "^8.0.2"
}
}