From b925f940f95dda97a6fbc02d5a5387e510abcfb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20H=C3=BCttmaier?= Date: Wed, 12 Jun 2024 10:50:01 +0200 Subject: [PATCH] API Generator: Support relation with primary key type `int` (in addition to `integer`) (#2118) improves test from https://github.com/vivid-planet/comet/pull/1285 The PrimaryKey decorator with type integer works fine: `@PrimaryKey({ columnType: "int", type: "integer" })` However, the PrimaryKey decorator with type int causes a generation issue: `@PrimaryKey({ columnType: "int", type: "int" })` The input file for type int should be generated exactly the same, and there is now a new test for this. --------- Co-authored-by: thomashuettmaier Co-authored-by: Niko Sams Co-authored-by: Niko Sams Co-authored-by: Johannes Obermair <48853629+johnnyomair@users.noreply.github.com> --- .changeset/nine-comics-design.md | 5 ++ .../src/generator/generate-crud-input.ts | 2 +- ...generate-crud-relations-id-integer.spec.ts | 57 ++++++++++++++++--- 3 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 .changeset/nine-comics-design.md diff --git a/.changeset/nine-comics-design.md b/.changeset/nine-comics-design.md new file mode 100644 index 0000000000..f969a321fa --- /dev/null +++ b/.changeset/nine-comics-design.md @@ -0,0 +1,5 @@ +--- +"@comet/cms-api": patch +--- + +API Generator: Support relation with primary key type `int` (in addition to `integer`) diff --git a/packages/api/cms-api/src/generator/generate-crud-input.ts b/packages/api/cms-api/src/generator/generate-crud-input.ts index 624d9c26c7..8281de6f54 100644 --- a/packages/api/cms-api/src/generator/generate-crud-input.ts +++ b/packages/api/cms-api/src/generator/generate-crud-input.ts @@ -32,7 +32,7 @@ function findReferenceTargetType( return "uuid"; } else if (referencedColumnProp.type == "string") { return "string"; - } else if (referencedColumnProp.type == "integer") { + } else if (referencedColumnProp.type == "integer" || referencedColumnProp.type == "int") { return "integer"; } else { return null; diff --git a/packages/api/cms-api/src/generator/generate-crud-relations-id-integer.spec.ts b/packages/api/cms-api/src/generator/generate-crud-relations-id-integer.spec.ts index 60dfc40177..c52c95bae5 100644 --- a/packages/api/cms-api/src/generator/generate-crud-relations-id-integer.spec.ts +++ b/packages/api/cms-api/src/generator/generate-crud-relations-id-integer.spec.ts @@ -11,7 +11,16 @@ class TestEntityCategoryWithIntegerId extends BaseEntity TestEntityProduct, (products) => products.category) + @OneToMany(() => TestEntityProduct, (products) => products.categoryWithInteger) + products = new Collection(this); +} + +@Entity() +class TestEntityCategoryWithIntId extends BaseEntity { + @PrimaryKey({ columnType: "int", type: "int" }) + id: number; + + @OneToMany(() => TestEntityProduct, (products) => products.categoryWithInt) products = new Collection(this); } @@ -21,17 +30,20 @@ class TestEntityProduct extends BaseEntity { id: string = uuid(); @ManyToOne(() => TestEntityCategoryWithIntegerId, { nullable: true, ref: true }) - category?: Ref; + categoryWithInteger?: Ref; + + @ManyToOne(() => TestEntityCategoryWithIntId, { nullable: true, ref: true }) + categoryWithInt?: Ref; } -describe("GenerateCrudRelationsIdString", () => { +describe("GenerateCrudRelationsIdNumber", () => { describe("resolver class", () => { - it("input type to category relation should be number with integer validator", async () => { + it("input type to category relation with primary key type integer should be number with integer validator", async () => { LazyMetadataStorage.load(); const orm = await MikroORM.init({ type: "postgresql", dbName: "test-db", - entities: [TestEntityProduct, TestEntityCategoryWithIntegerId], + entities: [TestEntityProduct, TestEntityCategoryWithIntegerId, TestEntityCategoryWithIntId], }); const out = await generateCrud({ targetDirectory: __dirname }, orm.em.getMetadata().get("TestEntityProduct")); @@ -45,8 +57,8 @@ describe("GenerateCrudRelationsIdString", () => { const cls = classes[0]; const structure = cls.getStructure(); - expect(structure.properties?.length).toBe(1); - expect(structure.properties?.[0].name).toBe("category"); + expect(structure.properties?.length).toBe(2); + expect(structure.properties?.[0].name).toBe("categoryWithInteger"); expect(structure.properties?.[0].type).toBe("number"); expect(structure.properties?.[0].decorators?.length).toBe(4); const decorators = structure.properties?.[0].decorators?.map((dec) => dec.name); @@ -56,5 +68,36 @@ describe("GenerateCrudRelationsIdString", () => { expect(decorators).not.toContain("IsString"); orm.close(); }); + + it("input type to category relation with primary key type int should be number with integer validator", async () => { + LazyMetadataStorage.load(); + const orm = await MikroORM.init({ + type: "postgresql", + dbName: "test-db", + entities: [TestEntityProduct, TestEntityCategoryWithIntegerId, TestEntityCategoryWithIntId], + }); + + const out = await generateCrud({ targetDirectory: __dirname }, orm.em.getMetadata().get("TestEntityProduct")); + const lintedOut = await lintGeneratedFiles(out); + const file = lintedOut.find((file) => file.name === "dto/test-entity-product.input.ts"); + if (!file) throw new Error("File not found"); + + const source = parseSource(file.content); + const classes = source.getClasses(); + expect(classes.length).toBe(2); + + const cls = classes[0]; + const structure = cls.getStructure(); + expect(structure.properties?.length).toBe(2); + expect(structure.properties?.[1].name).toBe("categoryWithInt"); + expect(structure.properties?.[1].type).toBe("number"); + expect(structure.properties?.[1].decorators?.length).toBe(4); + const decorators = structure.properties?.[1].decorators?.map((dec) => dec.name); + expect(decorators).toContain("Transform"); + expect(decorators).toContain("IsInt"); + expect(decorators).not.toContain("IsUUID"); + expect(decorators).not.toContain("IsString"); + orm.close(); + }); }); });