@open-mercato/search 0.6.5-develop.5337.1.534b781eac → 0.6.5
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/fulltext/drivers/meilisearch/index.js +4 -2
- package/dist/fulltext/drivers/meilisearch/index.js.map +2 -2
- package/dist/indexer/search-indexer.js +3 -3
- package/dist/indexer/search-indexer.js.map +2 -2
- package/dist/modules/search/api/index/route.js +25 -10
- package/dist/modules/search/api/index/route.js.map +3 -3
- package/dist/modules/search/cli.js +1 -1
- package/dist/modules/search/cli.js.map +2 -2
- package/dist/modules/search/subscribers/vector_purge.js +2 -1
- package/dist/modules/search/subscribers/vector_purge.js.map +2 -2
- package/dist/service.js +2 -2
- package/dist/service.js.map +2 -2
- package/dist/strategies/fulltext.strategy.js +2 -2
- package/dist/strategies/fulltext.strategy.js.map +2 -2
- package/dist/strategies/token.strategy.js +7 -2
- package/dist/strategies/token.strategy.js.map +2 -2
- package/dist/strategies/vector.strategy.js +2 -2
- package/dist/strategies/vector.strategy.js.map +2 -2
- package/dist/vector/drivers/pgvector/index.js +10 -3
- package/dist/vector/drivers/pgvector/index.js.map +2 -2
- package/dist/vector/services/vector-index.service.js +2 -2
- package/dist/vector/services/vector-index.service.js.map +2 -2
- package/dist/vector/types.js.map +1 -1
- package/package.json +12 -13
- package/src/__tests__/purge-org-scoping.test.ts +136 -0
- package/src/__tests__/service.test.ts +2 -1
- package/src/fulltext/drivers/meilisearch/index.ts +8 -2
- package/src/fulltext/types.ts +1 -1
- package/src/indexer/search-indexer.ts +4 -3
- package/src/modules/search/api/__tests__/org-scoping.routes.test.ts +111 -1
- package/src/modules/search/api/index/route.ts +36 -8
- package/src/modules/search/cli.ts +1 -1
- package/src/modules/search/subscribers/__tests__/vector_purge.test.ts +55 -0
- package/src/modules/search/subscribers/vector_purge.ts +1 -0
- package/src/service.ts +2 -2
- package/src/strategies/fulltext.strategy.ts +2 -2
- package/src/strategies/token.strategy.ts +8 -3
- package/src/strategies/vector.strategy.ts +2 -2
- package/src/vector/drivers/pgvector/index.ts +11 -3
- package/src/vector/services/vector-index.service.ts +2 -2
- package/src/vector/types.ts +1 -1
package/dist/vector/types.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/vector/types.ts"],
|
|
4
|
-
"sourcesContent": ["import type { EntityId } from '@open-mercato/shared/modules/entities'\nimport type {\n VectorDriverId,\n VectorIndexSource,\n VectorModuleConfig,\n VectorEntityConfig,\n VectorLinkDescriptor,\n VectorResultPresenter,\n VectorSearchHit,\n VectorQueryRequest,\n VectorIndexEntry,\n} from '@open-mercato/shared/modules/vector'\n\nexport type {\n VectorDriverId,\n VectorIndexSource,\n VectorModuleConfig,\n VectorEntityConfig,\n VectorLinkDescriptor,\n VectorResultPresenter,\n VectorSearchHit,\n VectorQueryRequest,\n VectorIndexEntry,\n}\n\nexport type VectorDriverDocument = {\n entityId: EntityId\n recordId: string\n tenantId: string\n organizationId?: string | null\n checksum: string\n embedding: number[]\n url?: string | null\n presenter?: VectorResultPresenter | null\n links?: VectorLinkDescriptor[] | null\n payload?: Record<string, unknown> | null\n driverId: VectorDriverId\n resultTitle: string\n resultSubtitle?: string | null\n resultIcon?: string | null\n resultBadge?: string | null\n resultSnapshot?: string | null\n primaryLinkHref?: string | null\n primaryLinkLabel?: string | null\n}\n\nexport type VectorDriverQuery = {\n vector: number[]\n limit?: number\n filter?: {\n entityIds?: EntityId[]\n organizationId?: string | null\n organizationIds?: string[] | null\n tenantId: string\n }\n}\n\nexport type VectorDriverQueryResult = {\n entityId: EntityId\n recordId: string\n organizationId?: string | null\n score: number\n checksum: string\n url?: string | null\n presenter?: VectorResultPresenter | null\n links?: VectorLinkDescriptor[] | null\n payload?: Record<string, unknown> | null\n resultTitle: string\n resultSubtitle?: string | null\n resultIcon?: string | null\n resultBadge?: string | null\n resultSnapshot?: string | null\n primaryLinkHref?: string | null\n primaryLinkLabel?: string | null\n}\n\nexport type VectorDriverListParams = {\n tenantId: string\n organizationId?: string | null\n entityId?: EntityId\n limit?: number\n offset?: number\n orderBy?: 'created' | 'updated'\n}\n\nexport type VectorDriverCountParams = {\n tenantId: string\n organizationId?: string | null\n entityId?: EntityId\n}\n\nexport type VectorDriverRemoveOrphansParams = {\n entityId: EntityId\n tenantId?: string | null\n organizationId?: string | null\n olderThan: Date\n}\n\nexport interface VectorDriver {\n readonly id: VectorDriverId\n ensureReady(): Promise<void>\n upsert(doc: VectorDriverDocument): Promise<void>\n delete(entityId: EntityId, recordId: string, tenantId: string): Promise<void>\n query(input: VectorDriverQuery): Promise<VectorDriverQueryResult[]>\n getChecksum(entityId: EntityId, recordId: string, tenantId: string): Promise<string | null>\n purge?(entityId: EntityId, tenantId: string): Promise<void>\n list?(params: VectorDriverListParams): Promise<VectorIndexEntry[]>\n count?(params: VectorDriverCountParams): Promise<number>\n removeOrphans?(params: VectorDriverRemoveOrphansParams): Promise<number | void>\n getTableDimension?(): Promise<number | null>\n recreateWithDimension?(newDimension: number): Promise<void>\n}\n\n// ============================================================================\n// Embedding Provider Configuration Types\n// ============================================================================\n\nexport type EmbeddingProviderId =\n | 'openai'\n | 'google'\n | 'mistral'\n | 'cohere'\n | 'bedrock'\n | 'ollama'\n\nexport type EmbeddingProviderConfig = {\n providerId: EmbeddingProviderId\n model: string\n dimension: number\n outputDimensionality?: number\n baseUrl?: string\n updatedAt: string\n}\n\nexport type EmbeddingModelInfo = {\n id: string\n name: string\n dimension: number\n configurableDimension?: boolean\n minDimension?: number\n maxDimension?: number\n}\n\nexport type EmbeddingProviderInfo = {\n name: string\n envKeyRequired: string\n defaultModel: string\n models: EmbeddingModelInfo[]\n}\n\nexport const EMBEDDING_PROVIDERS: Record<EmbeddingProviderId, EmbeddingProviderInfo> = {\n openai: {\n name: 'OpenAI',\n envKeyRequired: 'OPENAI_API_KEY',\n defaultModel: 'text-embedding-3-small',\n models: [\n { id: 'text-embedding-3-small', name: 'text-embedding-3-small', dimension: 1536 },\n { id: 'text-embedding-3-large', name: 'text-embedding-3-large', dimension: 3072, configurableDimension: true, minDimension: 256, maxDimension: 3072 },\n { id: 'text-embedding-ada-002', name: 'text-embedding-ada-002', dimension: 1536 },\n ],\n },\n google: {\n name: 'Google Generative AI',\n envKeyRequired: 'GOOGLE_GENERATIVE_AI_API_KEY',\n defaultModel: 'text-embedding-004',\n models: [\n { id: 'text-embedding-004', name: 'text-embedding-004', dimension: 768, configurableDimension: true, minDimension: 1, maxDimension: 768 },\n { id: 'embedding-001', name: 'embedding-001', dimension: 768 },\n ],\n },\n mistral: {\n name: 'Mistral',\n envKeyRequired: 'MISTRAL_API_KEY',\n defaultModel: 'mistral-embed',\n models: [\n { id: 'mistral-embed', name: 'mistral-embed', dimension: 1024 },\n ],\n },\n cohere: {\n name: 'Cohere',\n envKeyRequired: 'COHERE_API_KEY',\n defaultModel: 'embed-english-v3.0',\n models: [\n { id: 'embed-english-v3.0', name: 'embed-english-v3.0', dimension: 1024 },\n { id: 'embed-multilingual-v3.0', name: 'embed-multilingual-v3.0', dimension: 1024 },\n { id: 'embed-english-light-v3.0', name: 'embed-english-light-v3.0', dimension: 384 },\n { id: 'embed-multilingual-light-v3.0', name: 'embed-multilingual-light-v3.0', dimension: 384 },\n ],\n },\n bedrock: {\n name: 'Amazon Bedrock',\n envKeyRequired: 'AWS_ACCESS_KEY_ID',\n defaultModel: 'amazon.titan-embed-text-v2:0',\n models: [\n { id: 'amazon.titan-embed-text-v2:0', name: 'Titan Embed Text v2', dimension: 1024, configurableDimension: true, minDimension: 256, maxDimension: 1024 },\n { id: 'amazon.titan-embed-text-v1', name: 'Titan Embed Text v1', dimension: 1536 },\n { id: 'cohere.embed-english-v3', name: 'Cohere Embed English v3', dimension: 1024 },\n { id: 'cohere.embed-multilingual-v3', name: 'Cohere Embed Multilingual v3', dimension: 1024 },\n ],\n },\n ollama: {\n name: 'Ollama (Local)',\n envKeyRequired: 'OLLAMA_BASE_URL',\n defaultModel: 'nomic-embed-text',\n models: [\n { id: 'nomic-embed-text', name: 'nomic-embed-text', dimension: 768 },\n { id: 'mxbai-embed-large', name: 'mxbai-embed-large', dimension: 1024 },\n { id: 'all-minilm', name: 'all-minilm', dimension: 384 },\n { id: 'snowflake-arctic-embed', name: 'snowflake-arctic-embed', dimension: 1024 },\n ],\n },\n}\n\nexport const EMBEDDING_CONFIG_KEY = 'embedding_provider'\n\nexport const DEFAULT_EMBEDDING_CONFIG: EmbeddingProviderConfig = {\n providerId: 'openai',\n model: 'text-embedding-3-small',\n dimension: 1536,\n updatedAt: new Date().toISOString(),\n}\n"],
|
|
4
|
+
"sourcesContent": ["import type { EntityId } from '@open-mercato/shared/modules/entities'\nimport type {\n VectorDriverId,\n VectorIndexSource,\n VectorModuleConfig,\n VectorEntityConfig,\n VectorLinkDescriptor,\n VectorResultPresenter,\n VectorSearchHit,\n VectorQueryRequest,\n VectorIndexEntry,\n} from '@open-mercato/shared/modules/vector'\n\nexport type {\n VectorDriverId,\n VectorIndexSource,\n VectorModuleConfig,\n VectorEntityConfig,\n VectorLinkDescriptor,\n VectorResultPresenter,\n VectorSearchHit,\n VectorQueryRequest,\n VectorIndexEntry,\n}\n\nexport type VectorDriverDocument = {\n entityId: EntityId\n recordId: string\n tenantId: string\n organizationId?: string | null\n checksum: string\n embedding: number[]\n url?: string | null\n presenter?: VectorResultPresenter | null\n links?: VectorLinkDescriptor[] | null\n payload?: Record<string, unknown> | null\n driverId: VectorDriverId\n resultTitle: string\n resultSubtitle?: string | null\n resultIcon?: string | null\n resultBadge?: string | null\n resultSnapshot?: string | null\n primaryLinkHref?: string | null\n primaryLinkLabel?: string | null\n}\n\nexport type VectorDriverQuery = {\n vector: number[]\n limit?: number\n filter?: {\n entityIds?: EntityId[]\n organizationId?: string | null\n organizationIds?: string[] | null\n tenantId: string\n }\n}\n\nexport type VectorDriverQueryResult = {\n entityId: EntityId\n recordId: string\n organizationId?: string | null\n score: number\n checksum: string\n url?: string | null\n presenter?: VectorResultPresenter | null\n links?: VectorLinkDescriptor[] | null\n payload?: Record<string, unknown> | null\n resultTitle: string\n resultSubtitle?: string | null\n resultIcon?: string | null\n resultBadge?: string | null\n resultSnapshot?: string | null\n primaryLinkHref?: string | null\n primaryLinkLabel?: string | null\n}\n\nexport type VectorDriverListParams = {\n tenantId: string\n organizationId?: string | null\n entityId?: EntityId\n limit?: number\n offset?: number\n orderBy?: 'created' | 'updated'\n}\n\nexport type VectorDriverCountParams = {\n tenantId: string\n organizationId?: string | null\n entityId?: EntityId\n}\n\nexport type VectorDriverRemoveOrphansParams = {\n entityId: EntityId\n tenantId?: string | null\n organizationId?: string | null\n olderThan: Date\n}\n\nexport interface VectorDriver {\n readonly id: VectorDriverId\n ensureReady(): Promise<void>\n upsert(doc: VectorDriverDocument): Promise<void>\n delete(entityId: EntityId, recordId: string, tenantId: string): Promise<void>\n query(input: VectorDriverQuery): Promise<VectorDriverQueryResult[]>\n getChecksum(entityId: EntityId, recordId: string, tenantId: string): Promise<string | null>\n purge?(entityId: EntityId, tenantId: string, organizationId?: string | null): Promise<void>\n list?(params: VectorDriverListParams): Promise<VectorIndexEntry[]>\n count?(params: VectorDriverCountParams): Promise<number>\n removeOrphans?(params: VectorDriverRemoveOrphansParams): Promise<number | void>\n getTableDimension?(): Promise<number | null>\n recreateWithDimension?(newDimension: number): Promise<void>\n}\n\n// ============================================================================\n// Embedding Provider Configuration Types\n// ============================================================================\n\nexport type EmbeddingProviderId =\n | 'openai'\n | 'google'\n | 'mistral'\n | 'cohere'\n | 'bedrock'\n | 'ollama'\n\nexport type EmbeddingProviderConfig = {\n providerId: EmbeddingProviderId\n model: string\n dimension: number\n outputDimensionality?: number\n baseUrl?: string\n updatedAt: string\n}\n\nexport type EmbeddingModelInfo = {\n id: string\n name: string\n dimension: number\n configurableDimension?: boolean\n minDimension?: number\n maxDimension?: number\n}\n\nexport type EmbeddingProviderInfo = {\n name: string\n envKeyRequired: string\n defaultModel: string\n models: EmbeddingModelInfo[]\n}\n\nexport const EMBEDDING_PROVIDERS: Record<EmbeddingProviderId, EmbeddingProviderInfo> = {\n openai: {\n name: 'OpenAI',\n envKeyRequired: 'OPENAI_API_KEY',\n defaultModel: 'text-embedding-3-small',\n models: [\n { id: 'text-embedding-3-small', name: 'text-embedding-3-small', dimension: 1536 },\n { id: 'text-embedding-3-large', name: 'text-embedding-3-large', dimension: 3072, configurableDimension: true, minDimension: 256, maxDimension: 3072 },\n { id: 'text-embedding-ada-002', name: 'text-embedding-ada-002', dimension: 1536 },\n ],\n },\n google: {\n name: 'Google Generative AI',\n envKeyRequired: 'GOOGLE_GENERATIVE_AI_API_KEY',\n defaultModel: 'text-embedding-004',\n models: [\n { id: 'text-embedding-004', name: 'text-embedding-004', dimension: 768, configurableDimension: true, minDimension: 1, maxDimension: 768 },\n { id: 'embedding-001', name: 'embedding-001', dimension: 768 },\n ],\n },\n mistral: {\n name: 'Mistral',\n envKeyRequired: 'MISTRAL_API_KEY',\n defaultModel: 'mistral-embed',\n models: [\n { id: 'mistral-embed', name: 'mistral-embed', dimension: 1024 },\n ],\n },\n cohere: {\n name: 'Cohere',\n envKeyRequired: 'COHERE_API_KEY',\n defaultModel: 'embed-english-v3.0',\n models: [\n { id: 'embed-english-v3.0', name: 'embed-english-v3.0', dimension: 1024 },\n { id: 'embed-multilingual-v3.0', name: 'embed-multilingual-v3.0', dimension: 1024 },\n { id: 'embed-english-light-v3.0', name: 'embed-english-light-v3.0', dimension: 384 },\n { id: 'embed-multilingual-light-v3.0', name: 'embed-multilingual-light-v3.0', dimension: 384 },\n ],\n },\n bedrock: {\n name: 'Amazon Bedrock',\n envKeyRequired: 'AWS_ACCESS_KEY_ID',\n defaultModel: 'amazon.titan-embed-text-v2:0',\n models: [\n { id: 'amazon.titan-embed-text-v2:0', name: 'Titan Embed Text v2', dimension: 1024, configurableDimension: true, minDimension: 256, maxDimension: 1024 },\n { id: 'amazon.titan-embed-text-v1', name: 'Titan Embed Text v1', dimension: 1536 },\n { id: 'cohere.embed-english-v3', name: 'Cohere Embed English v3', dimension: 1024 },\n { id: 'cohere.embed-multilingual-v3', name: 'Cohere Embed Multilingual v3', dimension: 1024 },\n ],\n },\n ollama: {\n name: 'Ollama (Local)',\n envKeyRequired: 'OLLAMA_BASE_URL',\n defaultModel: 'nomic-embed-text',\n models: [\n { id: 'nomic-embed-text', name: 'nomic-embed-text', dimension: 768 },\n { id: 'mxbai-embed-large', name: 'mxbai-embed-large', dimension: 1024 },\n { id: 'all-minilm', name: 'all-minilm', dimension: 384 },\n { id: 'snowflake-arctic-embed', name: 'snowflake-arctic-embed', dimension: 1024 },\n ],\n },\n}\n\nexport const EMBEDDING_CONFIG_KEY = 'embedding_provider'\n\nexport const DEFAULT_EMBEDDING_CONFIG: EmbeddingProviderConfig = {\n providerId: 'openai',\n model: 'text-embedding-3-small',\n dimension: 1536,\n updatedAt: new Date().toISOString(),\n}\n"],
|
|
5
5
|
"mappings": "AAsJO,MAAM,sBAA0E;AAAA,EACrF,QAAQ;AAAA,IACN,MAAM;AAAA,IACN,gBAAgB;AAAA,IAChB,cAAc;AAAA,IACd,QAAQ;AAAA,MACN,EAAE,IAAI,0BAA0B,MAAM,0BAA0B,WAAW,KAAK;AAAA,MAChF,EAAE,IAAI,0BAA0B,MAAM,0BAA0B,WAAW,MAAM,uBAAuB,MAAM,cAAc,KAAK,cAAc,KAAK;AAAA,MACpJ,EAAE,IAAI,0BAA0B,MAAM,0BAA0B,WAAW,KAAK;AAAA,IAClF;AAAA,EACF;AAAA,EACA,QAAQ;AAAA,IACN,MAAM;AAAA,IACN,gBAAgB;AAAA,IAChB,cAAc;AAAA,IACd,QAAQ;AAAA,MACN,EAAE,IAAI,sBAAsB,MAAM,sBAAsB,WAAW,KAAK,uBAAuB,MAAM,cAAc,GAAG,cAAc,IAAI;AAAA,MACxI,EAAE,IAAI,iBAAiB,MAAM,iBAAiB,WAAW,IAAI;AAAA,IAC/D;AAAA,EACF;AAAA,EACA,SAAS;AAAA,IACP,MAAM;AAAA,IACN,gBAAgB;AAAA,IAChB,cAAc;AAAA,IACd,QAAQ;AAAA,MACN,EAAE,IAAI,iBAAiB,MAAM,iBAAiB,WAAW,KAAK;AAAA,IAChE;AAAA,EACF;AAAA,EACA,QAAQ;AAAA,IACN,MAAM;AAAA,IACN,gBAAgB;AAAA,IAChB,cAAc;AAAA,IACd,QAAQ;AAAA,MACN,EAAE,IAAI,sBAAsB,MAAM,sBAAsB,WAAW,KAAK;AAAA,MACxE,EAAE,IAAI,2BAA2B,MAAM,2BAA2B,WAAW,KAAK;AAAA,MAClF,EAAE,IAAI,4BAA4B,MAAM,4BAA4B,WAAW,IAAI;AAAA,MACnF,EAAE,IAAI,iCAAiC,MAAM,iCAAiC,WAAW,IAAI;AAAA,IAC/F;AAAA,EACF;AAAA,EACA,SAAS;AAAA,IACP,MAAM;AAAA,IACN,gBAAgB;AAAA,IAChB,cAAc;AAAA,IACd,QAAQ;AAAA,MACN,EAAE,IAAI,gCAAgC,MAAM,uBAAuB,WAAW,MAAM,uBAAuB,MAAM,cAAc,KAAK,cAAc,KAAK;AAAA,MACvJ,EAAE,IAAI,8BAA8B,MAAM,uBAAuB,WAAW,KAAK;AAAA,MACjF,EAAE,IAAI,2BAA2B,MAAM,2BAA2B,WAAW,KAAK;AAAA,MAClF,EAAE,IAAI,gCAAgC,MAAM,gCAAgC,WAAW,KAAK;AAAA,IAC9F;AAAA,EACF;AAAA,EACA,QAAQ;AAAA,IACN,MAAM;AAAA,IACN,gBAAgB;AAAA,IAChB,cAAc;AAAA,IACd,QAAQ;AAAA,MACN,EAAE,IAAI,oBAAoB,MAAM,oBAAoB,WAAW,IAAI;AAAA,MACnE,EAAE,IAAI,qBAAqB,MAAM,qBAAqB,WAAW,KAAK;AAAA,MACtE,EAAE,IAAI,cAAc,MAAM,cAAc,WAAW,IAAI;AAAA,MACvD,EAAE,IAAI,0BAA0B,MAAM,0BAA0B,WAAW,KAAK;AAAA,IAClF;AAAA,EACF;AACF;AAEO,MAAM,uBAAuB;AAE7B,MAAM,2BAAoD;AAAA,EAC/D,YAAY;AAAA,EACZ,OAAO;AAAA,EACP,WAAW;AAAA,EACX,YAAW,oBAAI,KAAK,GAAE,YAAY;AACpC;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-mercato/search",
|
|
3
|
-
"version": "0.6.5
|
|
3
|
+
"version": "0.6.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"exports": {
|
|
@@ -114,21 +114,21 @@
|
|
|
114
114
|
"typecheck": "tsc --noEmit"
|
|
115
115
|
},
|
|
116
116
|
"dependencies": {
|
|
117
|
-
"@ai-sdk/amazon-bedrock": "^4.0.
|
|
118
|
-
"@ai-sdk/cohere": "^3.0.
|
|
119
|
-
"@ai-sdk/google": "^3.0.
|
|
120
|
-
"@ai-sdk/mistral": "^3.0.
|
|
121
|
-
"@ai-sdk/openai": "^3.0.
|
|
122
|
-
"ai": "^6.0.
|
|
123
|
-
"ai-sdk-ollama": "3.8.
|
|
117
|
+
"@ai-sdk/amazon-bedrock": "^4.0.117",
|
|
118
|
+
"@ai-sdk/cohere": "^3.0.38",
|
|
119
|
+
"@ai-sdk/google": "^3.0.82",
|
|
120
|
+
"@ai-sdk/mistral": "^3.0.39",
|
|
121
|
+
"@ai-sdk/openai": "^3.0.71",
|
|
122
|
+
"ai": "^6.0.205",
|
|
123
|
+
"ai-sdk-ollama": "3.8.7",
|
|
124
124
|
"meilisearch": "^0.58.0",
|
|
125
125
|
"pg": "8.21.0",
|
|
126
126
|
"zod": "^4.4.3"
|
|
127
127
|
},
|
|
128
128
|
"peerDependencies": {
|
|
129
|
-
"@open-mercato/core": "0.6.5
|
|
130
|
-
"@open-mercato/queue": "0.6.5
|
|
131
|
-
"@open-mercato/shared": "0.6.5
|
|
129
|
+
"@open-mercato/core": "0.6.5",
|
|
130
|
+
"@open-mercato/queue": "0.6.5",
|
|
131
|
+
"@open-mercato/shared": "0.6.5"
|
|
132
132
|
},
|
|
133
133
|
"devDependencies": {
|
|
134
134
|
"@types/jest": "^30.0.0",
|
|
@@ -142,6 +142,5 @@
|
|
|
142
142
|
"type": "git",
|
|
143
143
|
"url": "https://github.com/open-mercato/open-mercato",
|
|
144
144
|
"directory": "packages/search"
|
|
145
|
-
}
|
|
146
|
-
"stableVersion": "0.6.4"
|
|
145
|
+
}
|
|
147
146
|
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { SearchService } from '../service'
|
|
2
|
+
import { VectorSearchStrategy } from '../strategies/vector.strategy'
|
|
3
|
+
import { FullTextSearchStrategy } from '../strategies/fulltext.strategy'
|
|
4
|
+
import { TokenSearchStrategy } from '../strategies/token.strategy'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Regression coverage for issue #2935: the search purge chain ignored
|
|
8
|
+
* organizationId and deleted index entries by (tenantId, entityType) only,
|
|
9
|
+
* letting a holder of search.embeddings.manage in one organization destroy
|
|
10
|
+
* another organization's entries within the same tenant. organizationId must
|
|
11
|
+
* now flow through SearchService.purge -> every strategy.purge -> the driver,
|
|
12
|
+
* and each store must scope the delete to that organization when provided
|
|
13
|
+
* (while preserving the tenant-wide purge when no organization is given).
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
function fakeStrategy(id: string) {
|
|
17
|
+
return {
|
|
18
|
+
id,
|
|
19
|
+
name: id,
|
|
20
|
+
priority: 1,
|
|
21
|
+
isAvailable: jest.fn().mockResolvedValue(true),
|
|
22
|
+
search: jest.fn().mockResolvedValue([]),
|
|
23
|
+
index: jest.fn().mockResolvedValue(undefined),
|
|
24
|
+
delete: jest.fn().mockResolvedValue(undefined),
|
|
25
|
+
purge: jest.fn().mockResolvedValue(undefined),
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
describe('SearchService.purge organization scoping (issue #2935)', () => {
|
|
30
|
+
it('forwards organizationId to every available strategy', async () => {
|
|
31
|
+
const vector = fakeStrategy('vector')
|
|
32
|
+
const tokens = fakeStrategy('tokens')
|
|
33
|
+
const service = new SearchService({ strategies: [vector, tokens] as never })
|
|
34
|
+
|
|
35
|
+
await service.purge('demo:item', 'tenant-1', 'org-A')
|
|
36
|
+
|
|
37
|
+
expect(vector.purge).toHaveBeenCalledWith('demo:item', 'tenant-1', 'org-A')
|
|
38
|
+
expect(tokens.purge).toHaveBeenCalledWith('demo:item', 'tenant-1', 'org-A')
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('performs a tenant-wide purge (no organization) when organizationId is omitted', async () => {
|
|
42
|
+
const vector = fakeStrategy('vector')
|
|
43
|
+
const service = new SearchService({ strategies: [vector] as never })
|
|
44
|
+
|
|
45
|
+
await service.purge('demo:item', 'tenant-1')
|
|
46
|
+
|
|
47
|
+
expect(vector.purge).toHaveBeenCalledWith('demo:item', 'tenant-1', undefined)
|
|
48
|
+
})
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
describe('VectorSearchStrategy.purge organization scoping (issue #2935)', () => {
|
|
52
|
+
function createDriver() {
|
|
53
|
+
return {
|
|
54
|
+
id: 'pgvector' as const,
|
|
55
|
+
ensureReady: jest.fn().mockResolvedValue(undefined),
|
|
56
|
+
upsert: jest.fn().mockResolvedValue(undefined),
|
|
57
|
+
delete: jest.fn().mockResolvedValue(undefined),
|
|
58
|
+
query: jest.fn().mockResolvedValue([]),
|
|
59
|
+
getChecksum: jest.fn().mockResolvedValue(null),
|
|
60
|
+
purge: jest.fn().mockResolvedValue(undefined),
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
it('forwards organizationId to the vector driver', async () => {
|
|
65
|
+
const driver = createDriver()
|
|
66
|
+
const strategy = new VectorSearchStrategy({ available: true } as never, driver as never)
|
|
67
|
+
|
|
68
|
+
await strategy.purge('demo:item', 'tenant-1', 'org-A')
|
|
69
|
+
|
|
70
|
+
expect(driver.purge).toHaveBeenCalledWith('demo:item', 'tenant-1', 'org-A')
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
it('forwards an undefined organizationId for a tenant-wide purge', async () => {
|
|
74
|
+
const driver = createDriver()
|
|
75
|
+
const strategy = new VectorSearchStrategy({ available: true } as never, driver as never)
|
|
76
|
+
|
|
77
|
+
await strategy.purge('demo:item', 'tenant-1')
|
|
78
|
+
|
|
79
|
+
expect(driver.purge).toHaveBeenCalledWith('demo:item', 'tenant-1', undefined)
|
|
80
|
+
})
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
describe('FullTextSearchStrategy.purge organization scoping (issue #2935)', () => {
|
|
84
|
+
it('forwards organizationId to the fulltext driver', async () => {
|
|
85
|
+
const driver = {
|
|
86
|
+
ensureReady: jest.fn().mockResolvedValue(undefined),
|
|
87
|
+
isHealthy: jest.fn().mockResolvedValue(true),
|
|
88
|
+
search: jest.fn().mockResolvedValue([]),
|
|
89
|
+
index: jest.fn().mockResolvedValue(undefined),
|
|
90
|
+
delete: jest.fn().mockResolvedValue(undefined),
|
|
91
|
+
purge: jest.fn().mockResolvedValue(undefined),
|
|
92
|
+
}
|
|
93
|
+
const strategy = new FullTextSearchStrategy(driver as never)
|
|
94
|
+
|
|
95
|
+
await strategy.purge('demo:item', 'tenant-1', 'org-A')
|
|
96
|
+
|
|
97
|
+
expect(driver.purge).toHaveBeenCalledWith('demo:item', 'tenant-1', 'org-A')
|
|
98
|
+
})
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
describe('TokenSearchStrategy.purge organization scoping (issue #2935)', () => {
|
|
102
|
+
function createMockDb() {
|
|
103
|
+
const wheres: Array<[string, string, unknown]> = []
|
|
104
|
+
const builder: Record<string, unknown> = {
|
|
105
|
+
where: jest.fn((column: string, op: string, value: unknown) => {
|
|
106
|
+
wheres.push([column, op, value])
|
|
107
|
+
return builder
|
|
108
|
+
}),
|
|
109
|
+
execute: jest.fn().mockResolvedValue([]),
|
|
110
|
+
}
|
|
111
|
+
const db = { deleteFrom: jest.fn(() => builder) }
|
|
112
|
+
return { db, wheres }
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
it('adds an organization_id filter to the delete when an organization is provided', async () => {
|
|
116
|
+
const { db, wheres } = createMockDb()
|
|
117
|
+
const strategy = new TokenSearchStrategy(db as never)
|
|
118
|
+
|
|
119
|
+
await strategy.purge('demo:item', 'tenant-1', 'org-A')
|
|
120
|
+
|
|
121
|
+
expect(wheres).toContainEqual(['entity_type', '=', 'demo:item'])
|
|
122
|
+
expect(wheres).toContainEqual(['tenant_id', '=', 'tenant-1'])
|
|
123
|
+
expect(wheres).toContainEqual(['organization_id', '=', 'org-A'])
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
it('does NOT add an organization_id filter for a tenant-wide purge', async () => {
|
|
127
|
+
const { db, wheres } = createMockDb()
|
|
128
|
+
const strategy = new TokenSearchStrategy(db as never)
|
|
129
|
+
|
|
130
|
+
await strategy.purge('demo:item', 'tenant-1')
|
|
131
|
+
|
|
132
|
+
expect(wheres.map((entry) => entry[0])).not.toContain('organization_id')
|
|
133
|
+
expect(wheres).toContainEqual(['entity_type', '=', 'demo:item'])
|
|
134
|
+
expect(wheres).toContainEqual(['tenant_id', '=', 'tenant-1'])
|
|
135
|
+
})
|
|
136
|
+
})
|
|
@@ -457,7 +457,8 @@ describe('SearchService', () => {
|
|
|
457
457
|
|
|
458
458
|
await service.purge('test:entity', 'tenant-123')
|
|
459
459
|
|
|
460
|
-
|
|
460
|
+
// organizationId is forwarded as undefined for a tenant-wide purge (issue #2935)
|
|
461
|
+
expect(strategyWithPurge.purge).toHaveBeenCalledWith('test:entity', 'tenant-123', undefined)
|
|
461
462
|
})
|
|
462
463
|
})
|
|
463
464
|
|
|
@@ -296,14 +296,20 @@ export function createMeilisearchDriver(
|
|
|
296
296
|
}
|
|
297
297
|
},
|
|
298
298
|
|
|
299
|
-
async purge(entityId: EntityId, tenantId: string): Promise<void> {
|
|
299
|
+
async purge(entityId: EntityId, tenantId: string, organizationId?: string | null): Promise<void> {
|
|
300
300
|
const meiliClient = getClient()
|
|
301
301
|
const indexName = buildIndexName(tenantId)
|
|
302
|
+
const normalizedOrganizationId =
|
|
303
|
+
typeof organizationId === 'string' && organizationId.trim().length > 0 ? organizationId.trim() : null
|
|
304
|
+
const filter =
|
|
305
|
+
normalizedOrganizationId !== null
|
|
306
|
+
? `_entityId = "${escapeFilterValue(entityId)}" AND _organizationId = "${escapeFilterValue(normalizedOrganizationId)}"`
|
|
307
|
+
: `_entityId = "${escapeFilterValue(entityId)}"`
|
|
302
308
|
|
|
303
309
|
try {
|
|
304
310
|
const index = meiliClient.index(indexName)
|
|
305
311
|
await index.deleteDocuments({
|
|
306
|
-
filter
|
|
312
|
+
filter,
|
|
307
313
|
})
|
|
308
314
|
} catch (error: unknown) {
|
|
309
315
|
const meilisearchError = error as { code?: string }
|
package/src/fulltext/types.ts
CHANGED
|
@@ -99,7 +99,7 @@ export interface FullTextSearchDriver {
|
|
|
99
99
|
|
|
100
100
|
// Batch operations (optional)
|
|
101
101
|
bulkIndex?(docs: FullTextSearchDocument[]): Promise<void>
|
|
102
|
-
purge?(entityId: EntityId, tenantId: string): Promise<void>
|
|
102
|
+
purge?(entityId: EntityId, tenantId: string, organizationId?: string | null): Promise<void>
|
|
103
103
|
|
|
104
104
|
// Index management (optional)
|
|
105
105
|
clearIndex?(tenantId: string): Promise<void>
|
|
@@ -48,6 +48,7 @@ export type DeleteRecordParams = {
|
|
|
48
48
|
export type PurgeEntityParams = {
|
|
49
49
|
entityId: EntityId
|
|
50
50
|
tenantId: string
|
|
51
|
+
organizationId?: string | null
|
|
51
52
|
}
|
|
52
53
|
|
|
53
54
|
/**
|
|
@@ -352,7 +353,7 @@ export class SearchIndexer {
|
|
|
352
353
|
* Purge all records of an entity type from the search index.
|
|
353
354
|
*/
|
|
354
355
|
async purgeEntity(params: PurgeEntityParams): Promise<void> {
|
|
355
|
-
await this.searchService.purge(params.entityId, params.tenantId)
|
|
356
|
+
await this.searchService.purge(params.entityId, params.tenantId, params.organizationId)
|
|
356
357
|
}
|
|
357
358
|
|
|
358
359
|
/**
|
|
@@ -394,7 +395,7 @@ export class SearchIndexer {
|
|
|
394
395
|
// Optionally purge first
|
|
395
396
|
if (params.purgeFirst) {
|
|
396
397
|
try {
|
|
397
|
-
await this.searchService.purge(params.entityId, params.tenantId)
|
|
398
|
+
await this.searchService.purge(params.entityId, params.tenantId, params.organizationId)
|
|
398
399
|
} catch (error) {
|
|
399
400
|
searchDebugWarn('SearchIndexer', 'Failed to purge entity before reindex', {
|
|
400
401
|
entityId: params.entityId,
|
|
@@ -865,7 +866,7 @@ export class SearchIndexer {
|
|
|
865
866
|
// Optionally purge vector index first
|
|
866
867
|
if (params.purgeFirst) {
|
|
867
868
|
try {
|
|
868
|
-
await this.searchService.purge(params.entityId, params.tenantId)
|
|
869
|
+
await this.searchService.purge(params.entityId, params.tenantId, params.organizationId)
|
|
869
870
|
} catch (error) {
|
|
870
871
|
searchDebugWarn('SearchIndexer', 'Failed to purge entity before vector reindex', {
|
|
871
872
|
entityId: params.entityId,
|
|
@@ -27,9 +27,19 @@ jest.mock('../../lib/global-search-config', () => ({
|
|
|
27
27
|
resolveGlobalSearchStrategies: jest.fn().mockResolvedValue(['tokens']),
|
|
28
28
|
}))
|
|
29
29
|
|
|
30
|
+
const mockRecordIndexerLog = jest.fn().mockResolvedValue(undefined)
|
|
31
|
+
jest.mock('@open-mercato/shared/lib/indexers/status-log', () => ({
|
|
32
|
+
recordIndexerLog: (...args: unknown[]) => mockRecordIndexerLog(...args),
|
|
33
|
+
}))
|
|
34
|
+
|
|
35
|
+
const mockWriteCoverageCounts = jest.fn().mockResolvedValue(undefined)
|
|
36
|
+
jest.mock('@open-mercato/core/modules/query_index/lib/coverage', () => ({
|
|
37
|
+
writeCoverageCounts: (...args: unknown[]) => mockWriteCoverageCounts(...args),
|
|
38
|
+
}))
|
|
39
|
+
|
|
30
40
|
import { GET as hybridSearchGet } from '../search/route'
|
|
31
41
|
import { GET as globalSearchGet } from '../search/global/route'
|
|
32
|
-
import { GET as indexGet } from '../index/route'
|
|
42
|
+
import { GET as indexGet, DELETE as indexDelete } from '../index/route'
|
|
33
43
|
|
|
34
44
|
type MockOrganizationScope = {
|
|
35
45
|
selectedId: string | null
|
|
@@ -161,3 +171,103 @@ describe('Search API organizationId scoping', () => {
|
|
|
161
171
|
expect(passedOptions.organizationId).toEqual('org-A')
|
|
162
172
|
})
|
|
163
173
|
})
|
|
174
|
+
|
|
175
|
+
describe('Search API DELETE /api/search/index organizationId scoping (issue #2935)', () => {
|
|
176
|
+
beforeEach(() => {
|
|
177
|
+
jest.clearAllMocks()
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
function setupContainer() {
|
|
181
|
+
const searchIndexer = {
|
|
182
|
+
listEnabledEntities: jest.fn().mockReturnValue(['demo:item', 'demo:other']),
|
|
183
|
+
purgeEntity: jest.fn().mockResolvedValue(undefined),
|
|
184
|
+
}
|
|
185
|
+
const container = {
|
|
186
|
+
resolve: jest.fn((name: string) => (name === 'searchIndexer' ? searchIndexer : undefined)),
|
|
187
|
+
dispose: jest.fn(),
|
|
188
|
+
}
|
|
189
|
+
mockCreateRequestContainer.mockResolvedValue(container)
|
|
190
|
+
return { searchIndexer, container }
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
test('scopes the purge to the caller selected organization (never tenant-wide)', async () => {
|
|
194
|
+
mockGetAuthFromRequest.mockResolvedValue({ tenantId: 't1', orgId: 'org-A', sub: 'user-1', isSuperAdmin: false })
|
|
195
|
+
const { searchIndexer } = setupContainer()
|
|
196
|
+
mockResolveOrganizationScopeForRequest.mockResolvedValue({
|
|
197
|
+
selectedId: 'org-A',
|
|
198
|
+
filterIds: ['org-A'],
|
|
199
|
+
allowedIds: ['org-A'],
|
|
200
|
+
tenantId: 't1',
|
|
201
|
+
} satisfies MockOrganizationScope)
|
|
202
|
+
|
|
203
|
+
const req = new Request('http://localhost/api/search/index?entityId=demo:item', { method: 'DELETE' })
|
|
204
|
+
const res = await indexDelete(req)
|
|
205
|
+
|
|
206
|
+
expect(res.status).toBe(200)
|
|
207
|
+
expect(mockResolveOrganizationScopeForRequest).toHaveBeenCalledWith(
|
|
208
|
+
expect.objectContaining({ container: expect.anything(), auth: expect.anything(), request: req }),
|
|
209
|
+
)
|
|
210
|
+
expect(searchIndexer.purgeEntity).toHaveBeenCalledTimes(1)
|
|
211
|
+
expect(searchIndexer.purgeEntity).toHaveBeenCalledWith(
|
|
212
|
+
expect.objectContaining({ entityId: 'demo:item', tenantId: 't1', organizationId: 'org-A' }),
|
|
213
|
+
)
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
test('purges the full authorized org set (never tenant-wide) for a restricted user with no selected org', async () => {
|
|
217
|
+
mockGetAuthFromRequest.mockResolvedValue({ tenantId: 't1', orgId: null, sub: 'user-1', isSuperAdmin: false })
|
|
218
|
+
const { searchIndexer } = setupContainer()
|
|
219
|
+
mockResolveOrganizationScopeForRequest.mockResolvedValue({
|
|
220
|
+
selectedId: null,
|
|
221
|
+
filterIds: ['org-A', 'org-B'],
|
|
222
|
+
allowedIds: ['org-A', 'org-B'],
|
|
223
|
+
tenantId: 't1',
|
|
224
|
+
} satisfies MockOrganizationScope)
|
|
225
|
+
|
|
226
|
+
const req = new Request('http://localhost/api/search/index?entityId=demo:item', { method: 'DELETE' })
|
|
227
|
+
const res = await indexDelete(req)
|
|
228
|
+
|
|
229
|
+
expect(res.status).toBe(200)
|
|
230
|
+
const purgedOrgs = (searchIndexer.purgeEntity as jest.Mock).mock.calls.map(
|
|
231
|
+
(call) => (call[0] as { organizationId?: string | null }).organizationId,
|
|
232
|
+
)
|
|
233
|
+
expect(purgedOrgs).toEqual(expect.arrayContaining(['org-A', 'org-B']))
|
|
234
|
+
expect(purgedOrgs).not.toContain(undefined)
|
|
235
|
+
expect(purgedOrgs).not.toContain(null)
|
|
236
|
+
})
|
|
237
|
+
|
|
238
|
+
test('purges nothing when the caller has no accessible organizations', async () => {
|
|
239
|
+
mockGetAuthFromRequest.mockResolvedValue({ tenantId: 't1', orgId: 'org-A', sub: 'user-1', isSuperAdmin: false })
|
|
240
|
+
const { searchIndexer } = setupContainer()
|
|
241
|
+
mockResolveOrganizationScopeForRequest.mockResolvedValue({
|
|
242
|
+
selectedId: null,
|
|
243
|
+
filterIds: [],
|
|
244
|
+
allowedIds: [],
|
|
245
|
+
tenantId: 't1',
|
|
246
|
+
} satisfies MockOrganizationScope)
|
|
247
|
+
|
|
248
|
+
const req = new Request('http://localhost/api/search/index?entityId=demo:item', { method: 'DELETE' })
|
|
249
|
+
const res = await indexDelete(req)
|
|
250
|
+
|
|
251
|
+
expect(res.status).toBe(200)
|
|
252
|
+
expect(searchIndexer.purgeEntity).not.toHaveBeenCalled()
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
test('allows a tenant-wide purge for an unrestricted (super-admin) caller', async () => {
|
|
256
|
+
mockGetAuthFromRequest.mockResolvedValue({ tenantId: 't1', orgId: null, sub: 'user-1', isSuperAdmin: true })
|
|
257
|
+
const { searchIndexer } = setupContainer()
|
|
258
|
+
mockResolveOrganizationScopeForRequest.mockResolvedValue({
|
|
259
|
+
selectedId: null,
|
|
260
|
+
filterIds: null,
|
|
261
|
+
allowedIds: null,
|
|
262
|
+
tenantId: 't1',
|
|
263
|
+
} satisfies MockOrganizationScope)
|
|
264
|
+
|
|
265
|
+
const req = new Request('http://localhost/api/search/index?entityId=demo:item', { method: 'DELETE' })
|
|
266
|
+
const res = await indexDelete(req)
|
|
267
|
+
|
|
268
|
+
expect(res.status).toBe(200)
|
|
269
|
+
expect(searchIndexer.purgeEntity).toHaveBeenCalledTimes(1)
|
|
270
|
+
const arg = (searchIndexer.purgeEntity as jest.Mock).mock.calls[0][0] as { organizationId?: string | null }
|
|
271
|
+
expect(arg.organizationId).toBeUndefined()
|
|
272
|
+
})
|
|
273
|
+
})
|
|
@@ -163,13 +163,36 @@ export async function DELETE(req: Request) {
|
|
|
163
163
|
? [entityIdParam]
|
|
164
164
|
: searchIndexer.listEnabledEntities()
|
|
165
165
|
|
|
166
|
+
// Constrain the purge to the caller's authorized organization scope so a
|
|
167
|
+
// holder of search.embeddings.manage in one organization cannot destroy
|
|
168
|
+
// another organization's index entries within the same tenant (issue #2935).
|
|
169
|
+
const scope = await resolveOrganizationScopeForRequest({ container, auth, request: req })
|
|
170
|
+
if (Array.isArray(scope.filterIds) && scope.filterIds.length === 0) {
|
|
171
|
+
return NextResponse.json({ ok: true })
|
|
172
|
+
}
|
|
173
|
+
const selectedOrganizationId =
|
|
174
|
+
typeof scope.selectedId === 'string' && scope.selectedId.trim().length > 0 ? scope.selectedId.trim() : null
|
|
175
|
+
// The single selected org, else the explicit authorized set, else
|
|
176
|
+
// (unrestricted/super-admin) a single tenant-wide purge.
|
|
177
|
+
const purgeOrganizationIds: Array<string | undefined> = selectedOrganizationId
|
|
178
|
+
? [selectedOrganizationId]
|
|
179
|
+
: Array.isArray(scope.filterIds)
|
|
180
|
+
? scope.filterIds
|
|
181
|
+
: [undefined]
|
|
182
|
+
|
|
166
183
|
const scopes = new Set<string>()
|
|
167
|
-
const registerScope = (org: string |
|
|
184
|
+
const registerScope = (org: string | undefined) => {
|
|
168
185
|
const key = org ?? '__null__'
|
|
169
186
|
if (!scopes.has(key)) scopes.add(key)
|
|
170
187
|
}
|
|
171
|
-
|
|
172
|
-
|
|
188
|
+
for (const organizationId of purgeOrganizationIds) {
|
|
189
|
+
if (organizationId === undefined) {
|
|
190
|
+
registerScope(undefined)
|
|
191
|
+
if (auth.orgId) registerScope(auth.orgId)
|
|
192
|
+
} else {
|
|
193
|
+
registerScope(organizationId)
|
|
194
|
+
}
|
|
195
|
+
}
|
|
173
196
|
|
|
174
197
|
await recordIndexerLog(
|
|
175
198
|
{ em: em ?? undefined },
|
|
@@ -186,12 +209,17 @@ export async function DELETE(req: Request) {
|
|
|
186
209
|
},
|
|
187
210
|
).catch(() => undefined)
|
|
188
211
|
|
|
189
|
-
// Purge each entity using SearchIndexer
|
|
212
|
+
// Purge each entity using SearchIndexer, scoped to the authorized
|
|
213
|
+
// organization(s). An undefined organizationId performs a tenant-wide purge
|
|
214
|
+
// and is only reachable for unrestricted (e.g. super-admin) callers.
|
|
190
215
|
for (const entityId of entityIds) {
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
216
|
+
for (const organizationId of purgeOrganizationIds) {
|
|
217
|
+
await searchIndexer.purgeEntity({
|
|
218
|
+
entityId: entityId as EntityId,
|
|
219
|
+
tenantId: auth.tenantId,
|
|
220
|
+
organizationId,
|
|
221
|
+
})
|
|
222
|
+
}
|
|
195
223
|
}
|
|
196
224
|
|
|
197
225
|
// Update coverage counts
|
|
@@ -577,7 +577,7 @@ async function reindexCommand(rest: string[]): Promise<void> {
|
|
|
577
577
|
if (purgeFirst && tenantId) {
|
|
578
578
|
try {
|
|
579
579
|
console.log(' -> purging existing vector index rows...')
|
|
580
|
-
await searchIndexer.purgeEntity({ entityId: entityType as EntityId, tenantId })
|
|
580
|
+
await searchIndexer.purgeEntity({ entityId: entityType as EntityId, tenantId, organizationId })
|
|
581
581
|
await resetVectorCoverageAfterPurge(baseEm, entityType, tenantId ?? null, organizationId ?? null)
|
|
582
582
|
if (baseEventBus) {
|
|
583
583
|
const scopes = new Set<string>()
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
jest.mock('@open-mercato/shared/lib/indexers/status-log', () => ({
|
|
2
|
+
recordIndexerLog: jest.fn().mockResolvedValue(undefined),
|
|
3
|
+
}))
|
|
4
|
+
jest.mock('@open-mercato/shared/lib/indexers/error-log', () => ({
|
|
5
|
+
recordIndexerError: jest.fn().mockResolvedValue(undefined),
|
|
6
|
+
}))
|
|
7
|
+
jest.mock('@open-mercato/core/modules/query_index/lib/coverage', () => ({
|
|
8
|
+
writeCoverageCounts: jest.fn().mockResolvedValue(undefined),
|
|
9
|
+
}))
|
|
10
|
+
jest.mock('../../lib/embedding-config', () => ({
|
|
11
|
+
resolveEmbeddingConfig: jest.fn().mockResolvedValue(null),
|
|
12
|
+
}))
|
|
13
|
+
|
|
14
|
+
import handle from '../vector_purge'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Regression coverage for issue #2935 (sibling of the DELETE endpoint fix):
|
|
18
|
+
* the query_index.vectorize_purge subscriber received an organizationId in the
|
|
19
|
+
* event payload but dropped it when calling purgeEntity, performing a
|
|
20
|
+
* tenant-wide purge that destroys other organizations' vector entries during an
|
|
21
|
+
* org-scoped force-reindex. organizationId must now flow into purgeEntity.
|
|
22
|
+
*/
|
|
23
|
+
function createCtx(purgeEntity: jest.Mock) {
|
|
24
|
+
return {
|
|
25
|
+
resolve: jest.fn((name: string) => {
|
|
26
|
+
if (name === 'searchIndexer') return { purgeEntity }
|
|
27
|
+
throw new Error(`not registered: ${name}`)
|
|
28
|
+
}),
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
describe('query_index.vectorize_purge subscriber organization scoping (issue #2935)', () => {
|
|
33
|
+
it('forwards the payload organizationId to purgeEntity (org-scoped purge)', async () => {
|
|
34
|
+
const purgeEntity = jest.fn().mockResolvedValue(undefined)
|
|
35
|
+
|
|
36
|
+
await handle(
|
|
37
|
+
{ entityType: 'demo:item', tenantId: 'tenant-1', organizationId: 'org-A' },
|
|
38
|
+
createCtx(purgeEntity) as never,
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
expect(purgeEntity).toHaveBeenCalledTimes(1)
|
|
42
|
+
expect(purgeEntity).toHaveBeenCalledWith(
|
|
43
|
+
expect.objectContaining({ entityId: 'demo:item', tenantId: 'tenant-1', organizationId: 'org-A' }),
|
|
44
|
+
)
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('performs a tenant-wide purge (organizationId null) when the event carries no organization', async () => {
|
|
48
|
+
const purgeEntity = jest.fn().mockResolvedValue(undefined)
|
|
49
|
+
|
|
50
|
+
await handle({ entityType: 'demo:item', tenantId: 'tenant-1' }, createCtx(purgeEntity) as never)
|
|
51
|
+
|
|
52
|
+
expect(purgeEntity).toHaveBeenCalledTimes(1)
|
|
53
|
+
expect((purgeEntity.mock.calls[0][0] as { organizationId?: string | null }).organizationId).toBeNull()
|
|
54
|
+
})
|
|
55
|
+
})
|
package/src/service.ts
CHANGED
|
@@ -300,13 +300,13 @@ export class SearchService {
|
|
|
300
300
|
* @param entityId - Entity type to purge
|
|
301
301
|
* @param tenantId - Tenant for isolation
|
|
302
302
|
*/
|
|
303
|
-
async purge(entityId: string, tenantId: string): Promise<void> {
|
|
303
|
+
async purge(entityId: string, tenantId: string, organizationId?: string | null): Promise<void> {
|
|
304
304
|
const strategies = await this.getAvailableStrategies()
|
|
305
305
|
|
|
306
306
|
const results = await Promise.allSettled(
|
|
307
307
|
strategies.map((strategy) => {
|
|
308
308
|
if (strategy.purge) {
|
|
309
|
-
return strategy.purge(entityId, tenantId)
|
|
309
|
+
return strategy.purge(entityId, tenantId, organizationId)
|
|
310
310
|
}
|
|
311
311
|
return Promise.resolve()
|
|
312
312
|
}),
|
|
@@ -75,11 +75,11 @@ export class FullTextSearchStrategy implements SearchStrategy {
|
|
|
75
75
|
return this.driver.bulkIndex(docs)
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
async purge(entityId: EntityId, tenantId: string): Promise<void> {
|
|
78
|
+
async purge(entityId: EntityId, tenantId: string, organizationId?: string | null): Promise<void> {
|
|
79
79
|
if (!this.driver.purge) {
|
|
80
80
|
return
|
|
81
81
|
}
|
|
82
|
-
return this.driver.purge(entityId, tenantId)
|
|
82
|
+
return this.driver.purge(entityId, tenantId, organizationId)
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
// Additional methods exposed for enrichment and admin purposes
|
|
@@ -170,11 +170,16 @@ export class TokenSearchStrategy implements SearchStrategy {
|
|
|
170
170
|
await replaceSearchTokensForBatch(this.db, payloads)
|
|
171
171
|
}
|
|
172
172
|
|
|
173
|
-
async purge(entityId: EntityId, tenantId: string): Promise<void> {
|
|
174
|
-
|
|
173
|
+
async purge(entityId: EntityId, tenantId: string, organizationId?: string | null): Promise<void> {
|
|
174
|
+
const normalizedOrganizationId =
|
|
175
|
+
typeof organizationId === 'string' && organizationId.trim().length > 0 ? organizationId.trim() : null
|
|
176
|
+
let query = this.db
|
|
175
177
|
.deleteFrom('search_tokens' as any)
|
|
176
178
|
.where('entity_type' as any, '=', entityId)
|
|
177
179
|
.where('tenant_id' as any, '=', tenantId)
|
|
178
|
-
|
|
180
|
+
if (normalizedOrganizationId !== null) {
|
|
181
|
+
query = query.where('organization_id' as any, '=', normalizedOrganizationId)
|
|
182
|
+
}
|
|
183
|
+
await query.execute()
|
|
179
184
|
}
|
|
180
185
|
}
|
|
@@ -157,10 +157,10 @@ export class VectorSearchStrategy implements SearchStrategy {
|
|
|
157
157
|
await this.vectorDriver.delete(entityId, recordId, tenantId)
|
|
158
158
|
}
|
|
159
159
|
|
|
160
|
-
async purge(entityId: EntityId, tenantId: string): Promise<void> {
|
|
160
|
+
async purge(entityId: EntityId, tenantId: string, organizationId?: string | null): Promise<void> {
|
|
161
161
|
await this.ensureReady()
|
|
162
162
|
if (this.vectorDriver.purge) {
|
|
163
|
-
await this.vectorDriver.purge(entityId, tenantId)
|
|
163
|
+
await this.vectorDriver.purge(entityId, tenantId, organizationId)
|
|
164
164
|
}
|
|
165
165
|
}
|
|
166
166
|
|