Using the same database abstraction in Flask and non-Flask

Basically, I am trying to use as much database-level code as possible between the Flask application (for the REST API) and the checkbox-free API.

Is it good to use the same Flask-SQLAlchemy level as in pure Python API (intended for import into non-Python web applications) and in the Flask REST API daemon?

I suggest a different way of saying, although I'm not sure of the terminology: "What is the best way to use a database model between a Flask application and a separate Python import library?"

<h / "> Or from a different angle, does it make sense to use Flask-SQLAlchemy in the Flask REST API if you also want to share the SQL abstraction using the import library. Is it better to use regular SQLAlchemy ?


Use case: we have a large database with many tables, and we want to create both a REST API (for client access) and a Python import library (for our own internal tools) to access the database, but, of course, share the code with it between them as much as possible.

Connected:

+4
source share
1 answer

Using Flask-SQLAlchemy models from a web context involves creating a Flask application and calling

 app.test_request_context().push() 

Here is what you will do with your non web library. If this is not a problem, if the Flask library is installed, when you need to use the library, then there is no problem using it that way.

If you plan to improve the performance of access code for library data, for example, using different sessions, concurrency, etc., you will modify your source code to be a completely different scenario. In this case, a pure-SQLAlchemy approach might be better, but it really depends on the differences between the two templates.

Typically, models come with methods and using 2 different ORM templates ( Flask-SQLAlchemy wrapper models and pure SQLAlchemy ) means duplicate code.

+2
source

Source: https://habr.com/ru/post/1500289/


All Articles