-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
137 lines (118 loc) · 3.12 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const { argv } = require("process");
const assert = require("assert");
const { Client } = require("pg");
require("dotenv").config();
argv.splice(0, 2);
assert(
argv.length == 1 || argv.length == 2,
"no of input args must be less than 2"
);
//db
const client = new Client({
host: process.env.PG_HOST,
port: process.env.PG_PORT,
user: process.env.PG_USER,
password: process.env.PG_PASSWORD,
database: process.env.PG_DATABASE,
});
console.log(`client made`);
const logAllOptions = () => {
console.log(`Options:
--new to add a new todo item
--list [all|pending|done] to list the todo items
--done [id] to update a todo item
--delete [id] to delete a todo item
--help to list all the available options
--version to print the version of the application`);
};
const logAppVersion = () => {
const ver = require("./package.json").version;
console.log(ver);
};
// db funcs
const insertItem = async (todoItem) => {
await client.connect();
try {
const res = await client.query(
`INSERT INTO todos (todo, status) VALUES ($1, $2);`,
[todoItem, "pending"]
);
} catch (e) {
console.log("db insertion error", e);
} finally {
await client.end();
}
console.log(`added the todo - "${todoItem}"`);
};
const logTodosAccToFormat = async (opt) => {
await client.connect();
let res = null;
try {
if (opt === "all") {
res = await client.query(`SELECT * FROM todos;`);
} else {
res = await client.query(`SELECT * FROM todos WHERE status = $1;`, [opt]);
}
} catch (e) {
console.log("db read error: ", e);
} finally {
await client.end();
}
console.table(res.rows);
};
const updateTodoAsDone = async (id) => {
await client.connect();
try {
const res = await client.query(
`UPDATE todos SET status = $1 WHERE id = $2;`,
["done", id]
);
} catch (e) {
console.log("db updating error: ", e);
} finally {
await client.end();
}
console.log(`modified the todo with id: "${id}" as done`);
};
const deleteTodo = async (id) => {
await client.connect();
try {
const res = await client.query(`DELETE FROM todos WHERE id = $1`, [id]);
} catch (e) {
console.log("db row deleting error: ", e);
} finally {
await client.end();
}
console.log(`deleted the todo with id: ${id}`);
};
const init = async () => {
console.log(`init started`);
switch (argv[0]) {
case "--new":
console.log(`inserting item ${argv[1]}`);
await insertItem(argv[1]);
break;
case "--list":
assert(["all", "pending", "done"].includes(argv[1]));
console.log(`listing todos acc to format - ${argv[1]}`);
logTodosAccToFormat(argv[1]);
break;
case "--done":
console.log(`updating a todo item as done - id: ${argv[1]}`);
updateTodoAsDone(argv[1]);
break;
case "--delete":
console.log(`deleting a todo item -id: ${argv[1]}`);
deleteTodo(argv[1]);
break;
case "--help":
logAllOptions();
break;
case "--version":
logAppVersion();
break;
default:
console.log(`Pls enter a valid command - not doing anything`);
}
};
init();