Flask-SQLORM
Flask integration for sqlorm
Setup
Install:
$ pip install flask-sqlorm
Setup:
from flask import Flask
from flask_sqlorm import FlaskSQLORM
app = Flask()
db = FlaskSQLORM(app, "sqlite://:memory:")
Usage
All exports from the sqlorm package are available from the extension instance.
Define some models:
class Task(db.Model):
id: db.PrimaryKey[int]
title: str
done: bool = db.Column(default=False)
A session is automatically started everytime an app context is created. Perform queries directly in your endpoints:
@app.route("/tasks")
def list_tasks():
tasks = Task.find_all()
return render_template("tasks.html", tasks=tasks)
The session is rollbacked at the end of the request.
To commit some data, start a transaction using the db object:
@app.route("/tasks", methods=["POST"])
def create_task():
with db:
task = Task.create(title=request.form["title"])
return render_template("task.html", task=task)
The current session is available using db.session
Additional utilities provided by Flask-SQLORM
Model classes have the additional methods:
find_one_or_404: same asfind_onebut throw a 404 when no results are returnedget_or_404: same asgetbut throw a 404 when no results are returned
Managing the schema
Some CLI commands are available under the db command group. Check out flask db --help for a list of subcommands.
SQLite defaults
If using an SQLite database, the following settings will be applied:
- foreign_keys are ON
- fine tuning for web workloads
- the database directory will be created if missing
These settings are provided by the SQLORM SQLite driver.
Configuration
Configure the sqlorm engine using the extension's constructor or init_app(). Configuration of the engine is performed using the URI method.
Additional engine parameters can be provided as keyword arguments.
Configuration can also be provided via the app config under the SQLORM_ namespace. Use SQLORM_URI to define the database URI.
Using multiple engines
You can setup multiple engines via the config and use an EngineDispatcher to select an engine to use.
db = FlaskSQLORM(app, "sqlite://:memory:", alt_engines=[{"uri": "sqlite://:memory", "tags": ["readonly"]}])
with db.engines.readonly:
# Execute on an engine randomly selected from the one matching the readonly tag
with db.engines:
# Uses the default engine
The context can be used inside other contexts:
@app.route()
def endpoint():
objs = MyModel.find_all() # uses the default engine (in a non commit transaction)
with db.engines.master: # uses a random engine matching the master tag (in a committed transaction)
obj = MyModel.create()
with db.engines.readonly.session(): # uses a random engine matching the readonly tag (in a non commit transaction)
objs = MyModel.find_all()
You can create more advanced use case by subclassing EngineDispatcher. For example, to implement selection based on an http header that can be set by a load balancer to use the closest geographic replica. And use the primary server for write.
from sqlorm import EngineDispatcher
class HeaderEngineDispatcher(EngineDispatcher):
def select_all(self, tag=None):
if not tag and self.header and has_request_context() and self.header in request.headers:
return self.select_all(request.headers[self.header])
return super().select_all(tag)
db = FlaskSQLORM(app, "postgresql://primary", engine_dispatcher_class=HeaderEngineDispatcher,
alt_engines=[{"uri": "postresql://replica1", "tags": ["usa"]},
{"uri": "postresql://replica2", "tags": ["europe"]}])
@app.route()
def endpoint():
MyModel.find_all() # execute on engine selected via header
with db:
MyModel.create() # execute on default engine
Metadata
Release files for flask-sqlorm 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| flask_sqlorm-0.4.0.tar.gz | 4.6 kB | Without provenance | Details for flask_sqlorm-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| flask_sqlorm-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for flask_sqlorm-0.4.0-py3-none-any.whl |
Total release size: 10.1 kB
Release files / flask_sqlorm-0.4.0.tar.gz
| Download URL | flask_sqlorm-0.4.0.tar.gz |
|---|---|
| Size | 4.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3443e7784bc0e177848acfd9e9305b40957fdcb9deb5ad19df32845341254a07
|
|
BLAKE2b-256 checksum How to use checksums |
96fe07fb4dbb526c81cd0549035906c7b40e5feced16a23a002250eae7322fc5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / flask_sqlorm-0.4.0-py3-none-any.whl
| Download URL | flask_sqlorm-0.4.0-py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ef66b8a1e2f3ba07930ab2d3e600ea43a0306535ec816372e94acb9610cc14a6
|
|
BLAKE2b-256 checksum How to use checksums |
5b8c34edb4751e7c6c7012ea80a1516ee9512b3fa0016e204d0839aad33f2a05
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for flask-sqlorm 0.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.