dappyr 0.1.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.
@@ -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
+
@@ -0,0 +1,12 @@
1
+ dappyr/__init__.py,sha256=U5esjfpauYgwGhFEGjLP8tzcIBTTVKV-qfYA8o3uSOI,180
2
+ dappyr/asyncpg.py,sha256=aVm0frz4IAk2hdp7YuWo8EjMtZ0GYbUglRtFHTcQRvM,4682
3
+ dappyr/caching.py,sha256=xDSuMfsYb4SxprKK4LKc6uUZBAhjYdPfSwA3lXD_Wl8,2893
4
+ dappyr/dappyr.py,sha256=pothBPZ2szBBuc-1b16J6zSqCNS9-KH5ZK__WhvpB5o,13850
5
+ dappyr/errors.py,sha256=nd-vd8KqNFF3DsoA5LIjCapf1KVKU9i5z-okYcXnXI4,2314
6
+ dappyr/psycopg.py,sha256=6xgMmNohN2Pqe-lLyxgOhI-FXq_HA0PLhDYBBD-g4mI,3725
7
+ dappyr/specifications.py,sha256=xvrF8-m8eyn1-SAoC8TkNNhFzE5a5oFnbh1Z7UBuhCI,5717
8
+ dappyr/sqlite3.py,sha256=TIJLYmbxS3BKBs89BIXA2QN5zM4BfIzgF9hAocqaR2g,4627
9
+ dappyr-0.1.0.dist-info/METADATA,sha256=T53nx82kjGVjkpr5REld9_c6K4N3i70N3DaILiJn-co,8999
10
+ dappyr-0.1.0.dist-info/WHEEL,sha256=L-WvLdSBvJWHXv5zrQxDvNRnIgoBzLUpcbuiw19KCuQ,88
11
+ dappyr-0.1.0.dist-info/licenses/LICENSE,sha256=fWg2Mh2N0Qzc4lsChBy4Xbi8cn_d__MGKH2TsFCFyEk,910
12
+ dappyr-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: poetry-core 2.5.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -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.