-
Notifications
You must be signed in to change notification settings - Fork 1
/
db_helpers.rb
111 lines (106 loc) · 4.74 KB
/
db_helpers.rb
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
module DBHelpers
def self.create!
# track document
#{
# "itemid" => "2hn7f",
# "artist" => "Popeska",
# "title" => "I Believe",
# "dateposted" => 1470252496,
# "siteid" => 3488,
# "sitename" => "IHEARTCOMIX",
# "posturl" => "http://iheartcomix.com/tracks-of-the-week-3/",
# "postid" => 2985093,
# "loved_count" => 2381,
# "posted_count" => 5,
# "thumb_url" => "http://static.hypem.net/thumbs_new/85/2985093.jpg",
# "thumb_url_medium" => "http://static.hypem.net/thumbs_new/85/2985093_120.jpg",
# "thumb_url_large" => "http://static.hypem.net/thumbs_new/e2/2983138_500.jpg",
# "time" => 314,
# "description" => "It’s been pretty difficult keeping up with ",
# "itunes_link" => "http://hypem.com/go/itunes_search/Popeska",
# "spotify_result" => "6TJmQnO44YE5BtTxH8pop1",
# "no_spotify_result" => 1471194907,
# "loved_by" => ["longscott", "otheruser"]
#}
# user document
#{
# "name" => "longscott",
# "loved_songs" => [
# { itemid: "2hn7f",
# ts_loved: 1470075636
# ]
#}
require 'mongo'
client = Mongo::Client.new('mongodb://127.0.0.1:27017/hype')
client[:tracks].drop
client[:users].drop
client[:users,
{
'validator' => {
'$and' =>
[
{ 'name' => { '$type': "string" } },
# mongodb has fucked up $type: array behavior so I'm just not going
# to validate this field for now
# { 'loved_songs' => { '$type': "array" } }
]
}
}
].create
client[:tracks,
{
'validator' => {
'$and' =>
[
{ 'itemid' => { '$type': "string" } } ,
{ 'artist' => { '$type': "string" } } ,
{ 'title' => { '$type': "string" } } ,
{ 'title' => { '$type': "string" } } ,
{ 'dateposted' => { '$type': "int" } } ,
{ 'siteid' => { '$type': "int" } } ,
{ 'sitename' => { '$type': "string" } } ,
{ 'posturl' => { '$type': "string" } } ,
{ 'postid' => { '$type': "int" } } ,
{ 'loved_count' => { '$type': "int" } } ,
{ 'posted_count' => { '$type': "int" } } ,
{ 'thumb_url' => { '$regex': /^http:/ } } ,
{ 'time' => { '$type': "int" } } ,
{ 'description' => { '$type': "string" } } ,
{ 'itunes_link' => { '$regex': /^http:/ } } ,
{ 'is_remix' => { '$type': "bool" } } ,
# mongodb has fucked up $type: array behavior so I'm just not going
# to validate this field for now
#{ 'loved_by' => { '$type': "array" } }
],
'$or' =>
[
# https://jaihirsch.github.io/straw-in-a-haystack/mongodb/2015/12/04/mongodb-document-validation/
# so if you have a pair of validators within an "or" construct
# with one specifying type or regex or some query operator
# and the other specifying "exists => false", then this semantically
# means its optional, but if it exists it must match the query
# operator.
#
# The problem with this is that when you want to make more than one
# field optional (but valid), then you really can't. Consider below,
# only one of these has to be true, so if thumb_url_medium is missing,
# then that satisfies this condition, so thumb_url_large can contain
# anything at all.
#
# I'm leaving this in here because it doesn't hurt anything and maybe
# some day I'll come back and fix it.
{ 'thumb_url_medium' => { '$regex': /^http:/ } } ,
{ 'thumb_url_medium' => { 'exists': false } } ,
{ 'thumb_url_large' => { '$regex': /^http:/ } } ,
{ 'thumb_url_large' => { 'exists': false } } ,
{ 'no_spotify_results' => { '$type': "int" } } ,
{ 'no_spotify_results' => { 'exists': false } } ,
{ 'spotify_result' => { '$type': "string" } } ,
{ 'spotify_result' => { 'exists': false } } ,
]
}
}
].create
client[:tracks].indexes.create_one( { :itemid => 1 }, unique: true )
end
end