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.
This commit is contained in:
dswbx
2025-09-30 13:32:22 +02:00
parent 0f54e8267f
commit 610e263477
2 changed files with 32 additions and 1 deletions

View File

@@ -21,4 +21,34 @@ describe("EntityTypescript", () => {
const et = new EntityTypescript(schema.proto.withConnection(new DummyConnection())); const et = new EntityTypescript(schema.proto.withConnection(new DummyConnection()));
expect(et.toString()).toContain('users?: DB["users"];'); 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;");
});
}); });

View File

@@ -84,9 +84,10 @@ export class RelationField extends Field<RelationFieldConfig> {
} }
override toType(): TFieldTSType { override toType(): TFieldTSType {
const type = this.config.target_field_type === "integer" ? "number" : "string";
return { return {
...super.toType(), ...super.toType(),
type: "number", type,
}; };
} }
} }