-
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.
API Generator: Use correct type for
where
when getFindCondition
s…
…ervice method is not used (#1436) This caused problems if scope is used which is not possible to assign to {}. Before: `const where = {};` Now: `const where: ObjectQuery<TestEntity> = {};` If find/search is used: `const where = this.xxService.getFindCondition(...);` which also returns an ObjectQuery --------- Co-authored-by: Johannes Obermair <48853629+johnnyomair@users.noreply.github.com>
- Loading branch information
1 parent
76325f4
commit 1a170b9
Showing
3 changed files
with
57 additions
and
3 deletions.
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": patch | ||
--- | ||
|
||
API Generator: Use correct type for `where` when `getFindCondition` service method is not used |
49 changes: 49 additions & 0 deletions
49
packages/api/cms-api/src/generator/generate-crud-without-find.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,49 @@ | ||
import { BaseEntity, Embeddable, Embedded, Entity, PrimaryKey, Property } from "@mikro-orm/core"; | ||
import { MikroORM } from "@mikro-orm/postgresql"; | ||
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"; | ||
|
||
@Embeddable() | ||
export class TestEntityScope { | ||
@Property({ columnType: "text" }) | ||
language: string; | ||
} | ||
|
||
@Entity() | ||
export class TestEntity extends BaseEntity<TestEntity, "id"> { | ||
@PrimaryKey({ type: "uuid" }) | ||
id: string = uuid(); | ||
|
||
@Embedded(() => TestEntityScope) | ||
scope: TestEntityScope; | ||
} | ||
|
||
describe("GenerateCrud without find condition", () => { | ||
it("where should be typed as ObjectQuery as it is when findCondition is used", async () => { | ||
LazyMetadataStorage.load(); | ||
const orm = await MikroORM.init({ | ||
type: "postgresql", | ||
dbName: "test-db", | ||
entities: [TestEntity, TestEntityScope], | ||
}); | ||
|
||
const out = await generateCrud({ targetDirectory: __dirname }, orm.em.getMetadata().get("TestEntity")); | ||
const lintedOut = await lintGeneratedFiles(out); | ||
|
||
{ | ||
const file = lintedOut.find((file) => file.name === "test-entity.resolver.ts"); | ||
if (!file) throw new Error("File not found"); | ||
const source = parseSource(file.content); | ||
|
||
const cls = source.getClassOrThrow("TestEntityResolver"); | ||
const testEntitiesQuery = cls.getInstanceMethodOrThrow("testEntities"); | ||
const bodyText = testEntitiesQuery.getBodyText(); | ||
expect(bodyText).toContain("const where: ObjectQuery<TestEntity> = {};"); | ||
} | ||
|
||
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