Skip to content

Commit

Permalink
CRUD Generator: Correctly support type: "text" fields in input
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyomair committed Dec 7, 2023
1 parent 1d48d68 commit 6b240a0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-cherries-chew.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 input
45 changes: 45 additions & 0 deletions packages/api/cms-api/src/generator/generate-crud-input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ export class TestEntityWithUuid extends BaseEntity<TestEntityWithUuid, "id"> {
@Property({ type: "uuid" })
fooId: string;
}

@Entity()
export class TestEntityWithTextRuntimeType extends BaseEntity<TestEntityWithTextRuntimeType, "id"> {
@PrimaryKey({ type: "uuid" })
id: string = uuid();

@Property({ type: "text" })
title: string;
}

describe("GenerateCrudInput", () => {
describe("string input class", () => {
it("should be a valid generated ts file", async () => {
Expand Down Expand Up @@ -231,4 +241,39 @@ describe("GenerateCrudInput", () => {
orm.close();
});
});

describe("text type input class", () => {
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 generateCrudInput({ targetDirectory: __dirname }, orm.em.getMetadata().get("TestEntityWithTextRuntimeType"));
const lintedOutput = await lintSource(out[0].content);
//console.log(lintedOutput);
const source = parseSource(lintedOutput);

const classes = source.getClasses();
expect(classes.length).toBe(2);

const cls = classes[0];
const structure = cls.getStructure();

expect(structure.properties?.length).toBe(1);
{
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const prop = structure.properties![0];
expect(prop.name).toBe("title");
expect(prop.type).toBe("string");
const decorators = prop.decorators?.map((i) => i.name);
expect(decorators).toContain("Field");
expect(decorators).toContain("IsString");
expect(decorators).toContain("IsNotEmpty");
}

orm.close();
});
});
});
1 change: 1 addition & 0 deletions packages/api/cms-api/src/generator/generate-crud-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export async function generateCrudInput(
decorators.push("@IsSlug()");
}
decorators.push(`@Field(${fieldOptions})`);
type = "string";
} else if (prop.type === "DecimalType" || prop.type == "BigIntType" || prop.type === "number") {
const initializer = morphTsProperty(prop.name, metadata).getInitializer()?.getText();
const defaultValue = prop.nullable && (initializer == "undefined" || initializer == "null") ? "null" : initializer;
Expand Down

0 comments on commit 6b240a0

Please sign in to comment.