The PlanetScale Serverless Driver does not work with TypeORM or Cloudflare Workers out of the box. In order to get it working with these technologies the following changes where made:
When trying to import the PlanetScale driver in the TypeORM codebase the following error is produced: Error [ERR_REQUIRE_ESM]: require() of ES Module not supported
. Using a dynamic import like await import('@planetscale/database')
which is the common soluition online produced the same error. In addition, other tricks to get it working which relied on code generation were not compatible with Cloudflare which does not allow this. In the end the solution was to build this as a CommonJS module rather than an ESM.
Currently Cloudflare Workers does not have the cache
field implemented in the fetch call. The PlanetScale team recently added the cache
field to explicitly disable all caching when interacting with the database. See the following links:
- Issue: planetscale#101
- Change: planetscale#102
- Bug: cloudflare/workerd#698
Once Cloudflare has fixed the bug the cache
field should be added back in. Note I had to add the RequestCache
type to types.ts
for this to work in TypeORM.
export type RequestCache = "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload";
- The driver was not forwarding all of the exceptions which made debugging difficult, so a check was added to throw
exception.cause
if an exception is thrown and it is present.
npm run build
npm test
See the README.md for more information.
- main: Tracks the main branch from upstream:
git@github.com:planetscale/database-js.git
- typeorm-cloudflare: Contains the changes to
@planetscale/database
for compatibility with TypeORM on Cloudflare Workers.
- Checkout
main
and pull the latest upstreammain
. - Create a new
integration
branch off oftypeorm-cloudflare
and merge the changes frommain
. - Fix any conflicts as needed.
- Run the build and the tests to ensure everything seems to be working.
- Merge the changes into
typeorm-cloudflare
.
- Create new release tag
npm test
npm pack
- Upload package to github