Skip to content

Commit

Permalink
Fix hyperdrive update and add option to disable caching
Browse files Browse the repository at this point in the history
  • Loading branch information
OilyLime authored and RamIdeas committed Sep 26, 2023
1 parent 0c04728 commit 0fd87a7
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/wrangler/src/__tests__/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe("normalizeAndValidateConfig()", () => {
d1_databases: [],
vectorize: [],
constellation: [],
hyperdrive: [],
dev: {
ip: "0.0.0.0",
local_protocol: "http",
Expand Down
14 changes: 7 additions & 7 deletions packages/wrangler/src/__tests__/hyperdrive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ describe("hyperdrive commands", () => {
await runWrangler("hyperdrive list");
expect(std.out).toMatchInlineSnapshot(`
"📋 Listing Hyperdrive configs
┌──────────────────────────────────┬─────────┬────────┬─────────────────────────────┬──────┬──────────┐
│ id │ name │ user │ host │ port │ database │
├──────────────────────────────────┼─────────┼────────┼─────────────────────────────┼──────┼──────────┤
│ fb94f15a95ce4afa803bb21794b2802c │ new-db │ dbuser │ database.server.com │ 3211 │ mydb │
├──────────────────────────────────┼─────────┼────────┼─────────────────────────────┼──────┼──────────┤
│ 7a040c1eee714e91a30ea6707a2d125c │ test123 │ test │ foo.us-east-2.aws.neon.tech │ 5432 │ neondb │
└──────────────────────────────────┴─────────┴────────┴─────────────────────────────┴──────┴──────────┘"
┌──────────────────────────────────┬─────────┬────────┬─────────────────────────────┬──────┬──────────┬────────────────────
│ id │ name │ user │ host │ port │ database │ caching │
├──────────────────────────────────┼─────────┼────────┼─────────────────────────────┼──────┼──────────┼────────────────────
│ fb94f15a95ce4afa803bb21794b2802c │ new-db │ dbuser │ database.server.com │ 3211 │ mydb │ {\\"disabled\\":false} │
├──────────────────────────────────┼─────────┼────────┼─────────────────────────────┼──────┼──────────┼────────────────────
│ 7a040c1eee714e91a30ea6707a2d125c │ test123 │ test │ foo.us-east-2.aws.neon.tech │ 5432 │ neondb │ {\\"disabled\\":false} │
└──────────────────────────────────┴─────────┴────────┴─────────────────────────────┴──────┴──────────┴────────────────────┘"
`);
});

Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2359,7 +2359,7 @@ const validateHyperdriveBinding: ValidatorFn = (diagnostics, field, value) => {
);
isValid = false;
}
if (!isRequiredProperty(value, "id", "string")) {
if (!isRequiredProperty(value, "id", "string")) {
diagnostics.errors.push(
`"${field}" bindings must have a "id" field but got ${JSON.stringify(
value
Expand Down
8 changes: 8 additions & 0 deletions packages/wrangler/src/hyperdrive/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type HyperdriveConfig = {
id: string;
name: string;
origin: PublicOrigin;
caching: CachingOptions;
};

export type Origin = {
Expand All @@ -23,9 +24,16 @@ export type OriginWithPassword = PublicOrigin & {
password?: string;
};

export type CachingOptions = {
disabled?: boolean;
maxAge?: number;
staleWhileRevalidate?: number;
};

export type CreateUpdateHyperdriveBody = {
name?: string;
origin: OriginWithPassword;
caching: CachingOptions;
};

export async function createConfig(
Expand Down
6 changes: 6 additions & 0 deletions packages/wrangler/src/hyperdrive/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export function options(yargs: CommonYargsArgv) {
describe:
"The connection string for the database you want Hyperdrive to connect to - ex: protocol://user:password@host:port/database",
},
"caching-disabled": {
type: "boolean",
describe:
"Whether caching query results is disabled for this Hyperdrive config",
},
})
.epilogue(hyperdriveBetaWarning);
}
Expand Down Expand Up @@ -74,6 +79,7 @@ export async function handler(
user: url.username,
password: url.password,
},
caching: { disabled: args.cachingDisabled ?? false },
});
logger.log(
`✅ Created new Hyperdrive config\n`,
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/src/hyperdrive/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export async function handler(
host: database.origin.host ?? "",
port: database.origin.port?.toString() ?? "",
database: database.origin.database ?? "",
caching: JSON.stringify(database.caching),
}))
);
}
12 changes: 11 additions & 1 deletion packages/wrangler/src/hyperdrive/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,14 @@ export function options(yargs: CommonYargsArgv) {
},
"origin-password": {
type: "string",
demandOption: true,
describe: "The password used to connect to the origin database",
},
"caching-disabled": {
type: "boolean",
describe:
"Whether caching query results is disabled for this Hyperdrive config",
},
})
.epilogue(hyperdriveBetaWarning);
}
Expand All @@ -63,6 +69,8 @@ export async function handler(
}
if (args.originScheme) {
database.origin.scheme = args.originScheme;
} else if (!database.origin.scheme) {
database.origin.scheme = "postgresql";
}
if (args.database) {
database.origin.database = args.database;
Expand All @@ -73,7 +81,9 @@ export async function handler(
if (args.originPassword) {
database.origin.password = args.originPassword;
}

if (args.cachingDisabled !== undefined) {
database.caching.disabled = args.cachingDisabled;
}
const updated = await updateConfig(config, args.id, database);
logger.log(
`✅ Updated ${updated.id} Hyperdrive config\n`,
Expand Down

0 comments on commit 0fd87a7

Please sign in to comment.