|
1 | | -# FastAPI-Async-SQLA |
| 1 | +# FastSQLA |
2 | 2 |
|
3 | | -[](https://pypi.org/project/FastAPI-Async-SQLA/) |
| 3 | +[](https://pypi.org/project/FastSQLA/) |
4 | 4 | [](https://conventionalcommits.org) |
5 | | -[](https://codecov.io/gh/hadrien/fastapi-async-sqla) |
| 5 | +[](https://codecov.io/gh/hadrien/fastsqla) |
6 | 6 |
|
7 | | -FastAPI-Async-SQLA is an [SQLAlchemy] extension for [FastAPI]. It supports asynchronous |
8 | | -SQLAlchemy sessions using SQLAlchemy >= 2.0 and provides pagination support. |
| 7 | +FastSQLA is an [SQLAlchemy] extension for [FastAPI]. It supports asynchronous |
| 8 | +SQLAlchemy sessions using SQLAlchemy >= 2.0 and include built-in pagination. |
9 | 9 |
|
10 | 10 | # Installing |
11 | 11 |
|
| 12 | +Using [uv](https://docs.astral.sh/uv/): |
| 13 | +```bash |
| 14 | +uv add fastsqla |
| 15 | +``` |
| 16 | + |
12 | 17 | Using [pip](https://pip.pypa.io/): |
13 | 18 | ``` |
14 | | -pip install fastapi-async-sqla |
| 19 | +pip install fastsqla |
15 | 20 | ``` |
16 | 21 |
|
17 | 22 | # Quick Example |
18 | 23 |
|
| 24 | +>! Note |
| 25 | +>! Example uses an sqlite db, but FastSQLA is compatible with any async db supported by |
| 26 | +>! SQLAlchemy |
| 27 | +
|
19 | 28 | Assuming it runs against a DB with a table `user` with 2 columns `id` and `name`: |
20 | 29 |
|
21 | 30 | ```python |
22 | 31 | # main.py |
23 | 32 | from fastapi import FastAPI, HTTPException |
24 | | -from fastapi_async_sqla import Base, Item, Page, Paginate, Session, lifespan |
| 33 | +from fastsqla import Base, Item, Page, Paginate, Session, lifespan |
25 | 34 | from pydantic import BaseModel |
26 | 35 | from sqlalchemy import select |
27 | 36 |
|
@@ -62,16 +71,35 @@ async def create_user(new_user: UserIn, session: Session): |
62 | 71 | return {"data": user} |
63 | 72 | ``` |
64 | 73 |
|
65 | | -Creating a db using `sqlite3`: |
| 74 | + |
| 75 | +Create an `sqlite3` db: |
| 76 | + |
66 | 77 | ```bash |
67 | 78 | sqlite3 db.sqlite <<EOF |
68 | | -CREATE TABLE user ( |
69 | | - id INTEGER PRIMARY KEY AUTOINCREMENT, |
70 | | - name TEXT NOT NULL |
| 79 | +sqlite3 db.sqlite <<EOF |
| 80 | +CREATE TABLE hero ( |
| 81 | + id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 82 | + name TEXT NOT NULL UNIQUE, -- Hero name (e.g., Superman) |
| 83 | + secret_identity TEXT NOT NULL -- Secret identity (e.g., Clark Kent) |
71 | 84 | ); |
| 85 | +
|
| 86 | +-- Insert heroes with hero name and secret identity |
| 87 | +INSERT INTO hero (name, secret_identity) VALUES ('Superman', 'Clark Kent'); |
| 88 | +INSERT INTO hero (name, secret_identity) VALUES ('Batman', 'Bruce Wayne'); |
| 89 | +INSERT INTO hero (name, secret_identity) VALUES ('Wonder Woman', 'Diana Prince'); |
| 90 | +INSERT INTO hero (name, secret_identity) VALUES ('Iron Man', 'Tony Stark'); |
| 91 | +INSERT INTO hero (name, secret_identity) VALUES ('Spider-Man', 'Peter Parker'); |
| 92 | +INSERT INTO hero (name, secret_identity) VALUES ('Captain America', 'Steve Rogers'); |
| 93 | +INSERT INTO hero (name, secret_identity) VALUES ('Black Widow', 'Natasha Romanoff'); |
| 94 | +INSERT INTO hero (name, secret_identity) VALUES ('Thor', 'Thor Odinson'); |
| 95 | +INSERT INTO hero (name, secret_identity) VALUES ('Scarlet Witch', 'Wanda Maximoff'); |
| 96 | +INSERT INTO hero (name, secret_identity) VALUES ('Doctor Strange', 'Stephen Strange'); |
| 97 | +INSERT INTO hero (name, secret_identity) VALUES ('The Flash', 'Barry Allen'); |
| 98 | +INSERT INTO hero (name, secret_identity) VALUES ('Green Lantern', 'Hal Jordan'); |
72 | 99 | EOF |
73 | 100 | ``` |
74 | 101 |
|
| 102 | + |
75 | 103 | Installing [aiosqlite] to connect to the sqlite db asynchronously: |
76 | 104 | ```bash |
77 | 105 | pip install aiosqlite |
|
0 commit comments