-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement D1Database
using D1JS and D1 API
#480
Conversation
Previously, Miniflare had its own implementations of `BetaDatabase` and `Statement`. These were subtly different to the implementations in the D1 Wrangler shim D1JS, causing behaviour mismatches. This change switches the implementation to use the shim, with Miniflare implementing the underlying D1 HTTP API instead. We'll need to do this anyway when adding D1 support to Miniflare 3 using `workerd`. Specific changes: - Throw when calling `D1PreparedStatement#run()` with statements that return data, closes #441 - Fix response envelope format, closes #442 and cloudflare/workers-sdk#2504 - Fix binding/return of `BLOB`-typed values, closes wrangler2#2527 - Fix `D1Database#raw()` return, closes cloudflare/workers-sdk#2238 (already fixed in #474) - Add support for `D1Database#dump()` - Run `D1Database#{batch,exec}()` statements in implicit transaction - Only run first statement when calling `D1PreparedStatement#{first,run,all,raw}()`
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh, so you're still expecting Wrangler not to shim the binding (see https://github.com/cloudflare/wrangler2/blob/main/packages/wrangler/templates/d1-beta-facade.js#L217-L219) but then you're having Miniflare use the shim code to generate the right API?
That should work, but you might like to consider changing Wrangler's behaviour to always inject the shim, and then Miniflare's D1 only needs to export the D1DatabaseAPI
with its .fetch
method.
I'm not sure if your testing story would be worse in that case, though, so maybe it's better to keep it as it is... In fact, we do want the Wrangler shim to disappear eventually (which is why I built the Miniflare stuff the way I did), so this is probably the right approach.
Approved.
Yeah, the reason I did it this way is so users can continue to run Miniflare standalone (with Miniflare's CLI or in the testing environments) without having to bundle with Wrangler first. This would be a breaking change otherwise. For Miniflare 3, we'll probably switch to Wrangler doing the shimming, or if you're planning to remove that, none at all. 👍 |
@mrbbot
|
i added these lines to one of the tests (to make sure it's not my code):
and the test fails
|
`better-sqlite3` expects parameters of the form `?1, ?2, ...` to be bound as an object of the form `{ 1: params[0], 2: params[1], ...}`. In #480, we accidentally removed the code that handled this case. This PR adds it back, and lifts out some common functionality into a `#prepareAndBind()` function. :) Thanks @ruslantalpa for spotting the removed code. Closes #526 Closes cloudflare/workers-sdk#2811 Closes cloudflare/workers-sdk#2887
…544) `better-sqlite3` expects parameters of the form `?1, ?2, ...` to be bound as an object of the form `{ 1: params[0], 2: params[1], ...}`. In #480, we accidentally removed the code that handled this case. This PR adds it back, and lifts out some common functionality into a `#prepareAndBind()` function. :) Thanks @ruslantalpa for spotting the removed code. Closes #526 Closes cloudflare/workers-sdk#2811 Closes cloudflare/workers-sdk#2887
In #480, we updated the D1 implementation to use the same D1 shim injected by Wrangler. This caused the `@miniflare/d1` API to change a little, but we didn't update the `README`. :(
Previously, Miniflare had its own implementations of
BetaDatabase
andStatement
. These were subtly different to the implementations in the D1 Wrangler shim D1JS, causing behaviour mismatches.This change switches the implementation to use the shim, with Miniflare implementing the underlying D1 HTTP API instead. We'll need to do this anyway when adding D1 support to Miniflare 3 using
workerd
.Specific changes:
D1PreparedStatement#run()
with statements that return data, closes 🐛 BUG: .run() throws D1 Error in cloud #441BLOB
-typed values, closes 🐛 BUG: wrangler D1 not binding BLOB in the same format than cloud D1 workers-sdk#2527D1Database#raw()
return, closes 🐛 BUG: .raw() does not work locally workers-sdk#2238 (already fixed in Fix .raw() not executing statement in D1 #474)D1Database#dump()
D1Database#{batch,exec}()
statements in implicit transactionD1PreparedStatement#{first,run,all,raw}()