Skip to content

feat: hyperdrive postgres/mysql support#9

Merged
zpg6 merged 8 commits intomainfrom
feat/hyperdrive
Jul 26, 2025
Merged

feat: hyperdrive postgres/mysql support#9
zpg6 merged 8 commits intomainfrom
feat/hyperdrive

Conversation

@zpg6
Copy link
Owner

@zpg6 zpg6 commented Jul 20, 2025

Adding support for Cloudflare's Hyperdrive

Closes #6. Inspired by #7 from @imjlk

@zpg6 zpg6 requested a review from Copilot July 20, 2025 15:06

This comment was marked as outdated.

@zpg6 zpg6 requested a review from Copilot July 20, 2025 15:15
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for Cloudflare Hyperdrive to enable Postgres and MySQL database connectivity alongside the existing D1 (SQLite) support. The changes expand the database options available to users of the better-auth-cloudflare plugin.

  • Introduces generic DrizzleConfig type to support multiple database types
  • Adds validation to ensure only one database configuration is provided at a time
  • Updates documentation with Hyperdrive usage examples and configuration details

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/types.ts Refactors database configuration to support Postgres, MySQL, and D1 through a generic DrizzleConfig type
src/index.ts Implements database configuration validation and conditional adapter setup for all three database types
README.md Updates documentation to reflect Hyperdrive support with usage examples for Postgres and MySQL
Comments suppressed due to low confidence (1)

src/index.ts:198

  • [nitpick] The variable name database is ambiguous in this context. Consider renaming it to databaseAdapter or drizzleAdapter to clarify that it holds the adapter instance.
    let database;

@zpg6
Copy link
Owner Author

zpg6 commented Jul 20, 2025

@imjlk - curious what you think, can you give a review?

Copy link
Contributor

@imjlk imjlk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello! Thank you for your efforts. I left a few comments, please take a look when you have time.

No need to worry too much about my feedback. I'm not really used to do review. 😅 Thank you!

Comment on lines +189 to +215
// Assert that only one database configuration is provided
const dbConfigs = [cloudFlareOptions.postgres, cloudFlareOptions.mysql, cloudFlareOptions.d1].filter(Boolean);
if (dbConfigs.length > 1) {
throw new Error(
"Only one database configuration can be provided. Please provide only one of postgres, mysql, or d1."
);
}

// Determine which database configuration to use
let database;
if (cloudFlareOptions.postgres) {
database = drizzleAdapter(cloudFlareOptions.postgres.db, {
provider: "pg",
...cloudFlareOptions.postgres.options,
});
} else if (cloudFlareOptions.mysql) {
database = drizzleAdapter(cloudFlareOptions.mysql.db, {
provider: "mysql",
...cloudFlareOptions.mysql.options,
});
} else if (cloudFlareOptions.d1) {
database = drizzleAdapter(cloudFlareOptions.d1.db, {
provider: "sqlite",
...cloudFlareOptions.d1.options,
});
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about enforce the single database constraint at the type level, instead of using a runtime check?

We can define a type for the database options, like this:

type DatabaseOption =
    | {
        /**
         * D1 database configuration for SQLite
         */
        d1: DrizzleConfig<typeof d1Drizzle>;
        postgres?: never;
        mysql?: never;
    }
    | {
        /**
         * Postgres database configuration for Hyperdrive
         */
        postgres: DrizzleConfig<typeof postgresDrizzle>;
        d1?: never;
        mysql?: never;
    }
    | {
        /**
         * MySQL database configuration for Hyperdrive
         */
        mysql: DrizzleConfig<typeof mysqlDrizzle>;
        d1?: never;
        postgres?: never;
    };

By applying this DatabaseOption type to WithCloudflareOptions, we can guarantee that only one database configuration is provided at compile time, eliminating the need for the runtime check.

The final type would look like this:

type WithCloudflareOptions = CloudflarePluginOptions &
    Partial<DatabaseOption> & {
        /**
         * KV namespace for secondary storage, if you want to use that.
         */
        kv?: KVNamespace<string>;
    };

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@imjlk Great feedback! I've been thinking about this too... I wasn't sure which would be clearer to user of the plugin.

Copy link
Contributor

@imjlk imjlk Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, if it's for user feedback, the original method would be better. Also, I did a quick build with the code I wrote and didn't encounter any type errors when even if i set up multiple databases 😞
However, I think it's not common case to use multiple databases

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/zpg6/better-auth-cloudflare/pull/9/files#diff-a2a171449d862fe29692ce031981047d7ab755ae7f84c707aef80701b3ea0c80R5-R11
Could you change it to add .js at the end, like I did?

I'm not sure if changing the tsconfig settings will solve the issue, but for now, Vite environments are encountering module resolution errors when importing like below:

  Error [ERR_MODULE_NOT_FOUND]: Cannot find module './schema' imported from 
  './dist/index.js'

I don't think these changes affect the other examples.

If this is applied, I think I’ll be able to update just the example in the SvelteKit example PR after rebasing, without change anything else. I noticed that a type error occurs, but works well.

@zpg6
Copy link
Owner Author

zpg6 commented Jul 21, 2025

Hello! Thank you for your efforts. I left a few comments, please take a look when you have time.

No need to worry too much about my feedback. I'm not really used to do review. 😅 Thank you!

@imjlk Thank you for taking time and for the great feedback! Will look more into the .js issue too

@zpg6 zpg6 merged commit 20ddddd into main Jul 26, 2025
3 checks passed
@zpg6 zpg6 deleted the feat/hyperdrive branch July 26, 2025 21:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for Postgres and MySQL via Hyperdrive

3 participants