Skip to content

Commit 0522e4d

Browse files
committed
refactor(metadata-protocol): move the legacy-organization row filters out of protocol.ts into protocol-legacy-organization-rows.ts (#25467 cut 29)
Move-only, shape B. withoutLegacyOrganizationRows and legacyOrganizationRowsOf move, unedited, into one module. The engine field is handed over as a getter; withoutLegacyOrganizationRows's call to legacyOrganizationRowsOf is handed over as a closure, so it still reaches the class method. Claude-Session: https://claude.ai/code/session_016SP1spj613tyGCgxoD1An6 Co-authored-by: Claude <noreply@anthropic.com>
1 parent 3271a08 commit 0522e4d

2 files changed

Lines changed: 87 additions & 9 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* [#25467 cut 29] The legacy-organization row filters, from
5+
* `ObjectStackProtocolImplementation`, moved out of `protocol.ts` whole: on a
6+
* database the ADR-0131 ceremony has not migrated, the rows that still belong
7+
* to one organization rather than to the environment.
8+
*
9+
* - `legacyOrganizationRowsOf` — those rows of one table, read off the
10+
* physical column through the table's driver (a read inside a transaction
11+
* joins it).
12+
* - `withoutLegacyOrganizationRows` — a row list with those rows left out.
13+
*
14+
* A MOVE, not a redesign. Each function below is the body of the
15+
* `ObjectStackProtocolImplementation` method of the same name as it stood,
16+
* re-indented one level and otherwise unedited.
17+
*
18+
* ## How the moved bodies reach the class
19+
*
20+
* The shape #23050 set for `rest-server.ts` (its `data-action-routes.ts`
21+
* header carries the full reasoning), as `protocol-batch-data.ts` applies it.
22+
* The class keeps each method — the published `dist/*.d.ts` lists its name,
23+
* docblock and declared signature — and its body is one literal of exactly
24+
* the {@link LegacyOrganizationRowsHost} members it reads, plus a `.call`.
25+
* The `engine` field is handed over as a getter, so each read finds it as it
26+
* stands at call time; `withoutLegacyOrganizationRows`'s call to
27+
* `legacyOrganizationRowsOf` is handed over as a closure, so it still reaches
28+
* the class method. Every caller keeps calling the class methods.
29+
*
30+
* What the bodies read from elsewhere, they import from where `protocol.ts`
31+
* imports it. Nothing re-exports this module, so the published surface does
32+
* not move.
33+
*/
34+
35+
import { readLegacyOrganizationRows } from '@objectstack/metadata-core';
36+
import type { ObjectStackProtocolImplementation } from './protocol.js';
37+
38+
/**
39+
* The `ObjectStackProtocolImplementation` members the functions below read —
40+
* every one of them, and no other; each function's `this` is the `Pick` of
41+
* the members it reads. Each is typed AS the class member (an indexed access
42+
* on the class), so a change to a member's signature on the class is a change
43+
* here too, and naming a member the class does not have is a compile error.
44+
*/
45+
export interface LegacyOrganizationRowsHost {
46+
readonly engine: ObjectStackProtocolImplementation['engine'];
47+
readonly legacyOrganizationRowsOf: ObjectStackProtocolImplementation['legacyOrganizationRowsOf'];
48+
}
49+
50+
/** The body of `ObjectStackProtocolImplementation#withoutLegacyOrganizationRows`; the method's docblock there is its contract. */
51+
export async function withoutLegacyOrganizationRows<R>(
52+
this: Pick<LegacyOrganizationRowsHost, 'legacyOrganizationRowsOf'>,
53+
table: string,
54+
rows: R[] | null | undefined,
55+
context?: unknown,
56+
): Promise<R[]> {
57+
if (!rows || rows.length === 0) return rows ?? [];
58+
const legacy = await this.legacyOrganizationRowsOf(table, context);
59+
return legacy.size === 0 ? rows : rows.filter((row) => !legacy.has(String((row as { id?: unknown }).id)));
60+
}
61+
62+
/** The body of `ObjectStackProtocolImplementation#legacyOrganizationRowsOf`; the method's docblock there is its contract. */
63+
export function legacyOrganizationRowsOf(
64+
this: Pick<LegacyOrganizationRowsHost, 'engine'>,
65+
table: string,
66+
context?: unknown,
67+
): Promise<ReadonlyMap<string, string>> {
68+
const transaction = (context as { transaction?: unknown } | undefined)?.transaction;
69+
return readLegacyOrganizationRows(
70+
(this.engine as { getDriverForObject?: (objectName: string) => unknown }).getDriverForObject?.(table),
71+
table,
72+
transaction !== undefined ? { transaction } : {},
73+
);
74+
}

‎packages/metadata-protocol/src/protocol.ts‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,8 @@ import { hierarchyResolverMounted, orgWallEnforced, rlsMembershipResolverKeys, t
421421
import { flowRegistrationsOf, resolveFlowCanonicalizer, resolveFlowRegistrationReader } from './protocol-automation-flow-reads.js';
422422
// [#25467 cut 28] The code-datasource predicates: declared by an installed package, or registered by the host.
423423
import { isDeclaredCodeDatasource, isHostCodeDatasource } from './protocol-code-datasource-predicates.js';
424+
// [#25467 cut 29] The legacy-organization row filters: which rows a not-yet-migrated database still holds.
425+
import { legacyOrganizationRowsOf, withoutLegacyOrganizationRows } from './protocol-legacy-organization-rows.js';
424426

425427
/**
426428
* Canonical Zod schema per metadata type lives in
@@ -10210,18 +10212,20 @@ export class ObjectStackProtocolImplementation implements
1021010212
}
1021110213

1021210214
private async withoutLegacyOrganizationRows<R>(table: string, rows: R[] | null | undefined, context?: unknown): Promise<R[]> {
10213-
if (!rows || rows.length === 0) return rows ?? [];
10214-
const legacy = await this.legacyOrganizationRowsOf(table, context);
10215-
return legacy.size === 0 ? rows : rows.filter((row) => !legacy.has(String((row as { id?: unknown }).id)));
10215+
// [#25467 cut 29] The body moved to `protocol-legacy-organization-rows.ts` (see `LegacyOrganizationRowsHost`).
10216+
const host: ThisParameterType<typeof withoutLegacyOrganizationRows> = {
10217+
legacyOrganizationRowsOf: (...a) => this.legacyOrganizationRowsOf(...a),
10218+
};
10219+
return (withoutLegacyOrganizationRows<R>).call(host, table, rows, context);
1021610220
}
1021710221

1021810222
private legacyOrganizationRowsOf(table: string, context?: unknown): Promise<ReadonlyMap<string, string>> {
10219-
const transaction = (context as { transaction?: unknown } | undefined)?.transaction;
10220-
return readLegacyOrganizationRows(
10221-
(this.engine as { getDriverForObject?: (objectName: string) => unknown }).getDriverForObject?.(table),
10222-
table,
10223-
transaction !== undefined ? { transaction } : {},
10224-
);
10223+
// [#25467 cut 29] The body moved to `protocol-legacy-organization-rows.ts` (see `LegacyOrganizationRowsHost`).
10224+
const readEngine = () => this.engine;
10225+
const host: ThisParameterType<typeof legacyOrganizationRowsOf> = {
10226+
get engine() { return readEngine(); },
10227+
};
10228+
return legacyOrganizationRowsOf.call(host, table, context);
1022510229
}
1022610230

1022710231
/**

0 commit comments

Comments
 (0)