๐Ÿ—ƒ๏ธ SQL Helpers

Comprehensive SQL helper library for database operations with multi-database support.

๐ŸŽฏ Overview

This module provides a batteries-included SQL toolkit for building database-heavy applications:

Category Functions Purpose
๐Ÿ” Query Registry run_id, validate_params Execute queries by ID from centralized registries
โž• Insert-Only insert_only, bulk_insert_only Insert new records, skip conflicts
๐Ÿ”„ Upsert upsert, bulk_upsert Insert or update existing records
๐Ÿ“ CRUD get_by_id, update_record, delete_record, bulk_delete Standard database operations
๐Ÿ”ง Utilities with_transaction, paginate_sql, batch_execute Transaction management & helpers
๐Ÿ’ฐ Money to_cents, from_cents Currency conversion for int-only storage

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                     SQL Utilities Layer                         โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  ๐Ÿ” Query Registry        โ”‚  Execute queries by ID              โ”‚
โ”‚  โž• Insert Operations     โ”‚  insert_only, bulk_insert_only      โ”‚
โ”‚  ๐Ÿ”„ Upsert Operations     โ”‚  upsert, bulk_upsert                โ”‚
โ”‚  ๐Ÿ“ CRUD Operations       โ”‚  get, update, delete (single/bulk)  โ”‚
โ”‚  ๐Ÿ”ง Utilities             โ”‚  transactions, pagination, batching โ”‚
โ”‚  ๐Ÿ’ฐ Money Helpers         โ”‚  cents โ†” dollars conversion         โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  DB_TYPE Detection: PostgreSQL / SQLite (auto-switch syntax)    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                              โ”‚
                              โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚              fastsql Database Connection                        โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“š Quick Reference

Conflict Handling

Operation On Conflict Use Case
insert_only Skip (DO NOTHING) Append-only logs, idempotent imports
upsert Update existing Sync external data, refresh caches

Performance Tips

Scenario Recommended Why
Single record insert_only, upsert Simple, immediate
10+ records bulk_* variants 10-100x faster
100+ records batch_execute Commits per batch, memory-safe
Multi-table changes with_transaction All-or-nothing commits

๐Ÿ” Database Type Detection

Automatically detects PostgreSQL vs SQLite from DB_TYPE environment variable to generate appropriate SQL syntax.


source

get_db_type


def get_db_type(
    
):

Get database type from environment variable


๐ŸŽฏ Query Registry

Execute queries by ID from a centralized registry with automatic parameter validation.

Function Purpose
run_id Execute a registered query by its ID
validate_params Ensure all :param placeholders have values

source

run_id


def run_id(
    db:Database, registry:Dict, query_id:str, params:Optional=None
)->Any:

Execute a query by ID from a query registry.


source

validate_params


def validate_params(
    sql:str, params:Dict
)->None:

Validate that all required parameters are provided


โž• Insert-Only

Insert new records only if they donโ€™t exist โ€” conflicts are silently skipped.

Function Records SQL Generated
insert_only Single ON CONFLICT DO NOTHING (PG) / INSERT OR IGNORE (SQLite)
bulk_insert_only Multiple Same, with batch execution

๐Ÿ’ก Use case: Idempotent imports, append-only audit logs, webhook deduplication


source

bulk_insert_only


def bulk_insert_only(
    db:Database, table_name:str, records:List, conflict_cols:List, auto_commit:bool=True
)->None:

Insert multiple records, skipping conflicts (optimized batch operation).


source

insert_only


def insert_only(
    db:Database, table_name:str, record:Dict, conflict_cols:List, auto_commit:bool=True
)->None:

Insert a single record only if it doesnโ€™t exist (ignores conflicts).


๐Ÿ”„ Upsert

Insert new records or update existing ones on conflict.

Function Records SQL Generated
upsert Single ON CONFLICT DO UPDATE (PG) / INSERT OR REPLACE (SQLite)
bulk_upsert Multiple Same, with batch execution

๐Ÿ’ก Use case: Syncing external API data, refreshing caches, user settings


source

bulk_upsert


def bulk_upsert(
    db:Database, table_name:str, records:List, conflict_cols:List, update_cols:Optional=None, auto_commit:bool=True
)->None:

Insert or update multiple records (optimized batch operation).


source

upsert


def upsert(
    db:Database, table_name:str, record:Dict, conflict_cols:List, update_cols:Optional=None, auto_commit:bool=True
)->None:

Insert a record or update if it exists (upsert).


๐Ÿ“ CRUD

Standard Create, Read, Update, Delete operations.

Function Operation Description
get_by_id Read Fetch single record by primary key
update_record Update Modify record fields by ID
delete_record Delete Remove single record by ID
bulk_delete Delete Remove multiple records by ID list

source

bulk_delete


def bulk_delete(
    db:Database, table_name:str, id_list:List, id_col:str='id', auto_commit:bool=True
)->None:

Delete multiple records by ID list.


source

delete_record


def delete_record(
    db:Database, table_name:str, id_value:Any, id_col:str='id', auto_commit:bool=True
)->None:

Delete a single record by ID.


source

update_record


def update_record(
    db:Database, table_name:str, id_value:Any, id_col:str='id', auto_commit:bool=True, updates:VAR_KEYWORD
)->None:

Update a single record by ID.


source

get_by_id


def get_by_id(
    db:Database, table_name:str, id_value:Any, id_col:str='id'
)->Any:

Get a single record by ID.


๐Ÿ”ง Utilities

Transaction management, pagination, and batch processing.

Function Purpose
with_transaction Context manager for atomic operations
paginate_sql Add LIMIT/OFFSET to queries
batch_execute Process large lists in memory-safe chunks

source

batch_execute


def batch_execute(
    db:Database, operation_func, items:List, batch_size:int=100
)->None:

Execute an operation on items in batches with commits after each batch.


source

paginate_sql


def paginate_sql(
    sql:str, page:int, page_size:int
)->str:

Add LIMIT/OFFSET pagination to a SQL query.


source

with_transaction


def with_transaction(
    db:Database
):

Context manager for safe transaction handling with auto-rollback on error.


๐Ÿ’ฐ Money Helpers

Convert between dollar amounts and integer cents for database storage.

โš ๏ธ Why cents? fastsql only supports int and str types โ€” storing money as cents avoids floating-point precision issues.

Function Direction Example
to_cents "$150.00" โ†’ 15000 Store in DB
from_cents 15000 โ†’ "$150.00" Display to user

source

to_cents


def to_cents(
    dollars:str | float | None
)->int | None:

Convert dollar amount to integer cents for database storage.


source

from_cents


def from_cents(
    cents:int | None
)->str:

Convert integer cents to formatted dollar string for display.