A SQLAlchemy-based CRUD package
Project description
sqlalchemy-crud-base
Please import it into your project. By defining only the database connection information and Model, it provides CRUD operations using SQLAlchemy.
Tech Stack
Python 3.8 or higher
SQLAlchemy 2.0.23
RDBMS: PostgreSQL
Usage Instructions
1. Environment Setup
Before running the project, create a .env file in your project
and set the following environment variables:
These settings are used to connect to the PostgreSQL/MySQL/MariaDB database.
.env file
# DB_TYPE=postgresql+psycopg2
# DB_TYPE=mysql+pymysql
DB_TYPE=postgresql
DB_NAME={database name}
DB_USER={user name}
DB_PASSWORD={password}
DB_HOST={host ip adress}
DB_PORT={host port number}
Before using, make sure to install the 'python-dotenv' package.
You can set the environment variables as follows:
from dotenv import load_dotenv
# Load environment variables from the specified file
load_dotenv(dotenv_path='.env')
2. Write SqlAlchemy Model schema
The crudalchemy package accepts SQLAlchemy model objects as arguments.
You are free to change the filename and path as you wish.
models.py
from sqlalchemy import (
Column,
Integer,
String,
DateTime,
Sequence,
)
from sqlalchemy.orm import declarative_base
from sqlalchemy.sql import func
Base = declarative_base()
class ExampleModel(Base):
__tablename__ = 'example'
id = Column(Integer, Sequence('example_id_seq'), primary_key=True, index=True)
example_col = Column(String, index=True)
timestamp = Column(DateTime(timezone=True), server_default=func.now(), index=True)
3. Using crudalchemy for CRUD
By following a few simple steps, you can use the SQLAlchemy model you've defined to perform basic CRUD operations:
- Install crudalchemy packages
pip install git+https://github.com/Seuleeee/sqlalchemy-crud-base
- Get a DB session
:warning: ** Ensure that you have loaded the .env file using
load_envbefore proceeding with the following steps.** - Create a CRUD object
- Apply CRUD operations
example.py
from crudalchemy.config.connect import get_db
from crudalchemy.crud import CRUDBase
from .models import ExampleModel
# 1. Get a DB session
db = get_db()
# 2. Create a CRUD object
example_crud = CRUDBase(ExampleModel)
# 3. Create a CRUD object
# 1) Create
# Dummy Data
create_schema = {
"question": "What's your favorite?",
"answer": "BasketBall",
}
create_record = ExampleModel(**create_schema)
created_model = example_crud.create(db, create_record)
print("Created Record:")
print(f"ID: {created_model.id} \n"
f"Question: {created_model.question} \n"
f"Answer :{created_model.answer} \n"
f"TimeStamp: {created_model.timestamp}")
# 2) Read Single Object
single_model = example_crud.get(db, created_model.id)
print(f"Read Single Model!! {single_model}")
# 3) Read Multiple Object
multiple_model = example_crud.get_multi(db)
print(f"Read Multiple Model!! {multiple_model}")
# 4) Update
# Dummy Data
update_schema = {
"question": "What's your favorite programming language?",
"answer": "Python!!!"
}
update_record = ExampleModel(**update_schema)
updated_model = example_crud.update(db, created_model, update_record)
print("Updated Record:")
print(f"ID: {updated_model.id} \n"
f"Question: {updated_model.question} \n"
f"Answer :{updated_model.answer} \n"
f"TimeStamp: {updated_model.timestamp}")
# 5) Delete
delete_record_id = updated_model.id
deleted_record = example_crud.remove(db, delete_record_id)
print("Deleted Record:")
print(f"ID: {deleted_record.id}, Question: {deleted_record.question}, Answer: {deleted_record.answer}")
# 4. Connection Close
db.close()
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contact
For any inquiries or suggestions, please feel free to contact me at bryantjo1224@gmail.com.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file sqlalchemy-crud-base-0.1.1.tar.gz.
File metadata
- Download URL: sqlalchemy-crud-base-0.1.1.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
af0d6fd2fe05dca18af206b3854da0b65926fa83fae98035b365bad7d0fc8462
|
|
| MD5 |
97821fb62757e088d74ed72abd32b197
|
|
| BLAKE2b-256 |
3fcf91ddce01621b6d0bbac3c2e7a1149ce80811f3b96f73eb09460d9aa55927
|