-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.js
125 lines (108 loc) · 2.73 KB
/
storage.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
var mongodb = require("mongodb"),
config = require("./config.js");
var mongo = new mongodb.Server(
config.mongo.host,
config.mongo.port);
var db = new mongodb.Db(
config.mongo.database,
mongo);
db.open(function(error) {
if (error) throw error;
});
/*
Streams storage
stream = {
stream_id: "2323",
playlist: [],
playdata: {} (opt)
}
*/
exports.getStream = function(stream_id, result) {
db.collection("streams", function(err, collection) {
collection.find({ 'stream_id': stream_id }, function(err, cursor) {
cursor.toArray(function(err, items) {
if (items.length > 0) {
result(items[0]);
} else {
result();
}
});
});
});
}
exports.saveStream = function(stream_id, stream) {
db.collection('streams', function(err, collection) {
collection.update(
{ stream_id: stream_id },
stream,
{ 'upsert': true });
});
}
/*
Users storage
user = {
user_id: "323"
subscribes: [],
owns: []
}
*/
exports.getUser = function(user_id, result) {
db.collection("users", function(err, collection) {
collection.find({ 'user_id': user_id }, function(err, cursor) {
cursor.toArray(function(err, items) {
if (items.length > 0) {
result(items[0]);
} else {
result();
}
});
});
});
}
exports.saveUser = function(user_id, user) {
db.collection('users', function(err, collection) {
collection.update(
{ user_id: user_id },
user,
{ 'upsert': true });
});
}
/*
Station storage
station = {
station_id: "323",
stream: {
stream_id: "3434",
},
social: {
owners: [],
subscribers: []
},
content: {},
metadata: {
tags: [],
statistics: {}
}
}
*/
exports.getStation = function(station_id, result) {
db.collection("stations", function(err, collection) {
collection.find({ 'station_id': station_id }, function(err, cursor) {
cursor.toArray(function(err, items) {
if (items.length > 0) {
result(items[0]);
} else {
result();
}
});
});
});
}
exports.saveStation = function(station_id, station) {
db.collection('stations', function(err, collection) {
collection.update(
{ station_id: station_id },
station,
{ 'upsert': true });
});
}