-
Notifications
You must be signed in to change notification settings - Fork 0
/
deprecated-db.js
80 lines (75 loc) · 2.16 KB
/
deprecated-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
const r = require('rethinkdb');
module.exports = class Model {
constructor(cb) {
this.conn = null;
const p = r.connect({db: 'wodmeup'});
p.then((conn) => {
this.conn = conn;
cb();
}).error(function(err) {
if (err) {
console.error(err);
}
cb(err);
});
}
getMovements(cb) {
let query = r.db('wodmeup').table('movements').orderBy('name').pluck(['id', 'name']);
query.run(this.conn, (err, cursor) => {
if (err) throw err;
cursor.toArray((err, result) => {
cb(result);
});
});
}
getWorkouts(cb) {
let query = r.db('wodmeup').table('workouts')
// Cache movements
.map(function(workout) {
return workout.merge(function(workout) {
return {'clusters': workout('clusters').map(function(cluster) {
return cluster.merge(function(cluster) {
return {'units': cluster('units').map(function(unit) {
return unit.merge({
movement: r.db('wodmeup').table('movements').get(unit('movementID'))
.merge(function(movement) {
return {
equipment:movement('equipment').map(function(eqID) {
return eqID;
})
};
})
, rx:{},
});
})};
},
function(cluster) {
return r.expr({timing: {type: 'NoTiming'}}).merge(cluster);
});
})};
});
})
// Cache equipments
.map(function(workout) {
return workout.merge({
'movements': workout('clusters').concatMap(function(cluster) {
return cluster('units').map(function(unit) {
return unit('movementID');
});
}),
'equipments': workout('clusters').concatMap(function(cluster) {
return cluster('units').concatMap(function(unit) {
return unit('movement')('equipment');
});
}).distinct()
});
})
.orderBy('name');
query.run(this.conn, (err, cursor) => {
if (err) throw err;
cursor.toArray((err, result) => {
cb(result);
});
});
}
};