@nubitio/hydra 0.9.1 → 0.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -635,8 +635,14 @@ function resolveEntityValueField(relatedSchema) {
635
635
  function isStringLikeField(field) {
636
636
  return field.range === void 0 || field.range === "xmls:string" || field.range === "xsd:string";
637
637
  }
638
- function resolveEntityTextField(relatedSchema, valueField) {
639
- if (!relatedSchema) return "name";
638
+ function resolveEntityTextField(relatedSchema, valueField, relationTextFieldHint) {
639
+ if (!relatedSchema) return relationTextFieldHint ?? "name";
640
+ if (relationTextFieldHint) {
641
+ const hinted = relatedSchema.fields.find((field) => field.name === relationTextFieldHint);
642
+ if (hinted) return hinted.name;
643
+ }
644
+ const declared = relatedSchema.fields.find((field) => field.crudHints?.displayField === true);
645
+ if (declared) return declared.name;
640
646
  for (const fieldName of [
641
647
  "name",
642
648
  "nombre",
@@ -715,7 +721,25 @@ function mapHydraSchemaToFields(schema, urlLookup, schemaLookup) {
715
721
  const hydraTitle = fieldSchema["hydra:title"];
716
722
  const label = hydraTitle && hydraTitle.trim() !== "" && hydraTitle !== name ? hydraTitle : toLabel(name);
717
723
  const filterable = filterableProperties.size === 0 ? true : filterableProperties.has(name);
724
+ const tag = resolveRangeTag(range, propertyType);
718
725
  if (!writeable) {
726
+ if (tag === "entity" || propertyType === "Link") {
727
+ const resourceClass = range ? range.replace("#", "") : "";
728
+ const relatedSchema = resourceClass ? schemaLookup?.(resourceClass) : void 0;
729
+ const relatedTextField = resolveEntityTextField(relatedSchema, resolveEntityValueField(relatedSchema), fieldSchema.crudHints?.textField);
730
+ const field = {
731
+ ...(0, _nubitio_crud.noneField)().name(name).label(label).required(required).formatter((cell) => {
732
+ const value = cell.value;
733
+ if (value == null) return "";
734
+ if (typeof value !== "object") return String(value);
735
+ return String(value[relatedTextField] ?? "");
736
+ }).build(),
737
+ filterable
738
+ };
739
+ stampMappingReason(field, "rule-3 display-only entity-ref", applyCrudHints(field, fieldSchema.crudHints));
740
+ fields.push(field);
741
+ continue;
742
+ }
719
743
  const field = {
720
744
  ...(fieldSchema.crudHints?.format === "currency" ? (0, _nubitio_crud.currencyField)().readonly(true) : (0, _nubitio_crud.noneField)()).name(name).label(label).required(required).build(),
721
745
  filterable
@@ -724,7 +748,6 @@ function mapHydraSchemaToFields(schema, urlLookup, schemaLookup) {
724
748
  fields.push(field);
725
749
  continue;
726
750
  }
727
- const tag = resolveRangeTag(range, propertyType);
728
751
  const enumOptions = fieldSchema.enumOptions;
729
752
  if (enumOptions && enumOptions.length > 0 && (tag === "text" || tag === "integer" || tag === "decimal")) {
730
753
  const field = {
@@ -801,7 +824,7 @@ function mapHydraSchemaToFields(schema, urlLookup, schemaLookup) {
801
824
  const url = resourceClass ? urlLookup?.(resourceClass) ?? `/api/${pluralize(toDashCase(resourceClass))}` : "";
802
825
  const valueField = resolveEntityValueField(relatedSchema);
803
826
  const field = {
804
- ...(0, _nubitio_crud.entityField)(url, valueField, resolveEntityTextField(relatedSchema, valueField)).name(name).label(label).required(required).build(),
827
+ ...(0, _nubitio_crud.entityField)(url, valueField, resolveEntityTextField(relatedSchema, valueField, fieldSchema.crudHints?.textField)).name(name).label(label).required(required).build(),
805
828
  filterable
806
829
  };
807
830
  stampMappingReason(field, `rule-8 entity → ${url || "unknown-url"}`, applyCrudHints(field, fieldSchema.crudHints));
package/dist/index.d.cts CHANGED
@@ -90,6 +90,33 @@ interface CrudHints {
90
90
  summable?: boolean;
91
91
  /** Aggregate function for summable columns. @default 'sum' */
92
92
  summaryType?: 'sum' | 'count' | 'avg' | 'min' | 'max';
93
+ /**
94
+ * Set on an entity-relation property (e.g. `Cotizacion::$proveedor`).
95
+ * Names the property of the *related* resource to display in pickers,
96
+ * columns and form labels for this relation specifically — takes priority
97
+ * over `displayField` and the built-in name-heuristic. Use this when the
98
+ * same related entity should show a different field depending on which
99
+ * relation is pointing at it; for the common case (one natural label per
100
+ * entity), prefer `displayField` on the related entity itself so every
101
+ * relation to it picks it up automatically.
102
+ *
103
+ * #[ApiProperty(openapiContext: ['x-crud' => ['textField' => 'razonSocial']])]
104
+ * private ?Actor $proveedor = null;
105
+ */
106
+ textField?: string;
107
+ /**
108
+ * Set on a property of the entity being *referenced* (e.g.
109
+ * `CentroCosto::$nombre`), not on the relation that points to it. Declares
110
+ * "this is the natural human-readable label for this entity" once, so
111
+ * every relation to it — anywhere in the app, present or future — resolves
112
+ * to the right field without per-relation configuration. Falls back to the
113
+ * built-in name-heuristic (`name`, `nombre`, `businessName`, `description`,
114
+ * `descripcion`, …) when no field on the related entity sets this.
115
+ *
116
+ * #[ApiProperty(openapiContext: ['x-crud' => ['displayField' => true]])]
117
+ * private string $nombre = '';
118
+ */
119
+ displayField?: boolean;
93
120
  }
94
121
  interface HydraSupportedProperty {
95
122
  '@type': 'SupportedProperty';
package/dist/index.d.mts CHANGED
@@ -90,6 +90,33 @@ interface CrudHints {
90
90
  summable?: boolean;
91
91
  /** Aggregate function for summable columns. @default 'sum' */
92
92
  summaryType?: 'sum' | 'count' | 'avg' | 'min' | 'max';
93
+ /**
94
+ * Set on an entity-relation property (e.g. `Cotizacion::$proveedor`).
95
+ * Names the property of the *related* resource to display in pickers,
96
+ * columns and form labels for this relation specifically — takes priority
97
+ * over `displayField` and the built-in name-heuristic. Use this when the
98
+ * same related entity should show a different field depending on which
99
+ * relation is pointing at it; for the common case (one natural label per
100
+ * entity), prefer `displayField` on the related entity itself so every
101
+ * relation to it picks it up automatically.
102
+ *
103
+ * #[ApiProperty(openapiContext: ['x-crud' => ['textField' => 'razonSocial']])]
104
+ * private ?Actor $proveedor = null;
105
+ */
106
+ textField?: string;
107
+ /**
108
+ * Set on a property of the entity being *referenced* (e.g.
109
+ * `CentroCosto::$nombre`), not on the relation that points to it. Declares
110
+ * "this is the natural human-readable label for this entity" once, so
111
+ * every relation to it — anywhere in the app, present or future — resolves
112
+ * to the right field without per-relation configuration. Falls back to the
113
+ * built-in name-heuristic (`name`, `nombre`, `businessName`, `description`,
114
+ * `descripcion`, …) when no field on the related entity sets this.
115
+ *
116
+ * #[ApiProperty(openapiContext: ['x-crud' => ['displayField' => true]])]
117
+ * private string $nombre = '';
118
+ */
119
+ displayField?: boolean;
93
120
  }
94
121
  interface HydraSupportedProperty {
95
122
  '@type': 'SupportedProperty';
package/dist/index.mjs CHANGED
@@ -611,8 +611,14 @@ function resolveEntityValueField(relatedSchema) {
611
611
  function isStringLikeField(field) {
612
612
  return field.range === void 0 || field.range === "xmls:string" || field.range === "xsd:string";
613
613
  }
614
- function resolveEntityTextField(relatedSchema, valueField) {
615
- if (!relatedSchema) return "name";
614
+ function resolveEntityTextField(relatedSchema, valueField, relationTextFieldHint) {
615
+ if (!relatedSchema) return relationTextFieldHint ?? "name";
616
+ if (relationTextFieldHint) {
617
+ const hinted = relatedSchema.fields.find((field) => field.name === relationTextFieldHint);
618
+ if (hinted) return hinted.name;
619
+ }
620
+ const declared = relatedSchema.fields.find((field) => field.crudHints?.displayField === true);
621
+ if (declared) return declared.name;
616
622
  for (const fieldName of [
617
623
  "name",
618
624
  "nombre",
@@ -691,7 +697,25 @@ function mapHydraSchemaToFields(schema, urlLookup, schemaLookup) {
691
697
  const hydraTitle = fieldSchema["hydra:title"];
692
698
  const label = hydraTitle && hydraTitle.trim() !== "" && hydraTitle !== name ? hydraTitle : toLabel(name);
693
699
  const filterable = filterableProperties.size === 0 ? true : filterableProperties.has(name);
700
+ const tag = resolveRangeTag(range, propertyType);
694
701
  if (!writeable) {
702
+ if (tag === "entity" || propertyType === "Link") {
703
+ const resourceClass = range ? range.replace("#", "") : "";
704
+ const relatedSchema = resourceClass ? schemaLookup?.(resourceClass) : void 0;
705
+ const relatedTextField = resolveEntityTextField(relatedSchema, resolveEntityValueField(relatedSchema), fieldSchema.crudHints?.textField);
706
+ const field = {
707
+ ...noneField().name(name).label(label).required(required).formatter((cell) => {
708
+ const value = cell.value;
709
+ if (value == null) return "";
710
+ if (typeof value !== "object") return String(value);
711
+ return String(value[relatedTextField] ?? "");
712
+ }).build(),
713
+ filterable
714
+ };
715
+ stampMappingReason(field, "rule-3 display-only entity-ref", applyCrudHints(field, fieldSchema.crudHints));
716
+ fields.push(field);
717
+ continue;
718
+ }
695
719
  const field = {
696
720
  ...(fieldSchema.crudHints?.format === "currency" ? currencyField().readonly(true) : noneField()).name(name).label(label).required(required).build(),
697
721
  filterable
@@ -700,7 +724,6 @@ function mapHydraSchemaToFields(schema, urlLookup, schemaLookup) {
700
724
  fields.push(field);
701
725
  continue;
702
726
  }
703
- const tag = resolveRangeTag(range, propertyType);
704
727
  const enumOptions = fieldSchema.enumOptions;
705
728
  if (enumOptions && enumOptions.length > 0 && (tag === "text" || tag === "integer" || tag === "decimal")) {
706
729
  const field = {
@@ -777,7 +800,7 @@ function mapHydraSchemaToFields(schema, urlLookup, schemaLookup) {
777
800
  const url = resourceClass ? urlLookup?.(resourceClass) ?? `/api/${pluralize(toDashCase(resourceClass))}` : "";
778
801
  const valueField = resolveEntityValueField(relatedSchema);
779
802
  const field = {
780
- ...entityField(url, valueField, resolveEntityTextField(relatedSchema, valueField)).name(name).label(label).required(required).build(),
803
+ ...entityField(url, valueField, resolveEntityTextField(relatedSchema, valueField, fieldSchema.crudHints?.textField)).name(name).label(label).required(required).build(),
781
804
  filterable
782
805
  };
783
806
  stampMappingReason(field, `rule-8 entity → ${url || "unknown-url"}`, applyCrudHints(field, fieldSchema.crudHints));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nubitio/hydra",
3
- "version": "0.9.1",
3
+ "version": "0.9.3",
4
4
  "type": "module",
5
5
  "description": "Hydra / OpenAPI adapter for @nubitio/crud — automatic schema discovery and data source from API Platform docs.",
6
6
  "license": "MIT",
@@ -50,7 +50,7 @@
50
50
  "react": "^18.2.0 || ^19.0.0",
51
51
  "react-dom": "^18.2.0 || ^19.0.0",
52
52
  "react-i18next": "^14.0.0",
53
- "@nubitio/core": "^0.9.1",
54
- "@nubitio/crud": "^0.9.1"
53
+ "@nubitio/crud": "^0.9.3",
54
+ "@nubitio/core": "^0.9.3"
55
55
  }
56
56
  }