planetscale/database-js

Doesn't work out of the box with cloudflair workers

i-Welch opened this issue ยท 3 comments

Problem Encountered
When using the package in a cloudflair worker you will get an error message reading
The 'cache' field on RequestInitializerDict' is not implemented
This is due to the packages use of that cache option in the fetch call options.

Work Around
You can override the fetch request with your own implementation and omit the cache option which then fixes this issue. I've also experimented with removing that option myself from the library locally and it seems to work.

Sought Resolution
It would be pretty trivial to open a PR which removes the cache option from the request but I'd imagine that it was put there for a good reason. I was hoping there would be a way to maintain all the nice things which the fetch request built into the library does for us while also being able to configure the cache option so that this package could work either out of the box or with much simpler configuration with cloudflair workers.

Propose solutions

  • One solution could be to allow the user to override that option via a config with a default the same as we have today. I believe allowing the user to set that to undefined would at least provide some resolution to the issue. However the knowledge that one would even need to do that would probably be fairly esoteric and hard to find. Maybe we could craft a special error message to notify the users in cases like this, but I'm not sure whether that's possible in this specific case.
  • Another solution is to expose a function called CloudFlairRequestFunction which can be easily slotted in to the request function field on the existing API. This somehow feels better to me than the first option but not sure why.
  • Intelligently detect if being run in a cloudflair environment and omit the cache value in that case. It would be the most magical for the user but would introduce both different functionality depending on where the code is run (in this case it would be minimal since the outcome of setting cache: none and making requests on cloudflair results in the same outcome (nothing being cached)) and we will need to track if cloudflair ever adds that functionality and respond accordingly in this code base ๐Ÿคฎ.
  • Decide that the existing API workaround is good enough and let users just deal with it on their own. In this case I would at least suggest adding an aside about this somewhere in the README.md and also adding an example in the examples folder so users can just copy paste the implementation from there.

I'm more than happy to open the PR for this myself but wanted to get some feedback from the team before trying to and wasting effort.

For anyone coming across this here is the code snippet that fixed it for me

const conn = connect({
	host: env['DB_HOST'],
	username: env['DB_USER'],
	password: env['DB_PASS'],
	fetch: (opts, init) => fetch(new Request(opts, { ...init, cache: undefined })),
});

Thanks for posting your solution!

cc #102