mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-15 20:17:22 +00:00
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:
@@ -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;");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user