@open-mercato/shared 0.10.1-develop.8004.1.a8a79ee43f → 0.10.1-develop.8024.1.02dbc16b49

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.
@@ -0,0 +1,116 @@
1
+ jest.mock('../../query/engine', () => ({
2
+ resolveEntityTableName: jest.fn((_em: unknown, entityType: string) => entityType.split(':')[1] ?? entityType),
3
+ }))
4
+
5
+ import {
6
+ loadEntityRecordScope,
7
+ resetEntityScopeColumnsCacheForTests,
8
+ resolveEntityScopeColumns,
9
+ } from '../recordScope'
10
+
11
+ type Row = Record<string, string | null>
12
+
13
+ function buildEm(options: {
14
+ metadata?: Array<{ tableName: string; properties: Record<string, { fieldNames: string[] }> }>
15
+ row?: Row
16
+ probeColumns?: string[]
17
+ probeError?: Error
18
+ }) {
19
+ const selects: Array<{ table: string; columns: unknown }> = []
20
+ const db = {
21
+ selectFrom: jest.fn((table: string) => {
22
+ const builder = {
23
+ select: jest.fn((columns: unknown) => {
24
+ selects.push({ table, columns })
25
+ return builder
26
+ }),
27
+ where: jest.fn(() => builder),
28
+ execute: jest.fn(async () => {
29
+ if (options.probeError) throw options.probeError
30
+ return (options.probeColumns ?? []).map((column_name) => ({ column_name }))
31
+ }),
32
+ executeTakeFirst: jest.fn(async () => options.row),
33
+ }
34
+ return builder
35
+ }),
36
+ }
37
+ const em = {
38
+ getKysely: jest.fn(() => db),
39
+ getMetadata: jest.fn(() => ({ getAll: () => options.metadata ?? [] })),
40
+ }
41
+ return { em: em as never, db, selects }
42
+ }
43
+
44
+ const scopedProperties = {
45
+ id: { fieldNames: ['id'] },
46
+ organizationId: { fieldNames: ['organization_id'] },
47
+ tenantId: { fieldNames: ['tenant_id'] },
48
+ }
49
+
50
+ describe('recordScope', () => {
51
+ beforeEach(() => resetEntityScopeColumnsCacheForTests())
52
+
53
+ it('does not query a global table that has no scope columns', async () => {
54
+ const { em, db } = buildEm({
55
+ metadata: [{ tableName: 'feature_toggles', properties: { id: { fieldNames: ['id'] } } }],
56
+ })
57
+
58
+ await expect(loadEntityRecordScope(em, 'feature_toggles:feature_toggles', 'toggle-1')).resolves.toBeNull()
59
+ expect(db.selectFrom).not.toHaveBeenCalled()
60
+ })
61
+
62
+ it('reads both scope columns for a fully scoped table', async () => {
63
+ const { em, selects } = buildEm({
64
+ metadata: [{ tableName: 'customer_people', properties: scopedProperties }],
65
+ row: { organization_id: 'org-1', tenant_id: 'tenant-1' },
66
+ })
67
+
68
+ await expect(loadEntityRecordScope(em, 'customers:customer_people', 'p1'))
69
+ .resolves.toEqual({ organizationId: 'org-1', tenantId: 'tenant-1' })
70
+ expect(selects).toEqual([{ table: 'customer_people', columns: ['organization_id', 'tenant_id'] }])
71
+ })
72
+
73
+ it('selects only tenant_id for a tenant-level table', async () => {
74
+ const { em, selects } = buildEm({
75
+ metadata: [{ tableName: 'tenant_things', properties: { tenantId: { fieldNames: ['tenant_id'] } } }],
76
+ row: { tenant_id: 'tenant-1' },
77
+ })
78
+
79
+ await expect(loadEntityRecordScope(em, 'mod:tenant_things', 't1'))
80
+ .resolves.toEqual({ organizationId: null, tenantId: 'tenant-1' })
81
+ expect(selects).toEqual([{ table: 'tenant_things', columns: ['tenant_id'] }])
82
+ })
83
+
84
+ it('returns null when the row is missing', async () => {
85
+ const { em } = buildEm({ metadata: [{ tableName: 'customer_people', properties: scopedProperties }] })
86
+ await expect(loadEntityRecordScope(em, 'customers:customer_people', 'missing')).resolves.toBeNull()
87
+ })
88
+
89
+ it('probes information_schema when the table has no ORM metadata', async () => {
90
+ const { em, selects } = buildEm({ probeColumns: ['tenant_id'], row: { tenant_id: 'tenant-1' } })
91
+
92
+ await expect(resolveEntityScopeColumns(em, 'mod:unregistered'))
93
+ .resolves.toEqual({ table: 'unregistered', organizationColumn: null, tenantColumn: 'tenant_id' })
94
+ expect(selects[0]).toEqual({ table: 'information_schema.columns', columns: ['column_name'] })
95
+ })
96
+
97
+ it('skips the row lookup when the column probe fails', async () => {
98
+ const { em, selects } = buildEm({ probeError: new Error('boom') })
99
+
100
+ await expect(loadEntityRecordScope(em, 'mod:unregistered', 'r1')).resolves.toBeNull()
101
+ expect(selects).toEqual([{ table: 'information_schema.columns', columns: ['column_name'] }])
102
+ })
103
+
104
+ it('keeps probed columns isolated between database clients', async () => {
105
+ const first = buildEm({ probeColumns: ['tenant_id'] })
106
+ const second = buildEm({ probeColumns: ['organization_id'] })
107
+
108
+ await expect(resolveEntityScopeColumns(first.em, 'mod:unregistered'))
109
+ .resolves.toEqual({ table: 'unregistered', organizationColumn: null, tenantColumn: 'tenant_id' })
110
+ await expect(resolveEntityScopeColumns(second.em, 'mod:unregistered'))
111
+ .resolves.toEqual({ table: 'unregistered', organizationColumn: 'organization_id', tenantColumn: null })
112
+ expect(first.db.selectFrom).toHaveBeenCalledTimes(1)
113
+ expect(second.db.selectFrom).toHaveBeenCalledTimes(1)
114
+ })
115
+
116
+ })
@@ -0,0 +1,130 @@
1
+ import type { Kysely } from 'kysely'
2
+ import { sql } from 'kysely'
3
+ import { resolveEntityTableName } from '../query/engine'
4
+ import { listEntityMetadata } from './entityMetadata'
5
+
6
+ export type EntityScopeColumns = {
7
+ table: string
8
+ organizationColumn: string | null
9
+ tenantColumn: string | null
10
+ }
11
+
12
+ export type EntityRecordScope = {
13
+ organizationId: string | null
14
+ tenantId: string | null
15
+ }
16
+
17
+ type ScopeLookupEntityManager = {
18
+ getKysely: <T = unknown>() => Kysely<T>
19
+ getMetadata?: () => unknown
20
+ }
21
+
22
+ type ScopePropertyMetadata = { fieldNames?: unknown }
23
+ type ScopeDatabase = Record<string, Record<string, unknown>>
24
+
25
+ const ORGANIZATION_COLUMN = 'organization_id'
26
+ const TENANT_COLUMN = 'tenant_id'
27
+
28
+ let probedColumnsCache = new WeakMap<Kysely<ScopeDatabase>, Map<string, { organizationColumn: string | null; tenantColumn: string | null }>>()
29
+
30
+ function readScopeFieldName(property: ScopePropertyMetadata | undefined): string | null {
31
+ const fieldNames = property?.fieldNames
32
+ if (!Array.isArray(fieldNames) || fieldNames.length !== 1) return null
33
+ const [fieldName] = fieldNames
34
+ return typeof fieldName === 'string' && fieldName.length > 0 ? fieldName : null
35
+ }
36
+
37
+ function resolveScopeColumnsFromMetadata(
38
+ em: ScopeLookupEntityManager,
39
+ table: string,
40
+ ): { organizationColumn: string | null; tenantColumn: string | null } | null {
41
+ const matches = listEntityMetadata(em).filter((metadata) => metadata.tableName === table)
42
+ if (matches.length !== 1) return null
43
+ const properties = (matches[0].properties ?? {}) as Record<string, ScopePropertyMetadata | undefined>
44
+ return {
45
+ organizationColumn: readScopeFieldName(properties.organizationId),
46
+ tenantColumn: readScopeFieldName(properties.tenantId),
47
+ }
48
+ }
49
+
50
+ async function probeScopeColumns(
51
+ db: Kysely<ScopeDatabase>,
52
+ table: string,
53
+ ): Promise<{ organizationColumn: string | null; tenantColumn: string | null } | null> {
54
+ let cache = probedColumnsCache.get(db)
55
+ if (!cache) {
56
+ cache = new Map()
57
+ probedColumnsCache.set(db, cache)
58
+ }
59
+ const cached = cache.get(table)
60
+ if (cached) return cached
61
+ try {
62
+ const rows = await db
63
+ .selectFrom('information_schema.columns')
64
+ .select(['column_name'])
65
+ .where(sql<boolean>`table_schema = current_schema()`)
66
+ .where('table_name', '=', table)
67
+ .where('column_name', 'in', [ORGANIZATION_COLUMN, TENANT_COLUMN])
68
+ .execute()
69
+ const columns = new Set(rows.map((row) => typeof row.column_name === 'string' ? row.column_name.toLowerCase() : ''))
70
+ const resolved = {
71
+ organizationColumn: columns.has(ORGANIZATION_COLUMN) ? ORGANIZATION_COLUMN : null,
72
+ tenantColumn: columns.has(TENANT_COLUMN) ? TENANT_COLUMN : null,
73
+ }
74
+ cache.set(table, resolved)
75
+ return resolved
76
+ } catch {
77
+ return null
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Resolve which tenant/organization scope columns the table behind `entityType` actually has.
83
+ * Uses ORM metadata when the table is registered; otherwise probes `information_schema.columns`.
84
+ * Returns `null` when the columns cannot be determined, so callers skip the row lookup instead
85
+ * of issuing a select that fails on global tables (e.g. `feature_toggles`).
86
+ */
87
+ export async function resolveEntityScopeColumns(
88
+ em: ScopeLookupEntityManager,
89
+ entityType: string,
90
+ ): Promise<EntityScopeColumns | null> {
91
+ const table = resolveEntityTableName(em as never, entityType)
92
+ if (!table) return null
93
+ const columns = resolveScopeColumnsFromMetadata(em, table)
94
+ ?? await probeScopeColumns(em.getKysely<ScopeDatabase>(), table)
95
+ if (!columns) return null
96
+ return { table, ...columns }
97
+ }
98
+
99
+ /**
100
+ * Read the tenant/organization scope of one record by id, selecting only the scope columns its
101
+ * table has. Returns `null` when the table has no scope columns, the columns cannot be
102
+ * determined, or the row does not exist.
103
+ */
104
+ export async function loadEntityRecordScope(
105
+ em: ScopeLookupEntityManager,
106
+ entityType: string,
107
+ recordId: string,
108
+ ): Promise<EntityRecordScope | null> {
109
+ const columns = await resolveEntityScopeColumns(em, entityType)
110
+ if (!columns) return null
111
+ const selected = [columns.organizationColumn, columns.tenantColumn]
112
+ .filter((column): column is string => column !== null)
113
+ if (selected.length === 0) return null
114
+ const row = await em.getKysely<ScopeDatabase>()
115
+ .selectFrom(columns.table)
116
+ .select(selected)
117
+ .where('id', '=', recordId)
118
+ .executeTakeFirst()
119
+ if (!row) return null
120
+ const organizationId = columns.organizationColumn ? row[columns.organizationColumn] : null
121
+ const tenantId = columns.tenantColumn ? row[columns.tenantColumn] : null
122
+ return {
123
+ organizationId: typeof organizationId === 'string' ? organizationId : null,
124
+ tenantId: typeof tenantId === 'string' ? tenantId : null,
125
+ }
126
+ }
127
+
128
+ export function resetEntityScopeColumnsCacheForTests(): void {
129
+ probedColumnsCache = new WeakMap()
130
+ }