Skip to content

Commit

Permalink
Print copyable wrangler.toml text when creating a hyperdrive config (#…
Browse files Browse the repository at this point in the history
…7326)

* Print copyable wrangler.toml text when creating a hyperdrive config

* Add changeset

* Use formatConfigSnippet in printing out the Hyperdrive config
  • Loading branch information
OilyLime authored Nov 27, 2024
1 parent d03a72b commit 24c752e
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 117 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-chicken-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

Print wrangler.toml snippet when creating new Hyperdrive Config
248 changes: 134 additions & 114 deletions packages/wrangler/src/__tests__/hyperdrive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useMockIsTTY } from "./helpers/mock-istty";
import { createFetchResult, msw } from "./helpers/msw";
import { runInTempDir } from "./helpers/run-in-tmp";
import { runWrangler } from "./helpers/run-wrangler";
import { writeWranglerConfig } from "./helpers/write-wrangler-config";
import type {
CreateUpdateHyperdriveBody,
HyperdriveConfig,
Expand Down Expand Up @@ -117,21 +118,53 @@ describe("hyperdrive commands", () => {

expect(std.out).toMatchInlineSnapshot(`
"🚧 Creating 'test123'
✅ Created new Hyperdrive config
{
\\"id\\": \\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\\",
\\"name\\": \\"test123\\",
\\"origin\\": {
\\"host\\": \\"example.com\\",
\\"port\\": 12345,
\\"database\\": \\"neondb\\",
\\"scheme\\": \\"postgresql\\",
\\"user\\": \\"test\\"
}
✅ Created new Hyperdrive config: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
📋 To start using your config from a Worker, add the following binding configuration to your Wrangler configuration file:
{
\\"hyperdrive\\": [
{
\\"binding\\": \\"HYPERDRIVE\\",
\\"id\\": \\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\\"
}
]
}"
`);
});

it("should handle creating a hyperdrive and printing a TOML snipped", async () => {
const reqProm = mockHyperdriveCreate();
writeWranglerConfig();
await runWrangler(
"hyperdrive create test123 --connection-string='postgresql://test:[email protected]:12345/neondb'"
);

await expect(reqProm).resolves.toMatchInlineSnapshot(`
Object {
"name": "test123",
"origin": Object {
"database": "neondb",
"host": "example.com",
"password": "password",
"port": 12345,
"scheme": "postgresql",
"user": "test",
},
}
`);

expect(std.out).toMatchInlineSnapshot(`
"🚧 Creating 'test123'
✅ Created new Hyperdrive config: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
📋 To start using your config from a Worker, add the following binding configuration to your wrangler.toml file:
[[hyperdrive]]
binding = \\"HYPERDRIVE\\"
id = \\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\\"
"
`);
});

it("should handle creating a hyperdrive config for postgres without a port specified", async () => {
const reqProm = mockHyperdriveCreate();
await runWrangler(
Expand All @@ -153,17 +186,16 @@ describe("hyperdrive commands", () => {
`);
expect(std.out).toMatchInlineSnapshot(`
"🚧 Creating 'test123'
✅ Created new Hyperdrive config
{
\\"id\\": \\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\\",
\\"name\\": \\"test123\\",
\\"origin\\": {
\\"host\\": \\"example.com\\",
\\"port\\": 5432,
\\"database\\": \\"neondb\\",
\\"scheme\\": \\"postgresql\\",
\\"user\\": \\"test\\"
}
✅ Created new Hyperdrive config: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
📋 To start using your config from a Worker, add the following binding configuration to your Wrangler configuration file:
{
\\"hyperdrive\\": [
{
\\"binding\\": \\"HYPERDRIVE\\",
\\"id\\": \\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\\"
}
]
}"
`);
});
Expand Down Expand Up @@ -193,21 +225,16 @@ describe("hyperdrive commands", () => {
`);
expect(std.out).toMatchInlineSnapshot(`
"🚧 Creating 'test123'
✅ Created new Hyperdrive config
{
\\"id\\": \\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\\",
\\"name\\": \\"test123\\",
\\"origin\\": {
\\"host\\": \\"example.com\\",
\\"port\\": 12345,
\\"database\\": \\"neondb\\",
\\"scheme\\": \\"postgresql\\",
\\"user\\": \\"test\\"
},
\\"caching\\": {
\\"max_age\\": 30,
\\"stale_while_revalidate\\": 15
}
✅ Created new Hyperdrive config: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
📋 To start using your config from a Worker, add the following binding configuration to your Wrangler configuration file:
{
\\"hyperdrive\\": [
{
\\"binding\\": \\"HYPERDRIVE\\",
\\"id\\": \\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\\"
}
]
}"
`);
});
Expand All @@ -233,17 +260,16 @@ describe("hyperdrive commands", () => {
`);
expect(std.out).toMatchInlineSnapshot(`
"🚧 Creating 'test123'
✅ Created new Hyperdrive config
{
\\"id\\": \\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\\",
\\"name\\": \\"test123\\",
\\"origin\\": {
\\"host\\": \\"example.com\\",
\\"port\\": 5432,
\\"database\\": \\"neondb\\",
\\"scheme\\": \\"postgresql\\",
\\"user\\": \\"user:name\\"
}
✅ Created new Hyperdrive config: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
📋 To start using your config from a Worker, add the following binding configuration to your Wrangler configuration file:
{
\\"hyperdrive\\": [
{
\\"binding\\": \\"HYPERDRIVE\\",
\\"id\\": \\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\\"
}
]
}"
`);
});
Expand All @@ -269,17 +295,16 @@ describe("hyperdrive commands", () => {
`);
expect(std.out).toMatchInlineSnapshot(`
"🚧 Creating 'test123'
✅ Created new Hyperdrive config
{
\\"id\\": \\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\\",
\\"name\\": \\"test123\\",
\\"origin\\": {
\\"host\\": \\"example.com\\",
\\"port\\": 5432,
\\"database\\": \\"neondb\\",
\\"scheme\\": \\"postgresql\\",
\\"user\\": \\"test\\"
}
✅ Created new Hyperdrive config: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
📋 To start using your config from a Worker, add the following binding configuration to your Wrangler configuration file:
{
\\"hyperdrive\\": [
{
\\"binding\\": \\"HYPERDRIVE\\",
\\"id\\": \\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\\"
}
]
}"
`);
});
Expand All @@ -305,17 +330,16 @@ describe("hyperdrive commands", () => {
`);
expect(std.out).toMatchInlineSnapshot(`
"🚧 Creating 'test123'
✅ Created new Hyperdrive config
{
\\"id\\": \\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\\",
\\"name\\": \\"test123\\",
\\"origin\\": {
\\"host\\": \\"example.com\\",
\\"port\\": 5432,
\\"database\\": \\"/\\"weird/\\" dbname\\",
\\"scheme\\": \\"postgresql\\",
\\"user\\": \\"test\\"
}
✅ Created new Hyperdrive config: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
📋 To start using your config from a Worker, add the following binding configuration to your Wrangler configuration file:
{
\\"hyperdrive\\": [
{
\\"binding\\": \\"HYPERDRIVE\\",
\\"id\\": \\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\\"
}
]
}"
`);
});
Expand All @@ -341,17 +365,16 @@ describe("hyperdrive commands", () => {
`);
expect(std.out).toMatchInlineSnapshot(`
"🚧 Creating 'test123'
✅ Created new Hyperdrive config
{
\\"id\\": \\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\\",
\\"name\\": \\"test123\\",
\\"origin\\": {
\\"host\\": \\"example.com\\",
\\"port\\": 5432,
\\"database\\": \\"neondb\\",
\\"scheme\\": \\"postgresql\\",
\\"user\\": \\"test\\"
}
✅ Created new Hyperdrive config: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
📋 To start using your config from a Worker, add the following binding configuration to your Wrangler configuration file:
{
\\"hyperdrive\\": [
{
\\"binding\\": \\"HYPERDRIVE\\",
\\"id\\": \\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\\"
}
]
}"
`);
});
Expand All @@ -377,17 +400,16 @@ describe("hyperdrive commands", () => {
`);
expect(std.out).toMatchInlineSnapshot(`
"🚧 Creating 'test123'
✅ Created new Hyperdrive config
{
\\"id\\": \\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\\",
\\"name\\": \\"test123\\",
\\"origin\\": {
\\"host\\": \\"example.com\\",
\\"port\\": 1234,
\\"database\\": \\"neondb\\",
\\"scheme\\": \\"postgresql\\",
\\"user\\": \\"test\\"
}
✅ Created new Hyperdrive config: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
📋 To start using your config from a Worker, add the following binding configuration to your Wrangler configuration file:
{
\\"hyperdrive\\": [
{
\\"binding\\": \\"HYPERDRIVE\\",
\\"id\\": \\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\\"
}
]
}"
`);
});
Expand Down Expand Up @@ -439,17 +461,16 @@ describe("hyperdrive commands", () => {
`);
expect(std.out).toMatchInlineSnapshot(`
"🚧 Creating 'test123'
✅ Created new Hyperdrive config
{
\\"id\\": \\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\\",
\\"name\\": \\"test123\\",
\\"origin\\": {
\\"host\\": \\"example.com\\",
\\"database\\": \\"neondb\\",
\\"scheme\\": \\"postgresql\\",
\\"user\\": \\"test\\",
\\"access_client_id\\": \\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.access\\"
}
✅ Created new Hyperdrive config: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
📋 To start using your config from a Worker, add the following binding configuration to your Wrangler configuration file:
{
\\"hyperdrive\\": [
{
\\"binding\\": \\"HYPERDRIVE\\",
\\"id\\": \\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\\"
}
]
}"
`);
});
Expand All @@ -475,17 +496,16 @@ describe("hyperdrive commands", () => {
`);
expect(std.out).toMatchInlineSnapshot(`
"🚧 Creating 'test123'
✅ Created new Hyperdrive config
{
\\"id\\": \\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\\",
\\"name\\": \\"test123\\",
\\"origin\\": {
\\"host\\": \\"example.com/database\\",
\\"database\\": \\"neondb\\",
\\"scheme\\": \\"postgresql\\",
\\"user\\": \\"test\\",
\\"access_client_id\\": \\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.access\\"
}
✅ Created new Hyperdrive config: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
📋 To start using your config from a Worker, add the following binding configuration to your Wrangler configuration file:
{
\\"hyperdrive\\": [
{
\\"binding\\": \\"HYPERDRIVE\\",
\\"id\\": \\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\\"
}
]
}"
`);
});
Expand Down
14 changes: 11 additions & 3 deletions packages/wrangler/src/hyperdrive/create.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readConfig } from "../config";
import { configFileName, formatConfigSnippet, readConfig } from "../config";
import { logger } from "../logger";
import { createConfig } from "./client";
import { getCacheOptionsFromArgs, getOriginFromArgs, upsertOptions } from ".";
Expand Down Expand Up @@ -33,8 +33,16 @@ export async function handler(
origin,
caching: getCacheOptionsFromArgs(args),
});
logger.log(`✅ Created new Hyperdrive config: ${database.id}`);
logger.log(
`✅ Created new Hyperdrive config\n`,
JSON.stringify(database, null, 2)
`📋 To start using your config from a Worker, add the following binding configuration to your ${configFileName(config.configPath)} file:\n`
);
logger.log(
formatConfigSnippet(
{
hyperdrive: [{ binding: "HYPERDRIVE", id: database.id }],
},
config.configPath
)
);
}

0 comments on commit 24c752e

Please sign in to comment.