@open-mercato/shared 0.10.1-develop.7994.1.c9f31972aa → 0.10.1-develop.8022.1.b6753de892
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/.turbo/turbo-build.log +1 -1
- package/dist/lib/data/engine.js +40 -44
- package/dist/lib/data/engine.js.map +2 -2
- package/dist/lib/db/recordScope.js +72 -0
- package/dist/lib/db/recordScope.js.map +7 -0
- package/dist/lib/nextjs/server-external-packages.js +59 -0
- package/dist/lib/nextjs/server-external-packages.js.map +7 -0
- package/dist/lib/version.js +1 -1
- package/dist/lib/version.js.map +1 -1
- package/package.json +2 -2
- package/src/lib/data/__tests__/engine.custom-entity-encryption-sort.integration.test.ts +4 -0
- package/src/lib/data/__tests__/engine.custom-entity-encryption.integration.test.ts +4 -0
- package/src/lib/data/__tests__/engine.custom-entity-encryption.test.ts +4 -0
- package/src/lib/data/__tests__/engine.custom-entity-require-active.test.ts +4 -0
- package/src/lib/data/__tests__/engine.custom-entity-storage-scope.test.ts +4 -0
- package/src/lib/data/__tests__/engine.custom-entity-storage-upsert.test.ts +138 -0
- package/src/lib/data/__tests__/engine.custom-entity-storage.pg.test.ts +152 -0
- package/src/lib/data/__tests__/engine.pin-organization-id.test.ts +4 -0
- package/src/lib/data/__tests__/engine.set-custom-fields-signal.test.ts +100 -0
- package/src/lib/data/__tests__/engine.table-exists-cache.test.ts +4 -0
- package/src/lib/data/engine.ts +63 -59
- package/src/lib/db/__tests__/recordScope.test.ts +116 -0
- package/src/lib/db/recordScope.ts +130 -0
- package/src/lib/nextjs/__tests__/server-external-packages.test.ts +32 -0
- package/src/lib/nextjs/server-external-packages.ts +67 -0
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import fs from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
|
|
4
|
+
import { openMercatoServerExternalPackages } from '../server-external-packages'
|
|
5
|
+
|
|
6
|
+
type PackageManifest = {
|
|
7
|
+
dependencies?: Record<string, string>
|
|
8
|
+
peerDependencies?: Record<string, string>
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function readInstalledManifest(packageName: string): PackageManifest | null {
|
|
12
|
+
let directory = __dirname
|
|
13
|
+
for (;;) {
|
|
14
|
+
const candidate = path.join(directory, 'node_modules', packageName, 'package.json')
|
|
15
|
+
if (fs.existsSync(candidate)) return JSON.parse(fs.readFileSync(candidate, 'utf8')) as PackageManifest
|
|
16
|
+
const parent = path.dirname(directory)
|
|
17
|
+
if (parent === directory) return null
|
|
18
|
+
directory = parent
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
describe('openMercatoServerExternalPackages', () => {
|
|
23
|
+
it.each(openMercatoServerExternalPackages.map((name) => [name]))(
|
|
24
|
+
'%s is installed and does not depend on React',
|
|
25
|
+
(packageName) => {
|
|
26
|
+
const manifest = readInstalledManifest(packageName)
|
|
27
|
+
expect(manifest).not.toBeNull()
|
|
28
|
+
const declared = Object.keys({ ...manifest?.dependencies, ...manifest?.peerDependencies })
|
|
29
|
+
expect(declared.filter((name) => name === 'react' || name === 'react-dom')).toEqual([])
|
|
30
|
+
},
|
|
31
|
+
)
|
|
32
|
+
})
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node-only third-party dependencies of Open Mercato modules that the Next.js server
|
|
3
|
+
* should `require` from `node_modules` instead of bundling.
|
|
4
|
+
*
|
|
5
|
+
* Turbopack compiles every bundled server dependency once per server layer (route
|
|
6
|
+
* handlers, React Server Components, SSR), so these packages otherwise dominate the
|
|
7
|
+
* dev server's module graph and memory. Only list packages that never touch React,
|
|
8
|
+
* do not need bundler transforms, and are never `require()`d by bundled CommonJS code
|
|
9
|
+
* when they ship only ES modules (Turbopack cannot externalize those; `happy-dom`,
|
|
10
|
+
* loaded through `@tiptap/html/server`, is the known case). This config-only
|
|
11
|
+
* entrypoint has no runtime imports and is safe to evaluate from `next.config.ts`.
|
|
12
|
+
*/
|
|
13
|
+
export const openMercatoServerExternalPackages = [
|
|
14
|
+
'@ai-sdk/amazon-bedrock',
|
|
15
|
+
'@ai-sdk/anthropic',
|
|
16
|
+
'@ai-sdk/azure',
|
|
17
|
+
'@ai-sdk/cohere',
|
|
18
|
+
'@ai-sdk/gateway',
|
|
19
|
+
'@ai-sdk/google',
|
|
20
|
+
'@ai-sdk/mistral',
|
|
21
|
+
'@ai-sdk/openai',
|
|
22
|
+
'@ai-sdk/provider-utils',
|
|
23
|
+
'@aws-sdk/client-s3',
|
|
24
|
+
'@aws-sdk/client-sesv2',
|
|
25
|
+
'@grpc/grpc-js',
|
|
26
|
+
'@mikro-orm/migrations',
|
|
27
|
+
'@mikro-orm/postgresql',
|
|
28
|
+
'@mikro-orm/sql',
|
|
29
|
+
'@modelcontextprotocol/sdk',
|
|
30
|
+
'@parse/node-apn',
|
|
31
|
+
'@xmldom/xmldom',
|
|
32
|
+
'ai',
|
|
33
|
+
'ai-sdk-ollama',
|
|
34
|
+
'bcryptjs',
|
|
35
|
+
'bullmq',
|
|
36
|
+
'cron-parser',
|
|
37
|
+
'encoding-japanese',
|
|
38
|
+
'firebase-admin',
|
|
39
|
+
'gpt-tokenizer',
|
|
40
|
+
'html-to-text',
|
|
41
|
+
'iconv-lite',
|
|
42
|
+
'imapflow',
|
|
43
|
+
'ioredis',
|
|
44
|
+
'js-yaml',
|
|
45
|
+
'kysely',
|
|
46
|
+
'libmime',
|
|
47
|
+
'luxon',
|
|
48
|
+
'mailparser',
|
|
49
|
+
'mammoth',
|
|
50
|
+
'node-forge',
|
|
51
|
+
'node-html-markdown',
|
|
52
|
+
'node-html-parser',
|
|
53
|
+
'nodemailer',
|
|
54
|
+
'pg',
|
|
55
|
+
'postal-mime',
|
|
56
|
+
'protobufjs',
|
|
57
|
+
'rate-limiter-flexible',
|
|
58
|
+
're2js',
|
|
59
|
+
'resend',
|
|
60
|
+
'semver',
|
|
61
|
+
'sharp',
|
|
62
|
+
'stripe',
|
|
63
|
+
'svix',
|
|
64
|
+
'undici',
|
|
65
|
+
'xmlbuilder',
|
|
66
|
+
'yaml',
|
|
67
|
+
] as const
|