Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Kristian Klausen
aurweb
Commits
c77e9d1d
Commit
c77e9d1d
authored
Jun 08, 2020
by
Frédéric Mangano-Tarumi
Committed by
Lukas Fleischer
Feb 20, 2021
Browse files
Integrate SQLAlchemy into FastAPI
Signed-off-by:
Lukas Fleischer
<
lfleischer@archlinux.org
>
parent
a5554c19
Changes
1
Show whitespace changes
Inline
Side-by-side
aurweb/db.py
View file @
c77e9d1d
...
...
@@ -10,6 +10,8 @@ except ImportError:
import
aurweb.config
engine
=
None
# See get_engine
def
get_sqlalchemy_url
():
"""
...
...
@@ -38,6 +40,34 @@ def get_sqlalchemy_url():
raise
ValueError
(
'unsupported database backend'
)
def
get_engine
():
"""
Return the global SQLAlchemy engine.
The engine is created on the first call to get_engine and then stored in the
`engine` global variable for the next calls.
"""
from
sqlalchemy
import
create_engine
global
engine
if
engine
is
None
:
engine
=
create_engine
(
get_sqlalchemy_url
(),
# check_same_thread is for a SQLite technicality
# https://fastapi.tiangolo.com/tutorial/sql-databases/#note
connect_args
=
{
"check_same_thread"
:
False
})
return
engine
def
connect
():
"""
Return an SQLAlchemy connection. Connections are usually pooled. See
<https://docs.sqlalchemy.org/en/13/core/connections.html>.
Since SQLAlchemy connections are context managers too, you should use it
with Python’s `with` operator, or with FastAPI’s dependency injection.
"""
return
get_engine
().
connect
()
class
Connection
:
_conn
=
None
_paramstyle
=
None
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment