Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/anoziechibuike/flasksqlalchemybasemodel
The FlaskSQLAlchemyBaseModel package provides a foundation for building SQLAlchemy-based ORM models in a Flask application.
https://github.com/anoziechibuike/flasksqlalchemybasemodel
flask flask-extensions flask-sqlalchemy orm sqlalchemy
Last synced: about 1 month ago
JSON representation
The FlaskSQLAlchemyBaseModel package provides a foundation for building SQLAlchemy-based ORM models in a Flask application.
- Host: GitHub
- URL: https://github.com/anoziechibuike/flasksqlalchemybasemodel
- Owner: AnozieChibuike
- License: mit
- Created: 2024-06-05T22:28:46.000Z (7 months ago)
- Default Branch: master
- Last Pushed: 2024-06-07T14:31:56.000Z (7 months ago)
- Last Synced: 2024-11-12T17:03:09.860Z (about 1 month ago)
- Topics: flask, flask-extensions, flask-sqlalchemy, orm, sqlalchemy
- Language: Python
- Homepage: https://pypi.org/project/FlaskSQLAlchemyBaseModel/
- Size: 82 KB
- Stars: 16
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![PyPI Version](https://img.shields.io/pypi/v/FlaskSQLAlchemyBaseModel.svg)](https://pypi.org/project/FlaskSQLAlchemyBaseModel/)
[![Python Versions](https://img.shields.io/pypi/pyversions/FlaskSQLAlchemyBaseModel.svg)](https://pypi.org/project/FlaskSQLAlchemyBaseModel/)
[![License](https://img.shields.io/pypi/l/FlaskSQLAlchemyBaseModel.svg)](https://github.com/AnozieChibuike/FlaskSQLAlchemyBaseModel/blob/master/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`:
```bash
pip install FlaskSQLAlchemyBaseModel
```## Usage
### Initialize with your flask app
```python
from flasksqlalchemybasemodel import dbapp = Flask(__name__) # or you can replace with your flask instance
yourdb = db.init_app(app)
```### Import and use the BaseModel with your models
```python
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
```python
db.session.save()
db.session.commit()
```Example:
```python
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="[email protected]")
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()
db.session.commit()
```
Example:```python
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="[email protected]")
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:
```python
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="[email protected]")
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": , # Time instance was created
"updated_at": , # Time instance was updated last
"name": "John Doe",
"email": "[email protected]"
}
"""
```
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](https://mit-license.org/) file for details.
## Acknowledgments
- Inspired by [Al-Areef](https://github.com/NUCCASSJNR)
## Contact
- Anozie Joel
- Email: [email protected]
- GitHub: [Anozie Chibuike](https://github.com/AnozieChibuike)