Add crudkit!

This commit is contained in:
Yaro Kasear 2025-08-27 10:27:41 -05:00
parent 30ec29d497
commit 559fd56f33
28 changed files with 881 additions and 23 deletions

22
crudkit/serialize.py Normal file
View file

@ -0,0 +1,22 @@
def serialize(obj, *, fields=None, expand=None):
expand = set(expand or [])
fields = set(fields or [])
out = {}
# base columns
for col in obj.__table__.columns:
name = col.key
if fields and name not in fields:
continue
out[name] = getattr(obj, name)
# expansions
for rel in obj.__mapper__.relationships:
if rel.key not in expand:
continue
val = getattr(obj, rel.key)
if val is None:
out[rel.key] = None
elif rel.uselist:
out[rel.key] = [serialize(child) for child in val]
else:
out[rel.key] = serialize(val)
return out