Skip to content

Commit

Permalink
CRUD Generator: Correctly support type: "text" fields in filter and…
Browse files Browse the repository at this point in the history
… sort (#1492)

Fixes test added in #1491
(although differently)

---------

Co-authored-by: Johannes Obermair <johannes.obermair@vivid-planet.com>
Co-authored-by: Johannes Obermair <48853629+johnnyomair@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 14, 2023
1 parent 0fdf4ea commit dfb3c84
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-rats-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@comet/cms-api": patch
---

CRUD Generator: Correctly support `type: "text"` fields in filter and sort
42 changes: 42 additions & 0 deletions packages/api/cms-api/src/generator/generate-crud.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ export class TestEntityWithNumber extends BaseEntity<TestEntityWithNumber, "id">
foo: number;
}

@Entity()
export class TestEntityWithTextRuntimeType extends BaseEntity<TestEntityWithTextRuntimeType, "id"> {
@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 () => {
Expand Down Expand Up @@ -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();
});
});
});
8 changes: 5 additions & 3 deletions packages/api/cms-api/src/generator/generate-crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function buildOptions(metadata: EntityMetadata<any>) {
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) {
Expand All @@ -28,7 +28,7 @@ function buildOptions(metadata: EntityMetadata<any>) {
// 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}`);
}
});
Expand All @@ -43,6 +43,7 @@ function buildOptions(metadata: EntityMetadata<any>) {
!prop.name.startsWith("scope_") &&
(prop.enum ||
prop.type === "string" ||
prop.type === "text" ||
prop.type === "DecimalType" ||
prop.type === "number" ||
integerTypes.includes(prop.type) ||
Expand All @@ -58,6 +59,7 @@ function buildOptions(metadata: EntityMetadata<any>) {
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) ||
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit dfb3c84

Please sign in to comment.