igorbenav / fastcrud

FastCRUD is a Python package for FastAPI, offering robust async CRUD operations and flexible endpoint creation utilities.
MIT License
573 stars 34 forks source link

Sync Engine Support #122

Open kdcokenny opened 2 weeks ago

kdcokenny commented 2 weeks ago

Feature Request: Support for Synchronous Database Sessions

Is your feature request related to a problem?

No, this is a new feature proposal.

Proposed Solution

I'd like to discuss options for integrating support for synchronous database sessions. I'm open to implementing this myself, but I'd appreciate opinions on the best approach. Here are some potential solutions I've considered:

  1. Separate Synchronous Classes

    • Create new SyncFastCrud class and sync_crud_router instance
    • Drawback: This would introduce significant code redundancy
  2. Dual Function Approach (Similar to Langchain)

    • Introduce separate functions for sync and async operations
    • Example: create for sync and acreate for async
    • Implementation: Check if the passed db is a Session or AsyncSession
  3. Artificial Session Wrapper

    • Create a wrapper to convert sync Sessions into AsyncSessions
    • Use run_in_threadpool (which FastAPI would do internally for the previous two proposals)

I'm open to other ideas and would appreciate any feedback or alternative suggestions.

igorbenav commented 2 weeks ago

I like this feature. I need to think a bit about the possibilities before discussing best approaches

kdcokenny commented 2 weeks ago

Yes, I agree. I think the most "FastAPI" solution would be to keep the entire sync process synchronous as fastapi will automatically run that in the thread pool and will ensure that no extra "magic" is occurring behind the hood.