-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
79 lines (69 loc) · 1.61 KB
/
database.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
var Sequelize = require('sequelize')
, async = require('async')
;
var logger = function(content) {
if (database.logStream){
database.logStream.write(content);
database.logStream.write('\n');
} else {
console.log(content);
}
};
var db = new Sequelize(null, null, null, {
dialect : 'sqlite'
, storage : './db.sqlite'
//, logging : logger
});
var News = db.define('News', {
title : Sequelize.TEXT,
link : Sequelize.TEXT,
guid : Sequelize.STRING,
pubDate : Sequelize.DATE
});
var Category = db.define('Category', {
title : Sequelize.STRING,
link : Sequelize.TEXT
});
var Site = db.define('Site', {
name : Sequelize.STRING,
lastUpdate : Sequelize.DATE
});
Site.hasMany(Category);
Category.belongsTo(Site);
Category.hasMany(News);
News.belongsTo(Category);
var database = {
sql : db,
News : News,
Category : Category,
Site : Site,
init : function(rssSites, doneInit) {
// init db data
async.eachSeries(rssSites, function(site, done) {
Site.findOrCreate({
name : site.name,
lastUpdate : new Date(site.last_update)
}).success(function(newSite, created) {
if (created == true) {
async.eachSeries(site.rss, function(rssCat, doneRSS) {
Category.findOrCreate({
title : rssCat.category,
link : rssCat.link
}).success(function(catgry) {
newSite.addCategory(catgry);
doneRSS();
});
}, function(err) {
if (err)
console.log(err);
// finally save the site
newSite.save();
});
}
// run each site in parallel
done();
});
}, doneInit);
}
};
module.exports = database;