diff --git a/.changeset/cuddly-chicken-visit.md b/.changeset/cuddly-chicken-visit.md new file mode 100644 index 000000000000..1f5ec4cf156f --- /dev/null +++ b/.changeset/cuddly-chicken-visit.md @@ -0,0 +1,5 @@ +--- +"wrangler": patch +--- + +Print wrangler.toml snippet when creating new Hyperdrive Config diff --git a/packages/wrangler/src/__tests__/hyperdrive.test.ts b/packages/wrangler/src/__tests__/hyperdrive.test.ts index 2659c3bbff18..a860d53c1d47 100644 --- a/packages/wrangler/src/__tests__/hyperdrive.test.ts +++ b/packages/wrangler/src/__tests__/hyperdrive.test.ts @@ -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, @@ -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:password@example.com: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( @@ -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\\" + } + ] }" `); }); @@ -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\\" + } + ] }" `); }); @@ -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\\" + } + ] }" `); }); @@ -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\\" + } + ] }" `); }); @@ -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\\" + } + ] }" `); }); @@ -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\\" + } + ] }" `); }); @@ -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\\" + } + ] }" `); }); @@ -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\\" + } + ] }" `); }); @@ -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\\" + } + ] }" `); }); diff --git a/packages/wrangler/src/hyperdrive/create.ts b/packages/wrangler/src/hyperdrive/create.ts index 7d6b3997ce84..8293faa117c5 100644 --- a/packages/wrangler/src/hyperdrive/create.ts +++ b/packages/wrangler/src/hyperdrive/create.ts @@ -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 "."; @@ -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 + ) ); }