Skip to content

Commit

Permalink
Allow setting d1 database id in pages dev
Browse files Browse the repository at this point in the history
  • Loading branch information
GregBrimble committed Jun 17, 2023
1 parent ca4d27b commit c45d4a3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-dolphins-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

feat: Allow setting a D1 database ID when using `wrangler pages dev` by providing an optional `=<ID>` suffix to the argument like `--d1 BINDING_NAME=database-id`
19 changes: 14 additions & 5 deletions packages/wrangler/src/pages/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ const DURABLE_OBJECTS_BINDING_REGEXP = new RegExp(
/^(?<binding>[^=]+)=(?<className>[^@\s]+)(@(?<scriptName>.*)$)?$/
);

const D1_BINDING_REGEXP = new RegExp(
/^(?<binding>[^=]+)(?:=(?<databaseId>[^@\s]+))?$/
);

export function Options(yargs: CommonYargsArgv) {
return yargs
.positional("directory", {
Expand Down Expand Up @@ -563,11 +567,16 @@ export const Handler = async ({
experimental: {
processEntrypoint: true,
additionalModules: modules,
d1Databases: d1s.map((binding) => ({
binding: binding.toString(),
database_id: binding.toString(),
database_name: `local-${binding}`,
})),
d1Databases: d1s.map((d1) => {
const { binding, databaseId } =
D1_BINDING_REGEXP.exec(d1.toString())?.groups || {};

return {
binding,
database_id: databaseId || d1.toString(),
database_name: `local-${d1}`,
};
}),
disableExperimentalWarning: true,
enablePagesAssetsServiceBinding: {
proxyPort,
Expand Down

0 comments on commit c45d4a3

Please sign in to comment.