Skip to main content

PyPI Version Python Versions License

Description

FlaskSQLAlchemyBaseModel package provides a foundation for building SQLAlchemy-based ORM models in a Flask application. It offers common functionality required by most models, including automatic handling of unique identifiers, timestamps, and convenience methods for CRUD operations. This package streamlines the development process by offering a consistent and reusable base class for database models, ensuring that every model inherits essential features and reduces boilerplate code.

Key Features

  • UUID Primary Key: Automatically generates a unique identifier for each record.
  • Timestamps: Maintains created_at and updated_at fields for each record, updated automatically.
  • CRUD Operations: Provides methods for saving, deleting, and closing database sessions.
  • Dictionary Representation: Converts model instances to dictionaries, including related objects.
  • Convenience Methods: Includes class methods to retrieve all records, retrieve records by ID, and retrieve records by ID or return a 404 error if not found.

Installation

You can install this package using pip:

pip install FlaskSQLAlchemyBaseModel

Usage

Initialize with your flask app

from flasksqlalchemybasemodel import db

app = Flask(__name__) # or you can replace with your flask instance

yourdb = db.init_app(app)

Import and use the BaseModel with your models

from flasksqlalchemybasemodel import BaseModel # Import the baseModel

# for example a user model
class User(BaseModel):
    __tablename__ = 'users'
    name = db.Column(db.String(50))
    email = db.Column(db.String(100), unique=True)

Using the inherited methods

  • save : Used for saving an item to the database

    This will save you the time of doing

      db.session.save(<instance>)
      db.session.commit()
    

    Example:

    from flasksqlalchemybasemodel import BaseModel # Import the baseModel
    
    # for example a user model
    class User(BaseModel):
        __tablename__ = 'users'
        name = db.Column(db.String(50))
        email = db.Column(db.String(100), unique=True)
    
    user = User(name="John Doe",email="johnddoe@example.com")
    
    user.save() # Saves the instance to your db
    

  • delete : Used for deleting an item from the database

    This will save you the time of doing

    db.session.delete(<instance_that_exists_in_the_db>)
    db.session.commit()
    

    Example:

    from flasksqlalchemybasemodel import BaseModel # Import the baseModel
    
    # for example a user model
    class User(BaseModel):
        __tablename__ = 'users'
        name = db.Column(db.String(50))
        email = db.Column(db.String(100), unique=True)
    
    user = User(name="John Doe",email="johnddoe@example.com")
    
    user.save() # Saves the instance to your db
    
    user.delete() # This then deletes the saved instance
    

  • to_dict : Used for returning the instance as a python dictionary

    Example:

    from flasksqlalchemybasemodel import BaseModel # Import the baseModel
    
    # for example a user model
    class User(BaseModel):
        __tablename__ = 'users'
        name = db.Column(db.String(50))
        email = db.Column(db.String(100), unique=True)
    
    user = User(name="John Doe",email="johnddoe@example.com")
    
    user.save() # Saves the instance to your db
    
    userDict = user.to_dict() 
    print(userDict)
    """
    output
    {
        "id": "Some random uuid", # auto-generated by the package
        "created_at": <datetimeObject>, # Time instance was created
        "updated_at": <datetimeObject>, # Time instance was updated last
        "name": "John Doe",
        "email": "johnddoe@example.com"
    }
    """
    

    And many more methods... look into the source code to get the many methods

Contributing

Contributions are welcome! If you'd like to contribute to this project, please follow these steps:

  1. Fork the repository.
  2. Create a new branch for your feature or bug fix.
  3. Make your changes and test them thoroughly.
  4. Create a pull request with a clear description of your changes.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

Contact

Metadata

Release files for FlaskSQLAlchemyBaseModel 1.2.0

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

Source distribution (sdist)

Source distribution for FlaskSQLAlchemyBaseModel 1.2.0
File Size Uploaded Provenance Actions
flasksqlalchemybasemodel-1.2.0.tar.gz 5.1 kB Without provenance Details for flasksqlalchemybasemodel-1.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for FlaskSQLAlchemyBaseModel 1.2.0
File Interpreter ABI Platform Actions
flasksqlalchemybasemodel-1.2.0-py3-none-any.whl Python 3 none any Details for flasksqlalchemybasemodel-1.2.0-py3-none-any.whl

Total release size: 10.7 kB

Release files / flasksqlalchemybasemodel-1.2.0.tar.gz

Download URL flasksqlalchemybasemodel-1.2.0.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
04a1d09fae48fa03903d0d3aaf110786a39d93d5ace0faa2ce2062b37e583ae9
BLAKE2b-256 checksum
How to use checksums
3055664a2ffab4d1b29f92b8c16bb4bc5a7fd199036f13fcd3e2c881cc65b7cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.11.9 Windows/10

Release files / flasksqlalchemybasemodel-1.2.0-py3-none-any.whl

Download URL flasksqlalchemybasemodel-1.2.0-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6f08d108c8843d46986c330c9f7cb1278a485b211dcea6074372585283c7899b
BLAKE2b-256 checksum
How to use checksums
136edc4b68a19580e4d9f4e576a7d4f68d8a173ef36573c5718c6fbe4e35532c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.11.9 Windows/10

Security for FlaskSQLAlchemyBaseModel 1.2.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.

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