-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
51 lines (46 loc) · 1.82 KB
/
index.ts
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
require('dotenv').config({path: __dirname + '/.env'});
const PQuery = require('prettyquery');
const esc = require('sql-escape');
async function main() {
const pQuery = new PQuery({user: process.env.DB_USER, password: process.env.DB_PASSWORD, db: process.env.DATABASE});
let num;
switch (process.argv[2]) {
case 'help':
const fs = require('fs');
const man = fs.readFileSync('./help.txt', 'utf8');
console.log();
console.log(man);
break;
case 'prev':
if (/--amt/.test(process.argv[3])) {
if (/all/.test(process.argv[3])) {
num = 'all';
} else {
num = /\d+/.exec(process.argv[3])[0];
num ? true : num = 10;
}
} else {
num = 10;
}
let gs;
if (num === 'all') {
gs = await pQuery.query(`SELECT * FROM gratitude ORDER BY id DESC;`); // gsus!
} else {
gs = await pQuery.query(`SELECT * FROM gratitude ORDER BY id DESC LIMIT ${num};`); // gsus!
}
console.log('\nWas grateful for:\n');
gs.forEach(g => console.log(`'${g.grateful_for}' on:`, g.datetime_submitted));
break;
case 'count':
const count = (await pQuery.query(`SELECT COUNT(*) amount_of_gratitude FROM gratitude;`))[0].amount_of_gratitude;
console.log(`\nI have ${count} units of gratitude recorded.\n`);
break;
default:
await pQuery.insert('gratitude', ['grateful_for'], [[process.argv[2]]]);
console.log(`Was grateful for: ${process.argv[2]}`);
break;
}
pQuery.connection.end();
process.exit(0);
}
main().catch(err => console.error(err));