sqliter-py 0.12.0__py3-none-any.whl → 0.17.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sqliter/constants.py +4 -3
- sqliter/exceptions.py +29 -0
- sqliter/helpers.py +27 -0
- sqliter/model/model.py +21 -4
- sqliter/orm/__init__.py +17 -0
- sqliter/orm/fields.py +412 -0
- sqliter/orm/foreign_key.py +8 -0
- sqliter/orm/m2m.py +784 -0
- sqliter/orm/model.py +308 -0
- sqliter/orm/query.py +221 -0
- sqliter/orm/registry.py +440 -0
- sqliter/query/query.py +573 -51
- sqliter/sqliter.py +182 -47
- sqliter/tui/__init__.py +62 -0
- sqliter/tui/__main__.py +6 -0
- sqliter/tui/app.py +179 -0
- sqliter/tui/demos/__init__.py +96 -0
- sqliter/tui/demos/base.py +114 -0
- sqliter/tui/demos/caching.py +283 -0
- sqliter/tui/demos/connection.py +150 -0
- sqliter/tui/demos/constraints.py +211 -0
- sqliter/tui/demos/crud.py +154 -0
- sqliter/tui/demos/errors.py +231 -0
- sqliter/tui/demos/field_selection.py +150 -0
- sqliter/tui/demos/filters.py +389 -0
- sqliter/tui/demos/models.py +248 -0
- sqliter/tui/demos/ordering.py +156 -0
- sqliter/tui/demos/orm.py +537 -0
- sqliter/tui/demos/results.py +241 -0
- sqliter/tui/demos/string_filters.py +210 -0
- sqliter/tui/demos/timestamps.py +126 -0
- sqliter/tui/demos/transactions.py +177 -0
- sqliter/tui/runner.py +116 -0
- sqliter/tui/styles/app.tcss +130 -0
- sqliter/tui/widgets/__init__.py +7 -0
- sqliter/tui/widgets/code_display.py +81 -0
- sqliter/tui/widgets/demo_list.py +65 -0
- sqliter/tui/widgets/output_display.py +92 -0
- {sqliter_py-0.12.0.dist-info → sqliter_py-0.17.0.dist-info}/METADATA +28 -14
- sqliter_py-0.17.0.dist-info/RECORD +48 -0
- {sqliter_py-0.12.0.dist-info → sqliter_py-0.17.0.dist-info}/WHEEL +2 -2
- sqliter_py-0.17.0.dist-info/entry_points.txt +3 -0
- sqliter_py-0.12.0.dist-info/RECORD +0 -15
sqliter/tui/demos/orm.py
ADDED
|
@@ -0,0 +1,537 @@
|
|
|
1
|
+
"""ORM Features demos."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import io
|
|
6
|
+
from typing import Any, Optional, cast
|
|
7
|
+
|
|
8
|
+
from sqliter import SqliterDB
|
|
9
|
+
from sqliter.orm import BaseDBModel, ForeignKey, ManyToMany
|
|
10
|
+
from sqliter.tui.demos.base import Demo, DemoCategory, extract_demo_code
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _run_lazy_loading() -> str:
|
|
14
|
+
"""Load related objects on-demand using foreign keys.
|
|
15
|
+
|
|
16
|
+
Accessing a ForeignKey field triggers a database query to fetch the
|
|
17
|
+
related object only when you need it.
|
|
18
|
+
"""
|
|
19
|
+
output = io.StringIO()
|
|
20
|
+
|
|
21
|
+
class Author(BaseDBModel):
|
|
22
|
+
name: str
|
|
23
|
+
|
|
24
|
+
class Book(BaseDBModel):
|
|
25
|
+
title: str
|
|
26
|
+
author: ForeignKey[Author] = ForeignKey(Author)
|
|
27
|
+
|
|
28
|
+
db = SqliterDB(memory=True)
|
|
29
|
+
db.create_table(Author)
|
|
30
|
+
db.create_table(Book)
|
|
31
|
+
|
|
32
|
+
author = db.insert(Author(name="J.K. Rowling"))
|
|
33
|
+
book1 = db.insert(Book(title="Harry Potter 1", author=author))
|
|
34
|
+
book2 = db.insert(Book(title="Harry Potter 2", author=author))
|
|
35
|
+
|
|
36
|
+
output.write(f"Author: {author.name}\n")
|
|
37
|
+
output.write(f"Author ID: {author.pk}\n")
|
|
38
|
+
|
|
39
|
+
# Access related author through foreign key - triggers lazy load
|
|
40
|
+
output.write("\nAccessing book.author triggers lazy load:\n")
|
|
41
|
+
output.write(f" '{book1.title}' was written by {book1.author.name}\n")
|
|
42
|
+
|
|
43
|
+
output.write(f"\n'{book2.title}' was written by {book2.author.name}\n")
|
|
44
|
+
output.write("Related objects loaded on-demand from database\n")
|
|
45
|
+
|
|
46
|
+
db.close()
|
|
47
|
+
return output.getvalue()
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _run_orm_style_access() -> str:
|
|
51
|
+
"""Insert records with foreign key relationships.
|
|
52
|
+
|
|
53
|
+
BaseDBModel provides attribute-style access to fields, with automatic
|
|
54
|
+
primary key generation via the pk field. Foreign keys store related
|
|
55
|
+
object primary keys.
|
|
56
|
+
"""
|
|
57
|
+
output = io.StringIO()
|
|
58
|
+
|
|
59
|
+
class Author(BaseDBModel):
|
|
60
|
+
name: str
|
|
61
|
+
|
|
62
|
+
class Book(BaseDBModel):
|
|
63
|
+
title: str
|
|
64
|
+
author: ForeignKey[Author] = ForeignKey(Author)
|
|
65
|
+
|
|
66
|
+
db = SqliterDB(memory=True)
|
|
67
|
+
db.create_table(Author)
|
|
68
|
+
db.create_table(Book)
|
|
69
|
+
|
|
70
|
+
author = db.insert(Author(name="Jane Austen"))
|
|
71
|
+
book = db.insert(Book(title="Pride and Prejudice", author=author))
|
|
72
|
+
|
|
73
|
+
output.write("Created book:\n")
|
|
74
|
+
output.write(f" title: {book.title}\n")
|
|
75
|
+
output.write(f" author: {book.author.name}\n")
|
|
76
|
+
output.write(
|
|
77
|
+
"\nForeign key stores the primary key internally,\n"
|
|
78
|
+
"but access returns the object\n"
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
db.close()
|
|
82
|
+
return output.getvalue()
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _run_nullable_foreign_key() -> str:
|
|
86
|
+
"""Declare nullable FKs using Optional[T] in the type annotation.
|
|
87
|
+
|
|
88
|
+
SQLiter auto-detects nullability from the annotation so you don't
|
|
89
|
+
need to pass null=True explicitly.
|
|
90
|
+
|
|
91
|
+
Note: this demo already uses ForeignKey[Optional[Author]], but
|
|
92
|
+
annotation-based nullability is most reliable when models are defined at
|
|
93
|
+
module level (especially if you use type aliases). We include null=True
|
|
94
|
+
here for compatibility.
|
|
95
|
+
"""
|
|
96
|
+
output = io.StringIO()
|
|
97
|
+
|
|
98
|
+
class Author(BaseDBModel):
|
|
99
|
+
name: str
|
|
100
|
+
|
|
101
|
+
class Book(BaseDBModel):
|
|
102
|
+
title: str
|
|
103
|
+
author: ForeignKey[Optional[Author]] = ForeignKey(
|
|
104
|
+
Author, on_delete="SET NULL", null=True
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
db = SqliterDB(memory=True)
|
|
108
|
+
db.create_table(Author)
|
|
109
|
+
db.create_table(Book)
|
|
110
|
+
|
|
111
|
+
author = db.insert(Author(name="Jane Austen"))
|
|
112
|
+
book_with = db.insert(Book(title="Pride and Prejudice", author=author))
|
|
113
|
+
book_without = db.insert(Book(title="Anonymous Work", author=None))
|
|
114
|
+
|
|
115
|
+
book1 = db.get(Book, book_with.pk)
|
|
116
|
+
book2 = db.get(Book, book_without.pk)
|
|
117
|
+
|
|
118
|
+
if book1 is not None:
|
|
119
|
+
author_name = book1.author.name if book1.author else "None"
|
|
120
|
+
output.write(f"'{book1.title}' author: {author_name}\n")
|
|
121
|
+
if book2 is not None:
|
|
122
|
+
output.write(f"'{book2.title}' author: {book2.author}\n")
|
|
123
|
+
|
|
124
|
+
output.write("\nOptional[Author] auto-sets null=True on the FK column\n")
|
|
125
|
+
|
|
126
|
+
db.close()
|
|
127
|
+
return output.getvalue()
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def _run_relationship_navigation() -> str:
|
|
131
|
+
"""Navigate from one object to another using foreign keys.
|
|
132
|
+
|
|
133
|
+
ForeignKey fields let you traverse relationships by accessing
|
|
134
|
+
related objects as attributes.
|
|
135
|
+
"""
|
|
136
|
+
output = io.StringIO()
|
|
137
|
+
|
|
138
|
+
class Team(BaseDBModel):
|
|
139
|
+
name: str
|
|
140
|
+
|
|
141
|
+
class Player(BaseDBModel):
|
|
142
|
+
name: str
|
|
143
|
+
team: ForeignKey[Team] = ForeignKey(Team)
|
|
144
|
+
|
|
145
|
+
db = SqliterDB(memory=True)
|
|
146
|
+
db.create_table(Team)
|
|
147
|
+
db.create_table(Player)
|
|
148
|
+
|
|
149
|
+
team = db.insert(Team(name="Lakers"))
|
|
150
|
+
player1 = db.insert(Player(name="LeBron", team=team))
|
|
151
|
+
player2 = db.insert(Player(name="Davis", team=team))
|
|
152
|
+
|
|
153
|
+
output.write(f"Team: {team.name}\n")
|
|
154
|
+
|
|
155
|
+
# Navigate from player to team via FK
|
|
156
|
+
output.write(f"\n{player1.name} plays for: {player1.team.name}\n")
|
|
157
|
+
output.write(f"{player2.name} plays for: {player2.team.name}\n")
|
|
158
|
+
output.write("Foreign keys enable relationship navigation\n")
|
|
159
|
+
|
|
160
|
+
db.close()
|
|
161
|
+
return output.getvalue()
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def _run_reverse_relationships() -> str:
|
|
165
|
+
"""Access related objects in reverse using related_name.
|
|
166
|
+
|
|
167
|
+
When you define a ForeignKey, SQLiter automatically creates a reverse
|
|
168
|
+
relationship to access all objects that reference a given object.
|
|
169
|
+
"""
|
|
170
|
+
output = io.StringIO()
|
|
171
|
+
|
|
172
|
+
class Author(BaseDBModel):
|
|
173
|
+
name: str
|
|
174
|
+
|
|
175
|
+
class Book(BaseDBModel):
|
|
176
|
+
title: str
|
|
177
|
+
author: ForeignKey[Author] = ForeignKey(Author, related_name="books")
|
|
178
|
+
|
|
179
|
+
db = SqliterDB(memory=True)
|
|
180
|
+
db.create_table(Author)
|
|
181
|
+
db.create_table(Book)
|
|
182
|
+
|
|
183
|
+
author = db.insert(Author(name="Jane Austen"))
|
|
184
|
+
db.insert(Book(title="Pride and Prejudice", author=author))
|
|
185
|
+
db.insert(Book(title="Emma", author=author))
|
|
186
|
+
db.insert(Book(title="Sense and Sensibility", author=author))
|
|
187
|
+
|
|
188
|
+
output.write(f"Author: {author.name}\n")
|
|
189
|
+
|
|
190
|
+
# Access reverse relationship - get all books by this author
|
|
191
|
+
# Note: 'books' attribute added dynamically by ForeignKey descriptor
|
|
192
|
+
output.write("\nAccessing author.books (reverse relationship):\n")
|
|
193
|
+
reverse_attr = "books" # Dynamic attribute added by FK descriptor
|
|
194
|
+
books_query = getattr(author, reverse_attr)
|
|
195
|
+
books = books_query.fetch_all()
|
|
196
|
+
for book in books:
|
|
197
|
+
output.write(f" - {book.title}\n")
|
|
198
|
+
|
|
199
|
+
output.write(f"\nTotal books: {len(books)}\n")
|
|
200
|
+
output.write("Reverse relationships auto-generated from FKs\n")
|
|
201
|
+
|
|
202
|
+
db.close()
|
|
203
|
+
return output.getvalue()
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def _run_many_to_many_basic() -> str:
|
|
207
|
+
"""Show basic many-to-many usage with reverse access."""
|
|
208
|
+
output = io.StringIO()
|
|
209
|
+
|
|
210
|
+
class Tag(BaseDBModel):
|
|
211
|
+
name: str
|
|
212
|
+
|
|
213
|
+
class Article(BaseDBModel):
|
|
214
|
+
title: str
|
|
215
|
+
tags: ManyToMany[Tag] = ManyToMany(Tag, related_name="articles")
|
|
216
|
+
|
|
217
|
+
db = SqliterDB(memory=True)
|
|
218
|
+
db.create_table(Tag)
|
|
219
|
+
db.create_table(Article)
|
|
220
|
+
|
|
221
|
+
article = db.insert(Article(title="ORM Guide"))
|
|
222
|
+
python = db.insert(Tag(name="python"))
|
|
223
|
+
orm = db.insert(Tag(name="orm"))
|
|
224
|
+
|
|
225
|
+
article.tags.add(python, orm)
|
|
226
|
+
output.write("Article tags:\n")
|
|
227
|
+
for tag in article.tags.fetch_all():
|
|
228
|
+
output.write(f" - {tag.name}\n")
|
|
229
|
+
|
|
230
|
+
output.write("\nReverse access (tag.articles):\n")
|
|
231
|
+
entries = cast("Any", python.articles).fetch_all()
|
|
232
|
+
for entry in entries:
|
|
233
|
+
output.write(f" - {entry.title}\n")
|
|
234
|
+
|
|
235
|
+
db.close()
|
|
236
|
+
return output.getvalue()
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def _run_many_to_many_symmetrical() -> str:
|
|
240
|
+
"""Show symmetrical self-referential many-to-many."""
|
|
241
|
+
output = io.StringIO()
|
|
242
|
+
|
|
243
|
+
class User(BaseDBModel):
|
|
244
|
+
name: str
|
|
245
|
+
friends: ManyToMany[User] = ManyToMany("User", symmetrical=True)
|
|
246
|
+
|
|
247
|
+
db = SqliterDB(memory=True)
|
|
248
|
+
db.create_table(User)
|
|
249
|
+
|
|
250
|
+
alice = db.insert(User(name="Alice"))
|
|
251
|
+
bob = db.insert(User(name="Bob"))
|
|
252
|
+
|
|
253
|
+
alice.friends.add(bob)
|
|
254
|
+
|
|
255
|
+
output.write("Alice's friends:\n")
|
|
256
|
+
for friend in alice.friends.fetch_all():
|
|
257
|
+
output.write(f" - {friend.name}\n")
|
|
258
|
+
|
|
259
|
+
output.write("\nBob's friends (symmetrical):\n")
|
|
260
|
+
for friend in bob.friends.fetch_all():
|
|
261
|
+
output.write(f" - {friend.name}\n")
|
|
262
|
+
|
|
263
|
+
db.close()
|
|
264
|
+
return output.getvalue()
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
def _run_select_related_basic() -> str:
|
|
268
|
+
"""Demonstrate eager loading with select_related().
|
|
269
|
+
|
|
270
|
+
Shows how select_related() fetches related objects in a single JOIN query
|
|
271
|
+
instead of lazy loading (which causes N+1 queries).
|
|
272
|
+
"""
|
|
273
|
+
output = io.StringIO()
|
|
274
|
+
|
|
275
|
+
class Author(BaseDBModel):
|
|
276
|
+
name: str
|
|
277
|
+
|
|
278
|
+
class Book(BaseDBModel):
|
|
279
|
+
title: str
|
|
280
|
+
author: ForeignKey[Author] = ForeignKey(Author)
|
|
281
|
+
|
|
282
|
+
db = SqliterDB(memory=True)
|
|
283
|
+
db.create_table(Author)
|
|
284
|
+
db.create_table(Book)
|
|
285
|
+
|
|
286
|
+
# Insert test data
|
|
287
|
+
author1 = db.insert(Author(name="Jane Austen"))
|
|
288
|
+
author2 = db.insert(Author(name="Charles Dickens"))
|
|
289
|
+
|
|
290
|
+
db.insert(Book(title="Pride and Prejudice", author=author1))
|
|
291
|
+
db.insert(Book(title="Emma", author=author1))
|
|
292
|
+
db.insert(Book(title="Oliver Twist", author=author2))
|
|
293
|
+
|
|
294
|
+
# Eager load - single JOIN query
|
|
295
|
+
output.write("Fetching books with eager loading:\n")
|
|
296
|
+
books = db.select(Book).select_related("author").fetch_all()
|
|
297
|
+
|
|
298
|
+
for book in books:
|
|
299
|
+
output.write(f" '{book.title}' by {book.author.name}\n")
|
|
300
|
+
|
|
301
|
+
output.write("\nAll authors loaded in single query (no N+1 problem)\n")
|
|
302
|
+
|
|
303
|
+
db.close()
|
|
304
|
+
return output.getvalue()
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def _run_select_related_nested() -> str:
|
|
308
|
+
"""Demonstrate nested relationship eager loading.
|
|
309
|
+
|
|
310
|
+
Shows how to load nested relationships using double underscore syntax:
|
|
311
|
+
select_related("book__author") loads both Book and Author in one query.
|
|
312
|
+
"""
|
|
313
|
+
output = io.StringIO()
|
|
314
|
+
|
|
315
|
+
class Author(BaseDBModel):
|
|
316
|
+
name: str
|
|
317
|
+
|
|
318
|
+
class Book(BaseDBModel):
|
|
319
|
+
title: str
|
|
320
|
+
author: ForeignKey[Author] = ForeignKey(Author)
|
|
321
|
+
|
|
322
|
+
class Comment(BaseDBModel):
|
|
323
|
+
text: str
|
|
324
|
+
book: ForeignKey[Book] = ForeignKey(Book)
|
|
325
|
+
|
|
326
|
+
db = SqliterDB(memory=True)
|
|
327
|
+
db.create_table(Author)
|
|
328
|
+
db.create_table(Book)
|
|
329
|
+
db.create_table(Comment)
|
|
330
|
+
|
|
331
|
+
# Insert nested test data
|
|
332
|
+
author = db.insert(Author(name="Jane Austen"))
|
|
333
|
+
book = db.insert(Book(title="Pride and Prejudice", author=author))
|
|
334
|
+
db.insert(Comment(text="Amazing book!", book=book))
|
|
335
|
+
|
|
336
|
+
# Load nested relationship - single query joins Comment -> Book -> Author
|
|
337
|
+
output.write("Loading nested relationships:\n")
|
|
338
|
+
comment = db.select(Comment).select_related("book__author").fetch_one()
|
|
339
|
+
|
|
340
|
+
if comment is not None:
|
|
341
|
+
output.write(f"Comment: {comment.text}\n")
|
|
342
|
+
output.write(f"Book: {comment.book.title}\n")
|
|
343
|
+
# Access author through book's foreign key relationship
|
|
344
|
+
# Both book and author were loaded in a single JOIN query
|
|
345
|
+
output.write(f"Author: {comment.book.author.name}\n")
|
|
346
|
+
|
|
347
|
+
output.write("\nNested relationships loaded in single query\n")
|
|
348
|
+
|
|
349
|
+
db.close()
|
|
350
|
+
return output.getvalue()
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
def _run_relationship_filter_traversal() -> str:
|
|
354
|
+
"""Demonstrate relationship filter traversal.
|
|
355
|
+
|
|
356
|
+
Shows how to filter by fields on related models using double underscore
|
|
357
|
+
syntax: filter(author__name="Jane Austen")
|
|
358
|
+
"""
|
|
359
|
+
output = io.StringIO()
|
|
360
|
+
|
|
361
|
+
class Author(BaseDBModel):
|
|
362
|
+
name: str
|
|
363
|
+
|
|
364
|
+
class Book(BaseDBModel):
|
|
365
|
+
title: str
|
|
366
|
+
author: ForeignKey[Author] = ForeignKey(Author)
|
|
367
|
+
|
|
368
|
+
db = SqliterDB(memory=True)
|
|
369
|
+
db.create_table(Author)
|
|
370
|
+
db.create_table(Book)
|
|
371
|
+
|
|
372
|
+
# Insert test data
|
|
373
|
+
author1 = db.insert(Author(name="Jane Austen"))
|
|
374
|
+
author2 = db.insert(Author(name="Charles Dickens"))
|
|
375
|
+
|
|
376
|
+
db.insert(Book(title="Pride and Prejudice", author=author1))
|
|
377
|
+
db.insert(Book(title="Emma", author=author1))
|
|
378
|
+
db.insert(Book(title="Oliver Twist", author=author2))
|
|
379
|
+
db.insert(Book(title="Great Expectations", author=author2))
|
|
380
|
+
|
|
381
|
+
# Filter by related field
|
|
382
|
+
output.write("Filtering by author name:\n")
|
|
383
|
+
books = db.select(Book).filter(author__name="Jane Austen").fetch_all()
|
|
384
|
+
|
|
385
|
+
for book in books:
|
|
386
|
+
output.write(f" {book.title}\n")
|
|
387
|
+
|
|
388
|
+
output.write(f"\nFound {len(books)} book(s) by Jane Austen\n")
|
|
389
|
+
output.write("(Automatic JOIN added behind the scenes)\n")
|
|
390
|
+
|
|
391
|
+
db.close()
|
|
392
|
+
return output.getvalue()
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
def _run_select_related_combined() -> str:
|
|
396
|
+
"""Demonstrate combining select_related() with relationship filters.
|
|
397
|
+
|
|
398
|
+
Shows how to use select_related() with filter() for optimal performance:
|
|
399
|
+
load related objects AND filter by them in a single query.
|
|
400
|
+
"""
|
|
401
|
+
output = io.StringIO()
|
|
402
|
+
|
|
403
|
+
class Author(BaseDBModel):
|
|
404
|
+
name: str
|
|
405
|
+
|
|
406
|
+
class Book(BaseDBModel):
|
|
407
|
+
title: str
|
|
408
|
+
year: int
|
|
409
|
+
author: ForeignKey[Author] = ForeignKey(Author)
|
|
410
|
+
|
|
411
|
+
db = SqliterDB(memory=True)
|
|
412
|
+
db.create_table(Author)
|
|
413
|
+
db.create_table(Book)
|
|
414
|
+
|
|
415
|
+
# Insert test data
|
|
416
|
+
author1 = db.insert(Author(name="Jane Austen"))
|
|
417
|
+
author2 = db.insert(Author(name="Charles Dickens"))
|
|
418
|
+
|
|
419
|
+
db.insert(Book(title="Pride and Prejudice", year=1813, author=author1))
|
|
420
|
+
db.insert(Book(title="Emma", year=1815, author=author1))
|
|
421
|
+
db.insert(Book(title="Oliver Twist", year=1838, author=author2))
|
|
422
|
+
|
|
423
|
+
# Combine filter + eager load
|
|
424
|
+
output.write("Filter and eager load in single query:\n")
|
|
425
|
+
books = (
|
|
426
|
+
db.select(Book)
|
|
427
|
+
.select_related("author")
|
|
428
|
+
.filter(author__name__startswith="Jane")
|
|
429
|
+
.fetch_all()
|
|
430
|
+
)
|
|
431
|
+
|
|
432
|
+
for book in books:
|
|
433
|
+
output.write(f" {book.title} ({book.year}) by {book.author.name}\n")
|
|
434
|
+
|
|
435
|
+
output.write(f"\n{len(books)} result(s) with authors preloaded\n")
|
|
436
|
+
|
|
437
|
+
db.close()
|
|
438
|
+
return output.getvalue()
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
def get_category() -> DemoCategory:
|
|
442
|
+
"""Get the ORM Features demo category."""
|
|
443
|
+
return DemoCategory(
|
|
444
|
+
id="orm",
|
|
445
|
+
title="ORM Features",
|
|
446
|
+
icon="",
|
|
447
|
+
demos=[
|
|
448
|
+
Demo(
|
|
449
|
+
id="orm_lazy",
|
|
450
|
+
title="Lazy Loading",
|
|
451
|
+
description="Load related data on demand",
|
|
452
|
+
category="orm",
|
|
453
|
+
code=extract_demo_code(_run_lazy_loading),
|
|
454
|
+
execute=_run_lazy_loading,
|
|
455
|
+
),
|
|
456
|
+
Demo(
|
|
457
|
+
id="orm_fk_insert",
|
|
458
|
+
title="Inserting with Foreign Keys",
|
|
459
|
+
description="Create records linked to other records",
|
|
460
|
+
category="orm",
|
|
461
|
+
code=extract_demo_code(_run_orm_style_access),
|
|
462
|
+
execute=_run_orm_style_access,
|
|
463
|
+
),
|
|
464
|
+
Demo(
|
|
465
|
+
id="orm_nullable_fk",
|
|
466
|
+
title="Nullable Foreign Keys",
|
|
467
|
+
description="Auto-detect nullable FKs from annotations",
|
|
468
|
+
category="orm",
|
|
469
|
+
code=extract_demo_code(_run_nullable_foreign_key),
|
|
470
|
+
execute=_run_nullable_foreign_key,
|
|
471
|
+
),
|
|
472
|
+
Demo(
|
|
473
|
+
id="orm_relationships",
|
|
474
|
+
title="Relationship Navigation",
|
|
475
|
+
description="Navigate using foreign keys",
|
|
476
|
+
category="orm",
|
|
477
|
+
code=extract_demo_code(_run_relationship_navigation),
|
|
478
|
+
execute=_run_relationship_navigation,
|
|
479
|
+
),
|
|
480
|
+
Demo(
|
|
481
|
+
id="orm_reverse",
|
|
482
|
+
title="Reverse Relationships",
|
|
483
|
+
description="Access related objects via related_name",
|
|
484
|
+
category="orm",
|
|
485
|
+
code=extract_demo_code(_run_reverse_relationships),
|
|
486
|
+
execute=_run_reverse_relationships,
|
|
487
|
+
),
|
|
488
|
+
Demo(
|
|
489
|
+
id="orm_m2m_basic",
|
|
490
|
+
title="Many-to-Many Basics",
|
|
491
|
+
description="Relate records with a junction table",
|
|
492
|
+
category="orm",
|
|
493
|
+
code=extract_demo_code(_run_many_to_many_basic),
|
|
494
|
+
execute=_run_many_to_many_basic,
|
|
495
|
+
),
|
|
496
|
+
Demo(
|
|
497
|
+
id="orm_m2m_symmetrical",
|
|
498
|
+
title="Many-to-Many Symmetry",
|
|
499
|
+
description="Self-referential symmetrical relationships",
|
|
500
|
+
category="orm",
|
|
501
|
+
code=extract_demo_code(_run_many_to_many_symmetrical),
|
|
502
|
+
execute=_run_many_to_many_symmetrical,
|
|
503
|
+
),
|
|
504
|
+
Demo(
|
|
505
|
+
id="orm_select_related",
|
|
506
|
+
title="Eager Loading with select_related()",
|
|
507
|
+
description="Fetch related objects in a single JOIN query",
|
|
508
|
+
category="orm",
|
|
509
|
+
code=extract_demo_code(_run_select_related_basic),
|
|
510
|
+
execute=_run_select_related_basic,
|
|
511
|
+
),
|
|
512
|
+
Demo(
|
|
513
|
+
id="orm_select_related_nested",
|
|
514
|
+
title="Nested Relationship Loading",
|
|
515
|
+
description="Load nested relationships with double underscore",
|
|
516
|
+
category="orm",
|
|
517
|
+
code=extract_demo_code(_run_select_related_nested),
|
|
518
|
+
execute=_run_select_related_nested,
|
|
519
|
+
),
|
|
520
|
+
Demo(
|
|
521
|
+
id="orm_filter_traversal",
|
|
522
|
+
title="Relationship Filter Traversal",
|
|
523
|
+
description="Filter by related object fields",
|
|
524
|
+
category="orm",
|
|
525
|
+
code=extract_demo_code(_run_relationship_filter_traversal),
|
|
526
|
+
execute=_run_relationship_filter_traversal,
|
|
527
|
+
),
|
|
528
|
+
Demo(
|
|
529
|
+
id="orm_select_related_combined",
|
|
530
|
+
title="Combining select_related with Filters",
|
|
531
|
+
description="Eager load and filter by relationships",
|
|
532
|
+
category="orm",
|
|
533
|
+
code=extract_demo_code(_run_select_related_combined),
|
|
534
|
+
execute=_run_select_related_combined,
|
|
535
|
+
),
|
|
536
|
+
],
|
|
537
|
+
)
|