D1 bindings in Durable Objects

Hi, I’m trying to use D1 in conjunction with Durable Objects and found that the env binding for D1 in a durable object is different form what’s expected.

For example if I have a D1 database bound as MY_DATABASE and with a module worker I will be able to access it via env.MY_DATABASE. However in a durable object env.MY_DATABASE is undefined, instead the database is bound to env.__D1_BETA__MY_DATABASE.

Not sure if this is a known problem with D1 still in alpha but I didn’t find reference to this behaviour anywhere so I thought I mention this here.

1 Like

Just ran into this as well, here is a related github issue: https://github.com/cloudflare/wrangler2/issues/2335 with a possible workaround in case you missed it. Good luck.

This is a result of Wrangler currently needing to inject a shim into your Worker to enable using D1. As you’ve encountered here, it doesn’t do that with Durable Objects yet.

I’d consult the above Github issue for workarounds :+1: