Skip to main content

sqlaqb

  • utility for portable model definition with sqlalchemy

  • black magic? of cource.

what is portable model definition?

sqlalchemy, it is ok that just definition declarative model class depends on another model. (e.g. We have two model, Group and User. User depends on Group Model.)

But,when providing model classes like these as a library, we are thinking about portability.

  • Library User: creating Operator model depends on User. but User class is provided by Library.

  • Library Author: We have convinient User Model, so, share it.

  • (Both of Two): but’s what tablename is this Model?(e.g. User,user,users,mylib_users?)

This package’s motibation is solving such a problem like above.

How to use this?

Library Author code:

#foo/auth.py
import sqlalchemy as sa
from sqlalchemy.orm import relationship
from sqlaqb import (
    ModelCreation,
    ModuleProvider,
    ModelSeed,
    with_tablename,
    with_modelname,
)

creation = ModelCreation()
_provider = ModuleProvider(creation)
create_models = _provider

@creation.register("Group")
class Group(ModelSeed):
    id=sa.Column(sa.Integer, primary_key=True, nullable=False)
    name=sa.Column(sa.String(255), nullable=False)

@creation.register("User", depends=["Group"])
class User(ModelSeed):
    id=sa.Column(sa.Integer, primary_key=True, nullable=False)
    name=sa.Column(sa.String(255), nullable=False)

    @with_tablename("Group")
    def group_id(cls, group_table_name):
        return sa.Column(sa.Integer, sa.ForeignKey("{}.id".format(group_table_name)), nullable=True)

    @with_modelname("Group")
    def group(cls, name):
        return relationship(name)

    def verify_password(self, password):
        return self.password_digest == password

Library User can decide table name provided by library.

Library User code:

from foo.auth import create_models
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
contract = {"User": {"table_name": "users"},
            "Group": {"table_name": "groups", "model_name": "_Group"}}
models = create_models(Base, contract)

models._Group # Group model defined in foo.auth.py
models.User # User model defined in foo.auth.py

more over, also enable to inject dependents model.

Library User code:

from foo.auth import create_models
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
class MyGroup(Base):
    id = sa.Column(sa.Integer, primary_key=True, nullable=False)
    special = sa.Column(sa.String("32"), doc="this is special")
    __tablename__ = "my_groups"

contract = {"User": {"table_name": "users"},
            "Group": {"model": MyGroup}}

models = create_models(Base, contract)

models.Group # Group model define by library user
models.User # User model defined in foo.auth.py

Metadata

Release files for sqlaqb 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sqlaqb 0.0.1
File Size Uploaded Provenance Actions
sqlaqb-0.0.1.tar.gz 5.5 kB Without provenance Details for sqlaqb-0.0.1.tar.gz

Release files / sqlaqb-0.0.1.tar.gz

Download URL sqlaqb-0.0.1.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
34081a0ef5c7e8cd16136193b9f3b9020a188dc6eb4da654de36221174be1f82
BLAKE2b-256 checksum
How to use checksums
07ddac43b668e97a52a53c78ba1a1a924763074ce8b1c26cb1a46d325bb19cbf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for sqlaqb 0.0.1

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.

Release history Release notifications | RSS feed

This release

0.0.1 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page