Skip to main content

Active Record-style convenience methods (Model.query, save, delete) for SQLModel with Flask-SQLAlchemy.

Project description

sqlmodel-activerecord-mixin

Active Record–style convenience methods for SQLModel when using Flask-SQLAlchemy: Model.query, .save(), and .delete().

Install

pip install sqlmodel-activerecord-mixin

Usage

  1. Create a Flask app and init Flask-SQLAlchemy with SQLModel metadata (this package exposes the same db instance):
from flask import Flask
from sqlmodel_activerecord import db

app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///app.db"
db.init_app(app)
  1. Define models by inheriting from SQLModel, this mixin, and set table=True:
from typing import Optional
from sqlmodel import SQLModel, Field
from sqlmodel_activerecord import ActiveRecordMixin

class Hero(SQLModel, ActiveRecordMixin, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str
    secret_name: str
  1. Use Active Record–style APIs (inside a Flask request or app.app_context()):
# Create and save
hero = Hero(name="Iron Man", secret_name="Tony Stark")
hero.save()

# Query
Hero.query.all()
Hero.query.get(1)
Hero.query.filter_by(name="Iron Man").first()
Hero.query.count()

# Update (mutate then save)
hero.age = 50
hero.save()

# Delete
hero.delete()

Requirements

  • Python >= 3.9
  • Flask, Flask-SQLAlchemy, SQLModel

Development

pip install -e .
python usage_test.py   # run module tests

License

MIT

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

sqlmodel_activerecord_mixin-0.1.0.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlmodel_activerecord_mixin-0.1.0-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

File details

Details for the file sqlmodel_activerecord_mixin-0.1.0.tar.gz.

File metadata

File hashes

Hashes for sqlmodel_activerecord_mixin-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0598040c27b391b7d3ab4a9077fb574520a082ce4f557b43fe4e95d62306bc97
MD5 6bd904408a068a6b1c53ba6a06e92f21
BLAKE2b-256 5f791982cf296f3bd1e596651b24dab8eafe912016817f65c721f84ac275f9a9

See more details on using hashes here.

File details

Details for the file sqlmodel_activerecord_mixin-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlmodel_activerecord_mixin-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dda698d31f4594146336fbca00bdd49111b59bb14584f30b2d20f90687367b31
MD5 1a6970c6c6dd63151757274d59e1ccf0
BLAKE2b-256 3aff990ac125f5f84115cf3b72b88ea77d34afac8ecffb0177e3b0932dbcc12e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page