|
| 1 | +import { z } from "zod"; |
| 2 | +import { Service, Worker_Binding } from "../../runtime"; |
| 3 | +import { Plugin } from "../shared"; |
| 4 | + |
| 5 | +export const HYPERDRIVE_PLUGIN_NAME = "hyperdrive"; |
| 6 | + |
| 7 | +export const HyperdriveSchema = z |
| 8 | + .string() |
| 9 | + .url() |
| 10 | + .transform((urlString, ctx) => { |
| 11 | + const url = new URL(urlString); |
| 12 | + if (url.protocol === "") { |
| 13 | + ctx.addIssue({ |
| 14 | + code: z.ZodIssueCode.custom, |
| 15 | + message: "You must specify the database protocol - e.g. 'postgresql'.", |
| 16 | + }); |
| 17 | + } else if ( |
| 18 | + url.protocol !== "postgresql:" && |
| 19 | + url.protocol !== "postgres:" && |
| 20 | + url.protocol !== "" |
| 21 | + ) { |
| 22 | + ctx.addIssue({ |
| 23 | + code: z.ZodIssueCode.custom, |
| 24 | + message: |
| 25 | + "Only PostgreSQL or PostgreSQL compatible databases are currently supported.", |
| 26 | + }); |
| 27 | + } |
| 28 | + if (url.host === "") { |
| 29 | + ctx.addIssue({ |
| 30 | + code: z.ZodIssueCode.custom, |
| 31 | + message: |
| 32 | + "You must provide a hostname or IP address in your connection string - e.g. 'user:[email protected]:5432/databasename", |
| 33 | + }); |
| 34 | + } |
| 35 | + if (url.port === "") { |
| 36 | + ctx.addIssue({ |
| 37 | + code: z.ZodIssueCode.custom, |
| 38 | + message: |
| 39 | + "You must provide a port number - e.g. 'user:[email protected]:port/databasename", |
| 40 | + }); |
| 41 | + } |
| 42 | + if (url.pathname === "") { |
| 43 | + ctx.addIssue({ |
| 44 | + code: z.ZodIssueCode.custom, |
| 45 | + message: |
| 46 | + "You must provide a database name as the path component - e.g. /postgres", |
| 47 | + }); |
| 48 | + } |
| 49 | + if (url.username === "") { |
| 50 | + ctx.addIssue({ |
| 51 | + code: z.ZodIssueCode.custom, |
| 52 | + message: |
| 53 | + "You must provide a username - e.g. 'user:[email protected]:port/databasename'", |
| 54 | + }); |
| 55 | + } |
| 56 | + if (url.password === "") { |
| 57 | + ctx.addIssue({ |
| 58 | + code: z.ZodIssueCode.custom, |
| 59 | + message: |
| 60 | + "You must provide a password - e.g. 'user:[email protected]:port/databasename' ", |
| 61 | + }); |
| 62 | + } |
| 63 | + return { |
| 64 | + database: url.pathname.replace("/", ""), |
| 65 | + user: url.username, |
| 66 | + password: url.password, |
| 67 | + scheme: url.protocol.replace(":", ""), |
| 68 | + host: url.host, |
| 69 | + port: url.port, |
| 70 | + }; |
| 71 | + }); |
| 72 | + |
| 73 | +export const HyperdriveInputOptionsSchema = z.object({ |
| 74 | + hyperdrives: z.record(z.string(), HyperdriveSchema).optional(), |
| 75 | +}); |
| 76 | + |
| 77 | +export const HYPERDRIVE_PLUGIN: Plugin<typeof HyperdriveInputOptionsSchema> = { |
| 78 | + options: HyperdriveInputOptionsSchema, |
| 79 | + getBindings(options) { |
| 80 | + return Object.entries(options.hyperdrives ?? {}).map<Worker_Binding>( |
| 81 | + ([name, config]) => ({ |
| 82 | + name, |
| 83 | + hyperdrive: { |
| 84 | + designator: { |
| 85 | + name: `${HYPERDRIVE_PLUGIN_NAME}:${name}`, |
| 86 | + }, |
| 87 | + database: config.database, |
| 88 | + user: config.user, |
| 89 | + password: config.password, |
| 90 | + scheme: config.scheme, |
| 91 | + }, |
| 92 | + }) |
| 93 | + ); |
| 94 | + }, |
| 95 | + getNodeBindings() { |
| 96 | + return {}; |
| 97 | + }, |
| 98 | + async getServices({ options }) { |
| 99 | + return Object.entries(options.hyperdrives ?? {}).map<Service>( |
| 100 | + ([name, config]) => ({ |
| 101 | + name: `${HYPERDRIVE_PLUGIN_NAME}:${name}`, |
| 102 | + external: { |
| 103 | + address: `${config.host}:${config.port}`, |
| 104 | + tcp: {}, |
| 105 | + }, |
| 106 | + }) |
| 107 | + ); |
| 108 | + }, |
| 109 | +}; |
0 commit comments