Skip to content

feat: add Attribute base class for custom model casts and sqlite cast tests - #57

Merged
tmgbedu merged 5 commits into
mainfrom
feat/model-casts-attribute
May 18, 2026
Merged

feat: add Attribute base class for custom model casts and sqlite cast tests#57
tmgbedu merged 5 commits into
mainfrom
feat/model-casts-attribute

Conversation

@tmgbedu

@tmgbedu tmgbedu commented May 18, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add Attribute(BaseModel) to caster.py — a reusable mixin that turns any Pydantic model into a custom ORM cast. Subclasses get JSON serialization/deserialization for free via get/set classmethods backed by model_dump_json
  • Export Attribute from fastapi_startkit.masoniteorm so users can import it directly
  • Fix get_attributes_for_insert to apply set casts to _attributes, allowing Address instances and dicts to be passed directly to create() without manual JSON serialization
  • Add preferences (dict/list) and address (Attribute subclass) columns to the users fixture table with seeder and model updates
  • Add test_sqlite_model_casts.py with 11 tests covering int, str, bool, dict, list, and custom Pydantic Attribute casts — including insert with an instance and a dict

Usage

from fastapi_startkit.masoniteorm import Attribute

class Address(Attribute):
    street: Optional[str] = None
    city: Optional[str] = None

class User(Model):
    address: Address  # wired up automatically

# All three forms work on insert
await User.create({"address": Address(street="123 Main St", city="Sydney")})
await User.create({"address": {"street": "123 Main St", "city": "Sydney"}})

Test plan

  • All 11 tests in test_sqlite_model_casts.py pass
  • Existing sqlite test suite unaffected

🤖 Generated with Claude Code

tmgbedu and others added 5 commits May 18, 2026 00:08
… tests

- Add `Attribute(BaseModel)` to caster.py as a reusable mixin for custom
  ORM casts — subclasses get JSON serialize/deserialize for free via
  `get`/`set` classmethods backed by Pydantic's model_dump_json
- Export `Attribute` from `fastapi_startkit.masoniteorm`
- Fix `get_attributes_for_insert` to apply `set` casts to `_attributes`
  so `Address` instances and dicts can be passed directly to `create()`
  without manual JSON serialization
- Add `preferences` (dict/list) and `address` (Attribute) columns to the
  users fixture table; seed and model updated accordingly
- Add `test_sqlite_model_casts.py` covering int, str, bool, dict, list,
  and custom Pydantic Attribute casts including insert with instance/dict

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@tmgbedu
tmgbedu merged commit f1619d9 into main May 18, 2026
0 of 3 checks passed
@tmgbedu
tmgbedu deleted the feat/model-casts-attribute branch May 18, 2026 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant