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 that6 Replies
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 issuesomgggg 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!
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 backic 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!No worries 🙂
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!