Skip to main content

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.

Project description

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.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
    

  • 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

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

flasksqlalchemybasemodel-1.1.3.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

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

flasksqlalchemybasemodel-1.1.3-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file flasksqlalchemybasemodel-1.1.3.tar.gz.

File metadata

  • Download URL: flasksqlalchemybasemodel-1.1.3.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/5.15.146.1-microsoft-standard-WSL2

File hashes

Hashes for flasksqlalchemybasemodel-1.1.3.tar.gz
Algorithm Hash digest
SHA256 6429751ae39580065bcd9480b2d29779f244ca5a6fc8b04412e36237c8ba02fb
MD5 0bbbfa629ffae8ef55bbba920b6054f8
BLAKE2b-256 ce2d549d4680b90175e6730a7e2fba62bf39b3bddd04fa32e1b84123a68634b2

See more details on using hashes here.

File details

Details for the file flasksqlalchemybasemodel-1.1.3-py3-none-any.whl.

File metadata

  • Download URL: flasksqlalchemybasemodel-1.1.3-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/5.15.146.1-microsoft-standard-WSL2

File hashes

Hashes for flasksqlalchemybasemodel-1.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 de66e7f31a6f2b4ff0c944694fdd33a654b6edc6e39403783baef7ba5bb0817f
MD5 10b39f95746f542de8a1befc226b2cb4
BLAKE2b-256 82960697126f88226ef128268e5d6c8ce8ec17110cc0ec7c9c981e55f95da612

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