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

const pg = require('pg');

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

const client = new pg.Client(configs);

let queryDoneCallback = (err, result) => {
///////////////////////////////////////////////

const user_command = process.argv[2];
const input_index = process.argv[3];


const renderList = function(result){
const list = result.rows;
list.forEach(obj => console.log(`${obj.id}. ${obj.done} - ${obj.thing}`));
}

//callback function after sending query
const queryDoneCallback = (err, result) => {
if (err) {
console.log("query error", err.message);
} else {
console.log("result", result.rows );
// console.log(res.rows);
renderList(result);
}
client.end();
};

let clientConnectionCallback = (err) => {
const callbackShow = (err) => {
if (err) {
console.log("query error", err.message);
} else {
queryText = 'SELECT * FROM todolist ORDER BY id ASC';
client.query(queryText, queryDoneCallback);
}
}

//callback function after successful connection
const clientConnectionCallback = (err) => {
let queryText = undefined;
let values = undefined;

if( err ){
console.log( "error", err.message );
}
} else if (user_command === 'add'){
queryText = 'INSERT INTO todolist (thing, done) VALUES ($1, $2) RETURNING *';
values = [process.argv[3], '[ ]'];
client.query(queryText, values, callbackShow);

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

const values = ["hello"];
} else if (user_command === 'show'){
queryText = 'SELECT * FROM todolist;';
client.query(queryText, values, queryDoneCallback);

client.query(text, values, queryDoneCallback);

} else if (user_command === 'done'){
queryText = `UPDATE todolist SET done = '[X]' WHERE id = ${input_index}`;
client.query(queryText, values, callbackShow);

}
};

//connects to pg with a callback function
client.connect(clientConnectionCallback);
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/dannst/cli-todo-sql.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/dannst/cli-todo-sql/issues"
},
"homepage": "https://github.com/dannst/cli-todo-sql#readme",
"dependencies": {
"pg": "^8.0.2"
}
}