-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
163 lines (145 loc) · 3.63 KB
/
db.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var Sequelize = require('sequelize');
var config = require('./config');
var url = 'mysql://' + config.db_user + ':' + config.db_pass + '@localhost:3306/' + config.db_name;
const sequelize = new Sequelize(url, {logging: false});
const Tag = sequelize.define('tag', {
name: Sequelize.STRING,
createdAt: {
type: Sequelize.DATE,
defaultValue: sequelize.literal('NOW()')
},
updatedAt: {
type: Sequelize.DATE,
defaultValue: sequelize.literal('NOW()')
},
});
const Page = sequelize.define('page', {
title: Sequelize.STRING,
fbid: {
unique:true,
type: Sequelize.STRING
},
createdAt: {
type: Sequelize.DATE,
defaultValue: sequelize.literal('NOW()')
},
updatedAt: {
type: Sequelize.DATE,
defaultValue: sequelize.literal('NOW()')
},
tag_id: {
type: Sequelize.INTEGER,
defaultValue: 0
}
}, {
charset: 'utf8mb4',
collate: 'utf8mb4_unicode_ci'
});
const Events = sequelize.define('event', {
name: Sequelize.STRING,
place_name: Sequelize.STRING,
fbid: {
unique:true,
type: Sequelize.BIGINT.UNSIGNED
},
picture_url: Sequelize.STRING,
start_time: Sequelize.DATE,
end_time: Sequelize.DATE,
description: Sequelize.TEXT
}, {
charset: 'utf8mb4',
collate: 'utf8mb4_unicode_ci'
});
Page.belongsTo(Tag, {foreignKey: 'tag_id'});
Events.belongsTo(Page, {foreignKey: 'page_id'});
module.exports = {
sync : function (){
Tag.sync();
Events.sync();
Page.sync();
},
tag : {
all : function (callback) {
Tag.all().then(callback);
},
one : function (id, callback) {
Tag.findAll({
where: {
id: id
}
}).then(function (data){
if(data.length == 0) throw "oh no";
callback(data[0]);
});
}
},
page : {
all : function (callback) {
Page.all({
order:[['tag_id']],
include:[Tag]
}).then(callback);
}
},
event : {
clean : function (callback) {
Events.destroy({
where: {start_time: {
// delete all more than 3 days old
$lt: new Date(new Date() - 3 * 24 * 60 * 60 * 1000)
}}
}).then(callback);
},
all : function (callback) {
Events.all({
include: {model:Page, include: [Tag]},
order:[['start_time']],
where: {start_time: {
$gt: new Date(new Date() - 8 * 60 * 60 * 1000)
}}
}).then(callback);
},
tag : function (tag_id, callback) {
Events.all({
include: {model:Page, where: {tag_id: {$eq:tag_id}}, include: [Tag]},
order:[['start_time']],
where: {
start_time: {
$gt: new Date(new Date() - 24 * 60 * 60 * 1000)
}
}
}).then(callback);
},
upsert : function (e, callback) {
// findorcreate so we only make it once, dont upsert incase we manually change rows after scrape
Events.findOrCreate({
where: {
fbid: e.id
},
defaults:{
page_id: e.page_id,
name: e.name,
description: e.description,
fbid: e.id,
picture_url: e.cover ? e.cover.source : '',
start_time: e.start_time,
end_time: e.end_time,
place_name: e.place ? e.place.name : ''
}
}).then(function () {
callback();
});
}
}
};
/*
# super secret query to find pages with no new events
SELECT pages.title, tags.name AS "tag", pages.id, COUNT(events.id) AS "event_count" from pages
LEFT JOIN events on events.page_id = pages.id
AND (
events.createdAt > DATE_ADD(NOW(), interval -30 day)
)
LEFT JOIN tags ON pages.tag_id = tags.id
GROUP BY pages.id
ORDER BY event_count ASC;
*/