From dfb3c8409a2ef0852e02acfa67dc49fca6123195 Mon Sep 17 00:00:00 2001 From: Niko Sams Date: Thu, 14 Dec 2023 12:25:26 +0100 Subject: [PATCH] CRUD Generator: Correctly support `type: "text"` fields in filter and sort (#1492) Fixes test added in https://github.com/vivid-planet/comet/pull/1491 (although differently) --------- Co-authored-by: Johannes Obermair Co-authored-by: Johannes Obermair <48853629+johnnyomair@users.noreply.github.com> --- .changeset/lovely-rats-rest.md | 5 +++ .../src/generator/generate-crud.spec.ts | 42 +++++++++++++++++++ .../cms-api/src/generator/generate-crud.ts | 8 ++-- 3 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 .changeset/lovely-rats-rest.md diff --git a/.changeset/lovely-rats-rest.md b/.changeset/lovely-rats-rest.md new file mode 100644 index 0000000000..abfd6572f3 --- /dev/null +++ b/.changeset/lovely-rats-rest.md @@ -0,0 +1,5 @@ +--- +"@comet/cms-api": patch +--- + +CRUD Generator: Correctly support `type: "text"` fields in filter and sort diff --git a/packages/api/cms-api/src/generator/generate-crud.spec.ts b/packages/api/cms-api/src/generator/generate-crud.spec.ts index 03317c1ba5..a3af4476a0 100644 --- a/packages/api/cms-api/src/generator/generate-crud.spec.ts +++ b/packages/api/cms-api/src/generator/generate-crud.spec.ts @@ -23,6 +23,15 @@ export class TestEntityWithNumber extends BaseEntity foo: number; } +@Entity() +export class TestEntityWithTextRuntimeType extends BaseEntity { + @PrimaryKey({ type: "uuid" }) + id: string = uuid(); + + @Property({ type: "text" }) + title: string; +} + describe("GenerateCrud", () => { describe("resolver class", () => { it("should be a valid generated ts file", async () => { @@ -122,4 +131,37 @@ describe("GenerateCrud", () => { orm.close(); }); }); + + describe("text type filter", () => { + it("should be a valid generated ts file", async () => { + LazyMetadataStorage.load(); + const orm = await MikroORM.init({ + type: "postgresql", + dbName: "test-db", + entities: [TestEntityWithTextRuntimeType], + }); + + const out = await generateCrud({ targetDirectory: __dirname }, orm.em.getMetadata().get("TestEntityWithTextRuntimeType")); + const lintedOut = await lintGeneratedFiles(out); + const file = lintedOut.find((file) => file.name === "dto/test-entity-with-text-runtime-type.filter.ts"); + if (!file) throw new Error("File not found"); + + const source = parseSource(file.content); + + const classes = source.getClasses(); + expect(classes.length).toBe(1); + + const cls = classes[0]; + expect(cls.getName()).toBe("TestEntityWithTextRuntimeTypeFilter"); + const structure = cls.getStructure(); + + expect(structure.properties?.length).toBe(3); + if (!structure.properties || !structure.properties[0]) throw new Error("property not found"); + const filterProp = structure.properties[0]; + expect(filterProp.name).toBe("title"); + expect(filterProp.type).toBe("StringFilter"); + + orm.close(); + }); + }); }); diff --git a/packages/api/cms-api/src/generator/generate-crud.ts b/packages/api/cms-api/src/generator/generate-crud.ts index 55aa43ed7e..82a646f29b 100644 --- a/packages/api/cms-api/src/generator/generate-crud.ts +++ b/packages/api/cms-api/src/generator/generate-crud.ts @@ -18,7 +18,7 @@ function buildOptions(metadata: EntityMetadata) { const crudSearchPropNames = metadata.props .filter((prop) => hasFieldFeature(metadata.class, prop.name, "search") && !prop.name.startsWith("scope_")) .reduce((acc, prop) => { - if (prop.type === "string") { + if (prop.type === "string" || prop.type === "text") { acc.push(prop.name); } else if (prop.reference == "m:1") { if (!prop.targetMeta) { @@ -28,7 +28,7 @@ function buildOptions(metadata: EntityMetadata) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion .filter((innerProp) => hasFieldFeature(prop.targetMeta!.class, innerProp.name, "search") && !innerProp.name.startsWith("scope_")) .forEach((innerProp) => { - if (innerProp.type === "string") { + if (innerProp.type === "string" || innerProp.type === "text") { acc.push(`${prop.name}.${innerProp.name}`); } }); @@ -43,6 +43,7 @@ function buildOptions(metadata: EntityMetadata) { !prop.name.startsWith("scope_") && (prop.enum || prop.type === "string" || + prop.type === "text" || prop.type === "DecimalType" || prop.type === "number" || integerTypes.includes(prop.type) || @@ -58,6 +59,7 @@ function buildOptions(metadata: EntityMetadata) { hasFieldFeature(metadata.class, prop.name, "sort") && !prop.name.startsWith("scope_") && (prop.type === "string" || + prop.type === "text" || prop.type === "DecimalType" || prop.type === "number" || integerTypes.includes(prop.type) || @@ -140,7 +142,7 @@ function generateFilterDto({ generatorOptions, metadata }: { generatorOptions: C @Type(() => ${enumName}EnumFilter) ${prop.name}?: ${enumName}EnumFilter; `; - } else if (prop.type === "string") { + } else if (prop.type === "string" || prop.type === "text") { return `@Field(() => StringFilter, { nullable: true }) @ValidateNested() @IsOptional()