From b1c8fb564885fddeb3f602cd2d26759fabccdc26 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 19 Aug 2026 16:05:42 +0000 Subject: [PATCH] Fix Job.payload/result MySQL column size (text 64KB -> longtext) JsonColumn (simple-json) always maps to MySQL text (64KB) regardless of any type override -- TypeORM's mysql driver hardcodes this. The user-data-export handler can return a base64 zip in Job.result when S3 isn't configured, which routinely exceeds 64KB and fails to save with "Data too long for column 'result'". Add LongJsonColumn (LongTextColumn + a JSON transformer) and use it for Job.payload/Job.result. Verified against real MySQL: a longtext column accepts a 200KB value that a text column rejects. Found by Copilot review on buzz-code/react-admin-nestjs#141. Co-Authored-By: Claude Opus 4.8 Claude-Session: https://claude.ai/code/session_011dEYULADLgGNfZ4nv6gz8s --- entities/Job.entity.ts | 6 +-- .../__tests__/column-types.util.spec.ts | 51 +++++++++++++++++++ utils/entity/column-types.util.ts | 18 +++++++ 3 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 utils/entity/__tests__/column-types.util.spec.ts diff --git a/entities/Job.entity.ts b/entities/Job.entity.ts index 7ef5b95..607afdc 100644 --- a/entities/Job.entity.ts +++ b/entities/Job.entity.ts @@ -1,5 +1,5 @@ import { Column, Entity, Index, JoinColumn, ManyToOne, PrimaryGeneratedColumn } from 'typeorm'; -import { CreatedAtColumn, JsonColumn, UpdatedAtColumn } from '@shared/utils/entity/column-types.util'; +import { CreatedAtColumn, LongJsonColumn, UpdatedAtColumn } from '@shared/utils/entity/column-types.util'; import { IHasUserId } from '@shared/base-entity/interface'; import { User } from './User.entity'; @@ -32,10 +32,10 @@ export class Job implements IHasUserId { @Index('job_status_idx') status: JobStatus; - @JsonColumn({ nullable: true }) + @LongJsonColumn({ nullable: true }) payload: Record; - @JsonColumn({ nullable: true }) + @LongJsonColumn({ nullable: true }) result: Record; @Column('int', { default: 0 }) diff --git a/utils/entity/__tests__/column-types.util.spec.ts b/utils/entity/__tests__/column-types.util.spec.ts new file mode 100644 index 0000000..7809d93 --- /dev/null +++ b/utils/entity/__tests__/column-types.util.spec.ts @@ -0,0 +1,51 @@ +const columnCalls: any[] = []; + +jest.mock('typeorm', () => { + const actual = jest.requireActual('typeorm'); + return { + ...actual, + Column: (...args: any[]) => { + columnCalls.push(args); + return actual.Column(...args); + }, + }; +}); + +import { LongJsonColumn } from '../column-types.util'; + +describe('LongJsonColumn', () => { + beforeEach(() => { + columnCalls.length = 0; + }); + + it('backs the column with a longtext-mapped type (via LongTextColumn)', () => { + LongJsonColumn({ nullable: true }); + expect(columnCalls).toHaveLength(1); + const [, options] = columnCalls[0]; + expect(options.nullable).toBe(true); + expect(options.transformer).toBeDefined(); + }); + + it('round-trips an object through the default transformer', () => { + LongJsonColumn(); + const [, options] = columnCalls[0]; + const value = { a: 1, nested: { b: [1, 2, 3] } }; + const stored = options.transformer.to(value); + expect(typeof stored).toBe('string'); + expect(options.transformer.from(stored)).toEqual(value); + }); + + it('passes null through the transformer unchanged', () => { + LongJsonColumn(); + const [, options] = columnCalls[0]; + expect(options.transformer.to(null)).toBeNull(); + expect(options.transformer.from(null)).toBeNull(); + }); + + it('respects a caller-supplied transformer instead of the default', () => { + const customTransformer = { to: (v: any) => v, from: (v: any) => v }; + LongJsonColumn({ transformer: customTransformer }); + const [, options] = columnCalls[0]; + expect(options.transformer).toBe(customTransformer); + }); +}); diff --git a/utils/entity/column-types.util.ts b/utils/entity/column-types.util.ts index 46aae27..ad4bea4 100644 --- a/utils/entity/column-types.util.ts +++ b/utils/entity/column-types.util.ts @@ -148,6 +148,24 @@ export function JsonColumn(options?: ColumnOptions) { return Column('simple-json', options); } +/** + * JSON column with no MySQL size limit. TypeORM's `simple-json` (used by + * JsonColumn) always maps to `text` (64KB) in MySQL regardless of any type + * override, so it's unsuitable for JSON payloads that can exceed that (e.g. + * a base64-encoded file). Backed by LongTextColumn with a manual transformer + * instead. + */ +export function LongJsonColumn(options?: ColumnOptions) { + const { transformer, ...rest } = options ?? {}; + return LongTextColumn({ + ...rest, + transformer: transformer ?? { + to: (value: any) => (value == null ? value : JSON.stringify(value)), + from: (value: string) => (value == null || value === '' ? null : JSON.parse(value)), + }, + }); +} + /** * Varchar column with explicit length - works consistently across databases */