-
-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathdatabases_.py
173 lines (141 loc) · 3.99 KB
/
databases_.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import databases
from fastapi import FastAPI
from sqlalchemy import Column, Float, Integer, MetaData, String, Table, create_engine
from sqlalchemy_utils import create_database, database_exists, drop_database
from fastapi_crudrouter import DatabasesCRUDRouter
from tests import (
Carrot,
CarrotCreate,
CarrotUpdate,
CustomPotato,
PAGINATION_SIZE,
Potato,
DefaultFactoryPotato,
PotatoType,
POTATO_TAGS,
CUSTOM_TAGS,
config,
)
DSN_LIST = [
"sqlite:///./test.db?check_same_thread=false",
# config.MSSQL_URI,
config.POSTGRES_URI,
]
def _setup_database(db_uri: str = DSN_LIST[0]):
if database_exists(db_uri):
drop_database(db_uri)
create_database(db_uri)
database = databases.Database(db_uri)
engine = create_engine(db_uri)
return engine, database
def databases_implementation(db_uri: str):
engine, database = _setup_database(db_uri)
metadata = MetaData()
potatoes = Table(
"potatoes",
metadata,
Column("id", Integer, primary_key=True),
Column("thickness", Float),
Column("mass", Float),
Column("color", String),
Column("type", String),
)
defaultfactorypotatoes = Table(
"defaultfactorypotatoes",
metadata,
Column("id", String, primary_key=True),
Column("color", String),
Column("mass", Float),
)
carrots = Table(
"carrots",
metadata,
Column("id", Integer, primary_key=True),
Column("length", Float),
Column("color", String),
)
metadata.create_all(bind=engine)
app = FastAPI()
@app.on_event("startup")
async def startup():
await database.connect()
@app.on_event("shutdown")
async def shutdown():
await database.disconnect()
router_settings = [
dict(
database=database,
table=potatoes,
schema=Potato,
prefix="potato",
paginate=PAGINATION_SIZE,
),
dict(
database=database,
table=defaultfactorypotatoes,
schema=DefaultFactoryPotato,
prefix="defaultfactorypotato",
tags=POTATO_TAGS,
paginate=PAGINATION_SIZE,
),
dict(
database=database,
table=carrots,
schema=Carrot,
create_schema=CarrotCreate,
update_schema=CarrotUpdate,
prefix="carrot",
tags=CUSTOM_TAGS,
),
]
return app, DatabasesCRUDRouter, router_settings
def databases_implementation_custom_ids():
engine, database = _setup_database()
metadata = MetaData()
potatoes = Table(
"potatoes",
metadata,
Column("potato_id", Integer, primary_key=True),
Column("thickness", Float),
Column("mass", Float),
Column("color", String),
Column("type", String),
)
metadata.create_all(bind=engine)
app = FastAPI()
@app.on_event("startup")
async def startup():
await database.connect()
@app.on_event("shutdown")
async def shutdown():
await database.disconnect()
potato_router = DatabasesCRUDRouter(
database=database, table=potatoes, schema=CustomPotato
)
app.include_router(potato_router)
return app
def databases_implementation_string_pk():
engine, database = _setup_database()
metadata = MetaData()
potato_types = Table(
"potato_type",
metadata,
Column("name", String, primary_key=True),
Column("origin", String),
)
metadata.create_all(bind=engine)
app = FastAPI()
@app.on_event("startup")
async def startup():
await database.connect()
@app.on_event("shutdown")
async def shutdown():
await database.disconnect()
potato_router = DatabasesCRUDRouter(
database=database,
table=potato_types,
schema=PotatoType,
create_schema=PotatoType,
)
app.include_router(potato_router)
return app