|
1 | | -import { describe, expect, it } from 'vite-plus/test'; |
| 1 | +import { describe, expect, it, vi } from 'vite-plus/test'; |
2 | 2 | import { |
3 | 3 | generateVoidAuthDatabaseModule, |
4 | 4 | generateVoidAuthDrizzleSchema, |
@@ -152,6 +152,95 @@ describe('Better Auth Drizzle codegen', () => { |
152 | 152 | ); |
153 | 153 | }); |
154 | 154 |
|
| 155 | + it.each(['sqlite', 'postgresql', 'mysql'] as const)( |
| 156 | + 'leaves custom date update hooks out of the generated %s schema', |
| 157 | + (dialect) => { |
| 158 | + const onUpdate = vi.fn(() => new Date('2035-01-01T00:00:00.000Z')); |
| 159 | + const code = generateVoidAuthDrizzleSchema({ |
| 160 | + dialect, |
| 161 | + providers: null, |
| 162 | + userConfig: ({ defaults }) => ({ |
| 163 | + ...defaults, |
| 164 | + user: { |
| 165 | + additionalFields: { |
| 166 | + expiresAt: { type: 'date', required: false, onUpdate }, |
| 167 | + }, |
| 168 | + }, |
| 169 | + plugins: [ |
| 170 | + { |
| 171 | + id: 'custom-dates', |
| 172 | + schema: { |
| 173 | + audit: { |
| 174 | + fields: { updatedAt: { type: 'date', onUpdate } }, |
| 175 | + }, |
| 176 | + }, |
| 177 | + }, |
| 178 | + ], |
| 179 | + }), |
| 180 | + }); |
| 181 | + |
| 182 | + expect(code.split('\n').find((line) => line.includes('"expiresAt":'))).not.toContain( |
| 183 | + '.$onUpdate', |
| 184 | + ); |
| 185 | + const audit = code.slice(code.indexOf('export const auditTable')); |
| 186 | + expect(audit.split('\n').find((line) => line.includes('"updatedAt":'))).not.toContain( |
| 187 | + '.$onUpdate', |
| 188 | + ); |
| 189 | + expect(code.match(/\.\$onUpdate\(/g)).toHaveLength(4); |
| 190 | + expect(onUpdate).not.toHaveBeenCalled(); |
| 191 | + }, |
| 192 | + ); |
| 193 | + |
| 194 | + it.each(['user', 'session', 'account', 'verification'] as const)( |
| 195 | + 'does not replace a configured %s.updatedAt hook', |
| 196 | + (model) => { |
| 197 | + const onUpdate = vi.fn(() => new Date('2035-01-01T00:00:00.000Z')); |
| 198 | + for (const source of ['additionalFields', 'plugin'] as const) { |
| 199 | + const field = { type: 'date' as const, onUpdate }; |
| 200 | + const code = generateVoidAuthDrizzleSchema({ |
| 201 | + dialect: 'sqlite', |
| 202 | + providers: null, |
| 203 | + userConfig: ({ defaults }) => ({ |
| 204 | + ...defaults, |
| 205 | + ...(source === 'additionalFields' |
| 206 | + ? { [model]: { additionalFields: { updatedAt: field } } } |
| 207 | + : { |
| 208 | + plugins: [ |
| 209 | + { |
| 210 | + id: 'custom-updated-at', |
| 211 | + schema: { [model]: { fields: { updatedAt: field } } }, |
| 212 | + }, |
| 213 | + ], |
| 214 | + }), |
| 215 | + }), |
| 216 | + }); |
| 217 | + |
| 218 | + const table = code.slice(code.indexOf(`export const ${model}Table`)); |
| 219 | + expect(table.split('\n').find((line) => line.includes('"updatedAt":'))).not.toContain( |
| 220 | + '.$onUpdate', |
| 221 | + ); |
| 222 | + expect(code.match(/\.\$onUpdate\(/g)).toHaveLength(3); |
| 223 | + } |
| 224 | + expect(onUpdate).not.toHaveBeenCalled(); |
| 225 | + }, |
| 226 | + ); |
| 227 | + |
| 228 | + it('keeps the built-in update hook when its table and column are renamed', () => { |
| 229 | + const code = generateVoidAuthDrizzleSchema({ |
| 230 | + dialect: 'sqlite', |
| 231 | + providers: null, |
| 232 | + userConfig: ({ defaults }) => ({ |
| 233 | + ...defaults, |
| 234 | + user: { modelName: 'members', fields: { updatedAt: 'modifiedAt' } }, |
| 235 | + }), |
| 236 | + }); |
| 237 | + |
| 238 | + expect(code).toContain( |
| 239 | + '"modifiedAt": integer("modifiedAt", { mode: "timestamp_ms" }).notNull().$onUpdate(() => new Date())', |
| 240 | + ); |
| 241 | + expect(code.match(/\.\$onUpdate\(/g)).toHaveLength(4); |
| 242 | + }); |
| 243 | + |
155 | 244 | it('keeps optional defaults out of the migration metadata', () => { |
156 | 245 | const schema = resolveVoidAuthMigrationSchema({ |
157 | 246 | dialect: 'sqlite', |
|
0 commit comments