-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
50 lines (40 loc) · 1.21 KB
/
database.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
// Generated by CoffeeScript 1.10.0
var client, connectionString, loadData, pg, query, quotes;
pg = require('pg');
quotes = [];
connectionString = 'postgres://localhost:5432/benbot';
console.log(connectionString);
client = new pg.Client(connectionString);
client.connect();
query = client.query('CREATE TABLE quotes(id SERIAL PRIMARY KEY, text VARCHAR(255) not null, date_last_quoted TIMESTAMP)');
console.log('createdb benbot');
query.on('end', function(result) {
console.log('query end');
return loadData();
});
loadData = function() {
var data, readline, rl;
readline = require('linebyline');
data = 'data.txt';
rl = readline(data);
rl.on('line', function(line) {
console.log('Line from file:', line);
return quotes.push(line);
});
return rl.on('close', function() {
query;
var i, len, quote;
for (i = 0, len = quotes.length; i < len; i++) {
quote = quotes[i];
query = client.query("INSERT INTO quotes(text) values($1)", [quote]);
console.log('insert', query.text);
}
query.on('end', function() {
console.log('query end');
return client.end();
});
return query.on('error', function(error) {
return console.log(error);
});
});
};