summaryrefslogtreecommitdiff
path: root/lib/temp.js
diff options
context:
space:
mode:
authorWu Cheng-Han2015-05-15 12:58:13 +0800
committerWu Cheng-Han2015-05-15 12:58:13 +0800
commit4e64583a0b6175d2c9a6729ffde1472dd55d389c (patch)
tree75253f2425f2e4f5906ed4fd30eca29a906ee47a /lib/temp.js
parent2d36d7ce84c636faac17cef3d3a7c22568df38fe (diff)
Marked as 0.2.8
Diffstat (limited to 'lib/temp.js')
-rw-r--r--lib/temp.js83
1 files changed, 83 insertions, 0 deletions
diff --git a/lib/temp.js b/lib/temp.js
new file mode 100644
index 00000000..90d9343f
--- /dev/null
+++ b/lib/temp.js
@@ -0,0 +1,83 @@
+//temp
+//external modules
+var mongoose = require('mongoose');
+
+//core
+var config = require("../config.js");
+
+// 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) {
+ console.log('find temp failed: ' + err);
+ callback(err, null);
+ }
+ if (!err && temp) {
+ callback(null, temp);
+ } else {
+ console.log('find temp failed: ' + err);
+ 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) {
+ console.log('new temp failed: ' + err);
+ callback(err, null);
+ } else {
+ console.log("new temp success: " + temp.id);
+ callback(null, temp);
+ };
+ });
+}
+
+function removeTemp(id, callback) {
+ findTemp(id, function(err, temp) {
+ if(!err && temp) {
+ temp.remove(function(err) {
+ if(err) {
+ console.log('remove temp failed: ' + err);
+ callback(err, null);
+ } else {
+ callback(null, null);
+ }
+ });
+ } else {
+ console.log('remove temp failed: ' + err);
+ callback(err, null);
+ }
+ });
+}
+
+module.exports = temp; \ No newline at end of file