Avoid hardcoded IDs in the repository
Hi everyone, I'm testing the worker and I have a question:
Is there a way to avoid hardcoding the ID of a D1 in the wrangler.jsonc file to prevent it from being available in a repository, like GitHub?
4 Replies
D1 bindings must be declared at build time.. you can't create or switch them dynamically at runtime. What you could do is have a .gitignore and wrangler.secret.jsonc file, and then specify --config when you're building locally
If you're using CICD you could also do some hacky thing to modify your jsonc/toml prior to invoking Wrangler... but yeah no option that I know of to avoid hardcoding the ID for a D1 binding
I am using the deployment process of the worker through Cloudflare itself. In this case, I think I will create a custom script (something like npm run worker:deploy) that creates or modifies a wrangler.build.jsonc by taking environment variables and then executes the wrangler deploy using the file I created. Thank you very much for the response, Will.
Any reason to not just have them in the repo? They're public IDs, they're ok to be public
The issue is simply to separate what is configuration from the codebase. For example, if I want or need to deploy the same project pointing to another instance of D1, I can do this by just defining an environment variable, without having to change the code or even clone the project to avoid making changes in the first environment that is already live. But maybe it's just an excess of caution from having faced challenges for so many decades as a programmer.
@Walshy, I will use the approach suggested by @texan, but if you have any other ideas or different viewpoints, they are always welcome!