@sincpro/web-framework 2.1.0 → 2.3.0

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/README.md CHANGED
@@ -92,7 +92,7 @@ flowchart LR
92
92
  subgraph cf["@sincpro/client-framework · every platform"]
93
93
  direction TB
94
94
  wire["Transport · restTransport · entity()<br/>client contract · model definitions"]
95
- services["UseBoundedContext · Feature · ApplicationService<br/>configureProcess · processContext · ContextStore<br/>a span, a report, a context per run"]
95
+ services["BoundedContext · Feature · ApplicationService · EventHandler<br/>configureProcess · processContext · ContextStore<br/>a span, a report, a context per run"]
96
96
  generator["sincpro-client generate<br/>domain · client · entities"]
97
97
  end
98
98
  subgraph fw["@sincpro/web-framework · the web"]
@@ -225,7 +225,7 @@ export const invoices = entity<Invoice>("sales:invoice", {
225
225
  create: "POST /invoices",
226
226
  save: "PUT /invoices/{id}",
227
227
  commands: { post: "POST /invoices/{id}/post" },
228
- history: { route: "/sales-events", type: "Invoice" },
228
+ history: "/invoices/{id}/events", // its DomainEvents: a record's facts
229
229
  });
230
230
  ```
231
231
 
@@ -15,8 +15,8 @@ import { type Entity, type Transport } from "@sincpro/client-framework";
15
15
  *
16
16
  * A write forgets every collection its route NAMES, not only the first: «leave a tenant»
17
17
  * (`POST /users/{email}/tenants/{tenant}/leave`) changes the tenant's members as much as the
18
- * user. And an entity's business events live on a route of their own (`/project-events`): with
19
- * `entities`, a write under an entity's list also forgets its history.
18
+ * user. And a record's business events live on a route of their own (`/issues/{id}/events`):
19
+ * with `entities`, a write under an entity's list also forgets its history, wherever it lives.
20
20
  *
21
21
  * const transport = cachedTransport(restTransport({ base: "/v1" }), { ttl: 30_000 });
22
22
  * transport.invalidate("/facturas"); // forget an entity by hand (a push from the server)
@@ -14,7 +14,8 @@ function touchedBy(route, entities) {
14
14
  const segments = path.split("/").filter((one) => one !== "");
15
15
  const history = entities
16
16
  .filter((one) => one.history !== undefined && under(path, pathOf(one.list)))
17
- .map((one) => pathOf(one.history?.route ?? ""));
17
+ // The route up to its first key: `/issue-events/{id}` forgets `/issue-events`.
18
+ .map((one) => pathOf(one.history ?? "").split("/{")[0] ?? "");
18
19
  return [...segments.map((one) => `/${one}`), ...history];
19
20
  }
20
21
  export function cachedTransport(transport, options = {}) {
@@ -48,7 +48,9 @@ export type FrameworkCopy = {
48
48
  keep: string;
49
49
  loading: string;
50
50
  /** Under an embedded list the record holds only one page of. */
51
- partial: (shown: number, total: number) => string;
51
+ /** A relation that brought one page of its records: how many it shows, of how many — a
52
+ * floor when `exact` is false («at least 1000»). */
53
+ partial: (shown: number, total: number, exact?: boolean) => string;
52
54
  /** The title of a record that does not exist yet, after the model's name. */
53
55
  creating: (name: string) => string;
54
56
  /** The list's button that opens the form of a new record. */
package/dist/core/copy.js CHANGED
@@ -26,7 +26,7 @@ export const ENGLISH_FRAMEWORK_COPY = {
26
26
  missing: (fields) => `Missing: ${fields.join(", ")}.`,
27
27
  keep: "Keep draft",
28
28
  loading: "Loading the record",
29
- partial: (shown, total) => `Showing ${shown} of ${total}.`,
29
+ partial: (shown, total, exact = true) => `Showing ${shown} of ${exact ? "" : "at least "}${total}.`,
30
30
  creating: (name) => (name ? `${name} · new` : "New"),
31
31
  create: "New",
32
32
  add: "Add",
@@ -96,7 +96,7 @@ export const SPANISH_FRAMEWORK_COPY = {
96
96
  missing: (fields) => `Falta: ${fields.join(", ")}.`,
97
97
  keep: "Guardar borrador",
98
98
  loading: "Cargando el registro",
99
- partial: (shown, total) => `Se muestran ${shown} de ${total}.`,
99
+ partial: (shown, total, exact = true) => `Se muestran ${shown} de ${exact ? "" : "al menos "}${total}.`,
100
100
  creating: (name) => (name ? `${name} · nuevo` : "Nuevo"),
101
101
  create: "Nuevo",
102
102
  add: "Agregar",
@@ -2,9 +2,10 @@ import { type AnyRecord, type Meta } from "@sincpro/criteria";
2
2
  /**
3
3
  * A record's history as people read it — its business events, newest first.
4
4
  *
5
- * Context: sincpro_framework keeps every context's facts in one event table, read over
6
- * `GET /<context>-events`, each fact naming the record it is about (`entity_type`,
7
- * `entity_id`) and carrying a `label` in every language — the framework's `history_of`.
5
+ * Context: sincpro_framework keeps every context's facts in one event table, and answers a
6
+ * record's own with `DomainEvents` (`GET /issues/{id}/events`, the entity's `history`), each
7
+ * fact naming the record it is about (`entity_type`, `entity_id`) and carrying a `label` in
8
+ * every language.
8
9
  * forge's console read it with a dictionary of field names written by hand; here the words for
9
10
  * the rest of a fact come from the events' own model definition, like any other field.
10
11
  *
@@ -13,12 +13,12 @@ export { type Advice, adviceAt, changesOf, computedLocally, creationOf, defaults
13
13
  export { explain } from "./explain.js";
14
14
  export { type FormLayout, formOf } from "./form.js";
15
15
  export { type HistoryEntry, historyOf } from "./history.js";
16
- export { addingOf, type AddingRule, blankLine, type Declared, editableLines, type LineAdding, lineFields, lineKey, linesAnswered, moved, sameLine, writtenInCell, } from "./lines.js";
16
+ export { addingOf, type AddingRule, blankLine, type Declared, editableLines, isReference, type LineAdding, lineFields, lineKey, linesAnswered, moved, sameLine, writtenInCell, } from "./lines.js";
17
17
  export { defineList, type GroupLevel, isList, type ListDefinition, type ListSpec, type NamedFilter, scopeOf, } from "./list.js";
18
18
  export { lookupOf, type LookupOption } from "./lookup.js";
19
19
  export { detailOf, measuresOf, sumsOf } from "./measure.js";
20
20
  export { BOOKKEEPING, defaultFields, type FieldView, sortKeyOf, textOf, viewOf, } from "./present.js";
21
21
  export { type Related, relatedOf } from "./related.js";
22
- export { entityFor, type RelationField, relationFieldOf, } from "./relation.js";
22
+ export { backReference, entityFor, relatedScope, type RelationField, relationFieldOf, } from "./relation.js";
23
23
  export { type RecordSummary, summaryFieldsOf, summaryOf, } from "./summary.js";
24
24
  export { placeOf, stepIn, type Trail } from "./trail.js";
@@ -13,12 +13,12 @@ export { adviceAt, changesOf, computedLocally, creationOf, defaultsOf, errorAt,
13
13
  export { explain } from "./explain.js";
14
14
  export { formOf } from "./form.js";
15
15
  export { historyOf } from "./history.js";
16
- export { addingOf, blankLine, editableLines, lineFields, lineKey, linesAnswered, moved, sameLine, writtenInCell, } from "./lines.js";
16
+ export { addingOf, blankLine, editableLines, isReference, lineFields, lineKey, linesAnswered, moved, sameLine, writtenInCell, } from "./lines.js";
17
17
  export { defineList, isList, scopeOf, } from "./list.js";
18
18
  export { lookupOf } from "./lookup.js";
19
19
  export { detailOf, measuresOf, sumsOf } from "./measure.js";
20
20
  export { BOOKKEEPING, defaultFields, sortKeyOf, textOf, viewOf, } from "./present.js";
21
21
  export { relatedOf } from "./related.js";
22
- export { entityFor, relationFieldOf, } from "./relation.js";
22
+ export { backReference, entityFor, relatedScope, relationFieldOf, } from "./relation.js";
23
23
  export { summaryFieldsOf, summaryOf, } from "./summary.js";
24
24
  export { placeOf, stepIn } from "./trail.js";
@@ -1,4 +1,4 @@
1
- import type { AnyRecord, Meta } from "@sincpro/criteria";
1
+ import type { AnyRecord, FieldMeta, Meta } from "@sincpro/criteria";
2
2
  /** The key a line is drawn and found by: its identity, or a local one while it has none. */
3
3
  export declare function lineKey(line: AnyRecord, identity: string): string;
4
4
  /** `next` is `line` edited: it keeps the line's local key. */
@@ -6,10 +6,18 @@ export declare function sameLine<T extends AnyRecord>(line: AnyRecord, next: T):
6
6
  /** What the server answered about the list it was sent, each line keeping its key by position. */
7
7
  export declare function linesAnswered(sent: readonly AnyRecord[], answered: readonly AnyRecord[]): AnyRecord[];
8
8
  /**
9
- * The lines a form can edit, or `null` when it holds only part of them: a list written back
10
- * whole from one page would delete every line the page did not bring.
9
+ * Whether a to-many holds other aggregates rather than parts of the record — sincpro_framework's
10
+ * `References[Invoice]`, or a foreign key declared `owned=False` (`reference: true` on its
11
+ * definition): listed and searched, never edited as lines, because nothing the record saves
12
+ * writes them.
11
13
  */
12
- export declare function editableLines(value: unknown): AnyRecord[] | null;
14
+ export declare function isReference(field: FieldMeta | undefined): boolean;
15
+ /**
16
+ * The lines a form can edit, or `null` when it holds only part of them — a list written back
17
+ * whole from one page would delete every line the page did not bring — or when they are not
18
+ * the record's to write (`isReference`): the record would save them and nothing would happen.
19
+ */
20
+ export declare function editableLines(value: unknown, field?: FieldMeta): AnyRecord[] | null;
13
21
  /** A blank line: the line model's defaults, never its identity — the server mints that. */
14
22
  export declare function blankLine(defaults: AnyRecord, identity: string): AnyRecord;
15
23
  /** The list with the line `key` moved before `before` (`null`: to the end). */
@@ -39,10 +39,22 @@ export function linesAnswered(sent, answered) {
39
39
  return answered.map((line, i) => (sent[i] === undefined ? line : sameLine(sent[i], line)));
40
40
  }
41
41
  /**
42
- * The lines a form can edit, or `null` when it holds only part of them: a list written back
43
- * whole from one page would delete every line the page did not bring.
42
+ * Whether a to-many holds other aggregates rather than parts of the record — sincpro_framework's
43
+ * `References[Invoice]`, or a foreign key declared `owned=False` (`reference: true` on its
44
+ * definition): listed and searched, never edited as lines, because nothing the record saves
45
+ * writes them.
44
46
  */
45
- export function editableLines(value) {
47
+ export function isReference(field) {
48
+ return field?.reference === true;
49
+ }
50
+ /**
51
+ * The lines a form can edit, or `null` when it holds only part of them — a list written back
52
+ * whole from one page would delete every line the page did not bring — or when they are not
53
+ * the record's to write (`isReference`): the record would save them and nothing would happen.
54
+ */
55
+ export function editableLines(value, field) {
56
+ if (isReference(field))
57
+ return null;
46
58
  if (Array.isArray(value))
47
59
  return value;
48
60
  const related = relatedOf(value);
@@ -6,7 +6,11 @@ import type { Count } from "@sincpro/criteria";
6
6
  * `{items, count, cursor}` — so a tenant with a thousand members does not answer all of them;
7
7
  * an older answer, or a domain declared by hand, sends a bare array. A screen that reads only
8
8
  * `items` and counts them shows the first page and calls it the total (forge's console did:
9
- * `count={repositories.length}`). This reads both shapes and keeps the real count.
9
+ * `count={repositories.length}`). This reads both shapes and keeps the real count — a floor
10
+ * («at least 1000») when the backend stopped counting at its cap.
11
+ *
12
+ * Built on @sincpro/client-framework's `EntityCollection` (`isCollection`, `isPartial`,
13
+ * `totalOf`): a screen keeps this shape, with `count` always said.
10
14
  *
11
15
  * relatedOf({ items: [a, b], count: { value: 1000, exact: true }, cursor: "x" })
12
16
  * -> { items: [a, b], count: { value: 1000, exact: true }, cursor: "x", partial: true }
@@ -1,4 +1,8 @@
1
+ import { isCollection, isPartial, totalOf, } from "@sincpro/client-framework";
1
2
  export function relatedOf(value) {
3
+ // The shapes, and what «partial» means, are @sincpro/client-framework's — the same rule as the
4
+ // backend's `is_partial` on every platform. Read without copying: a table reads this for every
5
+ // one2many cell of a page.
2
6
  if (Array.isArray(value))
3
7
  return {
4
8
  items: value,
@@ -6,16 +10,13 @@ export function relatedOf(value) {
6
10
  cursor: null,
7
11
  partial: false,
8
12
  };
9
- if (typeof value === "object" && value !== null && "items" in value) {
10
- const page = value;
11
- const items = page.items ?? [];
12
- const count = page.count ?? { value: items.length, exact: page.cursor == null };
13
- return {
14
- items,
15
- count,
16
- cursor: page.cursor ?? null,
17
- partial: count.value > items.length || !count.exact || page.cursor != null,
18
- };
19
- }
20
- return { items: [], count: { value: 0, exact: true }, cursor: null, partial: false };
13
+ if (!isCollection(value))
14
+ return { items: [], count: { value: 0, exact: true }, cursor: null, partial: false };
15
+ const page = value;
16
+ return {
17
+ items: (page.items ?? []),
18
+ count: totalOf(page),
19
+ cursor: page.cursor ?? null,
20
+ partial: isPartial(page),
21
+ };
21
22
  }
@@ -1,5 +1,5 @@
1
1
  import { type Entity } from "@sincpro/client-framework";
2
- import type { FieldMeta, Meta } from "@sincpro/criteria";
2
+ import type { AnyRecord, Criteria, FieldMeta, Meta } from "@sincpro/criteria";
3
3
  /**
4
4
  * Which record a field of a form is about, read from the model — so a form draws a many2one
5
5
  * as a picker of the right entity without being told.
@@ -36,3 +36,25 @@ export declare function relationFieldOf(meta: Meta | null | undefined, name: str
36
36
  * named by its own name (`sincpro-forge-auth.Tenant`) where the application keys it by alias.
37
37
  */
38
38
  export declare function entityFor(entities: readonly Entity[], target: string, metaOf?: (key: string) => Meta | undefined): Entity | undefined;
39
+ /**
40
+ * The field of the related records that points back at a record, and the value it holds for this
41
+ * one — what a list of them hides as a column and fills on a new one. `null` when the relation
42
+ * does not say. It reads the keys as @sincpro/client-framework's `relatedCriteria` asks by them.
43
+ */
44
+ export declare function backReference(record: AnyRecord | null, field: FieldMeta | undefined, identity: string): {
45
+ field: string;
46
+ value: unknown;
47
+ } | null;
48
+ /** Whether a value can point a relation at a record: an empty key points at the orphans. */
49
+ export declare function isKeyValue(value: unknown): boolean;
50
+ /**
51
+ * The criteria a relation of ONE record is listed with, over the related entity — or `null` while
52
+ * there is no key to ask by: the record or its definition has not arrived, or the record holds no
53
+ * value in that key yet (a new one, a business key not written). Never a question that would
54
+ * match every orphan.
55
+ *
56
+ * relatedScope(customer, meta.fields.invoices, "id", criteria)
57
+ * -> criteria AND customer_id = customer.id (identified_by)
58
+ * -> criteria AND customer_code = customer.code (parent_field / related_field)
59
+ */
60
+ export declare function relatedScope<T extends AnyRecord>(record: AnyRecord | null, field: FieldMeta | undefined, identity: string, criteria?: Criteria<T>): Criteria<T> | null;
@@ -1,4 +1,4 @@
1
- import { entityKeyOf, kindOf } from "@sincpro/client-framework";
1
+ import { entityKeyOf, kindOf, relatedCriteria } from "@sincpro/client-framework";
2
2
  export function relationFieldOf(meta, name) {
3
3
  if (!meta)
4
4
  return undefined;
@@ -56,3 +56,35 @@ export function entityFor(entities, target, metaOf = () => undefined) {
56
56
  entities.find((one) => namedBy(one, target)) ??
57
57
  (ofKind.length === 1 ? ofKind[0] : undefined));
58
58
  }
59
+ /**
60
+ * The field of the related records that points back at a record, and the value it holds for this
61
+ * one — what a list of them hides as a column and fills on a new one. `null` when the relation
62
+ * does not say. It reads the keys as @sincpro/client-framework's `relatedCriteria` asks by them.
63
+ */
64
+ export function backReference(record, field, identity) {
65
+ const there = field?.related_field ?? field?.identified_by;
66
+ if (record === null || !there)
67
+ return null;
68
+ const here = field?.related_field ? (field.parent_field ?? identity) : identity;
69
+ return { field: there, value: record[here] };
70
+ }
71
+ /** Whether a value can point a relation at a record: an empty key points at the orphans. */
72
+ export function isKeyValue(value) {
73
+ return value !== null && value !== undefined && value !== "";
74
+ }
75
+ /**
76
+ * The criteria a relation of ONE record is listed with, over the related entity — or `null` while
77
+ * there is no key to ask by: the record or its definition has not arrived, or the record holds no
78
+ * value in that key yet (a new one, a business key not written). Never a question that would
79
+ * match every orphan.
80
+ *
81
+ * relatedScope(customer, meta.fields.invoices, "id", criteria)
82
+ * -> criteria AND customer_id = customer.id (identified_by)
83
+ * -> criteria AND customer_code = customer.code (parent_field / related_field)
84
+ */
85
+ export function relatedScope(record, field, identity, criteria) {
86
+ const back = backReference(record, field, identity);
87
+ if (record === null || field === undefined || back === null || !isKeyValue(back.value))
88
+ return null;
89
+ return relatedCriteria(field, record, identity, criteria);
90
+ }
@@ -1,16 +1,17 @@
1
1
  "use client";
2
- import {} from "@sincpro/client-framework";
2
+ import { listRoute } from "@sincpro/client-framework";
3
3
  import { historyOf } from "../core/history.js";
4
4
  import { useFramework } from "./context.js";
5
5
  import { useEffect, useState } from "react";
6
6
  /**
7
7
  * A record's business events, newest first — what `Record.Activity` draws.
8
8
  *
9
- * Context: the history of a record is a criteria over its context's events — `entity_type`
10
- * and `entity_id` — so it is read like any list, with the same transport. `undefined` while it
11
- * is not read, or when it cannot be: an empty history says «nothing happened», which is a
12
- * different claim from «you may not see it». Pass the record among `deps` so a write that
13
- * reloads it reads its new facts too.
9
+ * Context: the backend answers a record's events itself — sincpro_framework's `DomainEvents`,
10
+ * the entity's `history` route (`/issues/{id}/events`), which the generator writes from the
11
+ * contract — so it is read like any list, with the same transport, newest first. `undefined`
12
+ * while it is not read, for a record not saved yet, or when it cannot be read: an empty history
13
+ * says «nothing happened», which is a different claim from «you may not see it». Pass the
14
+ * record among `deps` so a write that reloads it reads its new facts too.
14
15
  *
15
16
  * Named after sincpro_framework's `history_of`; web-ui's `useActivity` is something else (the
16
17
  * panel's open state).
@@ -21,20 +22,16 @@ export function useHistory(entity, id, deps = []) {
21
22
  const [entries, setEntries] = useState(undefined);
22
23
  const history = entity.history;
23
24
  useEffect(() => {
24
- if (history === undefined)
25
+ if (history === undefined || id === "") {
26
+ setEntries(undefined);
25
27
  return;
28
+ }
26
29
  const aborter = new AbortController();
27
30
  const criteria = {
28
- selection: {
29
- all: [
30
- { field: "entity_type", operator: "=", value: history.type },
31
- { field: "entity_id", operator: "=", value: id },
32
- ],
33
- },
34
31
  order: [{ field: "created_at", descending: true }],
35
32
  pagination: { limit: SHOWN },
36
33
  };
37
- transport.list(history.route, criteria, aborter.signal).then((page) => aborter.signal.aborted || setEntries(historyOf(page.rows, page.meta, locale)), () => aborter.signal.aborted || setEntries(undefined));
34
+ transport.list(listRoute(history, { id }), criteria, aborter.signal).then((page) => aborter.signal.aborted || setEntries(historyOf(page.rows, page.meta, locale)), () => aborter.signal.aborted || setEntries(undefined));
38
35
  return () => aborter.abort();
39
36
  // eslint-disable-next-line react-hooks/exhaustive-deps -- the caller names what reloads it
40
37
  }, [transport, history, id, locale, context, ...deps]);
@@ -8,10 +8,11 @@ import type { RecordReading } from "./useRecord.js";
8
8
  * Context: sincpro_framework sends a one2many inside its record as ONE page (`RecordLines` draws
9
9
  * it). A customer with forty thousand invoices, a tenant with a thousand members, the lines of a
10
10
  * big order need more than that page: search in them, order them, walk them. The model says how
11
- * to ask — the related aggregate's key that points back (`identified_by`) — so the relation is
12
- * a criteria over the related entity, `where <that key> = <this record's key>`, answered by the
13
- * server like any list. The relation's scope is AND-ed with everything else, so no search, filter
14
- * or page can ever leave it.
11
+ * to ask — the related aggregate's key that points back (`identified_by`), or the two fields of a
12
+ * relation by a business key (`parent_field`, `related_field`) — so the relation is a criteria
13
+ * over the related entity (@sincpro/client-framework's `relatedCriteria`), answered by the server
14
+ * like any list. The relation's scope is AND-ed with everything else, so no search, filter or
15
+ * page can ever leave it.
15
16
  *
16
17
  * const facturas = useRelated(cliente, "invoices", invoicesEntity);
17
18
  * <EntityList listing={facturas} embedded />
@@ -1,5 +1,6 @@
1
1
  "use client";
2
2
  import {} from "@sincpro/client-framework";
3
+ import { relatedScope } from "../core/relation.js";
3
4
  import { useListing } from "./useListing.js";
4
5
  /**
5
6
  * The records a relation of ONE record points to, as a live listing of the related entity.
@@ -7,27 +8,27 @@ import { useListing } from "./useListing.js";
7
8
  * Context: sincpro_framework sends a one2many inside its record as ONE page (`RecordLines` draws
8
9
  * it). A customer with forty thousand invoices, a tenant with a thousand members, the lines of a
9
10
  * big order need more than that page: search in them, order them, walk them. The model says how
10
- * to ask — the related aggregate's key that points back (`identified_by`) — so the relation is
11
- * a criteria over the related entity, `where <that key> = <this record's key>`, answered by the
12
- * server like any list. The relation's scope is AND-ed with everything else, so no search, filter
13
- * or page can ever leave it.
11
+ * to ask — the related aggregate's key that points back (`identified_by`), or the two fields of a
12
+ * relation by a business key (`parent_field`, `related_field`) — so the relation is a criteria
13
+ * over the related entity (@sincpro/client-framework's `relatedCriteria`), answered by the server
14
+ * like any list. The relation's scope is AND-ed with everything else, so no search, filter or
15
+ * page can ever leave it.
14
16
  *
15
17
  * const facturas = useRelated(cliente, "invoices", invoicesEntity);
16
18
  * <EntityList listing={facturas} embedded />
17
19
  */
18
20
  export function useRelated(record, name, related, options = {}) {
19
21
  const field = record.meta?.fields[name];
20
- const key = field?.identified_by ?? "";
21
22
  const own = record.meta?.get_id || record.meta?.identity || "id";
22
- const value = record.record === null ? null : record.record[own];
23
- const scope = {
23
+ const asked = relatedScope(record.record, field, own, options.criteria);
24
+ // No key to ask by yet — the record or its definition has not arrived, or the record is new: a
25
+ // question that matches nothing (an empty `in`), never one that would list every orphan. It is
26
+ // asked over the field that points back when the definition names it: the related entity has
27
+ // that one, and it may have no `id`.
28
+ const nothing = field?.related_field || field?.identified_by || "id";
29
+ const scope = asked ?? {
24
30
  ...options.criteria,
25
- selection: {
26
- all: [
27
- ...(options.criteria?.selection ? [options.criteria.selection] : []),
28
- { field: key, operator: "=", value: value },
29
- ],
30
- },
31
+ selection: { field: nothing, operator: "in", value: [] },
31
32
  };
32
33
  return useListing(related, { pageSize: 10, ...options, criteria: scope });
33
34
  }
@@ -3,6 +3,7 @@ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-run
3
3
  import {} from "@sincpro/client-framework";
4
4
  import { frameworkCopy } from "../../core/copy.js";
5
5
  import { defaultFields } from "../../core/present.js";
6
+ import { backReference, isKeyValue } from "../../core/relation.js";
6
7
  import { useRelated } from "../../react/useRelated.js";
7
8
  import { EntityList } from "../list/EntityList.js";
8
9
  import { useRecordOpening } from "./opening.js";
@@ -26,22 +27,24 @@ import { useMemo } from "react";
26
27
  export function RelatedList({ record, name, entity, pageSize, ...list }) {
27
28
  const listing = useRelated(record, name, entity, pageSize === undefined ? {} : { pageSize });
28
29
  const opening = useRecordOpening();
29
- // Every row points back to THIS record: the column that says so is the same on all of them.
30
- const back = record.meta?.fields[name]?.identified_by;
30
+ // Every row points back to THIS record: the column that says so is the same on all of them —
31
+ // the same key `useRelated` scopes the list by, by identity or by a business key.
32
+ const identity = record.meta?.get_id || record.meta?.identity || "id";
33
+ const pointing = backReference(record.record, record.meta?.fields[name], identity);
34
+ const back = pointing?.field;
31
35
  const own = list.columns;
32
36
  const columns = useMemo(() => own ??
33
37
  (listing.meta === null || !back
34
38
  ? undefined
35
39
  : defaultFields(listing.meta).filter((field) => field !== back)), [own, listing.meta, back]);
36
40
  const create = opening?.create;
37
- // The value the rows point back with — the same key `useRelated` scopes the list by.
38
- const identity = record.meta?.get_id || record.meta?.identity || "id";
39
- const here = record.record === null ? undefined : record.record[identity];
41
+ // The value the rows point back with.
42
+ const here = pointing?.value;
40
43
  const adding = create === undefined ||
41
44
  entity.create === undefined ||
42
45
  !back ||
43
46
  record.isNew ||
44
- here === undefined ||
47
+ !isKeyValue(here) ||
45
48
  record.edit.state(name).readonly ? null : (_jsx(Button, { size: "sm", role: "secondary", onClick: () => create(entity, { [back]: here }, listing.reload), children: frameworkCopy().record.add }));
46
49
  return (_jsx(EntityList, { listing: listing, embedded: true, ...list, ...(columns === undefined ? {} : { columns }), ...(adding === null && list.actions === undefined
47
50
  ? {}
@@ -75,8 +75,9 @@ export function RecordField({ record, name, as, bare = false, lines: look, list:
75
75
  return bare ? lines : _jsx(FormSection, { title: view.label, children: lines });
76
76
  }
77
77
  if (as === undefined && relation?.field.type === "one2many" && target === undefined) {
78
- // Editable when the record holds ALL its lines; a page of them stays read only.
79
- const whole = editableLines(edit.values[name]);
78
+ // Editable when the record holds ALL its lines; a page of them stays read only, and so do
79
+ // the records it only references — nothing it saves writes them.
80
+ const whole = editableLines(edit.values[name], relation.field);
80
81
  const lines = whole === null ? (_jsx(RecordLines, { record: record.record ?? {}, meta: meta, name: name, locale: locale })) : (_jsx(RecordLinesEditor, { record: record, name: name, lines: whole, ...(look === undefined ? {} : { look }) }));
81
82
  return bare ? lines : _jsx(FormSection, { title: view.label, children: lines });
82
83
  }
@@ -60,5 +60,5 @@ export function RecordLines({ record, meta, name, columns, cells, locale, onOpen
60
60
  const definition = meta?.fields[name]?.definition ?? describeRows(related.items);
61
61
  const identity = definition.identity || "id";
62
62
  const copy = frameworkCopy();
63
- return (_jsxs("div", { className: "flex flex-col gap-2", children: [_jsx(List, { embedded: true, items: related.items, keyOf: (row) => String(row[identity]), present: "table", empty: { sentence: copy.list.empty }, ...(onOpen === undefined ? {} : { onOpen }), children: _jsx(List.Table, { columns: columnsOf(definition, columns, { locale, ...(cells ? { cells } : {}) }), ...(rowActions === undefined ? {} : { rowActions }) }) }), related.partial ? (_jsx("p", { className: "text-meta text-muted", children: copy.record.partial(related.items.length, related.count.value) })) : null] }));
63
+ return (_jsxs("div", { className: "flex flex-col gap-2", children: [_jsx(List, { embedded: true, items: related.items, keyOf: (row) => String(row[identity]), present: "table", empty: { sentence: copy.list.empty }, ...(onOpen === undefined ? {} : { onOpen }), children: _jsx(List.Table, { columns: columnsOf(definition, columns, { locale, ...(cells ? { cells } : {}) }), ...(rowActions === undefined ? {} : { rowActions }) }) }), related.partial ? (_jsx("p", { className: "text-meta text-muted", children: copy.record.partial(related.items.length, related.count.value, related.count.exact) })) : null] }));
64
64
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sincpro/web-framework",
3
- "version": "2.1.0",
3
+ "version": "2.3.0",
4
4
  "description": "The web client of sincpro_framework: entities, lists and records bound to the metadata the server publishes, drawn with @sincpro/web-ui and arranged with @sincpro/web-composer.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -40,9 +40,9 @@
40
40
  "@opentelemetry/resources": "^3.1.0",
41
41
  "@opentelemetry/sdk-trace": "^3.1.0",
42
42
  "@sentry/browser": "^11.11.0",
43
- "@sincpro/client-framework": "^0.5.0",
44
- "@sincpro/criteria": "^2.0.0",
45
- "@sincpro/criteria-react": "^2.0.0",
43
+ "@sincpro/client-framework": "^1.4.0",
44
+ "@sincpro/criteria": "^2.1.0",
45
+ "@sincpro/criteria-react": "^2.1.0",
46
46
  "@sincpro/web-composer": "^1.6.0",
47
47
  "@sincpro/web-ui": "^2.1.0",
48
48
  "react": ">=19",
@@ -72,9 +72,9 @@
72
72
  "@opentelemetry/resources": "3.1.0",
73
73
  "@opentelemetry/sdk-trace": "3.1.0",
74
74
  "@sentry/browser": "11.11.0",
75
- "@sincpro/client-framework": "^0.5.0",
76
- "@sincpro/criteria": "^2.0.0",
77
- "@sincpro/criteria-react": "^2.0.0",
75
+ "@sincpro/client-framework": "^1.4.0",
76
+ "@sincpro/criteria": "^2.1.0",
77
+ "@sincpro/criteria-react": "^2.1.0",
78
78
  "@sincpro/web-composer": "^1.6.0",
79
79
  "@sincpro/web-ui": "^2.1.0",
80
80
  "@storybook/addon-a11y": "10.6.5",