-
|
I am building a Wails application with a simple persistence layer, and I was wondering whether there is a best practice in the Wails world on whether to manage connections using global vars or if one should add them to the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Hi @eslksw! I'm Dosu and I’m helping the wails team. There isn’t an official Wails-specific best practice for managing DB connections, but the recommended approach is to add your DB connection (or persistence layer) as a field on your App struct. Initialize it in the App’s startup method and clean it up in shutdown. This leverages Wails’ lifecycle hooks and keeps your resources encapsulated, avoiding the pitfalls of global variables (like concurrency issues and testability problems). You can then expose DB operations as public methods on your App struct, which are easily bound to the frontend via Wails’ binding mechanism. This pattern is consistent with how Wails expects you to manage other resources and application logic, and is supported by the official docs on application structure and lifecycle methods [source]. In short: prefer encapsulating your DB connection in the App struct over using globals, and manage its lifecycle with startup/shutdown. This keeps your code clean, testable, and idiomatic for Wails. To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
Hi @eslksw! I'm Dosu and I’m helping the wails team.
There isn’t an official Wails-specific best practice for managing DB connections, but the recommended approach is to add your DB connection (or persistence layer) as a field on your App struct. Initialize it in the App’s startup method and clean it up in shutdown. This leverages Wails’ lifecycle hooks and keeps your resources encapsulated, avoiding the pitfalls of global variables (like concurrency issues and testability problems). You can then expose DB operations as public methods on your App struct, which are easily bound to the frontend via Wails’ binding mechanism. This pattern is consistent with how Wails expects you to manage ot…