feat(miniflare): Add implementation for local explorer D1 API#12208
feat(miniflare): Add implementation for local explorer D1 API#12208
Conversation
🦋 Changeset detectedLatest commit: 62994d5 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
create-cloudflare
@cloudflare/kv-asset-handler
miniflare
@cloudflare/pages-shared
@cloudflare/unenv-preset
@cloudflare/vite-plugin
@cloudflare/vitest-pool-workers
@cloudflare/workers-editor-shared
@cloudflare/workers-utils
wrangler
commit: |
|
Claude encountered an error —— View job Changeset Review
|
There was a problem hiding this comment.
This change might be a bit overkill for the fixture. Happy to revert if needed.
| "migrations": [ | ||
| { | ||
| "new_sqlite_classes": ["MyDurableObject"], | ||
| "tag": "v1", | ||
| }, | ||
| ], |
There was a problem hiding this comment.
Running the fixture just complained about this so may as well fix this now.
| if (!innerBinding || !("service" in innerBinding)) | ||
| throw new MiniflareCoreError( | ||
| "ERR_INVALID_WRAPPED", | ||
| "No or invalid inner binding service property" | ||
| ); | ||
|
|
||
| const databaseId = innerBinding.service?.name?.replace("d1:db:", ""); | ||
| if (!databaseId) | ||
| throw new MiniflareCoreError( | ||
| "ERR_INVALID_WRAPPED", | ||
| "No inner binding service database ID" | ||
| ); |
There was a problem hiding this comment.
I am uncertain about the error handling for this. Checking out USER_ERROR_CODES and SYSTEM_ERROR_CODES nothing really seems to fit this error. Do we want to throw here or just skip the D1 binding in question?
| const IDToBindingName: { kv: Record<string, string> } = { kv: {} }; | ||
| // Maps binding names to their actual resource IDs | ||
| const IDToBindingName: { | ||
| d1: Record<string, string>; |
There was a problem hiding this comment.
I would have preferred to make this into a Map but TIL that JSON.stringify cannot serialise a nested Map without explicitly transforming it. For example:
JSON.stringify({ d1: new Map([ ['foo', 1] ]) })
// '{ "d1": {} }'
JSON.stringify({ d1: Object.fromEntries(new Map([ ['foo', 1] ])) })
// '{ "d1": { "foo": 1 } }'Uses a JSON binding in the Local Explorer worker that maps binding names to namespace IDs
Uses a JSON binding in the Local Explorer worker that maps binding names to namespace IDs
d4eccb8 to
8d40724
Compare

This PR follows on from #12152 and adds a D1 compatible API to the local explorer API worker. That includes adding the following endpoints with their respective handling logic:
GET /d1/database- Returns a list of D1 databases.GET /d1/database/{database_id}- Returns the specified D1 database.POST /d1/database/{database_id}/raw- Returns the query result rows as arrays rather than objects.A picture of a cute animal (not mandatory, but encouraged)