PlaidWeb / Publ

Flexible publishing system for the web
http://publ.beesbuzz.biz/
MIT License
40 stars 4 forks source link

Remove global database model #391

Open fluffy-critter opened 4 years ago

fluffy-critter commented 4 years ago

ponyorm has a pattern for encapsulating the database binding, which is incredibly obvious in hindsight: https://github.com/ponyorm/pony/issues/330#issuecomment-470153594

tl;dr: encapsulate model's classes in a closure, something like:

def build_model():
    db = orm.Database()
    class GlobalConfig(db.Entity):
        #...

    class Model:
        def __init__(self):
            #pylint:disable=invalid-name
            self.GlobalConfig = GlobalConfig
            # ....
        def setup(self, config):
            # ...
        def reset(self):
            #...
    return Model()

And then the Model object which is returned gets attached to the Publ instance, which might also solve #134 since the model will persist as long as the app does.

Originally posted by @fluffy-critter in https://github.com/PlaidWeb/Publ/issues/113#issuecomment-636516531

fluffy-critter commented 4 years ago

I've tried implementing this a few times, and unfortunately this causes some other trickiness; type annotations get fouled up, for example, and when properties need to look up things on other types it gets really convoluted.

There might be a better pattern for doing this encapsulation, which is always seeming more necessary to improve the unit testing.