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 */