-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_pg_insert.js
More file actions
83 lines (70 loc) · 2.1 KB
/
06_pg_insert.js
File metadata and controls
83 lines (70 loc) · 2.1 KB
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
// Requires
var pg = require("pg"),
uuid = require("node-uuid").v4;
// Local variables
var database = "node_sample_dev",
host = "localhost",
description = process.argv.splice(2).join(" "),
id;
console.log(" [x] Creating new task: " + description);
function completeTask(client, id) {
var query = "update tasks set is_completed = $1 where id = $2";
var values = [true, id];
client.query(query, values, function (error, result) {
if (error) {
throw error;
}
console.log(" [x] Completed task " + id);
});
}
function getConnectionString() {
return "postgres://" + host + "/" + database;
}
function insertTask(client, params) {
var query = "";
query += "insert into tasks (";
query += " id, description, assigned_to, is_completed";
query += ") values ($1, $2, $3, $4)";
id = uuid();
var values = [id, params.description, params.assignedTo, false];
client.query(query, values, function (error, result) {
if (error) {
throw error;
}
console.log(" [x] Insert successful: " + values[0]);
});
}
function selectAllTasks(client) {
var query = "select * from tasks";
client.query(query, function (error, result) {
if (error) {
throw error;
}
console.log(" [x] Select all tasks result: " + result.rowCount);
for (var i = 0, len = result.rowCount; i < len; i += 1) {
console.log(" Row: " + i);
console.log(" ID: " + result.rows[i].id);
console.log(" Description: " + result.rows[i].description);
console.log(" Assigned To: " + result.rows[i].assigned_to);
console.log(" Is Completed: " + result.rows[i].is_completed);
}
});
}
pg.connect(getConnectionString(), function (error, client, done) {
console.log(" [x] Connected to database.");
if (error) {
throw error;
}
setTimeout(function () {
insertTask(client, { description: description, assignedTo: "No one" });
}, 250);
setTimeout(function () {
selectAllTasks(client);
}, 500);
setTimeout(function () {
completeTask(client, id);
}, 700);
setTimeout(function () {
client.end();
}, 1000);
});