mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
Bump sqlalchemy to version 2.0.22 There are quite some changes that happened with v2. We are currently relying on the "auto-commit" feature which was removed. For the moment we can use a wrapper class to mimic the auto-commit behavior allowing us to move to v2. Ultimately, the (db) session management needs some overhaul though. Signed-off-by: moson <moson@archlinux.org>
29 lines
686 B
Python
29 lines
686 B
Python
import json
|
|
|
|
from sqlalchemy.orm import declarative_base
|
|
|
|
from aurweb import util
|
|
|
|
|
|
def to_dict(model):
|
|
return {c.name: getattr(model, c.name) for c in model.__table__.columns}
|
|
|
|
|
|
def to_json(model, indent: int = None):
|
|
return json.dumps(
|
|
{k: util.jsonify(v) for k, v in to_dict(model).items()}, indent=indent
|
|
)
|
|
|
|
|
|
Base = declarative_base()
|
|
|
|
# Setup __table_args__ applicable to every table.
|
|
Base.__table_args__ = {"autoload": False, "extend_existing": True}
|
|
|
|
# Setup Base.as_dict and Base.json.
|
|
#
|
|
# With this, declarative models can use .as_dict() or .json()
|
|
# at any time to produce a dict and json out of table columns.
|
|
#
|
|
Base.as_dict = to_dict
|
|
Base.json = to_json
|