· 8 years ago · Apr 26, 2018, 09:50 AM
1const { Client } = require('pg')
2const fs = require('fs')
3const csv = require('csv-streamify')
4
5const parseCSV = require('./parseCSV')
6
7// Create a client
8const client = new Client()
9
10// Stuff to do to close well
11process.stdin.resume();//so the program will not close instantly
12
13function exitHandler(options, err) {
14 if (options.cleanup) client.end().then(() => {
15 console.log("Connection Closed 👌")
16 if (options.exit) process.exit();
17 })
18 else if (options.exit) process.exit();
19 if (err) console.log(err.stack);
20}
21
22//do something when app is closing
23process.on('exit', exitHandler.bind(null,{cleanup:true}));
24
25//catches ctrl+c event
26process.on('SIGINT', exitHandler.bind(null, {exit:true}));
27
28// catches "kill pid" (for example: nodemon restart)
29process.on('SIGUSR1', exitHandler.bind(null, {exit:true}));
30process.on('SIGUSR2', exitHandler.bind(null, {exit:true}));
31
32//catches uncaught exceptions
33process.on('uncaughtException', exitHandler.bind(null, {exit:true}));
34
35
36
37
38// print helper for the console
39global.print = (res) => {
40 console.log('Response from database :\x1b[35m')
41 console.log(res.rows)
42 console.log("\x1b[0m")
43}
44
45// Connect the client
46client.connect().then(() => {
47 client.query('SELECT COUNT(*) FROM clips', (err, res) => {
48 err ? createDatabase() : startRepl()
49 })
50})
51
52// If the database does not have the good tables, create & populate them
53const createDatabase = () => {
54 console.log('Database did not exist')
55 console.log(' Creating Table clips')
56 client.query(`
57 CREATE TABLE clips (
58 clipID serial,
59 clipTitle text,
60 clipYear smallint,
61 clipType char(100),
62 PRIMARY KEY (clipID)
63 );`
64 , (err, res) => {
65 if (err) { console.log(err); return }
66 console.log(' Creating Table links')
67 client.query(`
68 CREATE TABLE links (
69 clipIDFrom serial,
70 clipIDTo serial,
71 linkType char(100),
72 PRIMARY KEY (clipIDFrom, clipIDTo),
73 FOREIGN KEY (clipIDFrom) REFERENCES clips(clipId) ON DELETE CASCADE,
74 FOREIGN KEY (clipIDTo) REFERENCES clips(clipId) ON DELETE CASCADE
75 );`
76 , (err, res) => {
77 if (err) { console.log(err); return }
78 console.log(' Creating Table ratings')
79 client.query(`
80 CREATE TABLE ratings (
81 clipID serial,
82 numberOfVotes int,
83 rank bigint,
84 PRIMARY KEY (clipID),
85 FOREIGN KEY (clipID) REFERENCES clips ON DELETE CASCADE
86 );`
87 , (err, res) => {
88 if (err) { console.log(err); return }
89 console.log(' Creating Table people')
90 client.query(`
91 CREATE TABLE people (
92 pid serial,
93 FullName char(100),
94 PRIMARY KEY (pid)
95 );`
96 , (err, res) => {
97 if (err) { console.log(err); return }
98 console.log(' Creating Table acted')
99 client.query(`
100 CREATE TABLE acted (
101 pid serial,
102 clipID serial,
103 add_info text,
104 PRIMARY KEY (pid, clipID),
105 FOREIGN KEY (pid) REFERENCES people,
106 FOREIGN KEY (clipID) REFERENCES clips
107 );`
108 , (err, res) => {
109 if (err) { console.log(err); return }
110 console.log(' Creating Table roles')
111 client.query(`
112 CREATE TABLE roles (
113 pid serial,
114 order_credits int,
115 roles text,
116 FOREIGN KEY (pid) REFERENCES people
117 );`
118 , (err, res) => {
119 if (err) { console.log(err); return }
120 console.log(' Creating Table countries')
121 client.query(`
122 CREATE TABLE countries(
123 coID serial,
124 name char(100),
125 PRIMARY KEY (coID)
126 );`, (err, res) => {
127 if (err) { console.log(err); return }
128 startRepl()
129 parseCSV.run()
130 })
131 })
132 })
133 })
134 })
135 })
136 })
137
138}
139
140// Otherwise (and then) start the console
141const startRepl = () => {
142 global.client = client
143 console.log('Database Exists')
144 console.log('run \x1b[33mclient.query(\'SELECT * FROM A\').then(print)\x1b[0m')
145 require('repl').start({})
146}
147
148// The actual check of weather the good tables exists