sqloader
A lightweight Python utility for managing SQL migrations and loading SQL from JSON or .sql files.
Supports common relational databases and is designed for simple, clean integration with any Python backend (e.g., FastAPI).
Installation
pip install sqloader
Features
- âś… Easy database migration management
- âś… Load SQL queries from
.json or .sql files
- âś… Supports MySQL and SQLite
- âś… Clean API for integration
- âś… Lightweight and dependency-minimized
Quickstart
from sqloader.init import database_init
config = {
"type": "mysql",
"mysql": {
"host": "localhost",
"port": 3306,
"user": "root",
"password": "pass",
"database": "mydb"
},
"service": {
"sqloder": "res/sql/sqloader/mysql"
},
"migration": {
"auto_migration": True,
"migration_path": "res/sql/migration/mysql"
},
}
db, sqloader, migrator = database_init(config)
query = sqloader.load_sql("user_info", "user.get_user_by_id")
result = db.fetch_one(query, ['abc', 123])
SQL Loading Behavior
- If the value in the .json file ends with .sql, the referenced file will be loaded from the same directory.
- Otherwise, the value is treated as a raw SQL string.
Example JSON file user.json:
{
"user": {
"get_user_by_id": "SELECT * FROM users WHERE id = %s",
"get_all_users": "user_all.sql"
},
"get_etc": "SELECT * FROM etc"
}