@@ -39,6 +39,7 @@ function generateHonoAuth(config: AuthConfig): string {
3939 imports . push ( `import { drizzle } from "drizzle-orm/postgres-js";` ) ;
4040 } else {
4141 imports . push ( `import { drizzle } from "drizzle-orm/mysql2";` ) ;
42+ imports . push ( `import mysql from "mysql2";` ) ;
4243 }
4344
4445 imports . push ( `import { schema } from "../db";` , `import type { CloudflareBindings } from "../env";` ) ;
@@ -205,47 +206,49 @@ function generateHonoCloudflareConfig(config: AuthConfig): string {
205206 if ( config . resources . r2 ) {
206207 parts . push ( `
207208 // R2 configuration for file storage (${ config . bindings . r2 || "R2_BUCKET" } binding from wrangler.toml)
208- r2: {
209- bucket: env?.${ config . bindings . r2 || "R2_BUCKET" } ,
210- maxFileSize: 2 * 1024 * 1024, // 2MB
211- allowedTypes: [".jpg", ".jpeg", ".png", ".gif"],
212- additionalFields: {
213- category: { type: "string", required: false },
214- isPublic: { type: "boolean", required: false },
215- description: { type: "string", required: false },
216- },
217- hooks: {
218- upload: {
219- before: async (file, ctx) => {
220- // Only allow authenticated users to upload files
221- if (ctx.session === null) {
222- return null; // Blocks upload
223- }
224-
225- // Only allow paid users to upload files (for example)
226- const isPaidUser = (userId: string) => true; // example
227- if (isPaidUser(ctx.session.user.id) === false) {
228- return null; // Blocks upload
229- }
230-
231- // Allow upload
232- },
233- after: async (file, ctx) => {
234- // Track your analytics (for example)
235- console.log("File uploaded:", file);
236- },
209+ ...(env?.${ config . bindings . r2 || "R2_BUCKET" } ? {
210+ r2: {
211+ bucket: env.${ config . bindings . r2 || "R2_BUCKET" } ,
212+ maxFileSize: 2 * 1024 * 1024, // 2MB
213+ allowedTypes: [".jpg", ".jpeg", ".png", ".gif"],
214+ additionalFields: {
215+ category: { type: "string", required: false },
216+ isPublic: { type: "boolean", required: false },
217+ description: { type: "string", required: false },
237218 },
238- download: {
239- before: async (file, ctx) => {
240- // Only allow user to access their own files (by default all files are public)
241- if (file.isPublic === false && file.userId !== ctx.session?.user.id) {
242- return null; // Blocks download
243- }
244- // Allow download
219+ hooks: {
220+ upload: {
221+ before: async (file, ctx) => {
222+ // Only allow authenticated users to upload files
223+ if (ctx.session === null) {
224+ return null; // Blocks upload
225+ }
226+
227+ // Only allow paid users to upload files (for example)
228+ const isPaidUser = (userId: string) => true; // example
229+ if (isPaidUser(ctx.session.user.id) === false) {
230+ return null; // Blocks upload
231+ }
232+
233+ // Allow upload
234+ },
235+ after: async (file, ctx) => {
236+ // Track your analytics (for example)
237+ console.log("File uploaded:", file);
238+ },
239+ },
240+ download: {
241+ before: async (file, ctx) => {
242+ // Only allow user to access their own files (by default all files are public)
243+ if (file.isPublic === false && file.userId !== ctx.session?.user.id) {
244+ return null; // Blocks download
245+ }
246+ // Allow download
247+ },
245248 },
246249 },
247250 },
248- },` ) ;
251+ } : {}) ,` ) ;
249252 }
250253
251254 return parts . join ( "" ) ;
@@ -351,12 +354,19 @@ const generateHonoSchemaConfig = generateSchemaConfig;
351354const generateNextjsSchemaConfig = generateSchemaConfig ;
352355
353356function generateDbConnection ( config : AuthConfig ) : string {
357+ const binding = config . bindings . hyperdrive || "HYPERDRIVE" ;
354358 if ( config . database === "sqlite" ) {
355359 return `drizzle(env.${ config . bindings . d1 || "DATABASE" } , { schema, logger: true })` ;
356360 } else if ( config . database === "postgres" ) {
357- return `drizzle(env.${ config . bindings . hyperdrive || "HYPERDRIVE" } , { schema, logger: true })` ;
361+ return `drizzle(env.${ binding } , { schema, logger: true })` ;
358362 } else {
359- return `drizzle(env.${ config . bindings . hyperdrive || "HYPERDRIVE" } , { schema, logger: true })` ;
363+ return `drizzle(mysql.createPool({
364+ host: env.${ binding } .host,
365+ user: env.${ binding } .user,
366+ password: env.${ binding } .password,
367+ database: env.${ binding } .database,
368+ port: env.${ binding } .port,
369+ }), { schema, mode: "default", logger: true })` ;
360370 }
361371}
362372
0 commit comments