Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/code-generator/CodeFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ export class CodeFile {
const iw = new CodeWriter();
const tw = new CodeWriter();

const selfName = path.basename(this.fileName, path.extname(this.fileName));
for (const [module, names] of Object.entries(this.imports)) {
const moduleName = path.basename(module, path.extname(module));
if (module.startsWith("./") && moduleName === selfName) continue;
iw.write(`import { ${[...names].join(", ")} } from "${module}";`);
iw.write("\n");
}
Expand Down
33 changes: 33 additions & 0 deletions src/schema/schema-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -661,4 +661,37 @@ describe("schema generator tests", () => {
expect(code).toMatch(/implements\s+Auditable\b/);
expect(code).toMatch(/from "\.\/Auditable"/);
});

it("should not emit self-imports for relations targeting the entity itself", () => {
const Category = defineEntity({
name: "Category",
fields: [
{
name: "name",
type: "String",
required: true,
},
{
name: "parent",
type: "ManyToOne",
target: "Category",
},
{
name: "children",
type: "OneToMany",
target: "Category",
mappedBy: "parent",
},
],
});

generateSchema(outDir, { schema: [Category] });
const code = fs.readFileSync(path.join(outDir, "Category.ts"), {
encoding: "utf-8",
});

expect(code).not.toMatch(/from "\.\/Category"/);
expect(code).toMatch(/parent\?:\s*Relation<Category>/);
expect(code).toMatch(/children\?:\s*Relation<Category>\[\]/);
});
});