@platforma-sdk/model 1.88.15 → 1.88.16

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.
@@ -1,6 +1,8 @@
1
1
  import type {
2
2
  AxisId,
3
+ AxisQualification,
3
4
  CanonicalizedJson,
5
+ ColumnUniversalId,
4
6
  PObjectId,
5
7
  PTableColumnId,
6
8
  PTableColumnSpec,
@@ -10,16 +12,20 @@ import type {
10
12
  import {
11
13
  canonicalizeJson,
12
14
  getPTableColumnId,
15
+ isColumnUniversalKey,
13
16
  parseJsonSafely,
17
+ stringifyColumnDiscoveredId,
14
18
  } from "@milaboratories/pl-model-common";
15
19
  import { distillFilterSpec } from "../../filters";
16
20
  import type { PlDataTableFilterState, PlTableFilter } from "./typesV4";
17
21
  import type {
18
22
  PlDataTableFilters,
19
23
  PlDataTableFiltersWithMeta,
24
+ PlDataTableGridStateCore,
20
25
  PlDataTableSheetState,
21
26
  PlDataTableStateV2CacheEntry,
22
27
  PlDataTableStateV2Normalized,
28
+ PlTableColumnIdJson as PlTableColumnIdJsonV8,
23
29
  PTableParamsV2,
24
30
  } from "./typesV8";
25
31
  import type {
@@ -155,9 +161,17 @@ export type PlDataTableStateV2 =
155
161
  // v7 still had filter leaves serialised as `CanonicalizedJson<PTableColumnId>`
156
162
  // strings and `sorting: []` for the "untouched" case.
157
163
  | PlDataTableStateV2V7
164
+ // v8 has the current shape but may still hold column ids written before the
165
+ // column-access rework (see `upgradeLegacyColumnId`).
166
+ | PlDataTableStateV2V8
158
167
  // Normalized state
159
168
  | PlDataTableStateV2Normalized;
160
169
 
170
+ /** v8 state: the current shape, column ids not yet checked for the pre-rework form. */
171
+ export type PlDataTableStateV2V8 = Omit<PlDataTableStateV2Normalized, "version"> & {
172
+ version: 8;
173
+ };
174
+
161
175
  /** Upgrade PlDataTableStateV2 to the latest version */
162
176
  export function upgradePlDataTableStateV2(
163
177
  state: PlDataTableStateV2 | undefined,
@@ -206,6 +220,10 @@ export function upgradePlDataTableStateV2(
206
220
  if (state.version === 7) {
207
221
  state = migrateV7toV8(state);
208
222
  }
223
+ // v8 -> v9: rewrite column ids written before the column-access rework.
224
+ if (state.version === 8) {
225
+ state = migrateV8toV9(state);
226
+ }
209
227
  return state;
210
228
  }
211
229
 
@@ -442,7 +460,7 @@ export function createDefaultPTableParams(): PTableParamsV2 {
442
460
 
443
461
  export function createPlDataTableStateV2(): PlDataTableStateV2Normalized {
444
462
  return {
445
- version: 8,
463
+ version: 9,
446
464
  stateCache: [],
447
465
  pTableParams: createDefaultPTableParams(),
448
466
  };
@@ -454,7 +472,7 @@ export function createPlDataTableStateV2(): PlDataTableStateV2Normalized {
454
472
  // are dropped; cache entries whose tree fully prunes get filtersState: null.
455
473
  // Also translates `sorting: []` (legacy "untouched") into `sorting: null`.
456
474
 
457
- function migrateV7toV8(state: PlDataTableStateV2V7): PlDataTableStateV2Normalized {
475
+ function migrateV7toV8(state: PlDataTableStateV2V7): PlDataTableStateV2V8 {
458
476
  const newCache: PlDataTableStateV2CacheEntry[] = state.stateCache.map((entry) => ({
459
477
  sourceId: entry.sourceId,
460
478
  gridState: entry.gridState,
@@ -548,3 +566,190 @@ function parseLegacyLeafColumn(s: unknown): PTableColumnId | undefined {
548
566
  if (t !== "axis" && t !== "column") return undefined;
549
567
  return parsed as PTableColumnId;
550
568
  }
569
+
570
+ // --- v8 -> v9 migration ----------------------------------------------------
571
+ // The column-access rework (July 2026) marked discovered column ids with
572
+ // `__isDiscovered`. Ids saved before it — user sorting, hidden columns, column
573
+ // order, filter leaves — came through the v6 -> v8 steps unchanged, and the
574
+ // current id parser rejects them. Legacy discovered ids are rewritten to the
575
+ // current form, so the user's choices survive. Legacy anchored and filtered
576
+ // ids name a column relative to an anchor the state does not hold; entries
577
+ // using them are dropped.
578
+
579
+ function migrateV8toV9(state: PlDataTableStateV2V8): PlDataTableStateV2Normalized {
580
+ return {
581
+ version: 9,
582
+ stateCache: state.stateCache.map((entry): PlDataTableStateV2CacheEntry => ({
583
+ ...entry,
584
+ gridState: upgradeGridStateColumnIds(entry.gridState),
585
+ filtersState: upgradeFilterColumnIds(entry.filtersState),
586
+ defaultFiltersState: upgradeFilterColumnIds(entry.defaultFiltersState),
587
+ })),
588
+ pTableParams: upgradeParamsColumnIds(state.pTableParams),
589
+ };
590
+ }
591
+
592
+ /** Keys a pre-rework discovered column id could carry; it had no marker. */
593
+ const LEGACY_DISCOVERED_KEYS = new Set([
594
+ "column",
595
+ "path",
596
+ "columnQualifications",
597
+ "queriesQualifications",
598
+ ]);
599
+
600
+ type LegacyDiscoveredKey = {
601
+ column: string;
602
+ path?: { type: "linker"; column: string }[];
603
+ columnQualifications?: AxisQualification[];
604
+ queriesQualifications?: Record<PObjectId, AxisQualification[]>;
605
+ };
606
+
607
+ function isLegacyDiscoveredKey(value: unknown): value is LegacyDiscoveredKey {
608
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
609
+ if (!Object.keys(value).every((key) => LEGACY_DISCOVERED_KEYS.has(key))) return false;
610
+ const { column, path } = value as { column?: unknown; path?: unknown };
611
+ if (typeof column !== "string") return false;
612
+ if (path === undefined) return true;
613
+ return (
614
+ Array.isArray(path) &&
615
+ path.every(
616
+ (item: unknown) =>
617
+ typeof item === "object" &&
618
+ item !== null &&
619
+ (item as { type?: unknown }).type === "linker" &&
620
+ typeof (item as { column?: unknown }).column === "string",
621
+ )
622
+ );
623
+ }
624
+
625
+ /**
626
+ * A stored column id in its current form: unchanged when already current,
627
+ * rewritten when it is a pre-rework discovered id, `undefined` when it cannot be
628
+ * carried over (a pre-rework anchored or filtered id, or not an id at all).
629
+ */
630
+ export function upgradeLegacyColumnId(id: string): ColumnUniversalId | undefined {
631
+ const key: unknown = parseJsonSafely(id);
632
+ if (isColumnUniversalKey(key)) return id as ColumnUniversalId;
633
+ if (!isLegacyDiscoveredKey(key)) return undefined;
634
+
635
+ const column = upgradeLegacyColumnId(key.column);
636
+ if (column === undefined) return undefined;
637
+ const path: { type: "linker"; column: ColumnUniversalId }[] = [];
638
+ for (const item of key.path ?? []) {
639
+ const linker = upgradeLegacyColumnId(item.column);
640
+ if (linker === undefined) return undefined;
641
+ path.push({ type: "linker", column: linker });
642
+ }
643
+ // Discovery names a hit reached with no linker and no qualifications by its own id
644
+ // and wraps only the rest; the pre-rework form wrapped every hit.
645
+ const hasQualifications =
646
+ (key.columnQualifications?.length ?? 0) > 0 ||
647
+ Object.keys(key.queriesQualifications ?? {}).length > 0;
648
+ if (path.length === 0 && !hasQualifications) return column;
649
+ return stringifyColumnDiscoveredId({
650
+ column,
651
+ path,
652
+ columnQualifications: key.columnQualifications,
653
+ queriesQualifications: key.queriesQualifications,
654
+ });
655
+ }
656
+
657
+ function upgradeColumnRef(ref: PTableColumnId): PTableColumnId | undefined {
658
+ if (ref.type !== "column") return ref;
659
+ const id = upgradeLegacyColumnId(ref.id);
660
+ return id === undefined ? undefined : { type: "column", id };
661
+ }
662
+
663
+ function upgradeParamsColumnIds(params: PTableParamsV2): PTableParamsV2 {
664
+ if (params.sourceId === null) return params;
665
+
666
+ const hiddenColIds =
667
+ params.hiddenColIds === null
668
+ ? null
669
+ : params.hiddenColIds.flatMap((ref) => upgradeColumnRef(ref) ?? []);
670
+
671
+ return {
672
+ ...params,
673
+ hiddenColIds,
674
+ sorting: params.sorting === null ? null : upgradeSorting(params.sorting),
675
+ filters: upgradeFilterColumnIds(params.filters),
676
+ defaultFilters: upgradeFilterColumnIds(params.defaultFilters),
677
+ };
678
+ }
679
+
680
+ /** `[]` is the user's cleared sorting and stays. A sorting emptied by dropped
681
+ * entries falls back to the default (`null`) instead of reading as cleared. */
682
+ function upgradeSorting(sorting: PTableSorting[]): null | PTableSorting[] {
683
+ if (sorting.length === 0) return sorting;
684
+ const kept = sorting.flatMap((entry): PTableSorting[] => {
685
+ const column = upgradeColumnRef(entry.column);
686
+ return column === undefined ? [] : [{ ...entry, column }];
687
+ });
688
+ return kept.length === 0 ? null : kept;
689
+ }
690
+
691
+ function upgradeGridColId(colId: PlTableColumnIdJsonV8): PlTableColumnIdJsonV8 | undefined {
692
+ const parsed: unknown = parseJsonSafely(colId);
693
+ // Not a column id: the row-number sentinel passes through unchanged.
694
+ if (typeof parsed !== "object" || parsed === null || !("type" in parsed) || !("id" in parsed)) {
695
+ return colId;
696
+ }
697
+ const ref = upgradeColumnRef(parsed as PTableColumnId);
698
+ if (ref === undefined) return undefined;
699
+ return ref === parsed ? colId : canonicalizeJson<PTableColumnId>(ref);
700
+ }
701
+
702
+ function upgradeGridStateColumnIds(gridState: PlDataTableGridStateCore): PlDataTableGridStateCore {
703
+ const upgradeAll = (colIds: PlTableColumnIdJsonV8[]) =>
704
+ colIds.flatMap((colId) => upgradeGridColId(colId) ?? []);
705
+ return {
706
+ ...gridState,
707
+ ...(gridState.columnOrder && {
708
+ columnOrder: { orderedColIds: upgradeAll(gridState.columnOrder.orderedColIds) },
709
+ }),
710
+ ...(gridState.sort && {
711
+ sort: {
712
+ sortModel: gridState.sort.sortModel.flatMap((s) => {
713
+ const colId = upgradeGridColId(s.colId);
714
+ return colId === undefined ? [] : [{ ...s, colId }];
715
+ }),
716
+ },
717
+ }),
718
+ ...(gridState.columnVisibility && {
719
+ columnVisibility: { hiddenColIds: upgradeAll(gridState.columnVisibility.hiddenColIds) },
720
+ }),
721
+ };
722
+ }
723
+
724
+ function upgradeFilterColumnIds<T extends PlDataTableFilters | PlDataTableFiltersWithMeta>(
725
+ node: null | T,
726
+ ): null | T {
727
+ if (node === null) return null;
728
+ const result = upgradeFilterNode(node);
729
+ return result === undefined ? null : (result as T);
730
+ }
731
+
732
+ /** Rewrites the column ids in a filter tree. A leaf whose id cannot be carried
733
+ * over is dropped, and a group left empty with it. */
734
+ function upgradeFilterNode(node: unknown): unknown | undefined {
735
+ if (!node || typeof node !== "object") return undefined;
736
+ const n = node as { type?: unknown };
737
+ if (n.type === "and" || n.type === "or") {
738
+ const filters = (n as { filters?: unknown[] }).filters ?? [];
739
+ const kept = filters.map(upgradeFilterNode).filter((f): f is object => f !== undefined);
740
+ if (kept.length === 0) return undefined;
741
+ return { ...n, filters: kept };
742
+ }
743
+ if (n.type === "not") {
744
+ const inner = upgradeFilterNode((n as { filter?: unknown }).filter);
745
+ return inner === undefined ? undefined : { ...n, filter: inner };
746
+ }
747
+ const leaf: Record<string, unknown> = { ...n };
748
+ for (const field of ["column", "rhs"]) {
749
+ if (!(field in leaf)) continue;
750
+ const ref = upgradeColumnRef(leaf[field] as PTableColumnId);
751
+ if (ref === undefined) return undefined;
752
+ leaf[field] = ref;
753
+ }
754
+ return leaf;
755
+ }
@@ -102,8 +102,8 @@ export type PTableParamsV2 =
102
102
  };
103
103
 
104
104
  export type PlDataTableStateV2Normalized = {
105
- /** Version for upgrades */
106
- version: 8;
105
+ /** Version for upgrades. v9 has the v8 shape; it marks column ids rewritten to the current form. */
106
+ version: 9;
107
107
  /** Internal states, LRU cache for 5 sourceId-s */
108
108
  stateCache: PlDataTableStateV2CacheEntry[];
109
109
  /** PTable params derived from the cache state for the current sourceId */