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.
- 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.
You can install this package using pip
:
pip install FlaskSQLAlchemyBaseModel
from flasksqlalchemybasemodel import db
app = Flask(__name__) # or you can replace with your flask instance
yourdb = db.init_app(app)
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)
-
save
: Used for saving an item to the databaseThis 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 databaseThis 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 dictionaryExample:
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
Contributions are welcome! If you'd like to contribute to this project, please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Make your changes and test them thoroughly.
- Create a pull request with a clear description of your changes.
This project is licensed under the MIT License - see the LICENSE file for details.
- Inspired by Al-Areef
- Anozie Joel
- Email: chibuikeanozie0@gmail.com
- GitHub: Anozie Chibuike