HackMD/lib/db.js

151 lines
4.6 KiB
JavaScript
Raw Normal View History

2015-05-04 07:53:29 +00:00
//db
//external modules
var pg = require('pg');
var fs = require('fs');
var util = require('util');
//core
var config = require("../config.js");
2015-07-01 16:10:20 +00:00
var logger = require("./logger.js");
2015-05-04 07:53:29 +00:00
//public
var db = {
readFromFile: readFromDB,
saveToFile: saveToFile,
newToDB: newToDB,
readFromDB: readFromDB,
saveToDB: saveToDB,
countFromDB: countFromDB
};
function getDBClient() {
2015-05-15 04:58:13 +00:00
return new pg.Client(process.env.DATABASE_URL || config.postgresqlstring);
2015-05-04 07:53:29 +00:00
}
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) {
2015-05-15 04:58:13 +00:00
client.end();
2015-05-04 07:53:29 +00:00
callback(err, null);
2015-07-01 16:10:20 +00:00
return logger.error('could not connect to postgres', err);
2015-05-04 07:53:29 +00:00
}
var newnotequery = util.format(insertquery, id, owner, body);
2015-07-01 16:10:20 +00:00
//logger.info(newnotequery);
2015-05-04 07:53:29 +00:00
client.query(newnotequery, function (err, result) {
2015-05-15 04:58:13 +00:00
client.end();
2015-05-04 07:53:29 +00:00
if (err) {
callback(err, null);
2015-07-01 16:10:20 +00:00
return logger.error("new note to db failed: " + err);
2015-05-04 07:53:29 +00:00
} else {
if (config.debug)
2015-07-01 16:10:20 +00:00
logger.info("new note to db success");
2015-05-04 07:53:29 +00:00
callback(null, result);
}
});
});
}
function readFromDB(id, callback) {
var client = getDBClient();
client.connect(function (err) {
if (err) {
2015-05-15 04:58:13 +00:00
client.end();
2015-05-04 07:53:29 +00:00
callback(err, null);
2015-07-01 16:10:20 +00:00
return logger.error('could not connect to postgres', err);
2015-05-04 07:53:29 +00:00
}
var readquery = util.format(selectquery, id);
2015-07-01 16:10:20 +00:00
//logger.info(readquery);
2015-05-04 07:53:29 +00:00
client.query(readquery, function (err, result) {
2015-05-15 04:58:13 +00:00
client.end();
2015-05-04 07:53:29 +00:00
if (err) {
callback(err, null);
2015-07-01 16:10:20 +00:00
return logger.error("read from db failed: " + err);
2015-05-04 07:53:29 +00:00
} else {
2015-07-01 16:10:20 +00:00
//logger.info(result.rows);
2015-05-04 07:53:29 +00:00
if (result.rows.length <= 0) {
callback("not found note in db", null);
return logger.error("not found note in db: " + id, err);
2015-05-04 07:53:29 +00:00
} else {
2015-05-15 04:58:13 +00:00
if(config.debug)
2015-07-01 16:10:20 +00:00
logger.info("read from db success");
2015-05-04 07:53:29 +00:00
callback(null, result);
}
}
});
});
}
function saveToDB(id, title, data, callback) {
var client = getDBClient();
client.connect(function (err) {
if (err) {
2015-05-15 04:58:13 +00:00
client.end();
2015-05-04 07:53:29 +00:00
callback(err, null);
2015-07-01 16:10:20 +00:00
return logger.error('could not connect to postgres', err);
2015-05-04 07:53:29 +00:00
}
var savequery = util.format(updatequery, title, data, id);
2015-07-01 16:10:20 +00:00
//logger.info(savequery);
2015-05-04 07:53:29 +00:00
client.query(savequery, function (err, result) {
2015-05-15 04:58:13 +00:00
client.end();
2015-05-04 07:53:29 +00:00
if (err) {
callback(err, null);
2015-07-01 16:10:20 +00:00
return logger.error("save to db failed: " + err);
2015-05-04 07:53:29 +00:00
} else {
if (config.debug)
2015-07-01 16:10:20 +00:00
logger.info("save to db success");
2015-05-04 07:53:29 +00:00
callback(null, result);
}
});
});
}
function countFromDB(callback) {
var client = getDBClient();
client.connect(function (err) {
if (err) {
2015-05-15 04:58:13 +00:00
client.end();
2015-05-04 07:53:29 +00:00
callback(err, null);
2015-07-01 16:10:20 +00:00
return logger.error('could not connect to postgres', err);
2015-05-04 07:53:29 +00:00
}
client.query(countquery, function (err, result) {
2015-05-15 04:58:13 +00:00
client.end();
2015-05-04 07:53:29 +00:00
if (err) {
callback(err, null);
2015-07-01 16:10:20 +00:00
return logger.error("count from db failed: " + err);
2015-05-04 07:53:29 +00:00
} else {
2015-07-01 16:10:20 +00:00
//logger.info(result.rows);
2015-05-04 07:53:29 +00:00
if (result.rows.length <= 0) {
callback("not found note in db", null);
} else {
2015-05-15 04:58:13 +00:00
if(config.debug)
2015-07-01 16:10:20 +00:00
logger.info("count from db success");
2015-05-04 07:53:29 +00:00
callback(null, result);
}
}
});
});
}
module.exports = db;