-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeople_db.roga
116 lines (92 loc) · 2.16 KB
/
people_db.roga
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
module People { @People, Person, Location, oldestPeopleNear }
type @People :: [Person]
type Person :: {
name :: String
age :: Uint
location :: Location
}
type Location :: {
lat :: Lattitude
long :: Longitude
}
type @Locations :: [Location]
type City :: {
name :: String
Country :: Country
location :: Location
}
type @Cities :: [City]
// query from graph db
let oldestPeopleNear loc =
? p <- Person
! (Location.isNear p.location loc)
!> (orderBy (ascending .age))
// same as:
// !> (orderBy (.age |> ascending))
// // .age is syntax sugar for: let .age p = p.age
// // same as:
// !> (orderBy ((p -> p.age) |> ascending))
// // same as:
// !> (orderBy ((p1 p2 -> p1.age <=> p2.age) |> ascending))
let oldestPeopleNear loc personLimit =
? p <- (oldestPeopleNear loc)
! (limit personLimit)
!> p
// add some locations
let Germany = Country{name: "Germany"}
let Osnabrueck = City{
name: "Osnabrück",
country: Germany,
location: Location{lat: 150, long: 150}
}
let Bremen = City{
name: "Bremen",
country: Germany,
location: Location{lat: 150, long: 250}
}
let addLocations =
[
@Cities
|> new [Bremen, Osnabrueck],
// alternative:
@Locations
|> new [
Bremen.location#{city: Bremen},
Osnabrueck.location#{city: Osnabrueck}
]
]
let locationsNearOsna =
locationsWithin Osnabrueck.location (10 |> km)
let locationsWithin loc maxDistance =
? l <- @Locations
! ((Location.distance l loc) <= maxDistance)
!> l
// same as:
let locationsWithin loc maxDistance =
(from @Locations)
|> where (l -> (Location.distance l loc) <= maxDistance)
let locationsInGermany =
? l <- @Locations
! l#{country: Germany}
!> l
let hasPlaceForUpTo maxNum query =
? l <- query
! l.maxCapacity >= maxNum
!> l
let funPlaces city =
? l <- @Locations
! l#{isFun | relaxing | (hasPlaceForUpTo 20)}
!> l
let funLocationsInBerlinOrHamburg =
? l <- @Locations
! l#{city: (Berlin | Hamburg)}
! l <- funPlaces (Berlin | Hamburg)
!> l
// MAIN variations
let main ({ db }) =
db.exec addLocations
let main ({ db }) =
addLocations
|> List.map db.exec
let main ({ db }) =
db.exec <- addLocations