Skip to content

Commit 5d7c4c2

Browse files
authored
Add jurisdiction support in wrangler for d1 (#11078)
1 parent c3ed531 commit 5d7c4c2

File tree

4 files changed

+80
-4
lines changed

4 files changed

+80
-4
lines changed

.changeset/tasty-rockets-leave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": minor
3+
---
4+
5+
Add jurisdiction support to d1 db creation via command-line argument

packages/wrangler/src/__tests__/d1/create.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,57 @@ describe("create", () => {
8282
}"
8383
`);
8484
});
85+
86+
it("should fail if the jurisdiction provided is not supported", async () => {
87+
writeWranglerConfig({ name: "worker" }, "wrangler.json");
88+
89+
await expect(runWrangler("d1 create test --jurisdiction something")).rejects
90+
.toThrowErrorMatchingInlineSnapshot(`
91+
[Error: Invalid values:
92+
Argument: jurisdiction, Given: "something", Choices: "eu", "fedramp"]
93+
`);
94+
});
95+
96+
it("should try send jurisdiction to the API if it is a valid input", async () => {
97+
writeWranglerConfig({ name: "worker" }, "wrangler.json");
98+
99+
setIsTTY(false);
100+
mockGetMemberships([
101+
{ id: "IG-88", account: { id: "1701", name: "enterprise" } },
102+
]);
103+
msw.use(
104+
http.post("*/accounts/:accountId/d1/database", async () => {
105+
return HttpResponse.json({
106+
result: {
107+
uuid: "51e7c314-456e-4167-b6c3-869ad188fc23",
108+
name: "test",
109+
created_in_region: "WEUR",
110+
jurisdiction: "eu",
111+
},
112+
success: true,
113+
errors: [],
114+
messages: [],
115+
});
116+
})
117+
);
118+
await runWrangler("d1 create test --jurisdiction eu --binding MY_TEST_DB");
119+
expect(std.out).toMatchInlineSnapshot(`
120+
"
121+
⛅️ wrangler x.x.x
122+
──────────────────
123+
✅ Successfully created DB 'test' in region WEUR
124+
Created your new D1 database.
125+
126+
To access your new D1 Database in your Worker, add the following snippet to your configuration file:
127+
{
128+
\\"d1_databases\\": [
129+
{
130+
\\"binding\\": \\"MY_TEST_DB\\",
131+
\\"database_name\\": \\"test\\",
132+
\\"database_id\\": \\"51e7c314-456e-4167-b6c3-869ad188fc23\\"
133+
}
134+
]
135+
}"
136+
`);
137+
});
85138
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export const DEFAULT_MIGRATION_PATH = "./migrations";
22
export const DEFAULT_MIGRATION_TABLE = "d1_migrations";
33
export const LOCATION_CHOICES = ["weur", "eeur", "apac", "oc", "wnam", "enam"];
4+
export const JURISDICTION_CHOICES = ["eu", "fedramp"];

packages/wrangler/src/d1/create.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ import {
1010
sharedResourceCreationArgs,
1111
} from "../utils/add-created-resource-config";
1212
import { getValidBindingName } from "../utils/getValidBindingName";
13-
import { LOCATION_CHOICES } from "./constants";
13+
import { JURISDICTION_CHOICES, LOCATION_CHOICES } from "./constants";
1414
import type { ComplianceConfig } from "../environment-variables/misc-variables";
1515
import type { DatabaseCreationResult } from "./types";
1616

1717
export async function createD1Database(
1818
complianceConfig: ComplianceConfig,
1919
accountId: string,
2020
name: string,
21-
location?: string
21+
location?: string,
22+
jurisdiction?: string
2223
) {
2324
try {
2425
return await fetchResult<DatabaseCreationResult>(
@@ -32,6 +33,7 @@ export async function createD1Database(
3233
body: JSON.stringify({
3334
name,
3435
...(location && { primary_location_hint: location }),
36+
...(jurisdiction && { jurisdiction }),
3537
}),
3638
}
3739
);
@@ -73,13 +75,28 @@ export const d1CreateCommand = createCommand({
7375
7476
`,
7577
},
78+
jurisdiction: {
79+
type: "string",
80+
choices: [...JURISDICTION_CHOICES],
81+
description: dedent`
82+
The location to restrict the D1 database to run and store data within to comply with local regulations. Note that if jurisdictions are set, the location hint is ignored. Options:
83+
eu: The European Union
84+
fedramp: FedRAMP-compliant data centers
85+
`,
86+
},
7687
...sharedResourceCreationArgs,
7788
},
7889
positionalArgs: ["name"],
79-
async handler({ name, location, env, ...args }, { config }) {
90+
async handler({ name, location, jurisdiction, env, ...args }, { config }) {
8091
const accountId = await requireAuth(config);
8192

82-
const db = await createD1Database(config, accountId, name, location);
93+
const db = await createD1Database(
94+
config,
95+
accountId,
96+
name,
97+
location,
98+
jurisdiction
99+
);
83100

84101
logger.log(
85102
`✅ Successfully created DB '${db.name}'${

0 commit comments

Comments
 (0)