-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_mongo.js
139 lines (122 loc) · 3.6 KB
/
data_mongo.js
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
/**
* Created by JetBrains WebStorm.
* User: luigi.byun(@zziuni)
* Date: 12. 3. 28.
* Time: 오후 9:32
*/
var mongo= require('mongolian' )
, mongo_user = process.env['MONGODB_USER']
, mongo_pwd = process.env['MONGODB_PWD']
, mongo_port = process.env['MONGODB_PORT']
, db = new mongo('mongo://' + mongo_user + ':' + mongo_pwd + '@localhost:' + mongo_port + '/smtp_db' )
, objectId = mongo.ObjectId;
//document define
/*
{
"id" : '',
"title" : '',
"mdContent" : '',
"htmlContent" : '',
"createdDate" : '',
"modifiedDate" : '',
"pwd" : '',
"showLog" : [
{
"shownDate" : '',
"googCount" : '',
"badCount" : ''
}
]
}
*/
var slideshows = db.collection("slideshows");
exports.insert = function( input, callback ){
if(!input){return;}
var slideshow = {
"title" : input.title,
"mdContents" : input.mdContents,
"htmlContents" : '',
"createdDate" : new Date(),
"modifiedDate" : '',
"pwd" : ''
};
slideshows.insert( slideshow, function(err, value){ console.log(err)} );
callback();
};
exports.selectAll = function( callback ){
var result = [];
slideshows.find({},{"_id":1,"title": 1,"createdDate" : 1, "modifiedDate" : 1} )
.sort({createdDate: -1})
.toArray(function(err, data){
var i, cdate, mdate;
if(data){
for(i=data.length-1; i>=0; i-=1){
cdate = data[i].createdDate;
mdate = data[i].modifiedDate;
data[i].createdDate = cdate.getFullYear() + '-' + cdate.getMonth() + '-' + cdate.getDate() + ' ' + cdate.toLocaleTimeString();
if(mdate){
data[i].modifiedDate = mdate.getFullYear() + '-' + mdate.getMonth() + '-' + mdate.getDate() + ' ' + mdate.toLocaleTimeString();
}
}
}
callback(data);
}
);
};
exports.select = function( slideId, callback ){
var sid = new objectId(slideId) ;
slideshows.findOne( {"_id": sid }, function(err, data){
if(err){ console.log( err ); }
callback(data)
});
};
exports.remove = function( slideId, callback ){
var sid = new objectId(slideId) ;
slideshows.remove( {"_id": sid }, function(err, data){
if(err){ console.log(err);}
callback(data);
}
);
};
exports.editSlide = function( slideId, callback){
var sid = new objectId(slideId) ;
slideshows.findOne( {"_id": sid }, function(err, data){
if(err){ console.log( err ); }
callback(data)
});
};
exports.update = function( source, callback ){
var sid = new objectId(source.id) ;
slideshows.findOne( {"_id": sid }, function(err, target){
console.log(target);
target.title = source.title;
target.mdContents = source.mdContents;
target.modifiedDate = new Date();
slideshows.update({"_id": sid }, target, function(err, result){
callback(result);
});
});
};
/*
// Insert some data
posts.insert({
pageId: "hallo",
title: "Hallo",
created: new Date,
body: "Welcome to my new blog!"
})
// Get a single document
posts.findOne({ pageId: "hallo" }, function(err, post) {
//...
})
// Document cursors
posts.find().limit(5).sort({ created: 1 }).toArray(function (err, array) {
// do something with the array
})
posts.find({ title: /^hal/ }).forEach(function (post) {
// do something with a single post
}, function(err) {
// handle errors/completion
})
//exports.createConnect = createConnect;
*/