From 610e2634776a7e009e28e505f63b57e95abe80f1 Mon Sep 17 00:00:00 2001 From: dswbx Date: Tue, 30 Sep 2025 13:32:22 +0200 Subject: [PATCH] fix: handle correct type conversion for relation fields Updated `RelationField` to dynamically determine the type based on `target_field_type`. Added a test for proper TypeScript generation with text primary fields in system entities. --- .../data/entities/EntityTypescript.spec.ts | 30 +++++++++++++++++++ app/src/data/relations/RelationField.ts | 3 +- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/app/src/data/entities/EntityTypescript.spec.ts b/app/src/data/entities/EntityTypescript.spec.ts index 7afa092..7241659 100644 --- a/app/src/data/entities/EntityTypescript.spec.ts +++ b/app/src/data/entities/EntityTypescript.spec.ts @@ -21,4 +21,34 @@ describe("EntityTypescript", () => { const et = new EntityTypescript(schema.proto.withConnection(new DummyConnection())); expect(et.toString()).toContain('users?: DB["users"];'); }); + + it("should generate correct typescript for system entities with uuid primary field", () => { + const schema = proto.em( + { + test: proto.entity( + "test", + { + name: proto.text(), + }, + { + primary_format: "uuid", + }, + ), + users: proto.systemEntity( + "users", + { + name: proto.text(), + }, + { + primary_format: "uuid", + }, + ), + }, + ({ relation }, { test, users }) => { + relation(test).manyToOne(users); + }, + ); + const et = new EntityTypescript(schema.proto.withConnection(new DummyConnection())); + expect(et.toString()).toContain("users_id?: string;"); + }); }); diff --git a/app/src/data/relations/RelationField.ts b/app/src/data/relations/RelationField.ts index 3a2ab07..f6e4c0d 100644 --- a/app/src/data/relations/RelationField.ts +++ b/app/src/data/relations/RelationField.ts @@ -84,9 +84,10 @@ export class RelationField extends Field { } override toType(): TFieldTSType { + const type = this.config.target_field_type === "integer" ? "number" : "string"; return { ...super.toType(), - type: "number", + type, }; } }