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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
.svn
*~
.*.swp


node_modules
7 changes: 7 additions & 0 deletions archive.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
create table if not exists archive(
id INTEGER PRIMARY KEY,
name text,
done text,
created_date text not null default TO_CHAR(NOW() :: DATE, 'dd/mm/yyyy'),
updated_date date
);
107 changes: 95 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,117 @@ console.log("works!!", process.argv[2]);
const pg = require('pg');

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

const client = new pg.Client(configs);

// For showing items in the todolist
let queryDoneCallback = (err, result) => {
if (err) {
console.log("query error", err.message);
} else {
console.log("result", result.rows );
}
client.end();

};

let clientConnectionCallback = (err) => {

if( err ){
console.log( "error", err.message );
console.log( "Connection callback error", err.message );
}
// Show items in todo list
else if (process.argv[2] === 'show'){
let text = "select * from items";

client.query(text, (err, result) => {
if (err) {
console.log("query error", err.message);
}
else {
console.log(result.rows);
console.log(
`
____ _____ ____ __ ____ ___ ____
( _ \\( _ )( _ \\ ( ) (_ _)/ __)(_ _)
) _ < )(_)( ) _ < )(__ _)(_ \\__ \\ )(
(____/(_____)(____/ (____)(____)(___/ (__)

`
);
result.rows.forEach(el => {
console.log(`
${el.id}. ${el.name} - [${el.done}]
created_at: ${el.created_date}
updated_at: ${el.updated_date}
`);
})
}
client.end();
});
}
// Add items in todo list
else if (process.argv[2] === 'add'){
let text = "insert into items (name, done) values ($1, $2) returning id";

const values = [process.argv[3], " "];

client.query(text, values, (err, result) => {
if (err) {
console.log("query error: " + err)
}
else{
console.log("Successfully Added!")
}

client.end();
});
}
// Mark item as done
else if(process.argv[2] === 'done'){
const updatedDate = `TO_CHAR(NOW() :: DATE, 'dd/mm/yyyy')`;

let text = "INSERT INTO todo (name) VALUES ($1) RETURNING id";
let text = `update items set done='X', updated_date=${updatedDate} where id=${process.argv[3]}`;

client.query(text, (err, result) => {
if (err) {
console.log("query error: " + err)
}
else{
console.log("Update Successful!")
}

client.end();
})
}
// Archive item by copying row into another table and deleting from current table
else if (process.argv[2] === 'archive'){
let text1 = `insert into archive (select * from items where id=${process.argv[3]})`;

let text2 = `delete from items where id=${process.argv[3]}`;

// Insert archived data into separate archive table
client.query(text1, (err, result) => {
if (err) {
console.log("query error: " + err)
}
else{
console.log("insert into archive table Successful!")
}
})

// Delete archive data from items table
client.query(text2, (err, result) => {
if (err) {
console.log("query error: " + err)
}
else{
console.log("delete from items table Successful!")
}
client.end();
})
}

const values = ["hello"];

client.query(text, values, queryDoneCallback);
};

client.connect(clientConnectionCallback);
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/bobbykwong/cli-todo-sql.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/bobbykwong/cli-todo-sql/issues"
},
"homepage": "https://github.com/bobbykwong/cli-todo-sql#readme",
"dependencies": {
"pg": "^8.0.2"
}
}
7 changes: 7 additions & 0 deletions tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
create table if not exists items(
id serial primary key,
name text,
done text,
created_date text not null default TO_CHAR(NOW() :: DATE, 'dd/mm/yyyy'),
updated_date text
);