/
articleprovider-couchdb.js
51 lines (44 loc) · 1.33 KB
/
articleprovider-couchdb.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
var cradle = require('cradle');
ArticleProvider = function(host, port) {
this.connection= new (cradle.Connection)(host, port, {
cache: true,
raw: false
});
this.db = this.connection.database('articles');
};
ArticleProvider.prototype.findAll = function(callback) {
this.db.view('articles/all',function(error, result) {
if( error ){
callback(error)
}else{
var docs = [];
result.forEach(function (row){
docs.push(row);
});
callback(null, docs);
}
});
};
ArticleProvider.prototype.findById = function(id, callback) {
this.db.get(id, function(error, result) {
if( error ) callback(error)
else callback(null, result)
});
};
ArticleProvider.prototype.save = function(articles, callback) {
if( typeof(articles.length)=="undefined")
articles = [articles];
for( var i =0;i< articles.length;i++ ) {
article = articles[i];
article.created_at = new Date();
if( article.comments === undefined ) article.comments = [];
for(var j =0;j< article.comments.length; j++) {
article.comments[j].created_at = new Date();
}
}
this.db.save(articles, function(error, result) {
if( error ) callback(error)
else callback(null, articles);
});
};
exports.ArticleProvider = ArticleProvider;