Skip to main content

A handy, small, library to cut down SQLAlchemy builerplates

Project description

Kemampo

Small, Lightweight Library to cut down SQLAlchemy Controllers Boilerplates

Usage

Concept

  1. First define all of your tables, and all things necessary
  2. instead of implementing custom Controllers that handle errors and what not. you can use Kemampo to cut downs all of the Boilerplates. so all you need to do next is: implement sqlalchemy's sessionmaker and pass it into Kemampo intializer
  3. use Kemampo to generate all the basic CRUD controllers.
  4. Use it as is. or add it to your custom Controller class using setattr

Example

Define your table

DB_ENGINE = create_engine(
    "sqlite:///",
    pool_pre_ping=True,
    echo=False,
)

DB_BASE = declarative_base(bind=DB_ENGINE)
metadata = DB_BASE.metadata

class User(DB_BASE):
    __tablename__ = "user"
    id = Column(Integer, primary_key=True)
    username = Column(String(16), nullable=False, unique=True)
    password = Column(String(128), nullable=False)

Use Kemampo

from kemampo import Kemampo

kemampo = Kemampo(sessionmaker(bind=DB_ENGINE))
UserController = kemampo.generate_controller(User)

user_data = {"username": "test_new_user", "password": "a_password_hash"}
update_user_data = {"username": "updated_username"}
status, user = UserController.add(**new_user_data)                  # create new user
status, users = UserController.get(id=1)                            # get by id
status, user = UserController.update_by_id(1, **update_user_data)   # update by id
status, users = UserController.get_all()                            # get all
status, user = UserController.delete(1)                             # delete by id

Installation

You can install via PyPI

python3 -m pip install kemampo

API Documentations

kemampo.Kemampo(session_maker, log_callback)

  • Constructor
  • Methods
    • Kemampo.generate_controller(target_model)
      • The Main feature of kemampo, create a CRUD controller for target_model
      • target_model
        • Expected Type: your table specifically sqlalchemy.ext.declarative.DeclarativeMeta or it's subclass
        • returns <table-name>Controller

<table-name>Controller

The core feature of Kemampo, provide you with basic CRUD Controller Operations

  • Methods
    • add(**table_dict_model)
      • Add new row data to database
      • table_dict_model
        • type: Dict
        • scheme:
          {table_column_name: column_name_value, ...}
          
      • returns Tuple of:
        • (True, added_dict_of_model) or
        • (False, ReturnStatus)
    • get(**target_row_column_key_value)
      • Get rows data from database, based on column name you give with certain value. support multiple column name and value
      • target_row_column_key_value
        • type: Dict
        • scheme:
          {table_column_name: column_name_value, ...}
          
      • returns Tuple of:
        • (True, List[dict_of_model]) or
        • (False, ReturnStatus)
    • get_all()
      • Gather all row values from database
      • returns Tuple of:
        • (True, List[dict_of_model]) or
        • (False, ReturnStatus)
    • update_by_id(target_id, **updated_row_column_key_value)
      • target_id
        • type: int
      • updated_row_column_key_value
        • type: Dict
        • scheme:
          {table_column_name: column_name_value, ...}
          
      • returns Tuple of:
        • (True, dict_of_model) or
        • (False, ReturnStatus)
    • delete(**target_row_column_key_value)
      • target_row_column_key_value
        • type: Dict
        • scheme:
          {table_column_name: column_name_value, ...}
          
      • returns Tuple of:
        • (True, dict_of_model) or
        • (False, ReturnStatus)

ReturnStatus

an Enumeration of possible Errors happening inside Kemampo

  • ReturnStatus.DatabaseError
    • Error happened internally
    • Values: str -> "Database Error"
  • ReturnStatus.NotFound
    • Targeted row data was not found inside database
    • Values: str -> "Row Data Not Found"

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

kemampo-0.0.1.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

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

kemampo-0.0.1-py3-none-any.whl (20.1 kB view details)

Uploaded Python 3

File details

Details for the file kemampo-0.0.1.tar.gz.

File metadata

  • Download URL: kemampo-0.0.1.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for kemampo-0.0.1.tar.gz
Algorithm Hash digest
SHA256 c190bfb0164ecd8000d419a04c7280771acbd47662afe2a9ab9a0fe47f99c71c
MD5 c9cc53e1d8f755b550bd0b5ba36fbfa1
BLAKE2b-256 4679b1a929bb2f8304273891f3a778f2bf46278c0c732b10670660fdc999f78a

See more details on using hashes here.

File details

Details for the file kemampo-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: kemampo-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 20.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for kemampo-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e80b0dceb5518310d1ef4bd2362944f806c36acafbbb61f7f46c879b60815df8
MD5 399e4390a623699693d757257fe8b672
BLAKE2b-256 a571a9e88b591102ac84219d958446823a9ec2ccdd155891d6df9cbe7c25f738

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