You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jul 13, 2022. It is now read-only.
Redistribution and use in source and binary forms, with or without
5
-
modification, are NOT permitted.
4
+
5
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
+
7
+
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8
+
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9
+
Neither the name of the nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
+
11
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Pynocular is a lightweight ORM that lets you query your database using Pydantic models and asyncio.
6
+
7
+
With Pynocular you can decorate your existing Pydantic models to sync them with the corresponding table in your
8
+
database, allowing you to persist changes without ever having to think about the database. Transaction management is
9
+
automatically handled for you so you can focus on the important parts of your code. This integrates seamlessly with frameworks that use Pydantic models such as FastAPI.
2
10
3
-
Utilities for interacting with SQL databases
4
11
5
12
Features:
6
13
7
-
-<!-- list of features -->
14
+
- Fully supports asyncio to write to SQL databases
15
+
- Provides simple methods for basic SQLAlchemy support (create, delete, update, read)
16
+
- Contains access to more advanced functionality such as custom SQLAlchemy selects
17
+
- Contains helper functions for creating new database tables
18
+
- Advanced transaction management system allows you to conditionally put requests in transactions
8
19
9
20
Table of Contents:
10
21
@@ -14,21 +25,179 @@ Table of Contents:
14
25
15
26
## Installation
16
27
17
-
ns_sql_utils requires Python 3.6 or above.
28
+
pynocular requires Python 3.6 or above.
18
29
19
30
```bash
20
-
pip install ns_sql_utils
31
+
pip install pynocular
21
32
# or
22
-
poetry add ns_sql_utils
33
+
poetry add pynocular
23
34
```
24
35
25
36
## Guide
26
37
27
-
<!-- Subsections explaining how to use the package -->
38
+
### Basic Usage
39
+
Pynocular works by decorating your base Pydantic model with the function `database_model`. Once decorated
40
+
with the proper information, you can proceed to use that model to interface with your specified database table.
41
+
42
+
The first step is to define a `DBInfo` object. This will contain the connection information to your database.
43
+
44
+
```python
45
+
from pynocular.engines import DatabaseType, DBInfo
46
+
47
+
48
+
# Example below shows how to connect to a locally-running Postgres database
asyncwithawait DBEngine.transaction(Org._database_info, is_conditional=False) as conn:
172
+
result =await conn.execute(query)
173
+
return [dict(row) asyncfor row in result]
174
+
```
175
+
NOTE: `DBengine.transaction` is used to create a connection to the database using the credentials passed in.
176
+
If `is_conditional` is `False`, then it will add the query to any transaction that is opened in the call chain. This allows us to make database calls
177
+
in different functions but still have them all be under the same database transaction. If there is no transaction opened in the call chain it will open
178
+
a new one and any subsequent calls underneath that context manager will be added to the new transaction.
179
+
180
+
If `is_conditional` is `True` and there is no transaction in the call chain, then the connection will not create a new transaction. Instead, the query will be performed without a transaction.
181
+
182
+
183
+
### Creating database tables
184
+
When you decorate a Pydantic model with Pynocular, it creates a SQLAlchemy table as a private variable. This can be accessed via the `_table` property
185
+
(although accessing private variables is not recommended). Using this, along with Pynocular's `create_tracked_table` function, allows you to create tables
186
+
in your database based off of Pydantic models!
187
+
188
+
```python
189
+
from pynocular.db_utils import create_tracked_table
190
+
191
+
from my_package import Org
192
+
193
+
# Creates the table "organizations" in the database defined by db_info
0 commit comments