Sorry for the late response here. You're right, I think it would probably require writing to the global object, which is definitely a pattern I like to avoid. However, for database backed apps, I think it is normal to have the data defined as a global object. You would not necessarily need to redefine the
conn object in
reload_data_endpoint (since it is already defined globally). Even though Shiny is very different, you might have a look around the Shiny ecosystem for this type of pattern, since Shiny and Plumber do share some similar architectures (i.e. the global object, persistent connections, etc.).
One note: I would recommend using the
pool package as an abstraction on top of the database connection and to beware connection timeouts (since you will be leaving this connection open), which I have hit before but not taken much time to think about.
Also, I am excusing several bad practices in your pseudo code That's what pseudo code is for, right!?