Skip to main content

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 Python 3.8 or higher
  • SQLAlchemy SQLAlchemy 2.0.23
  • PostgreSQL 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:

  1. Install crudalchemy packages
    pip install git+https://github.com/Seuleeee/sqlalchemy-crud-base
    
  2. Get a DB session

    :warning: ** Ensure that you have loaded the .env file using load_env before proceeding with the following steps.**

  3. Create a CRUD object
  4. Apply CRUD operations

example.py

1. Create a CRUD object

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)

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

3. Read

  • Single Object
    single_model = example_crud.get(db, created_model.id)
    
  • Multiple Objectg
    multiple_model = example_crud.get_multi(db)
    

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)

5. Delete

delete_record_id = updated_model.id
deleted_record = example_crud.remove(db, delete_record_id)

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlalchemy-crud-base-0.1.2.tar.gz (4.8 kB view details)

Uploaded Source

File details

Details for the file sqlalchemy-crud-base-0.1.2.tar.gz.

File metadata

  • Download URL: sqlalchemy-crud-base-0.1.2.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for sqlalchemy-crud-base-0.1.2.tar.gz
Algorithm Hash digest
SHA256 569e3a2e73e296b6613b53bc7d0151230c65dffa182846f8a1e5f3367678eee1
MD5 72dfbb561ad8aca6b520f5ac59e9af5a
BLAKE2b-256 6e9f07ae9a805695488dd2206fd8f6a6e8e3fcff7d687cd310f306daba200871

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