Skip to content

Commit 629318e

Browse files
committed
Add jurisdiction support in wrangler for d1
1 parent d10836d commit 629318e

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
@@ -7,15 +7,16 @@ import { UserError } from "../errors";
77
import { logger } from "../logger";
88
import { requireAuth } from "../user";
99
import { getValidBindingName } from "../utils/getValidBindingName";
10-
import { LOCATION_CHOICES } from "./constants";
10+
import { JURISDICTION_CHOICES, LOCATION_CHOICES } from "./constants";
1111
import type { ComplianceConfig } from "../environment-variables/misc-variables";
1212
import type { DatabaseCreationResult } from "./types";
1313

1414
export async function createD1Database(
1515
complianceConfig: ComplianceConfig,
1616
accountId: string,
1717
name: string,
18-
location?: string
18+
location?: string,
19+
jurisdiction?: string
1920
) {
2021
try {
2122
return await fetchResult<DatabaseCreationResult>(
@@ -29,6 +30,7 @@ export async function createD1Database(
2930
body: JSON.stringify({
3031
name,
3132
...(location && { primary_location_hint: location }),
33+
...(jurisdiction && { jurisdiction }),
3234
}),
3335
}
3436
);
@@ -70,13 +72,28 @@ export const d1CreateCommand = createCommand({
7072
7173
`,
7274
},
75+
jurisdiction: {
76+
type: "string",
77+
choices: [...JURISDICTION_CHOICES],
78+
description: dedent`
79+
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:
80+
eu: The European Union
81+
fedramp: FedRAMP-compliant data centers
82+
`,
83+
},
7384
...sharedResourceCreationArgs,
7485
},
7586
positionalArgs: ["name"],
76-
async handler({ name, location, env, ...args }, { config }) {
87+
async handler({ name, location, jurisdiction, env, ...args }, { config }) {
7788
const accountId = await requireAuth(config);
7889

79-
const db = await createD1Database(config, accountId, name, location);
90+
const db = await createD1Database(
91+
config,
92+
accountId,
93+
name,
94+
location,
95+
jurisdiction
96+
);
8097

8198
logger.log(
8299
`✅ Successfully created DB '${db.name}'${

0 commit comments

Comments
 (0)