-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
120 lines (111 loc) · 2.22 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
const { Sequelize, DataTypes, Op } = require('sequelize');
const sequelize = new Sequelize(process.env.DATABASE_URL);
const Gym = sequelize.define('Gym', {
name: {
type: DataTypes.STRING,
allowNull: false,
},
}, {
indixes: [{
unique: true,
fields: ['name'],
}]
});
const Source = sequelize.define('Source', {
type: {
type: DataTypes.STRING,
defaultValue: 'ROCK_GYM_PRO',
},
data: {
type: DataTypes.JSONB,
allowNull: false,
}
});
Gym.Source = Gym.hasOne(Source);
Source.Gym = Source.belongsTo(Gym);
const Tick = sequelize.define('Tick', {
time: {
type: DataTypes.DATE,
allowNull: false,
},
count: {
type: DataTypes.INTEGER,
allowNull: false,
},
}, {
timestamps: false,
});
Gym.Ticks = Gym.hasMany(Tick);
Tick.Gym = Tick.belongsTo(Gym);
function getSources() {
return Source.findAll({
where: {
type: 'ROCK_GYM_PRO',
}
});
}
async function addTick(source, occupancy) {
const gym = await source.getGym();
return gym.createTick({
time: Date.now(),
count: occupancy,
});
}
async function getTicks() {
return Gym.findAll({
include: {
model: Tick,
where: {
time: {
[Op.gt]: new Date(new Date() - 7 * 24 * 60 * 60 * 1000)
}
}
}
});
}
module.exports = {
init(force) {
return sequelize.sync({ force });
},
async seed() {
await Gym.create({
name: "Fremont",
Source: {
type: 'ROCK_GYM_PRO',
data: {
short: 'FRE',
uuid: '314b60a77a6eada788f8cd7046931fc5',
},
},
}, {
include: [Gym.Source],
});
await Gym.create({
name: "Poplar",
Source: {
type: 'ROCK_GYM_PRO',
data: {
short: 'POP',
uuid: '314b60a77a6eada788f8cd7046931fc5',
},
},
}, {
include: [Gym.Source],
});
await Gym.create({
name: "Upper Walls",
Source: {
type: 'ROCK_GYM_PRO',
data: {
short: 'UPW',
uuid: '314b60a77a6eada788f8cd7046931fc5',
},
},
}, {
include: [Gym.Source],
});
},
getSources,
addTick,
getTicks,
};