-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CRUD Generator: Add support for enum arrays in input (#1454)
Co-authored-by: Johannes Obermair <48853629+johnnyomair@users.noreply.github.com>
- Loading branch information
1 parent
af7af20
commit 8d0e3ee
Showing
4 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@comet/cms-api": minor | ||
--- | ||
|
||
CRUD Generator: Add support for enum arrays in input |
60 changes: 60 additions & 0 deletions
60
packages/api/cms-api/src/generator/generate-crud-enum-array.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { BaseEntity, Entity, Enum, PrimaryKey } from "@mikro-orm/core"; | ||
import { MikroORM } from "@mikro-orm/postgresql"; | ||
import { Field, registerEnumType } from "@nestjs/graphql"; | ||
import { LazyMetadataStorage } from "@nestjs/graphql/dist/schema-builder/storages/lazy-metadata.storage"; | ||
import { v4 as uuid } from "uuid"; | ||
|
||
import { generateCrud } from "./generate-crud"; | ||
import { lintGeneratedFiles, parseSource } from "./utils/test-helper"; | ||
|
||
export enum TestEnum { | ||
ONE = "ONE", | ||
TWO = "TWO", | ||
} | ||
registerEnumType(TestEnum, { | ||
name: "TestEnum", | ||
}); | ||
|
||
@Entity() | ||
class TestEntity extends BaseEntity<TestEntity, "id"> { | ||
@PrimaryKey({ type: "uuid" }) | ||
id: string = uuid(); | ||
|
||
@Field(() => [TestEnum]) | ||
@Enum({ items: () => TestEnum, array: true }) | ||
foo?: TestEnum; | ||
} | ||
|
||
describe("GenerateCrudEnumArray", () => { | ||
it("should correctly add EnumArrayType in input type", async () => { | ||
LazyMetadataStorage.load(); | ||
const orm = await MikroORM.init({ | ||
type: "postgresql", | ||
dbName: "test-db", | ||
entities: [TestEntity], | ||
}); | ||
|
||
const out = await generateCrud({ targetDirectory: __dirname }, orm.em.getMetadata().get("TestEntity")); | ||
const lintedOut = await lintGeneratedFiles(out); | ||
const file = lintedOut.find((file) => file.name === "dto/test-entity.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(1); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const prop = structure.properties![0]; | ||
expect(prop.name).toBe("foo"); | ||
expect(prop.type).toBe("TestEnum[]"); | ||
const decorators = prop.decorators?.map((i) => i.name); | ||
expect(decorators).toContain("Field"); | ||
expect(decorators).toContain("IsEnum"); | ||
|
||
orm.close(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters