Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to implement global database connection pool with databases wrapper and set the schema at run time in a schema based multi tenant app #1040

Unanswered
cs-satish-mishra asked this question in Q&A
Discussion options

How can I implement a safe and efficient global database connection pool in a multi-tenant application using the PostgreSQL Schema-based approach, FastAPI, and asyncpg (databases wrapper)? Specifically, I need to dynamically set the appropriate schema in connection or query based on the API subdomain or headers received from the request at runtime. How can I achieve this?

from databases import Database
from sqlmodel import select
# Global databases pool
database = Database(DATABASE_URI, min_size=5, max_size=20)
async def list(db: Database):
 # User is SQLModel
 query = select(User) # how to set the schema dynamically here or make db connection schema aware 
 return await database.fetch_all(query) 

In the above sample code, I want to safely set the intended schema at the time of query but utilizing same global connection pool.

I am aware of sqlalchemy schema translation map but do not know how I can achieve similar with databases wrapper and connection pool.

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /