File tree Expand file tree Collapse file tree 4 files changed +26
-5
lines changed Expand file tree Collapse file tree 4 files changed +26
-5
lines changed Original file line number Diff line number Diff line change
1
+ Asynchronous routes will be automatically generated when using the ` GinoCRUDRouter ` . To use it, you must pass a
2
+ [ pydantic] ( https://pydantic-docs.helpmanual.io/ ) model, your SQLAlchemy Table, and the databases database.
3
+ This CRUDRouter is intended to be used with the python [ Gino] ( https://python-gino.org/ ) library. An example
4
+ of how to use [ Gino] ( https://python-gino.org/ ) with FastAPI can be found both
5
+ [ here] ( https://python-gino.org/docs/en/1.0/tutorials/fastapi.html ) and below.
6
+
7
+ !!! warning
8
+ To use the ` GinoCRUDRouter ` , Databases ** and** SQLAlchemy must be first installed.
9
+
10
+ ## Minimal Example
11
+ Below is a minimal example assuming that you have already imported and created
12
+ all the required models and database connections.
13
+
14
+ ``` python
15
+ router = GinoCRUDRouter(
16
+ schema = MyPydanticModel,
17
+ db = db,
18
+ db_model = MyModel
19
+ )
20
+ app.include_router(router)
21
+ ```
Original file line number Diff line number Diff line change 32
32
- In Memory : backends/memory.md
33
33
- SQLAlchemy : backends/sqlalchemy.md
34
34
- Databases (async) : backends/async.md
35
+ - Gino (async) : backends/gino.md
35
36
- Ormar (async) : backends/ormar.md
36
37
- Tortoise (async) : backends/tortoise.md
37
38
- Routing : routing.md
Original file line number Diff line number Diff line change 7
7
from ._types import PYDANTIC_SCHEMA as SCHEMA
8
8
9
9
try :
10
- import asyncpg
10
+ from asyncpg . exceptions import UniqueViolationError
11
11
from gino import Gino
12
-
13
12
from sqlalchemy .exc import IntegrityError
14
13
from sqlalchemy .ext .declarative import DeclarativeMeta as Model
15
14
except ImportError :
@@ -96,7 +95,7 @@ async def route(
96
95
async with self .db .transaction ():
97
96
db_model : Model = await self .db_model .create (** model .dict ())
98
97
return db_model
99
- except (IntegrityError , asyncpg . exceptions . UniqueViolationError ):
98
+ except (IntegrityError , UniqueViolationError ):
100
99
raise HTTPException (422 , "Key already exists" )
101
100
102
101
return route
@@ -113,7 +112,7 @@ async def route(
113
112
await db_model .update (** model ).apply ()
114
113
115
114
return db_model
116
- except (IntegrityError , asyncpg . exceptions . UniqueViolationError ) as e :
115
+ except (IntegrityError , UniqueViolationError ) as e :
117
116
raise HTTPException (422 , ", " .join (e .args ))
118
117
119
118
return route
Original file line number Diff line number Diff line change 1
- POSTGRES_HOST = db
1
+ POSTGRES_HOST = localhost
2
2
POSTGRES_DB = test
3
3
POSTGRES_USER = postgres
4
4
POSTGRES_PASSWORD = password
You can’t perform that action at this time.
0 commit comments