refactor: update id handling for media entity

Revised `id` and `entity_id` types to support both string and number for flexibility. Adjusted `PolymorphicRelation` to use `PrimaryFieldType` for improved type safety and maintainability.
This commit is contained in:
dswbx
2025-09-19 11:28:51 +02:00
parent 91120091a3
commit 059becbf09
3 changed files with 5 additions and 6 deletions

View File

@@ -6,6 +6,7 @@ import { EntityRelation, type KyselyJsonFrom, type KyselyQueryBuilder } from "./
import { EntityRelationAnchor } from "./EntityRelationAnchor";
import { type RelationType, RelationTypes } from "./relation-types";
import { s } from "bknd/utils";
import type { PrimaryFieldType } from "bknd";
export type PolymorphicRelationConfig = s.Static<typeof PolymorphicRelation.schema>;
@@ -70,7 +71,7 @@ export class PolymorphicRelation extends EntityRelation<typeof PolymorphicRelati
.groupBy(groupBy);
}
override getReferenceQuery(entity: Entity, id: number): Partial<RepoQuery> {
override getReferenceQuery(entity: Entity, id: PrimaryFieldType): Partial<RepoQuery> {
const info = this.queryInfo(entity);
return {

View File

@@ -181,16 +181,14 @@ export class MediaController extends Controller {
"param",
s.object({
entity: entitiesEnum,
id: s.number(),
id: s.anyOf([s.number(), s.string()]),
field: s.string(),
}),
),
jsc("query", s.object({ overwrite: s.boolean().optional() })),
permission([DataPermissions.entityCreate, MediaPermissions.uploadFile]),
async (c) => {
const entity_name = c.req.param("entity");
const field_name = c.req.param("field");
const entity_id = Number.parseInt(c.req.param("id"));
const { entity: entity_name, id: entity_id, field: field_name } = c.req.valid("param");
// check if entity exists
const entity = this.media.em.entity(entity_name);

View File

@@ -9,6 +9,6 @@ export const mediaFields = {
etag: text(),
modified_at: datetime(),
reference: text(),
entity_id: number(),
entity_id: text(),
metadata: json(),
};