dappyr 0.1.0__tar.gz
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.
- dappyr-0.1.0/LICENSE +18 -0
- dappyr-0.1.0/PKG-INFO +301 -0
- dappyr-0.1.0/README.md +272 -0
- dappyr-0.1.0/pyproject.toml +81 -0
- dappyr-0.1.0/src/dappyr/__init__.py +9 -0
- dappyr-0.1.0/src/dappyr/asyncpg.py +130 -0
- dappyr-0.1.0/src/dappyr/caching.py +87 -0
- dappyr-0.1.0/src/dappyr/dappyr.py +415 -0
- dappyr-0.1.0/src/dappyr/errors.py +68 -0
- dappyr-0.1.0/src/dappyr/psycopg.py +112 -0
- dappyr-0.1.0/src/dappyr/specifications.py +207 -0
- dappyr-0.1.0/src/dappyr/sqlite3.py +131 -0
dappyr-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
MIT No Attribution
|
|
2
|
+
|
|
3
|
+
Copyright 2026 curtissimo, llc
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so.
|
|
11
|
+
|
|
12
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
13
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
14
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
15
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
16
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
17
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
18
|
+
SOFTWARE.
|
dappyr-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: dappyr
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A python micro ORM inspired by the nuget package Dapper
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Author: Curtis Schlak
|
|
8
|
+
Author-email: 696163-realistschuckle@users.noreply.gitlab.com
|
|
9
|
+
Requires-Python: >=3.14,<4.0
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT No Attribution License (MIT-0)
|
|
13
|
+
Classifier: Programming Language :: Python
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.16
|
|
17
|
+
Classifier: Topic :: Database
|
|
18
|
+
Classifier: Topic :: Utilities
|
|
19
|
+
Provides-Extra: asyncpg
|
|
20
|
+
Provides-Extra: psycopg
|
|
21
|
+
Requires-Dist: asyncpg (>=0.32.0,<0.33.0) ; extra == "asyncpg"
|
|
22
|
+
Requires-Dist: asyncpg-stubs (>=0.32.0,<0.33.0) ; extra == "asyncpg"
|
|
23
|
+
Requires-Dist: psycopg[binary] (>=3.3,<4.0) ; extra == "psycopg"
|
|
24
|
+
Project-URL: Bug Tracker, https://gitlab.com/curtissimo/dappyr/-/work_items
|
|
25
|
+
Project-URL: Documentation, https://dappyr-621e3c.gitlab.io/
|
|
26
|
+
Project-URL: Repository, https://gitlab.com/curtissimo/dappyr
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# Dappyr
|
|
30
|
+
|
|
31
|
+
A micro ORM inspired by [Dapper](https://www.nuget.org/packages/Dapper/).
|
|
32
|
+
|
|
33
|
+
Dappyr is a bring-your-own-connection micro ORM.
|
|
34
|
+
|
|
35
|
+
## Documentation
|
|
36
|
+
|
|
37
|
+
Documentation is hosted on [GitLab Pages](https://dappyr-621e3c.gitlab.io/).
|
|
38
|
+
|
|
39
|
+
## Async by default
|
|
40
|
+
|
|
41
|
+
There's that whole `a` prefix thing in Python for asynchronous versions of
|
|
42
|
+
synchronous methods.
|
|
43
|
+
|
|
44
|
+
I don't like it.
|
|
45
|
+
|
|
46
|
+
Dappyr is async.
|
|
47
|
+
|
|
48
|
+
## A real-world example
|
|
49
|
+
|
|
50
|
+
Let's say you have a successful small business. Congratulations!
|
|
51
|
+
|
|
52
|
+
Rather than spend $8 per month on Clockify, you build your own time-tracking
|
|
53
|
+
software specifically for your business. You can specify clients, jobs for
|
|
54
|
+
clients, time against a job, and expenses.
|
|
55
|
+
|
|
56
|
+
How can you use Dappyr to interact with the data?
|
|
57
|
+
|
|
58
|
+
### Relational database tables
|
|
59
|
+
|
|
60
|
+
Here are some of the tables in the time-tracking database that manages the data
|
|
61
|
+
that you'll bill your clients for.
|
|
62
|
+
|
|
63
|
+
| `clients` table | | |
|
|
64
|
+
|-----------------|-----------|----|
|
|
65
|
+
| `id` | `SERIAL` | PK |
|
|
66
|
+
| `name` | `VARCHAR` | |
|
|
67
|
+
|
|
68
|
+
| `invoices` table | | |
|
|
69
|
+
|------------------|-------------|--------|
|
|
70
|
+
| `id` | `SERIAL` | PK |
|
|
71
|
+
| `client_id` | `INTEGER` | FK |
|
|
72
|
+
| `number` | `VARCHAR` | UNIQUE |
|
|
73
|
+
| `created_on` | `TIMESTAMP` | |
|
|
74
|
+
|
|
75
|
+
The line items for the invoice use what is called the ["joined table" strategy
|
|
76
|
+
in Hibernate](https://www.baeldung.com/hibernate-inheritance#joined-table) or
|
|
77
|
+
the ["table-per-type" strategy in EF
|
|
78
|
+
Core](https://learn.microsoft.com/en-us/ef/core/modeling/inheritance#table-per-type-configuration)
|
|
79
|
+
where there is a table that has common properties then other tables that have
|
|
80
|
+
the properties for the specific type of invoice line item. For large numbers of
|
|
81
|
+
records, this can have inferior performance to other strategies, but you're not
|
|
82
|
+
generating invoices with thousands of line items. (Yet!)
|
|
83
|
+
|
|
84
|
+
| `line_items` table | | |
|
|
85
|
+
|--------------------|-----------|----|
|
|
86
|
+
| `id` | `SERIAL` | PK |
|
|
87
|
+
| `invoice_id` | `INTEGER` | FK |
|
|
88
|
+
| `description` | `VARCHAR` | |
|
|
89
|
+
|
|
90
|
+
| `expense_line_items` table | | |
|
|
91
|
+
|----------------------------|-----------|--------|
|
|
92
|
+
| `id` | `INTEGER` | PK, FK |
|
|
93
|
+
| `amount` | `NUMERIC` | |
|
|
94
|
+
|
|
95
|
+
| `work_line_items` table | | |
|
|
96
|
+
|-------------------------|-----------|--------|
|
|
97
|
+
| `id` | `INTEGER` | PK, FK |
|
|
98
|
+
| `hourly_rate` | `NUMERIC` | |
|
|
99
|
+
| `hours` | `NUMERIC` | |
|
|
100
|
+
|
|
101
|
+
### Python classes
|
|
102
|
+
|
|
103
|
+
If you've ever used an ORM, it shouldn't be surprising that the classes for this
|
|
104
|
+
real-world example mirror the table structures. The examples use [Python
|
|
105
|
+
dataclasses](https://docs.python.org/3/library/dataclasses.html) to
|
|
106
|
+
auto-generate object initializers.
|
|
107
|
+
|
|
108
|
+
Dappyr recommends using specifically shaped objects for what you want to get
|
|
109
|
+
from the database.
|
|
110
|
+
|
|
111
|
+
## Using Dappyr
|
|
112
|
+
|
|
113
|
+
Let's take a look at how we can use Dappyr to get hydrated objects from the
|
|
114
|
+
database.
|
|
115
|
+
|
|
116
|
+
Because Dappyr uses Python >= 3.14, it allows you to use string literals or
|
|
117
|
+
[template strings](https://docs.python.org/3/library/string.templatelib.html)
|
|
118
|
+
for the SQL statements.
|
|
119
|
+
|
|
120
|
+
Because Dappyr is async by default, you use the psycopg
|
|
121
|
+
`AsyncConnection.connect` method to get a connection that Dappyr can use.
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
from datetime import datetime
|
|
125
|
+
from decimal import Decimal
|
|
126
|
+
from typing import ClassVar
|
|
127
|
+
from psycopg import AsyncConnection
|
|
128
|
+
import dappyr.pg as dappyr
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Get objects without relations
|
|
132
|
+
|
|
133
|
+
Here's how to get all the clients from the time-tracking database.
|
|
134
|
+
|
|
135
|
+
The `VO` suffix on the class name indicates the object is a [value
|
|
136
|
+
object](https://martinfowler.com/bliki/ValueObject.html).
|
|
137
|
+
|
|
138
|
+
```python
|
|
139
|
+
@dataclass(kw_only=True)
|
|
140
|
+
class ClientVO:
|
|
141
|
+
id: int
|
|
142
|
+
name: str
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
async with await AsyncConnection.connect() as conn, dappyr.use(conn) as query:
|
|
146
|
+
clients = query(
|
|
147
|
+
"""
|
|
148
|
+
SELECT id, name
|
|
149
|
+
FROM clients
|
|
150
|
+
ORDER BY name
|
|
151
|
+
""",
|
|
152
|
+
ClientVO,
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
async for client in clients:
|
|
156
|
+
assert type(client) is Client
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## Get one-to-many relationships
|
|
160
|
+
|
|
161
|
+
Now, let's say you want a list of `Client` objects with each client's invoices
|
|
162
|
+
in a list available on the `Client` object. We could do something like this.
|
|
163
|
+
|
|
164
|
+
```python
|
|
165
|
+
@dataclass(kw_only=True)
|
|
166
|
+
class Invoice:
|
|
167
|
+
id: int
|
|
168
|
+
number: str
|
|
169
|
+
created_on: datetime
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
@dataclass(kw_only=True)
|
|
173
|
+
class Client:
|
|
174
|
+
id: int
|
|
175
|
+
name: str
|
|
176
|
+
invoices: list[Invoice] = field(default_factory=list)
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
client_cache = dapper.cache(Client, lambda x: x.id)
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
async with await AsyncConnection.connect() as conn, dapper.use(conn) as query:
|
|
183
|
+
clients = query(
|
|
184
|
+
"""
|
|
185
|
+
SELECT c.id, c.name
|
|
186
|
+
, i.id, i.number, i.created_on
|
|
187
|
+
FROM clients c
|
|
188
|
+
LEFT JOIN invoices i ON (i.client_id = c.id)
|
|
189
|
+
ORDER BY name
|
|
190
|
+
""",
|
|
191
|
+
(client_cache, Invoice),
|
|
192
|
+
lambda client, invoice: client.invoices.append(invoice) or client,
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
async for client in clients:
|
|
196
|
+
assert type(client) is Client
|
|
197
|
+
for invoice in client.invoices:
|
|
198
|
+
assert type(invoice) is Invoice
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
## Get nested one-to-many relationships
|
|
202
|
+
|
|
203
|
+
What if you want a list of clients with their invoices, and the invoices have
|
|
204
|
+
their line items on them? Just `JOIN` deeper.
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
```python
|
|
208
|
+
@dataclass(kw_only=True)
|
|
209
|
+
class LineItem(ABC):
|
|
210
|
+
id: int
|
|
211
|
+
description: str
|
|
212
|
+
created_on: datetime
|
|
213
|
+
|
|
214
|
+
@abstractproperty
|
|
215
|
+
def total(self): ...
|
|
216
|
+
|
|
217
|
+
@staticmethod
|
|
218
|
+
def create(**kwargs) -> ExpenseLineItem | WorkLineItem:
|
|
219
|
+
if kwargs.get("amount") is not None:
|
|
220
|
+
return ExpenseLineItem(**kwargs)
|
|
221
|
+
if (
|
|
222
|
+
kwargs.get("hourly_rate") is not None
|
|
223
|
+
and kwargs.get("hours") is not None
|
|
224
|
+
):
|
|
225
|
+
return WorkLineItem(**kwargs)
|
|
226
|
+
raise TypeError("LineItem.create() could not create a subclass")
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
@dataclass(kw_only=True)
|
|
230
|
+
class ExpenseLineItem(LineItem):
|
|
231
|
+
amount: Decimal
|
|
232
|
+
|
|
233
|
+
@property
|
|
234
|
+
def total(self):
|
|
235
|
+
return self.amount
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
@dataclass(kw_only=True)
|
|
239
|
+
class WorkLineItem(LineItem):
|
|
240
|
+
hourly_rate: Decimal
|
|
241
|
+
hours: Decimal
|
|
242
|
+
|
|
243
|
+
@property
|
|
244
|
+
def total(self):
|
|
245
|
+
return self.hourly_rate * self.hours
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
@dataclass(kw_only=True)
|
|
249
|
+
class Invoice:
|
|
250
|
+
id: int
|
|
251
|
+
number: str
|
|
252
|
+
created_on: datetime
|
|
253
|
+
items: list[LineItem] = field(default_factory=list)
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
@dataclass(kw_only=True)
|
|
257
|
+
class Client:
|
|
258
|
+
id: int
|
|
259
|
+
name: str
|
|
260
|
+
invoices: dapper.OrderedSet[Invoice] = field(
|
|
261
|
+
default_factory=dapper.OrderedSet[Invoice](lambda x: x.id)
|
|
262
|
+
)
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
client_cache = dapper.cache(Client, lambda x: x.id)
|
|
266
|
+
invoice_cache = dapper.cache(Invoice, lambda x: x.id)
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
async with await AsyncConnection.connect() as conn, dapper.use(conn) as query:
|
|
270
|
+
clients = query(
|
|
271
|
+
"""
|
|
272
|
+
SELECT c.id, c.name
|
|
273
|
+
, i.id, i.number, i.created_on
|
|
274
|
+
, li.id, li.description, li.created_on
|
|
275
|
+
, eli.amount
|
|
276
|
+
, wli.hourly_rate, wli.hours
|
|
277
|
+
FROM clients c
|
|
278
|
+
LEFT JOIN invoices i ON (i.client_id = c.id)
|
|
279
|
+
LEFT JOIN line_items li ON (li.invoice_id = i.id)
|
|
280
|
+
LEFT JOIN expense_line_items eli ON (eli.id = li.id)
|
|
281
|
+
LEFT JOIN work_line_items wli ON (wli.id = li.id)
|
|
282
|
+
ORDER BY c.name, i.created_on DESC, li.created_on
|
|
283
|
+
""",
|
|
284
|
+
(client_cache, invoice_cache, LineItem.create),
|
|
285
|
+
lambda client, invoice, line_item: (
|
|
286
|
+
client.invoices.add(invoice)
|
|
287
|
+
or invoice.items.append(line_item)
|
|
288
|
+
or client
|
|
289
|
+
),
|
|
290
|
+
)
|
|
291
|
+
|
|
292
|
+
async for client in clients:
|
|
293
|
+
assert type(client) is Client
|
|
294
|
+
for invoice in client.invoices:
|
|
295
|
+
assert type(invoice) is Invoice
|
|
296
|
+
for item in invoice.items:
|
|
297
|
+
assert (
|
|
298
|
+
type(item) is ExpenseLineItem or type(item) is WorkLineItem
|
|
299
|
+
)
|
|
300
|
+
```
|
|
301
|
+
|
dappyr-0.1.0/README.md
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
# Dappyr
|
|
2
|
+
|
|
3
|
+
A micro ORM inspired by [Dapper](https://www.nuget.org/packages/Dapper/).
|
|
4
|
+
|
|
5
|
+
Dappyr is a bring-your-own-connection micro ORM.
|
|
6
|
+
|
|
7
|
+
## Documentation
|
|
8
|
+
|
|
9
|
+
Documentation is hosted on [GitLab Pages](https://dappyr-621e3c.gitlab.io/).
|
|
10
|
+
|
|
11
|
+
## Async by default
|
|
12
|
+
|
|
13
|
+
There's that whole `a` prefix thing in Python for asynchronous versions of
|
|
14
|
+
synchronous methods.
|
|
15
|
+
|
|
16
|
+
I don't like it.
|
|
17
|
+
|
|
18
|
+
Dappyr is async.
|
|
19
|
+
|
|
20
|
+
## A real-world example
|
|
21
|
+
|
|
22
|
+
Let's say you have a successful small business. Congratulations!
|
|
23
|
+
|
|
24
|
+
Rather than spend $8 per month on Clockify, you build your own time-tracking
|
|
25
|
+
software specifically for your business. You can specify clients, jobs for
|
|
26
|
+
clients, time against a job, and expenses.
|
|
27
|
+
|
|
28
|
+
How can you use Dappyr to interact with the data?
|
|
29
|
+
|
|
30
|
+
### Relational database tables
|
|
31
|
+
|
|
32
|
+
Here are some of the tables in the time-tracking database that manages the data
|
|
33
|
+
that you'll bill your clients for.
|
|
34
|
+
|
|
35
|
+
| `clients` table | | |
|
|
36
|
+
|-----------------|-----------|----|
|
|
37
|
+
| `id` | `SERIAL` | PK |
|
|
38
|
+
| `name` | `VARCHAR` | |
|
|
39
|
+
|
|
40
|
+
| `invoices` table | | |
|
|
41
|
+
|------------------|-------------|--------|
|
|
42
|
+
| `id` | `SERIAL` | PK |
|
|
43
|
+
| `client_id` | `INTEGER` | FK |
|
|
44
|
+
| `number` | `VARCHAR` | UNIQUE |
|
|
45
|
+
| `created_on` | `TIMESTAMP` | |
|
|
46
|
+
|
|
47
|
+
The line items for the invoice use what is called the ["joined table" strategy
|
|
48
|
+
in Hibernate](https://www.baeldung.com/hibernate-inheritance#joined-table) or
|
|
49
|
+
the ["table-per-type" strategy in EF
|
|
50
|
+
Core](https://learn.microsoft.com/en-us/ef/core/modeling/inheritance#table-per-type-configuration)
|
|
51
|
+
where there is a table that has common properties then other tables that have
|
|
52
|
+
the properties for the specific type of invoice line item. For large numbers of
|
|
53
|
+
records, this can have inferior performance to other strategies, but you're not
|
|
54
|
+
generating invoices with thousands of line items. (Yet!)
|
|
55
|
+
|
|
56
|
+
| `line_items` table | | |
|
|
57
|
+
|--------------------|-----------|----|
|
|
58
|
+
| `id` | `SERIAL` | PK |
|
|
59
|
+
| `invoice_id` | `INTEGER` | FK |
|
|
60
|
+
| `description` | `VARCHAR` | |
|
|
61
|
+
|
|
62
|
+
| `expense_line_items` table | | |
|
|
63
|
+
|----------------------------|-----------|--------|
|
|
64
|
+
| `id` | `INTEGER` | PK, FK |
|
|
65
|
+
| `amount` | `NUMERIC` | |
|
|
66
|
+
|
|
67
|
+
| `work_line_items` table | | |
|
|
68
|
+
|-------------------------|-----------|--------|
|
|
69
|
+
| `id` | `INTEGER` | PK, FK |
|
|
70
|
+
| `hourly_rate` | `NUMERIC` | |
|
|
71
|
+
| `hours` | `NUMERIC` | |
|
|
72
|
+
|
|
73
|
+
### Python classes
|
|
74
|
+
|
|
75
|
+
If you've ever used an ORM, it shouldn't be surprising that the classes for this
|
|
76
|
+
real-world example mirror the table structures. The examples use [Python
|
|
77
|
+
dataclasses](https://docs.python.org/3/library/dataclasses.html) to
|
|
78
|
+
auto-generate object initializers.
|
|
79
|
+
|
|
80
|
+
Dappyr recommends using specifically shaped objects for what you want to get
|
|
81
|
+
from the database.
|
|
82
|
+
|
|
83
|
+
## Using Dappyr
|
|
84
|
+
|
|
85
|
+
Let's take a look at how we can use Dappyr to get hydrated objects from the
|
|
86
|
+
database.
|
|
87
|
+
|
|
88
|
+
Because Dappyr uses Python >= 3.14, it allows you to use string literals or
|
|
89
|
+
[template strings](https://docs.python.org/3/library/string.templatelib.html)
|
|
90
|
+
for the SQL statements.
|
|
91
|
+
|
|
92
|
+
Because Dappyr is async by default, you use the psycopg
|
|
93
|
+
`AsyncConnection.connect` method to get a connection that Dappyr can use.
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
from datetime import datetime
|
|
97
|
+
from decimal import Decimal
|
|
98
|
+
from typing import ClassVar
|
|
99
|
+
from psycopg import AsyncConnection
|
|
100
|
+
import dappyr.pg as dappyr
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Get objects without relations
|
|
104
|
+
|
|
105
|
+
Here's how to get all the clients from the time-tracking database.
|
|
106
|
+
|
|
107
|
+
The `VO` suffix on the class name indicates the object is a [value
|
|
108
|
+
object](https://martinfowler.com/bliki/ValueObject.html).
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
@dataclass(kw_only=True)
|
|
112
|
+
class ClientVO:
|
|
113
|
+
id: int
|
|
114
|
+
name: str
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
async with await AsyncConnection.connect() as conn, dappyr.use(conn) as query:
|
|
118
|
+
clients = query(
|
|
119
|
+
"""
|
|
120
|
+
SELECT id, name
|
|
121
|
+
FROM clients
|
|
122
|
+
ORDER BY name
|
|
123
|
+
""",
|
|
124
|
+
ClientVO,
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
async for client in clients:
|
|
128
|
+
assert type(client) is Client
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Get one-to-many relationships
|
|
132
|
+
|
|
133
|
+
Now, let's say you want a list of `Client` objects with each client's invoices
|
|
134
|
+
in a list available on the `Client` object. We could do something like this.
|
|
135
|
+
|
|
136
|
+
```python
|
|
137
|
+
@dataclass(kw_only=True)
|
|
138
|
+
class Invoice:
|
|
139
|
+
id: int
|
|
140
|
+
number: str
|
|
141
|
+
created_on: datetime
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
@dataclass(kw_only=True)
|
|
145
|
+
class Client:
|
|
146
|
+
id: int
|
|
147
|
+
name: str
|
|
148
|
+
invoices: list[Invoice] = field(default_factory=list)
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
client_cache = dapper.cache(Client, lambda x: x.id)
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
async with await AsyncConnection.connect() as conn, dapper.use(conn) as query:
|
|
155
|
+
clients = query(
|
|
156
|
+
"""
|
|
157
|
+
SELECT c.id, c.name
|
|
158
|
+
, i.id, i.number, i.created_on
|
|
159
|
+
FROM clients c
|
|
160
|
+
LEFT JOIN invoices i ON (i.client_id = c.id)
|
|
161
|
+
ORDER BY name
|
|
162
|
+
""",
|
|
163
|
+
(client_cache, Invoice),
|
|
164
|
+
lambda client, invoice: client.invoices.append(invoice) or client,
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
async for client in clients:
|
|
168
|
+
assert type(client) is Client
|
|
169
|
+
for invoice in client.invoices:
|
|
170
|
+
assert type(invoice) is Invoice
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## Get nested one-to-many relationships
|
|
174
|
+
|
|
175
|
+
What if you want a list of clients with their invoices, and the invoices have
|
|
176
|
+
their line items on them? Just `JOIN` deeper.
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
```python
|
|
180
|
+
@dataclass(kw_only=True)
|
|
181
|
+
class LineItem(ABC):
|
|
182
|
+
id: int
|
|
183
|
+
description: str
|
|
184
|
+
created_on: datetime
|
|
185
|
+
|
|
186
|
+
@abstractproperty
|
|
187
|
+
def total(self): ...
|
|
188
|
+
|
|
189
|
+
@staticmethod
|
|
190
|
+
def create(**kwargs) -> ExpenseLineItem | WorkLineItem:
|
|
191
|
+
if kwargs.get("amount") is not None:
|
|
192
|
+
return ExpenseLineItem(**kwargs)
|
|
193
|
+
if (
|
|
194
|
+
kwargs.get("hourly_rate") is not None
|
|
195
|
+
and kwargs.get("hours") is not None
|
|
196
|
+
):
|
|
197
|
+
return WorkLineItem(**kwargs)
|
|
198
|
+
raise TypeError("LineItem.create() could not create a subclass")
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
@dataclass(kw_only=True)
|
|
202
|
+
class ExpenseLineItem(LineItem):
|
|
203
|
+
amount: Decimal
|
|
204
|
+
|
|
205
|
+
@property
|
|
206
|
+
def total(self):
|
|
207
|
+
return self.amount
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
@dataclass(kw_only=True)
|
|
211
|
+
class WorkLineItem(LineItem):
|
|
212
|
+
hourly_rate: Decimal
|
|
213
|
+
hours: Decimal
|
|
214
|
+
|
|
215
|
+
@property
|
|
216
|
+
def total(self):
|
|
217
|
+
return self.hourly_rate * self.hours
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
@dataclass(kw_only=True)
|
|
221
|
+
class Invoice:
|
|
222
|
+
id: int
|
|
223
|
+
number: str
|
|
224
|
+
created_on: datetime
|
|
225
|
+
items: list[LineItem] = field(default_factory=list)
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
@dataclass(kw_only=True)
|
|
229
|
+
class Client:
|
|
230
|
+
id: int
|
|
231
|
+
name: str
|
|
232
|
+
invoices: dapper.OrderedSet[Invoice] = field(
|
|
233
|
+
default_factory=dapper.OrderedSet[Invoice](lambda x: x.id)
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
client_cache = dapper.cache(Client, lambda x: x.id)
|
|
238
|
+
invoice_cache = dapper.cache(Invoice, lambda x: x.id)
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
async with await AsyncConnection.connect() as conn, dapper.use(conn) as query:
|
|
242
|
+
clients = query(
|
|
243
|
+
"""
|
|
244
|
+
SELECT c.id, c.name
|
|
245
|
+
, i.id, i.number, i.created_on
|
|
246
|
+
, li.id, li.description, li.created_on
|
|
247
|
+
, eli.amount
|
|
248
|
+
, wli.hourly_rate, wli.hours
|
|
249
|
+
FROM clients c
|
|
250
|
+
LEFT JOIN invoices i ON (i.client_id = c.id)
|
|
251
|
+
LEFT JOIN line_items li ON (li.invoice_id = i.id)
|
|
252
|
+
LEFT JOIN expense_line_items eli ON (eli.id = li.id)
|
|
253
|
+
LEFT JOIN work_line_items wli ON (wli.id = li.id)
|
|
254
|
+
ORDER BY c.name, i.created_on DESC, li.created_on
|
|
255
|
+
""",
|
|
256
|
+
(client_cache, invoice_cache, LineItem.create),
|
|
257
|
+
lambda client, invoice, line_item: (
|
|
258
|
+
client.invoices.add(invoice)
|
|
259
|
+
or invoice.items.append(line_item)
|
|
260
|
+
or client
|
|
261
|
+
),
|
|
262
|
+
)
|
|
263
|
+
|
|
264
|
+
async for client in clients:
|
|
265
|
+
assert type(client) is Client
|
|
266
|
+
for invoice in client.invoices:
|
|
267
|
+
assert type(invoice) is Invoice
|
|
268
|
+
for item in invoice.items:
|
|
269
|
+
assert (
|
|
270
|
+
type(item) is ExpenseLineItem or type(item) is WorkLineItem
|
|
271
|
+
)
|
|
272
|
+
```
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "dappyr"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "A python micro ORM inspired by the nuget package Dapper"
|
|
5
|
+
authors = [
|
|
6
|
+
{ name = "Curtis Schlak", email = "696163-realistschuckle@users.noreply.gitlab.com" },
|
|
7
|
+
]
|
|
8
|
+
license = "MIT"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.14,<4.0"
|
|
11
|
+
dependencies = []
|
|
12
|
+
classifiers = [
|
|
13
|
+
"Development Status :: 4 - Beta",
|
|
14
|
+
"Intended Audience :: Developers",
|
|
15
|
+
"License :: OSI Approved :: MIT No Attribution License (MIT-0)",
|
|
16
|
+
"Programming Language :: Python",
|
|
17
|
+
"Programming Language :: Python :: 3.14",
|
|
18
|
+
"Programming Language :: Python :: 3.15",
|
|
19
|
+
"Programming Language :: Python :: 3.16",
|
|
20
|
+
"Topic :: Database",
|
|
21
|
+
"Topic :: Utilities",
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
[project.urls]
|
|
25
|
+
repository = "https://gitlab.com/curtissimo/dappyr"
|
|
26
|
+
documentation = "https://dappyr-621e3c.gitlab.io/"
|
|
27
|
+
"Bug Tracker" = "https://gitlab.com/curtissimo/dappyr/-/work_items"
|
|
28
|
+
|
|
29
|
+
[project.optional-dependencies]
|
|
30
|
+
psycopg = ["psycopg[binary]>=3.3,<4.0"]
|
|
31
|
+
asyncpg = ["asyncpg>=0.32.0,<0.33.0", "asyncpg-stubs (>=0.32.0,<0.33.0)"]
|
|
32
|
+
|
|
33
|
+
[dependency-groups]
|
|
34
|
+
dev = [
|
|
35
|
+
"ruff (>=0.17.6,<0.18.0)",
|
|
36
|
+
"pytest (>=9.2.0,<10.0.0)",
|
|
37
|
+
"pytest-asyncio (>=1.5.0,<2.0.0)",
|
|
38
|
+
"pyclean (>=3.7.0,<4.0.0)",
|
|
39
|
+
"pytest-integration-mark (>=0.2.0,<0.3.0)",
|
|
40
|
+
"python-dotenv (>=1.2.4,<2.0.0)",
|
|
41
|
+
"basedpyright (>=1.40.2,<2.0.0)",
|
|
42
|
+
]
|
|
43
|
+
docs = ["zensical (>=0.1.4,<0.2.0)", "mkdocstrings-python (>=2.0.11,<3.0.0)"]
|
|
44
|
+
deploy = ["twine (>=7.0.0,<8.0.0)"]
|
|
45
|
+
|
|
46
|
+
[tool.poetry.group.dev]
|
|
47
|
+
optional = true
|
|
48
|
+
|
|
49
|
+
[tool.poetry.group.docs]
|
|
50
|
+
optional = true
|
|
51
|
+
|
|
52
|
+
[tool.poetry]
|
|
53
|
+
packages = [{ include = "dappyr", from = "src" }]
|
|
54
|
+
|
|
55
|
+
[build-system]
|
|
56
|
+
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
|
57
|
+
build-backend = "poetry.core.masonry.api"
|
|
58
|
+
|
|
59
|
+
[tool.pytest]
|
|
60
|
+
asyncio_default_fixture_loop_scope = "session"
|
|
61
|
+
asyncio_default_test_loop_scope = "session"
|
|
62
|
+
asyncio_mode = "auto"
|
|
63
|
+
|
|
64
|
+
[tool.ruff]
|
|
65
|
+
line-length = 80
|
|
66
|
+
target-version = "py314"
|
|
67
|
+
|
|
68
|
+
[tool.ruff.lint]
|
|
69
|
+
preview = true
|
|
70
|
+
select = [
|
|
71
|
+
"correctness",
|
|
72
|
+
"suspicious",
|
|
73
|
+
"complexity",
|
|
74
|
+
"performance",
|
|
75
|
+
"style",
|
|
76
|
+
"pedantic",
|
|
77
|
+
]
|
|
78
|
+
ignore = ["multi-line-summary-second-line"]
|
|
79
|
+
|
|
80
|
+
[tool.ruff.lint.pydocstyle]
|
|
81
|
+
convention = "google"
|