codimd/lib/temp.js

84 lines
1.9 KiB
JavaScript
Raw Normal View History

2015-05-15 04:58:13 +00:00
//temp
//external modules
var mongoose = require('mongoose');
//core
var config = require("../config.js");
2015-07-01 16:10:20 +00:00
var logger = require("./logger.js");
2015-05-15 04:58:13 +00:00
// create a temp model
var model = mongoose.model('temp', {
id: String,
data: String,
created: Date
});
//public
var temp = {
model: model,
findTemp: findTemp,
newTemp: newTemp,
removeTemp: removeTemp,
getTempCount: getTempCount
};
function getTempCount(callback) {
model.count(function(err, count){
if(err) callback(err, null);
else callback(null, count);
});
}
function findTemp(id, callback) {
model.findOne({
id: id
}, function (err, temp) {
if (err) {
2015-07-01 16:10:20 +00:00
logger.error('find temp failed: ' + err);
2015-05-15 04:58:13 +00:00
callback(err, null);
}
if (!err && temp) {
callback(null, temp);
} else {
2015-07-01 16:10:20 +00:00
logger.error('find temp failed: ' + err);
2015-05-15 04:58:13 +00:00
callback(err, null);
};
});
}
function newTemp(id, data, callback) {
var temp = new model({
id: id,
data: data,
created: Date.now()
});
temp.save(function (err) {
if (err) {
2015-07-01 16:10:20 +00:00
logger.error('new temp failed: ' + err);
2015-05-15 04:58:13 +00:00
callback(err, null);
} else {
2015-07-01 16:10:20 +00:00
logger.info("new temp success: " + temp.id);
2015-05-15 04:58:13 +00:00
callback(null, temp);
};
});
}
function removeTemp(id, callback) {
findTemp(id, function(err, temp) {
if(!err && temp) {
temp.remove(function(err) {
if(err) {
2015-07-01 16:10:20 +00:00
logger.error('remove temp failed: ' + err);
2015-05-15 04:58:13 +00:00
callback(err, null);
} else {
callback(null, null);
}
});
} else {
2015-07-01 16:10:20 +00:00
logger.error('remove temp failed: ' + err);
2015-05-15 04:58:13 +00:00
callback(err, null);
}
});
}
module.exports = temp;