FineSQL ORM¶
FineSQL is a lightweight Python ORM built on top of sqlite3
for educational purposes.
It provides simple table definitions, CRUD operations, and foreign key support while keeping the codebase minimal and easy to understand.
Installation¶
FineSQL requires only standard Python libraries and can be installed easily.
$ pip install finesql
---> 100%
Successfully installed finesql
$ uv add finesql
---> 100%
Successfully installed finesql
Quick Start¶
1. Define Tables¶
from finesql import Database, Table, Column, ForeignKey
class User(Table):
username = Column(str)
age = Column(int)
class Post(Table):
title = Column(str)
body = Column(str)
author = ForeignKey(User)
2. Initialize Database¶
db = Database("app.db")
db.create(User)
db.create(Post)
3. Create Records¶
# Create users
alice = User(full_name="Alice Johnson", age=25)
db.save(alice)
robert = User(full_name="Robert Smith", age=30)
db.save(robert)
# Create a post
post = Post(title="Hello World", body="This is my first post", author=robert)
db.save(post)
4. Query Records¶
# Get all users
users = db.all(User)
print(users)
# Get by id
user1 = db.get(User, id=1)
print(user1)
# Filter by full_name (reuse first name)
alice = db.get_by_field(User, field_name="full_name", value="Alice Johnson")
print(alice)
robert = db.get_by_field(User, field_name="full_name", value="Robert Smith")
print(robert)
5. Update Records¶
alice = db.get(User, id=1)
alice.age = 26
db.update(alice)
6. Delete Records¶
db.delete(User, id=1)
Relationships¶
Foreign keys can be defined using ForeignKey
.
For example, Post
has an author = ForeignKey(User)
.
When fetching posts, the related User
instance will be automatically resolved:
post = db.get(Post, id=1)
print(post.author)