Repository navigation
Expand file tree
/
Copy path6_self_joins.py
More file actions
73 lines (59 loc) · 1.66 KB
/
Copy path6_self_joins.py
File metadata and controls
73 lines (59 loc) · 1.66 KB
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
from chalk import online
from chalk.features import DataFrame, features, has_many, has_one
@features
class SeriesLink:
id: int
books: "DataFrame[Book]"
@features
class Author:
id: int
name: str
books: "DataFrame[Book]"
@features
class PrequelLink:
id: int
prequel_id: int
book: "Book" = has_one(lambda: Book.id == PrequelLink.prequel_id)
@features
class Book:
id: int
title: str
author_id: Author.id
prequel_id: PrequelLink.id | None
prequel: PrequelLink | None = has_one(lambda: Book.id == PrequelLink.prequel_id)
series_id: SeriesLink.id | None
series: SeriesLink = has_one(lambda: SeriesLink.id == Book.series_id)
@online
def get_books() -> DataFrame[Book]:
return DataFrame(
[
Book(
id=1,
title="To the Lighthouse",
author_id=1,
series_id=None,
prequel_id=None,
),
Book(
id=2,
title="The Fellowship of the Ring",
series_id=1,
author_id=2,
prequel_id=None,
),
Book(id=3, title="The Two Towers", series_id=1, author_id=2, prequel_id=2),
Book(
id=4,
title="The Return of the King",
series_id=1,
author_id=2,
prequel_id=3,
),
]
)
@online
def get_prequel_links() -> DataFrame[PrequelLink]:
return DataFrame([PrequelLink(id=3, prequel_id=2), PrequelLink(id=4, prequel_id=3)])
@online
def get_series_links() -> DataFrame[SeriesLink]:
return DataFrame([SeriesLink(id=1)])