summaryrefslogtreecommitdiff
path: root/lib/db.js
blob: 4c51c291cc31c7affc117fa6c74158c1f846bbc8 (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
//db
//external modules
var pg = require('pg');
var fs = require('fs');
var util = require('util');

//core
var config = require("../config.js");
var logger = require("./logger.js");

//public
var db = {
    readFromFile: readFromDB,
    saveToFile: saveToFile,
    newToDB: newToDB,
    readFromDB: readFromDB,
    saveToDB: saveToDB,
    countFromDB: countFromDB
};

function getDBClient() {
    return new pg.Client(process.env.DATABASE_URL || config.postgresqlstring);
}

function readFromFile(callback) {
    fs.readFile('hackmd', 'utf8', function (err, data) {
        if (err) throw err;
        callback(data);
    });
}

function saveToFile(doc) {
    fs.writeFile('hackmd', doc, function (err) {
        if (err) throw err;
    });
}

var updatequery = "UPDATE notes SET title='%s', content='%s', update_time=NOW() WHERE id='%s';";
var insertquery = "INSERT INTO notes (id, owner, content) VALUES ('%s', '%s', '%s');";
var insertifnotexistquery = "INSERT INTO notes (id, owner, content) \
SELECT '%s', '%s', '%s' \
WHERE NOT EXISTS (SELECT 1 FROM notes WHERE id='%s') RETURNING *;";
var selectquery = "SELECT * FROM notes WHERE id='%s';";
var countquery = "SELECT count(*) FROM notes;";

function newToDB(id, owner, body, callback) {
    var client = getDBClient();
    client.connect(function (err) {
        if (err) {
            client.end();
            callback(err, null);
            return logger.error('could not connect to postgres', err);
        }
        var newnotequery = util.format(insertquery, id, owner, body);
        //logger.info(newnotequery);
        client.query(newnotequery, function (err, result) {
            client.end();
            if (err) {
                callback(err, null);
                return logger.error("new note to db failed: " + err);
            } else {
                if (config.debug)
                    logger.info("new note to db success");
                callback(null, result);
            }
        });
    });
}

function readFromDB(id, callback) {
    var client = getDBClient();
    client.connect(function (err) {
        if (err) {
            client.end();
            callback(err, null);
            return logger.error('could not connect to postgres', err);
        }
        var readquery = util.format(selectquery, id);
        //logger.info(readquery);
        client.query(readquery, function (err, result) {
            client.end();
            if (err) {
                callback(err, null);
                return logger.error("read from db failed: " + err);
            } else {
                //logger.info(result.rows);
                if (result.rows.length <= 0) {
                    callback("not found note in db: " + util.inspect(id), null);
                } else {
                    if(config.debug)
                        logger.info("read from db success");
                    callback(null, result);
                }
            }
        });
    });
}

function saveToDB(id, title, data, callback) {
    var client = getDBClient();
    client.connect(function (err) {
        if (err) {
            client.end();
            callback(err, null);
            return logger.error('could not connect to postgres', err);
        }
        var savequery = util.format(updatequery, title, data, id);
        //logger.info(savequery);
        client.query(savequery, function (err, result) {
            client.end();
            if (err) {
                callback(err, null);
                return logger.error("save to db failed: " + err);
            } else {
                if (config.debug)
                    logger.info("save to db success");
                callback(null, result);
            }
        });
    });
}

function countFromDB(callback) {
    var client = getDBClient();
    client.connect(function (err) {
        if (err) {
            client.end();
            callback(err, null);
            return logger.error('could not connect to postgres', err);
        }
        client.query(countquery, function (err, result) {
            client.end();
            if (err) {
                callback(err, null);
                return logger.error("count from db failed: " + err);
            } else {
                //logger.info(result.rows);
                if (result.rows.length <= 0) {
                    callback("not found note in db", null);
                } else {
                    if(config.debug)
                        logger.info("count from db success");
                    callback(null, result);
                }
            }
        });
    });
}

module.exports = db;