Skip to content

Commit

Permalink
API Generator: Support relation with primary key type int (in addit…
Browse files Browse the repository at this point in the history
…ion to `integer`) (#2118)

improves test from #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 <[email protected]>
Co-authored-by: Niko Sams <[email protected]>
Co-authored-by: Niko Sams <[email protected]>
Co-authored-by: Johannes Obermair <[email protected]>
  • Loading branch information
5 people authored Jun 12, 2024
1 parent 04fffd1 commit b925f94
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/nine-comics-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@comet/cms-api": patch
---

API Generator: Support relation with primary key type `int` (in addition to `integer`)
2 changes: 1 addition & 1 deletion packages/api/cms-api/src/generator/generate-crud-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@ class TestEntityCategoryWithIntegerId extends BaseEntity<TestEntityCategoryWithI
@PrimaryKey({ columnType: "int", type: "integer" })
id: number;

@OneToMany(() => TestEntityProduct, (products) => products.category)
@OneToMany(() => TestEntityProduct, (products) => products.categoryWithInteger)
products = new Collection<TestEntityProduct>(this);
}

@Entity()
class TestEntityCategoryWithIntId extends BaseEntity<TestEntityCategoryWithIntId, "id"> {
@PrimaryKey({ columnType: "int", type: "int" })
id: number;

@OneToMany(() => TestEntityProduct, (products) => products.categoryWithInt)
products = new Collection<TestEntityProduct>(this);
}

Expand All @@ -21,17 +30,20 @@ class TestEntityProduct extends BaseEntity<TestEntityProduct, "id"> {
id: string = uuid();

@ManyToOne(() => TestEntityCategoryWithIntegerId, { nullable: true, ref: true })
category?: Ref<TestEntityCategoryWithIntegerId>;
categoryWithInteger?: Ref<TestEntityCategoryWithIntegerId>;

@ManyToOne(() => TestEntityCategoryWithIntId, { nullable: true, ref: true })
categoryWithInt?: Ref<TestEntityCategoryWithIntId>;
}

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"));
Expand All @@ -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);
Expand All @@ -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();
});
});
});

0 comments on commit b925f94

Please sign in to comment.