n00b Q: next-on-pages + D1 binding

i have seen some conversations and examples on discord, but still running into some mistakes, 1. if i try to access a d1 binding using the suggested dev approach (watch build + wrangler pages dev), i am able to access the d1 instance (the instance is not undefined and the dev command shows a binding to the d1 db created from the gui), but not view the table (e.g., wrangler crashes saying table does not exist, but i can confirm it exists by using the CLI), i think i am misunderstanding how wrangler pages links to the d1 db, i.e., does the dev command create a local one or is there a flag to pass? 2. when using the npm run dev + cf-proxy workflow, i am able to access kv, but not d1, e.g., it returns undefined when wrapping it: binding(process.env.db), if there is any documentation or an example repo please do not hesitate to simply link me there! the KV repo example was super informative, not sure if there is anything else like that
6 Replies
James
Jamesā€¢10mo ago
Hey there, is binding(process.env.db) the code you are using for the proxy? The correct syntax with cf-bindings-proxy is binding('BINDING_NAME'), e.g. binding('db'). That might be why you're running into issues
Donnager
Donnagerā€¢10mo ago
omgggg that explains so much, let me test this out! thank you for the quick reply as well sir! thanks again that worked!! after testing using that workflow, i realized i can just seed the database using the available APIs from the code, so is the ideal way to use this to create a script that i can run from within the codebase, whereas wrangler is for the alternative workflow? (i deleted the d1 db from wrangler and the data still persists from the proxy so they are completely separate as i understand it now) if there is a doc to read don't hesitate to point me there and sorry for the n00b Qs! also if anyone has any ideas super open to any feedback!
James
Jamesā€¢10mo ago
The proxy just runs an instance of wrangler pages dev, so it's the same as when you would normally use wrangler. If your data is still there, that would suggest that it wasnt actually deleted. The binding proxy is intended for use with next dev so you get a better DX with the nextjs dev server instead of waiting for a build with each change, and should function how you would expect bindings to function in production. I didn't really write any docs for cf-bindings-proxy since it just spawns an instance of wrangler, so I'd recommend checking out the cloudflare pages docs or wrangler docs for specific things In next dev, the binding function just deconstructs your function call and sends it to the proxy where it executes it and sends the response back
Donnager
Donnagerā€¢10mo ago
ic fascinating, okay when i do wrangler d1 list it now longer shows after doing wrangler d1 delete ${name}, but i think this highlights i have something misconfigured, will definitely read up on the cli now! ahhh i probably would have the same result with the build workflow, i just tested the bindings one since its so much faster! ic, makes sense (as far as my n00biness allows hehe), thank you for the explanations!
James
Jamesā€¢10mo ago
No worries šŸ™‚
Donnager
Donnagerā€¢10mo ago
i can't find a way to adjust the tags to solved for this question, but thank you again! if there is a way or if there is a mod, please mark it as resolved!
Want results from more Discord servers?
Add your server
More Posts
How to set domain for preview branch?I can't find the options to set preview domain to `preview.<myapp>.pages.dev`Workers is not working correctly with a render postgres databaseWorkers is working as expected for a local postgres database, but remote render postgres db(even nVSCode Remote with SSH TunnelI have created a tunnel for my domain which I want to use to connect to SSH with rather than the IP find the http response size of a workerwe are using r2 in an worker with sample code from here: https://developers.cloudflare.com/r2/examplPages application deployed, can access it via pages.dev but custom domain 404'sI have a remix site deployed to twitchroulette2.pages.dev and have https://twitchroulette.net/ pointFrom reading the docs this service seemsFrom reading the docs this service seems most useful for looking at trends and data points at a big github link on tutorial missinghttps://developers.cloudflare.com/workers/tutorials/openai-function-calls-workers/ fwiw the github I need help with cloudflare turnstile widget Crashing on my windows 7this started happening about a week ago, Iā€™m pretty sure it was working fine then all of a sudden foProtected web socket connection dies after 30 secs - Cloudflare IPv6 issueHello, I've been recently developing a web application in my spare time for the past few months. AfPages+custom domain+access: public on main, private on previewIt's a basic use case that I am surprised Cloudflare hasn't thought about. * I use Cloudflare Pages