| 63 | `; |
| 64 | |
| 65 | const patchAppSchema = (content: string, filePrefix: string): string => { |
| 66 | if (content.includes(`export const ${filePrefix} = app.table`)) { |
| 67 | throw new Error( |
| 68 | `Table export "${filePrefix}" already exists in app.schema.ts` |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | const block = ` |
| 73 | export const ${filePrefix} = app.table( |
| 74 | "${filePrefix}", |
| 75 | { |
| 76 | id: uuid().defaultRandom().primaryKey().notNull(), |
| 77 | userId: uuid("user_id").notNull(), |
| 78 | name: varchar({ length: 100 }).notNull(), |
| 79 | createdAt: timestamp("created_at", { withTimezone: true, mode: "string" }) |
| 80 | .defaultNow() |
| 81 | .notNull(), |
| 82 | updatedAt: timestamp("updated_at", { withTimezone: true, mode: "string" }) |
| 83 | .defaultNow() |
| 84 | .notNull(), |
| 85 | }, |
| 86 | (table) => [ |
| 87 | index("idx_${filePrefix}_user_id").on(table.userId), |
| 88 | foreignKey({ |
| 89 | columns: [table.userId], |
| 90 | foreignColumns: [users.id], |
| 91 | name: "${filePrefix}_user_id_fkey", |
| 92 | }).onDelete("cascade"), |
| 93 | ] |
| 94 | ); |
| 95 | `; |
| 96 | |
| 97 | /* |
| 98 | * When app.schema.ts has no Drizzle imports yet (fresh template with |
| 99 | * no app-domain tables), replace the whole shell with a real header |
| 100 | * plus the new table. Once at least one table exists, future calls |
| 101 | * just append. |
| 102 | */ |
| 103 | return !content.includes('from "drizzle-orm/pg-core"') |
| 104 | ? APP_SCHEMA_HEADER + block |
| 105 | : content.trimEnd() + "\n" + block; |
| 106 | }; |
| 107 | |
| 108 | const patchAuditConstants = ( |
| 109 | content: string, |
| 110 | auditKey: string, |
| 111 | auditValue: string |