-
Notifications
You must be signed in to change notification settings - Fork 27
feat: hyperdrive postgres/mysql support #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
98e4169
d9877a5
4eee9fb
b8cb296
aae557d
b0739e1
8cc9c47
e70863b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -186,14 +186,36 @@ export const withCloudflare = <T extends BetterAuthOptions>( | |
| // If user explicitly set it to true/false, that will be respected. | ||
| } | ||
|
|
||
| // 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, | ||
| }); | ||
| } | ||
|
|
||
|
Comment on lines
+189
to
+215
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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>;
};
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 😞 |
||
| return { | ||
| ...options, | ||
| database: cloudFlareOptions.d1 | ||
| ? drizzleAdapter(cloudFlareOptions.d1.db, { | ||
| provider: "sqlite", | ||
| ...cloudFlareOptions.d1.options, | ||
| }) | ||
| : undefined, | ||
| database, | ||
| secondaryStorage: cloudFlareOptions.kv ? createKVStorage(cloudFlareOptions.kv) : undefined, | ||
| plugins: [cloudflare(cloudFlareOptions), ...(options.plugins ?? [])], | ||
| advanced: updatedAdvanced, | ||
|
|
||
There was a problem hiding this comment.
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
.jsat 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:
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.