Flask extension for working with SQLAlchemy
Project description
Flask-Alchemist
Flask extension for working with SQLAlchemy.
It can be used as an alternative for Flask-SQLAlchemy.
Instead of the scoped session tied to a thread you can instantiate database session on demand with a context manager. The advantage is that when the Flask application handles a request you can precisely control when the session starts and ends.
There is also a Model class with modified MetaData configuration which tells SQLAlchemy how to name indexes and constraints in a database. You can subclass it to define your own model classes.
The Model class provides also show_column method to display information about columns defined in a model (it helps when you want to create database objects in the Flask shell).
Installation
$ pip install Flask-Alchemist
Usage
First create the db object:
from flask_alchemist import Alchemist
db = Alchemist()
Then initialize it using init_app method:
db.init_app(app) # app is your Flask app instance
The Flask app config should have DATABASE_URL key. That is a connection string that tells SQLAlchemy what database to connect to.
Using the db object you can create database session on demand in your views:
with db.Session() as db_session:
user = db_session.get(User, obj_id)
The session is closed when the context manager block ends.
When you want to modify database you can combine two context managers:
with db.Session() as db_session:
with db_session.begin():
db_session.add(obj)
or
with db.Session() as db_session, db_session.begin():
db_session.add(obj)
The session automatically commits (inner context manager) and closes (outer context manager) at the end. If an error occurs inside the inner context manager the session is rolled back.
After defining your User model class you can inspect it in the Flask shell:
>>> User.show_columns()
Column('id', Integer(), table=<users>, primary_key=True, nullable=False)
Column('username', String(length=64), table=<users>, nullable=False)
Column('password_hash', String(length=162), table=<users>)
Column('first_name', String(length=64), table=<users>, nullable=False)
Column('last_name', String(length=64), table=<users>, nullable=False)
Column('email', String(length=64), table=<users>, nullable=False, default=ScalarElementColumnDefault(''))
Column('active', Boolean(), table=<users>, nullable=False, default=ScalarElementColumnDefault(True))
Other features
Alchemist object can be used as a proxy for any attribute of the SQLAlchemy Core and the SQLAlchemy ORM. For example when making SQL queries you can use db.select instead of importing this from SQLAlchemy.
There is also a Pagination class for paging query results (similar to Flask-SQLAlchemy).
License
Flask-Alchemist was created by Rafal Padkowski. It is licensed under the terms
of the MIT license.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flask_alchemist-1.0.2.tar.gz.
File metadata
- Download URL: flask_alchemist-1.0.2.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.10.12 Linux/5.15.0-79-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
170973b4ce494a3ff1ffc22605397b297bb370e68362d99fd3ae68209a281fa3
|
|
| MD5 |
f4529d31f800dbf180d1d2aaccc80280
|
|
| BLAKE2b-256 |
ee36134a912d5052f49d54faa206389896b777828140ac30fb0555ce689c11ec
|
File details
Details for the file flask_alchemist-1.0.2-py3-none-any.whl.
File metadata
- Download URL: flask_alchemist-1.0.2-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.10.12 Linux/5.15.0-79-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
91d55b7d39878d2cc8dfb7c482ab6e722b17466a80850db526cce8cda226e452
|
|
| MD5 |
d9a1682f7f134f030c6c2e94bc2b9096
|
|
| BLAKE2b-256 |
8656c4d0f7a19badb4471987ebaee0da34e94b5490d12e97c7d0cb60ef1e57aa
|