jacebrowning / datafiles

A file-based ORM for Python dataclasses.
https://datafiles.readthedocs.io
MIT License
198 stars 18 forks source link

Support class relationships and foreign keys #305

Open wmusial opened 1 year ago

wmusial commented 1 year ago

datafiles package is great, but it stops short of providing support for object relationships and foreign keys. is that feature anywhere on the horizon? if not, would you be open to discussing it and providing guidance on how to implement it?

jacebrowning commented 1 year ago

That's an interesting idea. Perhaps you could provide some pseudocode of how it might look to use such a feature?

ramiboutas commented 6 months ago

Great package @jacebrowning, congrats!

This request could be done like this:

from dataclasses import dataclass

from datafiles import datafile

@datafile("parents/{self.name}/metadata.yml")
@dataclass
class Parent:
    name: str

@datafile("parents/{self.parent.name}/items/{self.name}/metadata.yml")
@dataclass
class Item:
    name: str
    parent: Parent
    count: int
    available: bool

It works fine for me!