@open-mercato/search 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/dist/modules/search/subscribers/fulltext_upsert.js +4 -6
- package/dist/modules/search/subscribers/fulltext_upsert.js.map +2 -2
- package/dist/modules/search/subscribers/vector_delete.js +4 -6
- package/dist/modules/search/subscribers/vector_delete.js.map +2 -2
- package/dist/modules/search/subscribers/vector_upsert.js +4 -6
- package/dist/modules/search/subscribers/vector_upsert.js.map +2 -2
- package/package.json +4 -4
- package/src/modules/search/subscribers/__tests__/scope-lookup-isolation.test.ts +51 -20
- package/src/modules/search/subscribers/__tests__/scope-lookup.pg.test.ts +148 -0
- package/src/modules/search/subscribers/fulltext_upsert.ts +4 -10
- package/src/modules/search/subscribers/vector_delete.ts +4 -10
- package/src/modules/search/subscribers/vector_upsert.ts +4 -10
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { loadEntityRecordScope } from "@open-mercato/shared/lib/db/recordScope";
|
|
2
2
|
import { resolveAutoIndexingEnabled } from "../lib/auto-indexing.js";
|
|
3
3
|
import { searchDebugWarn, searchError } from "../../../lib/debug.js";
|
|
4
4
|
const metadata = { event: "search.index_record", persistent: false };
|
|
@@ -19,11 +19,9 @@ async function handle(payload, ctx) {
|
|
|
19
19
|
if ((organizationId == null || tenantId == null) && em) {
|
|
20
20
|
try {
|
|
21
21
|
const lookupEm = typeof em.fork === "function" ? em.fork() : em;
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
if (organizationId == null) organizationId = row?.organization_id ?? organizationId;
|
|
26
|
-
if (tenantId == null) tenantId = row?.tenant_id ?? tenantId;
|
|
22
|
+
const scope = await loadEntityRecordScope(lookupEm, entityType, recordId);
|
|
23
|
+
if (organizationId == null) organizationId = scope?.organizationId ?? organizationId;
|
|
24
|
+
if (tenantId == null) tenantId = scope?.tenantId ?? tenantId;
|
|
27
25
|
} catch {
|
|
28
26
|
}
|
|
29
27
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/search/subscribers/fulltext_upsert.ts"],
|
|
4
|
-
"sourcesContent": ["import {
|
|
5
|
-
"mappings": "AAAA,SAAS,
|
|
4
|
+
"sourcesContent": ["import { loadEntityRecordScope } from '@open-mercato/shared/lib/db/recordScope'\nimport type { Queue } from '@open-mercato/queue'\nimport type { FulltextIndexJobPayload } from '../../../queue/fulltext-indexing'\nimport { resolveAutoIndexingEnabled } from '../lib/auto-indexing'\nimport { searchDebugWarn, searchError } from '../../../lib/debug'\n\nexport const metadata = { event: 'search.index_record', persistent: false }\n\ntype Payload = {\n entityId?: string\n recordId?: string\n organizationId?: string | null\n tenantId?: string | null\n}\n\ntype HandlerContext = { resolve: <T = unknown>(name: string) => T }\n\nexport default async function handle(payload: Payload, ctx: HandlerContext) {\n const entityType = String(payload?.entityId ?? '')\n const recordId = String(payload?.recordId ?? '')\n\n if (!entityType || !recordId) {\n return\n }\n\n let organizationId = payload?.organizationId ?? null\n let tenantId = payload?.tenantId ?? null\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let em: any | null = null\n try {\n em = ctx.resolve('em')\n } catch {\n em = null\n }\n\n // Resolve missing scope from DB if needed (same pattern as vector_upsert.ts)\n if ((organizationId == null || tenantId == null) && em) {\n try {\n const lookupEm = typeof em.fork === 'function' ? em.fork() : em\n const scope = await loadEntityRecordScope(lookupEm, entityType, recordId)\n if (organizationId == null) organizationId = scope?.organizationId ?? organizationId\n if (tenantId == null) tenantId = scope?.tenantId ?? tenantId\n } catch {\n // Ignore lookup errors\n }\n }\n\n if (!tenantId) {\n return\n }\n\n const autoIndexingEnabled = await resolveAutoIndexingEnabled(ctx, { defaultValue: true })\n if (!autoIndexingEnabled) {\n return\n }\n\n let queue: Queue<FulltextIndexJobPayload>\n try {\n queue = ctx.resolve<Queue<FulltextIndexJobPayload>>('fulltextIndexQueue')\n } catch {\n searchDebugWarn('search.fulltext', 'fulltextIndexQueue not available, skipping fulltext indexing')\n return\n }\n\n try {\n await queue.enqueue({\n jobType: 'index',\n entityType,\n recordId,\n tenantId: String(tenantId),\n organizationId: organizationId ? String(organizationId) : null,\n })\n } catch (error) {\n searchError('search.fulltext', 'Failed to enqueue fulltext index job', {\n entityType,\n recordId,\n error: error instanceof Error ? error.message : error,\n })\n throw error\n }\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,6BAA6B;AAGtC,SAAS,kCAAkC;AAC3C,SAAS,iBAAiB,mBAAmB;AAEtC,MAAM,WAAW,EAAE,OAAO,uBAAuB,YAAY,MAAM;AAW1E,eAAO,OAA8B,SAAkB,KAAqB;AAC1E,QAAM,aAAa,OAAO,SAAS,YAAY,EAAE;AACjD,QAAM,WAAW,OAAO,SAAS,YAAY,EAAE;AAE/C,MAAI,CAAC,cAAc,CAAC,UAAU;AAC5B;AAAA,EACF;AAEA,MAAI,iBAAiB,SAAS,kBAAkB;AAChD,MAAI,WAAW,SAAS,YAAY;AAGpC,MAAI,KAAiB;AACrB,MAAI;AACF,SAAK,IAAI,QAAQ,IAAI;AAAA,EACvB,QAAQ;AACN,SAAK;AAAA,EACP;AAGA,OAAK,kBAAkB,QAAQ,YAAY,SAAS,IAAI;AACtD,QAAI;AACF,YAAM,WAAW,OAAO,GAAG,SAAS,aAAa,GAAG,KAAK,IAAI;AAC7D,YAAM,QAAQ,MAAM,sBAAsB,UAAU,YAAY,QAAQ;AACxE,UAAI,kBAAkB,KAAM,kBAAiB,OAAO,kBAAkB;AACtE,UAAI,YAAY,KAAM,YAAW,OAAO,YAAY;AAAA,IACtD,QAAQ;AAAA,IAER;AAAA,EACF;AAEA,MAAI,CAAC,UAAU;AACb;AAAA,EACF;AAEA,QAAM,sBAAsB,MAAM,2BAA2B,KAAK,EAAE,cAAc,KAAK,CAAC;AACxF,MAAI,CAAC,qBAAqB;AACxB;AAAA,EACF;AAEA,MAAI;AACJ,MAAI;AACF,YAAQ,IAAI,QAAwC,oBAAoB;AAAA,EAC1E,QAAQ;AACN,oBAAgB,mBAAmB,8DAA8D;AACjG;AAAA,EACF;AAEA,MAAI;AACF,UAAM,MAAM,QAAQ;AAAA,MAClB,SAAS;AAAA,MACT;AAAA,MACA;AAAA,MACA,UAAU,OAAO,QAAQ;AAAA,MACzB,gBAAgB,iBAAiB,OAAO,cAAc,IAAI;AAAA,IAC5D,CAAC;AAAA,EACH,SAAS,OAAO;AACd,gBAAY,mBAAmB,wCAAwC;AAAA,MACrE;AAAA,MACA;AAAA,MACA,OAAO,iBAAiB,QAAQ,MAAM,UAAU;AAAA,IAClD,CAAC;AACD,UAAM;AAAA,EACR;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { loadEntityRecordScope } from "@open-mercato/shared/lib/db/recordScope";
|
|
2
2
|
import { resolveAutoIndexingEnabled } from "../lib/auto-indexing.js";
|
|
3
3
|
import { searchDebugWarn, searchError } from "../../../lib/debug.js";
|
|
4
4
|
const metadata = { event: "query_index.delete_one", persistent: false };
|
|
@@ -17,11 +17,9 @@ async function handle(payload, ctx) {
|
|
|
17
17
|
if ((organizationId == null || tenantId == null) && em) {
|
|
18
18
|
try {
|
|
19
19
|
const lookupEm = typeof em.fork === "function" ? em.fork() : em;
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
if (organizationId == null) organizationId = row?.organization_id ?? organizationId;
|
|
24
|
-
if (tenantId == null) tenantId = row?.tenant_id ?? tenantId;
|
|
20
|
+
const scope = await loadEntityRecordScope(lookupEm, entityType, recordId);
|
|
21
|
+
if (organizationId == null) organizationId = scope?.organizationId ?? organizationId;
|
|
22
|
+
if (tenantId == null) tenantId = scope?.tenantId ?? tenantId;
|
|
25
23
|
} catch {
|
|
26
24
|
}
|
|
27
25
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/search/subscribers/vector_delete.ts"],
|
|
4
|
-
"sourcesContent": ["import {
|
|
5
|
-
"mappings": "AAAA,SAAS,
|
|
4
|
+
"sourcesContent": ["import { loadEntityRecordScope } from '@open-mercato/shared/lib/db/recordScope'\nimport type { Queue } from '@open-mercato/queue'\nimport type { VectorIndexJobPayload } from '../../../queue/vector-indexing'\nimport { resolveAutoIndexingEnabled } from '../lib/auto-indexing'\nimport { searchDebugWarn, searchError } from '../../../lib/debug'\n\nexport const metadata = { event: 'query_index.delete_one', persistent: false }\n\ntype Payload = {\n entityType?: string\n recordId?: string\n organizationId?: string | null\n tenantId?: string | null\n}\n\ntype HandlerContext = { resolve: <T = unknown>(name: string) => T }\n\nexport default async function handle(payload: Payload, ctx: HandlerContext) {\n const entityType = String(payload?.entityType ?? '')\n const recordId = String(payload?.recordId ?? '')\n if (!entityType || !recordId) return\n\n let organizationId = payload?.organizationId ?? null\n let tenantId = payload?.tenantId ?? null\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let em: any | null = null\n try {\n em = ctx.resolve('em')\n } catch {\n em = null\n }\n\n if ((organizationId == null || tenantId == null) && em) {\n try {\n const lookupEm = typeof em.fork === 'function' ? em.fork() : em\n const scope = await loadEntityRecordScope(lookupEm, entityType, recordId)\n if (organizationId == null) organizationId = scope?.organizationId ?? organizationId\n if (tenantId == null) tenantId = scope?.tenantId ?? tenantId\n } catch {\n // Ignore lookup errors\n }\n }\n\n if (!tenantId) return\n\n const autoIndexingEnabled = await resolveAutoIndexingEnabled(ctx, { defaultValue: true })\n if (!autoIndexingEnabled) return\n\n let queue: Queue<VectorIndexJobPayload>\n try {\n queue = ctx.resolve<Queue<VectorIndexJobPayload>>('vectorIndexQueue')\n } catch {\n searchDebugWarn('search.vector', 'vectorIndexQueue not available, skipping vector delete')\n return\n }\n\n try {\n await queue.enqueue({\n jobType: 'delete',\n entityType,\n recordId,\n tenantId: String(tenantId),\n organizationId: organizationId ? String(organizationId) : null,\n })\n } catch (error) {\n searchError('search.vector', 'Failed to enqueue vector delete job', {\n entityType,\n recordId,\n error: error instanceof Error ? error.message : error,\n })\n throw error // Propagate to caller so failure is visible\n }\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,6BAA6B;AAGtC,SAAS,kCAAkC;AAC3C,SAAS,iBAAiB,mBAAmB;AAEtC,MAAM,WAAW,EAAE,OAAO,0BAA0B,YAAY,MAAM;AAW7E,eAAO,OAA8B,SAAkB,KAAqB;AAC1E,QAAM,aAAa,OAAO,SAAS,cAAc,EAAE;AACnD,QAAM,WAAW,OAAO,SAAS,YAAY,EAAE;AAC/C,MAAI,CAAC,cAAc,CAAC,SAAU;AAE9B,MAAI,iBAAiB,SAAS,kBAAkB;AAChD,MAAI,WAAW,SAAS,YAAY;AAGpC,MAAI,KAAiB;AACrB,MAAI;AACF,SAAK,IAAI,QAAQ,IAAI;AAAA,EACvB,QAAQ;AACN,SAAK;AAAA,EACP;AAEA,OAAK,kBAAkB,QAAQ,YAAY,SAAS,IAAI;AACtD,QAAI;AACF,YAAM,WAAW,OAAO,GAAG,SAAS,aAAa,GAAG,KAAK,IAAI;AAC7D,YAAM,QAAQ,MAAM,sBAAsB,UAAU,YAAY,QAAQ;AACxE,UAAI,kBAAkB,KAAM,kBAAiB,OAAO,kBAAkB;AACtE,UAAI,YAAY,KAAM,YAAW,OAAO,YAAY;AAAA,IACtD,QAAQ;AAAA,IAER;AAAA,EACF;AAEA,MAAI,CAAC,SAAU;AAEf,QAAM,sBAAsB,MAAM,2BAA2B,KAAK,EAAE,cAAc,KAAK,CAAC;AACxF,MAAI,CAAC,oBAAqB;AAE1B,MAAI;AACJ,MAAI;AACF,YAAQ,IAAI,QAAsC,kBAAkB;AAAA,EACtE,QAAQ;AACN,oBAAgB,iBAAiB,wDAAwD;AACzF;AAAA,EACF;AAEA,MAAI;AACF,UAAM,MAAM,QAAQ;AAAA,MAClB,SAAS;AAAA,MACT;AAAA,MACA;AAAA,MACA,UAAU,OAAO,QAAQ;AAAA,MACzB,gBAAgB,iBAAiB,OAAO,cAAc,IAAI;AAAA,IAC5D,CAAC;AAAA,EACH,SAAS,OAAO;AACd,gBAAY,iBAAiB,uCAAuC;AAAA,MAClE;AAAA,MACA;AAAA,MACA,OAAO,iBAAiB,QAAQ,MAAM,UAAU;AAAA,IAClD,CAAC;AACD,UAAM;AAAA,EACR;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { loadEntityRecordScope } from "@open-mercato/shared/lib/db/recordScope";
|
|
2
2
|
import { resolveAutoIndexingEnabled } from "../lib/auto-indexing.js";
|
|
3
3
|
import { searchDebugWarn, searchError } from "../../../lib/debug.js";
|
|
4
4
|
const metadata = { event: "query_index.vectorize_one", persistent: false };
|
|
@@ -17,11 +17,9 @@ async function handle(payload, ctx) {
|
|
|
17
17
|
if ((organizationId == null || tenantId == null) && em) {
|
|
18
18
|
try {
|
|
19
19
|
const lookupEm = typeof em.fork === "function" ? em.fork() : em;
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
if (organizationId == null) organizationId = row?.organization_id ?? organizationId;
|
|
24
|
-
if (tenantId == null) tenantId = row?.tenant_id ?? tenantId;
|
|
20
|
+
const scope = await loadEntityRecordScope(lookupEm, entityType, recordId);
|
|
21
|
+
if (organizationId == null) organizationId = scope?.organizationId ?? organizationId;
|
|
22
|
+
if (tenantId == null) tenantId = scope?.tenantId ?? tenantId;
|
|
25
23
|
} catch {
|
|
26
24
|
}
|
|
27
25
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/search/subscribers/vector_upsert.ts"],
|
|
4
|
-
"sourcesContent": ["import {
|
|
5
|
-
"mappings": "AAAA,SAAS,
|
|
4
|
+
"sourcesContent": ["import { loadEntityRecordScope } from '@open-mercato/shared/lib/db/recordScope'\nimport type { Queue } from '@open-mercato/queue'\nimport type { VectorIndexJobPayload } from '../../../queue/vector-indexing'\nimport { resolveAutoIndexingEnabled } from '../lib/auto-indexing'\nimport { searchDebugWarn, searchError } from '../../../lib/debug'\n\nexport const metadata = { event: 'query_index.vectorize_one', persistent: false }\n\ntype Payload = {\n entityType?: string\n recordId?: string\n organizationId?: string | null\n tenantId?: string | null\n}\n\ntype HandlerContext = { resolve: <T = unknown>(name: string) => T }\n\nexport default async function handle(payload: Payload, ctx: HandlerContext) {\n const entityType = String(payload?.entityType ?? '')\n const recordId = String(payload?.recordId ?? '')\n if (!entityType || !recordId) return\n\n let organizationId = payload?.organizationId ?? null\n let tenantId = payload?.tenantId ?? null\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let em: any | null = null\n try {\n em = ctx.resolve('em')\n } catch {\n em = null\n }\n\n if ((organizationId == null || tenantId == null) && em) {\n try {\n const lookupEm = typeof em.fork === 'function' ? em.fork() : em\n const scope = await loadEntityRecordScope(lookupEm, entityType, recordId)\n if (organizationId == null) organizationId = scope?.organizationId ?? organizationId\n if (tenantId == null) tenantId = scope?.tenantId ?? tenantId\n } catch {\n // Ignore lookup errors\n }\n }\n\n if (!tenantId) return\n\n const autoIndexingEnabled = await resolveAutoIndexingEnabled(ctx, { defaultValue: true })\n if (!autoIndexingEnabled) return\n\n let queue: Queue<VectorIndexJobPayload>\n try {\n queue = ctx.resolve<Queue<VectorIndexJobPayload>>('vectorIndexQueue')\n } catch {\n searchDebugWarn('search.vector', 'vectorIndexQueue not available, skipping vector indexing')\n return\n }\n\n try {\n await queue.enqueue({\n jobType: 'index',\n entityType,\n recordId,\n tenantId: String(tenantId),\n organizationId: organizationId ? String(organizationId) : null,\n })\n } catch (error) {\n searchError('search.vector', 'Failed to enqueue vector index job', {\n entityType,\n recordId,\n error: error instanceof Error ? error.message : error,\n })\n throw error // Propagate to caller so failure is visible\n }\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,6BAA6B;AAGtC,SAAS,kCAAkC;AAC3C,SAAS,iBAAiB,mBAAmB;AAEtC,MAAM,WAAW,EAAE,OAAO,6BAA6B,YAAY,MAAM;AAWhF,eAAO,OAA8B,SAAkB,KAAqB;AAC1E,QAAM,aAAa,OAAO,SAAS,cAAc,EAAE;AACnD,QAAM,WAAW,OAAO,SAAS,YAAY,EAAE;AAC/C,MAAI,CAAC,cAAc,CAAC,SAAU;AAE9B,MAAI,iBAAiB,SAAS,kBAAkB;AAChD,MAAI,WAAW,SAAS,YAAY;AAGpC,MAAI,KAAiB;AACrB,MAAI;AACF,SAAK,IAAI,QAAQ,IAAI;AAAA,EACvB,QAAQ;AACN,SAAK;AAAA,EACP;AAEA,OAAK,kBAAkB,QAAQ,YAAY,SAAS,IAAI;AACtD,QAAI;AACF,YAAM,WAAW,OAAO,GAAG,SAAS,aAAa,GAAG,KAAK,IAAI;AAC7D,YAAM,QAAQ,MAAM,sBAAsB,UAAU,YAAY,QAAQ;AACxE,UAAI,kBAAkB,KAAM,kBAAiB,OAAO,kBAAkB;AACtE,UAAI,YAAY,KAAM,YAAW,OAAO,YAAY;AAAA,IACtD,QAAQ;AAAA,IAER;AAAA,EACF;AAEA,MAAI,CAAC,SAAU;AAEf,QAAM,sBAAsB,MAAM,2BAA2B,KAAK,EAAE,cAAc,KAAK,CAAC;AACxF,MAAI,CAAC,oBAAqB;AAE1B,MAAI;AACJ,MAAI;AACF,YAAQ,IAAI,QAAsC,kBAAkB;AAAA,EACtE,QAAQ;AACN,oBAAgB,iBAAiB,0DAA0D;AAC3F;AAAA,EACF;AAEA,MAAI;AACF,UAAM,MAAM,QAAQ;AAAA,MAClB,SAAS;AAAA,MACT;AAAA,MACA;AAAA,MACA,UAAU,OAAO,QAAQ;AAAA,MACzB,gBAAgB,iBAAiB,OAAO,cAAc,IAAI;AAAA,IAC5D,CAAC;AAAA,EACH,SAAS,OAAO;AACd,gBAAY,iBAAiB,sCAAsC;AAAA,MACjE;AAAA,MACA;AAAA,MACA,OAAO,iBAAiB,QAAQ,MAAM,UAAU;AAAA,IAClD,CAAC;AACD,UAAM;AAAA,EACR;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-mercato/search",
|
|
3
|
-
"version": "0.10.1-develop.
|
|
3
|
+
"version": "0.10.1-develop.8022.1.b6753de892",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -127,9 +127,9 @@
|
|
|
127
127
|
"zod": "^4.6.6"
|
|
128
128
|
},
|
|
129
129
|
"peerDependencies": {
|
|
130
|
-
"@open-mercato/core": "0.10.1-develop.
|
|
131
|
-
"@open-mercato/queue": "0.10.1-develop.
|
|
132
|
-
"@open-mercato/shared": "0.10.1-develop.
|
|
130
|
+
"@open-mercato/core": "0.10.1-develop.8022.1.b6753de892",
|
|
131
|
+
"@open-mercato/queue": "0.10.1-develop.8022.1.b6753de892",
|
|
132
|
+
"@open-mercato/shared": "0.10.1-develop.8022.1.b6753de892"
|
|
133
133
|
},
|
|
134
134
|
"devDependencies": {
|
|
135
135
|
"@types/jest": "^30.0.0",
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
jest.mock('@open-mercato/shared/lib/query/engine', () => ({
|
|
2
|
-
resolveEntityTableName: jest.fn(() =>
|
|
2
|
+
resolveEntityTableName: jest.fn((_em: unknown, entityType: string) =>
|
|
3
|
+
entityType.startsWith('feature_toggles:') ? 'feature_toggles' : 'customer_people'),
|
|
3
4
|
}))
|
|
4
5
|
jest.mock('../../lib/auto-indexing', () => ({
|
|
5
6
|
resolveAutoIndexingEnabled: jest.fn().mockResolvedValue(false),
|
|
@@ -10,19 +11,34 @@ import vectorDelete from '../vector_delete'
|
|
|
10
11
|
import fulltextUpsert from '../fulltext_upsert'
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
|
-
* The unscoped-record lookup reads
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* run on a fork so a swallowed error can never poison a caller's transaction.
|
|
14
|
+
* The unscoped-record lookup reads the scope of a record from its entity table. It must
|
|
15
|
+
* run on a fork so an error can never poison a caller's transaction, and it must only
|
|
16
|
+
* select the scope columns the table actually has: global tables such as
|
|
17
|
+
* `feature_toggles` have neither `organization_id` nor `tenant_id`, and selecting them
|
|
18
|
+
* logged `column "organization_id" does not exist` for every toggle seeded during init.
|
|
19
19
|
*/
|
|
20
|
+
const metadata = [
|
|
21
|
+
{ tableName: 'feature_toggles', properties: { id: { fieldNames: ['id'] } } },
|
|
22
|
+
{
|
|
23
|
+
tableName: 'customer_people',
|
|
24
|
+
properties: {
|
|
25
|
+
id: { fieldNames: ['id'] },
|
|
26
|
+
organizationId: { fieldNames: ['organization_id'] },
|
|
27
|
+
tenantId: { fieldNames: ['tenant_id'] },
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
]
|
|
31
|
+
|
|
20
32
|
function buildContext() {
|
|
21
|
-
const executeTakeFirst = jest.fn().
|
|
33
|
+
const executeTakeFirst = jest.fn().mockResolvedValue({ organization_id: 'org-1', tenant_id: 'tenant-1' })
|
|
22
34
|
const where = jest.fn(() => ({ executeTakeFirst }))
|
|
23
35
|
const select = jest.fn(() => ({ where }))
|
|
24
|
-
const
|
|
25
|
-
const
|
|
36
|
+
const selectFrom = jest.fn(() => ({ select }))
|
|
37
|
+
const forkKysely = { selectFrom }
|
|
38
|
+
const forkEm = {
|
|
39
|
+
getKysely: jest.fn(() => forkKysely),
|
|
40
|
+
getMetadata: jest.fn(() => ({ getAll: () => metadata })),
|
|
41
|
+
}
|
|
26
42
|
const requestEm = { getKysely: jest.fn(), fork: jest.fn(() => forkEm) }
|
|
27
43
|
const ctx = {
|
|
28
44
|
resolve: jest.fn((name: string) => {
|
|
@@ -30,24 +46,39 @@ function buildContext() {
|
|
|
30
46
|
throw new Error(`[internal] not registered: ${name}`)
|
|
31
47
|
}),
|
|
32
48
|
}
|
|
33
|
-
return { ctx, requestEm, forkEm, executeTakeFirst }
|
|
49
|
+
return { ctx, requestEm, forkEm, selectFrom, select, executeTakeFirst }
|
|
34
50
|
}
|
|
35
51
|
|
|
36
52
|
type SubscriberHandler = (payload: Record<string, unknown>, ctx: unknown) => Promise<void>
|
|
37
53
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
54
|
+
const handlers: Array<[string, SubscriberHandler, string]> = [
|
|
55
|
+
['query_index.vectorize_one', vectorUpsert as SubscriberHandler, 'entityType'],
|
|
56
|
+
['query_index.delete_one', vectorDelete as SubscriberHandler, 'entityType'],
|
|
57
|
+
['search.index_record', fulltextUpsert as SubscriberHandler, 'entityId'],
|
|
58
|
+
]
|
|
59
|
+
|
|
60
|
+
describe('search subscriber scope lookup', () => {
|
|
61
|
+
it.each(handlers)('%s skips the row lookup for a table without scope columns', async (_event, handler, entityKey) => {
|
|
62
|
+
const { ctx, requestEm, selectFrom } = buildContext()
|
|
63
|
+
|
|
64
|
+
await expect(handler({ [entityKey]: 'feature_toggles:feature_toggle', recordId: 'r1', tenantId: null, organizationId: null }, ctx))
|
|
65
|
+
.resolves.toBeUndefined()
|
|
66
|
+
|
|
67
|
+
expect(requestEm.getKysely).not.toHaveBeenCalled()
|
|
68
|
+
expect(selectFrom).not.toHaveBeenCalled()
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
it.each(handlers)('%s reads scope columns on a fork for scoped tables', async (_event, handler, entityKey) => {
|
|
72
|
+
const { ctx, requestEm, forkEm, selectFrom, select, executeTakeFirst } = buildContext()
|
|
45
73
|
|
|
46
|
-
await expect(handler(
|
|
74
|
+
await expect(handler({ [entityKey]: 'customers:customer_person', recordId: 'r1', tenantId: null, organizationId: null }, ctx))
|
|
75
|
+
.resolves.toBeUndefined()
|
|
47
76
|
|
|
48
77
|
expect(requestEm.getKysely).not.toHaveBeenCalled()
|
|
49
78
|
expect(requestEm.fork).toHaveBeenCalledTimes(1)
|
|
50
|
-
expect(forkEm.getKysely).
|
|
79
|
+
expect(forkEm.getKysely).toHaveBeenCalled()
|
|
80
|
+
expect(selectFrom).toHaveBeenCalledWith('customer_people')
|
|
81
|
+
expect(select).toHaveBeenCalledWith(['organization_id', 'tenant_id'])
|
|
51
82
|
expect(executeTakeFirst).toHaveBeenCalledTimes(1)
|
|
52
83
|
})
|
|
53
84
|
})
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto'
|
|
2
|
+
import { Kysely, PostgresDialect, sql } from 'kysely'
|
|
3
|
+
import { Pool } from 'pg'
|
|
4
|
+
import { resetEntityScopeColumnsCacheForTests } from '@open-mercato/shared/lib/db/recordScope'
|
|
5
|
+
import vectorUpsert from '../vector_upsert'
|
|
6
|
+
import vectorDelete from '../vector_delete'
|
|
7
|
+
import fulltextUpsert from '../fulltext_upsert'
|
|
8
|
+
|
|
9
|
+
jest.mock('../../lib/auto-indexing', () => ({ resolveAutoIndexingEnabled: async () => true }))
|
|
10
|
+
|
|
11
|
+
const postgresUrl = process.env.OM_QUERY_INDEX_PG_URL
|
|
12
|
+
const describePostgres = postgresUrl ? describe : describe.skip
|
|
13
|
+
const tenantId = randomUUID()
|
|
14
|
+
const organizationId = randomUUID()
|
|
15
|
+
const otherTenantId = randomUUID()
|
|
16
|
+
const otherOrganizationId = randomUUID()
|
|
17
|
+
const recordId = randomUUID()
|
|
18
|
+
const otherRecordId = randomUUID()
|
|
19
|
+
type ScopeDatabase = Record<string, Record<string, unknown>>
|
|
20
|
+
type Job = {
|
|
21
|
+
jobType: string
|
|
22
|
+
entityType: string
|
|
23
|
+
recordId: string
|
|
24
|
+
tenantId: string
|
|
25
|
+
organizationId: string | null
|
|
26
|
+
}
|
|
27
|
+
type Context = { resolve: <T = unknown>(name: string) => T }
|
|
28
|
+
type Handler = (payload: Record<string, string | null>, ctx: Context) => Promise<void>
|
|
29
|
+
const subscribers: Array<[string, Handler, string, string]> = [
|
|
30
|
+
['vector upsert', vectorUpsert, 'entityType', 'index'],
|
|
31
|
+
['vector delete', vectorDelete, 'entityType', 'delete'],
|
|
32
|
+
['fulltext upsert', fulltextUpsert, 'entityId', 'index'],
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
const properties = {
|
|
36
|
+
id: { fieldNames: ['id'] },
|
|
37
|
+
tenantId: { fieldNames: ['tenant_id'] },
|
|
38
|
+
organizationId: { fieldNames: ['organization_id'] },
|
|
39
|
+
}
|
|
40
|
+
const metadata = [
|
|
41
|
+
{ className: 'ScopeGlobal', tableName: 'scope_global', properties: { id: properties.id } },
|
|
42
|
+
{ className: 'ScopeTenant', tableName: 'scope_tenant', properties: { id: properties.id, tenantId: properties.tenantId } },
|
|
43
|
+
{ className: 'ScopeScoped', tableName: 'scope_scoped', properties },
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
describePostgres('search subscribers resolve record scope against PostgreSQL', () => {
|
|
47
|
+
const schema = `scope_lookup_${randomUUID().replaceAll('-', '')}`
|
|
48
|
+
let admin: Kysely<ScopeDatabase>
|
|
49
|
+
let db: Kysely<ScopeDatabase>
|
|
50
|
+
const queries: string[] = []
|
|
51
|
+
|
|
52
|
+
beforeAll(async () => {
|
|
53
|
+
admin = new Kysely<ScopeDatabase>({ dialect: new PostgresDialect({ pool: new Pool({ connectionString: postgresUrl }) }) })
|
|
54
|
+
await sql`create schema ${sql.id(schema)}`.execute(admin)
|
|
55
|
+
db = new Kysely<ScopeDatabase>({
|
|
56
|
+
dialect: new PostgresDialect({ pool: new Pool({ connectionString: postgresUrl, options: `-c search_path=${schema}` }) }),
|
|
57
|
+
log(event) { if (event.level === 'query') queries.push(event.query.sql) },
|
|
58
|
+
})
|
|
59
|
+
await sql`create table scope_global (id uuid primary key)`.execute(db)
|
|
60
|
+
await sql`create table scope_tenant (id uuid primary key, tenant_id uuid not null)`.execute(db)
|
|
61
|
+
await sql`create table scope_scoped (id uuid primary key, tenant_id uuid not null, organization_id uuid not null)`.execute(db)
|
|
62
|
+
await sql`create table scope_probeds (id uuid primary key, tenant_id uuid not null)`.execute(db)
|
|
63
|
+
await sql`insert into scope_global values (${recordId}::uuid)`.execute(db)
|
|
64
|
+
for (const table of ['scope_tenant', 'scope_probeds']) {
|
|
65
|
+
await sql`insert into ${sql.table(table)} values (${recordId}::uuid, ${tenantId}::uuid), (${otherRecordId}::uuid, ${otherTenantId}::uuid)`.execute(db)
|
|
66
|
+
}
|
|
67
|
+
await sql`insert into scope_scoped values
|
|
68
|
+
(${recordId}::uuid, ${tenantId}::uuid, ${organizationId}::uuid),
|
|
69
|
+
(${otherRecordId}::uuid, ${otherTenantId}::uuid, ${otherOrganizationId}::uuid)`.execute(db)
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
afterAll(async () => {
|
|
73
|
+
resetEntityScopeColumnsCacheForTests()
|
|
74
|
+
if (db) await db.destroy()
|
|
75
|
+
if (admin) {
|
|
76
|
+
await sql`drop schema if exists ${sql.id(schema)} cascade`.execute(admin)
|
|
77
|
+
await admin.destroy()
|
|
78
|
+
}
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
beforeEach(() => {
|
|
82
|
+
resetEntityScopeColumnsCacheForTests()
|
|
83
|
+
queries.length = 0
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
function context() {
|
|
87
|
+
const enqueue = jest.fn<Promise<void>, [Job]>().mockResolvedValue(undefined)
|
|
88
|
+
const fork = jest.fn(() => ({
|
|
89
|
+
getKysely: <Database = unknown>() => db as unknown as Kysely<Database>,
|
|
90
|
+
getMetadata: () => ({
|
|
91
|
+
find: (className: string) => metadata.find((entry) => entry.className === className),
|
|
92
|
+
getAll: () => new Map(metadata.map((entry) => [entry.className, entry])),
|
|
93
|
+
}),
|
|
94
|
+
}))
|
|
95
|
+
const ctx: Context = {
|
|
96
|
+
resolve: <T = unknown>(name: string): T => {
|
|
97
|
+
if (name === 'em') return { fork } as T
|
|
98
|
+
if (name === 'fulltextIndexQueue' || name === 'vectorIndexQueue') return { enqueue } as T
|
|
99
|
+
throw new Error(`[internal] Unknown test dependency: ${name}`)
|
|
100
|
+
},
|
|
101
|
+
}
|
|
102
|
+
return { ctx, enqueue, fork }
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
it.each(subscribers)('%s skips global records without querying nonexistent scope columns', async (_name, handle, entityKey) => {
|
|
106
|
+
const { ctx, enqueue, fork } = context()
|
|
107
|
+
await handle({ [entityKey]: 'scope_tests:scope_global', recordId }, ctx)
|
|
108
|
+
expect(fork).toHaveBeenCalledTimes(1)
|
|
109
|
+
expect(queries).toEqual([])
|
|
110
|
+
expect(enqueue).not.toHaveBeenCalled()
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
it.each(subscribers)('%s reads tenant-only scope and enqueues the owning tenant', async (_name, handle, entityKey, jobType) => {
|
|
114
|
+
const { ctx, enqueue } = context()
|
|
115
|
+
await handle({ [entityKey]: 'scope_tests:scope_tenant', recordId }, ctx)
|
|
116
|
+
expect(queries).toEqual(['select "tenant_id" from "scope_tenant" where "id" = $1'])
|
|
117
|
+
expect(enqueue).toHaveBeenCalledWith({ jobType, entityType: 'scope_tests:scope_tenant', recordId, tenantId, organizationId: null })
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
it.each(subscribers)('%s reads organization scope without assigning another record owner', async (_name, handle, entityKey, jobType) => {
|
|
121
|
+
const { ctx, enqueue } = context()
|
|
122
|
+
await handle({ [entityKey]: 'scope_tests:scope_scoped', recordId }, ctx)
|
|
123
|
+
await handle({ [entityKey]: 'scope_tests:scope_scoped', recordId: otherRecordId }, ctx)
|
|
124
|
+
expect(queries).toEqual([
|
|
125
|
+
'select "organization_id", "tenant_id" from "scope_scoped" where "id" = $1',
|
|
126
|
+
'select "organization_id", "tenant_id" from "scope_scoped" where "id" = $1',
|
|
127
|
+
])
|
|
128
|
+
expect(enqueue.mock.calls.map(([job]) => job)).toEqual([
|
|
129
|
+
{ jobType, entityType: 'scope_tests:scope_scoped', recordId, tenantId, organizationId },
|
|
130
|
+
{ jobType, entityType: 'scope_tests:scope_scoped', recordId: otherRecordId, tenantId: otherTenantId, organizationId: otherOrganizationId },
|
|
131
|
+
])
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
it.each(subscribers)('%s does not queue a nonexistent record', async (_name, handle, entityKey) => {
|
|
135
|
+
const { ctx, enqueue } = context()
|
|
136
|
+
await handle({ [entityKey]: 'scope_tests:scope_scoped', recordId: randomUUID() }, ctx)
|
|
137
|
+
expect(enqueue).not.toHaveBeenCalled()
|
|
138
|
+
expect(queries).toHaveLength(1)
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
it.each(subscribers)('%s probes unregistered tables and selects only existing scope columns', async (_name, handle, entityKey, jobType) => {
|
|
142
|
+
const { ctx, enqueue } = context()
|
|
143
|
+
await handle({ [entityKey]: 'scope_tests:scope_probed', recordId }, ctx)
|
|
144
|
+
expect(queries[0]).toContain('information_schema"."columns')
|
|
145
|
+
expect(queries[1]).toBe('select "tenant_id" from "scope_probeds" where "id" = $1')
|
|
146
|
+
expect(enqueue).toHaveBeenCalledWith({ jobType, entityType: 'scope_tests:scope_probed', recordId, tenantId, organizationId: null })
|
|
147
|
+
})
|
|
148
|
+
})
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { loadEntityRecordScope } from '@open-mercato/shared/lib/db/recordScope'
|
|
2
2
|
import type { Queue } from '@open-mercato/queue'
|
|
3
3
|
import type { FulltextIndexJobPayload } from '../../../queue/fulltext-indexing'
|
|
4
4
|
import { resolveAutoIndexingEnabled } from '../lib/auto-indexing'
|
|
@@ -38,15 +38,9 @@ export default async function handle(payload: Payload, ctx: HandlerContext) {
|
|
|
38
38
|
if ((organizationId == null || tenantId == null) && em) {
|
|
39
39
|
try {
|
|
40
40
|
const lookupEm = typeof em.fork === 'function' ? em.fork() : em
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
.selectFrom(table as any)
|
|
45
|
-
.select(['organization_id' as any, 'tenant_id' as any])
|
|
46
|
-
.where('id' as any, '=', recordId)
|
|
47
|
-
.executeTakeFirst() as { organization_id?: string | null; tenant_id?: string | null } | undefined
|
|
48
|
-
if (organizationId == null) organizationId = row?.organization_id ?? organizationId
|
|
49
|
-
if (tenantId == null) tenantId = row?.tenant_id ?? tenantId
|
|
41
|
+
const scope = await loadEntityRecordScope(lookupEm, entityType, recordId)
|
|
42
|
+
if (organizationId == null) organizationId = scope?.organizationId ?? organizationId
|
|
43
|
+
if (tenantId == null) tenantId = scope?.tenantId ?? tenantId
|
|
50
44
|
} catch {
|
|
51
45
|
// Ignore lookup errors
|
|
52
46
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { loadEntityRecordScope } from '@open-mercato/shared/lib/db/recordScope'
|
|
2
2
|
import type { Queue } from '@open-mercato/queue'
|
|
3
3
|
import type { VectorIndexJobPayload } from '../../../queue/vector-indexing'
|
|
4
4
|
import { resolveAutoIndexingEnabled } from '../lib/auto-indexing'
|
|
@@ -34,15 +34,9 @@ export default async function handle(payload: Payload, ctx: HandlerContext) {
|
|
|
34
34
|
if ((organizationId == null || tenantId == null) && em) {
|
|
35
35
|
try {
|
|
36
36
|
const lookupEm = typeof em.fork === 'function' ? em.fork() : em
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
.selectFrom(table as any)
|
|
41
|
-
.select(['organization_id' as any, 'tenant_id' as any])
|
|
42
|
-
.where('id' as any, '=', recordId)
|
|
43
|
-
.executeTakeFirst() as { organization_id?: string | null; tenant_id?: string | null } | undefined
|
|
44
|
-
if (organizationId == null) organizationId = row?.organization_id ?? organizationId
|
|
45
|
-
if (tenantId == null) tenantId = row?.tenant_id ?? tenantId
|
|
37
|
+
const scope = await loadEntityRecordScope(lookupEm, entityType, recordId)
|
|
38
|
+
if (organizationId == null) organizationId = scope?.organizationId ?? organizationId
|
|
39
|
+
if (tenantId == null) tenantId = scope?.tenantId ?? tenantId
|
|
46
40
|
} catch {
|
|
47
41
|
// Ignore lookup errors
|
|
48
42
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { loadEntityRecordScope } from '@open-mercato/shared/lib/db/recordScope'
|
|
2
2
|
import type { Queue } from '@open-mercato/queue'
|
|
3
3
|
import type { VectorIndexJobPayload } from '../../../queue/vector-indexing'
|
|
4
4
|
import { resolveAutoIndexingEnabled } from '../lib/auto-indexing'
|
|
@@ -34,15 +34,9 @@ export default async function handle(payload: Payload, ctx: HandlerContext) {
|
|
|
34
34
|
if ((organizationId == null || tenantId == null) && em) {
|
|
35
35
|
try {
|
|
36
36
|
const lookupEm = typeof em.fork === 'function' ? em.fork() : em
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
.selectFrom(table as any)
|
|
41
|
-
.select(['organization_id' as any, 'tenant_id' as any])
|
|
42
|
-
.where('id' as any, '=', recordId)
|
|
43
|
-
.executeTakeFirst() as { organization_id?: string | null; tenant_id?: string | null } | undefined
|
|
44
|
-
if (organizationId == null) organizationId = row?.organization_id ?? organizationId
|
|
45
|
-
if (tenantId == null) tenantId = row?.tenant_id ?? tenantId
|
|
37
|
+
const scope = await loadEntityRecordScope(lookupEm, entityType, recordId)
|
|
38
|
+
if (organizationId == null) organizationId = scope?.organizationId ?? organizationId
|
|
39
|
+
if (tenantId == null) tenantId = scope?.tenantId ?? tenantId
|
|
46
40
|
} catch {
|
|
47
41
|
// Ignore lookup errors
|
|
48
42
|
}
|