@open-mercato/search 0.11.1-develop.8417.1.36c73b01e6 → 0.11.1-develop.8418.1.8ddb502575
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/di.js +40 -9
- package/dist/di.js.map +2 -2
- package/dist/modules/search/di.js +3 -1
- package/dist/modules/search/di.js.map +2 -2
- package/package.json +4 -4
- package/src/__tests__/di-generation-fulltext-driver.test.ts +91 -0
- package/src/__tests__/di-scoped-services.test.ts +54 -0
- package/src/__tests__/di-singletons.test.ts +1 -0
- package/src/di.ts +52 -11
- package/src/modules/search/di.ts +4 -1
package/dist/di.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { resolveEntityIndexStrategies } from "./lib/entity-strategy-policy.js";
|
|
2
2
|
import { asValue } from "awilix";
|
|
3
|
+
import { asScopedService } from "@open-mercato/shared/lib/di/process-registrar";
|
|
3
4
|
import { SearchService } from "./service.js";
|
|
4
5
|
import { TokenSearchStrategy } from "./strategies/token.strategy.js";
|
|
5
6
|
import { VectorSearchStrategy } from "./strategies/vector.strategy.js";
|
|
@@ -35,6 +36,39 @@ function createUnavailableEncryptionMapResolver() {
|
|
|
35
36
|
};
|
|
36
37
|
}
|
|
37
38
|
function registerSearchModule(container, options) {
|
|
39
|
+
const services = createSearchServices(container, options);
|
|
40
|
+
container.register({
|
|
41
|
+
searchService: asValue(services.searchService),
|
|
42
|
+
searchStrategies: asValue(services.searchStrategies),
|
|
43
|
+
searchIndexer: asValue(services.searchIndexer)
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
function registerScopedSearchModule(root, options) {
|
|
47
|
+
const fieldPolicies = /* @__PURE__ */ new Map();
|
|
48
|
+
for (const moduleConfig of options?.moduleConfigs ?? []) {
|
|
49
|
+
for (const entityConfig of moduleConfig.entities) {
|
|
50
|
+
if (entityConfig.enabled !== false) fieldPolicies.set(entityConfig.entityId, entityConfig.fieldPolicy);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const fulltextDriverCache = {
|
|
54
|
+
fieldPolicyResolver: (entityId) => fieldPolicies.get(entityId)
|
|
55
|
+
};
|
|
56
|
+
const instances = /* @__PURE__ */ new WeakMap();
|
|
57
|
+
const resolveServices = (caller) => {
|
|
58
|
+
let services = instances.get(caller);
|
|
59
|
+
if (!services) {
|
|
60
|
+
services = createSearchServices(caller, options, false, fulltextDriverCache);
|
|
61
|
+
instances.set(caller, services);
|
|
62
|
+
}
|
|
63
|
+
return services;
|
|
64
|
+
};
|
|
65
|
+
root.register({
|
|
66
|
+
searchService: asScopedService((caller) => resolveServices(caller).searchService),
|
|
67
|
+
searchStrategies: asScopedService((caller) => resolveServices(caller).searchStrategies),
|
|
68
|
+
searchIndexer: asScopedService((caller) => resolveServices(caller).searchIndexer)
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
function createSearchServices(container, options, cacheFulltextDriver = true, generationFulltextDriverCache) {
|
|
38
72
|
const moduleConfigs = (options?.moduleConfigs ?? []).map((moduleConfig) => ({
|
|
39
73
|
...moduleConfig,
|
|
40
74
|
entities: moduleConfig.entities.map((entityConfig) => {
|
|
@@ -77,8 +111,8 @@ function registerSearchModule(container, options) {
|
|
|
77
111
|
}
|
|
78
112
|
if (!options?.skipFulltext) {
|
|
79
113
|
const excludeEncrypted = shouldExcludeEncryptedFields();
|
|
80
|
-
const singletonCacheEnabled = process.env.SEARCH_DISABLE_SINGLETON_CACHE !== "1";
|
|
81
|
-
const g = getSearchModuleGlobals();
|
|
114
|
+
const singletonCacheEnabled = (cacheFulltextDriver || !!generationFulltextDriverCache) && process.env.SEARCH_DISABLE_SINGLETON_CACHE !== "1";
|
|
115
|
+
const g = generationFulltextDriverCache ?? getSearchModuleGlobals();
|
|
82
116
|
const canMemoize = singletonCacheEnabled && !excludeEncrypted;
|
|
83
117
|
let fulltextDriver = canMemoize ? g[FULLTEXT_DRIVER_KEY] ?? null : null;
|
|
84
118
|
if (!fulltextDriver) {
|
|
@@ -93,10 +127,10 @@ function registerSearchModule(container, options) {
|
|
|
93
127
|
}
|
|
94
128
|
}
|
|
95
129
|
fulltextDriver = createFulltextDriver({
|
|
96
|
-
fieldPolicyResolver: (entityId) => {
|
|
130
|
+
fieldPolicyResolver: generationFulltextDriverCache?.fieldPolicyResolver ?? ((entityId) => {
|
|
97
131
|
const config = entityConfigMap.get(entityId);
|
|
98
132
|
return config?.fieldPolicy;
|
|
99
|
-
},
|
|
133
|
+
}),
|
|
100
134
|
encryptionMapResolver
|
|
101
135
|
});
|
|
102
136
|
if (canMemoize && fulltextDriver) {
|
|
@@ -162,11 +196,7 @@ function registerSearchModule(container, options) {
|
|
|
162
196
|
vectorQueue,
|
|
163
197
|
encryptedCustomFieldKeysResolver
|
|
164
198
|
});
|
|
165
|
-
|
|
166
|
-
searchService: asValue(searchService),
|
|
167
|
-
searchStrategies: asValue(strategies),
|
|
168
|
-
searchIndexer: asValue(searchIndexer)
|
|
169
|
-
});
|
|
199
|
+
return { searchService, searchStrategies: strategies, searchIndexer };
|
|
170
200
|
}
|
|
171
201
|
function determineDefaultStrategies(strategies) {
|
|
172
202
|
const available = new Set(strategies.map((s) => s.id));
|
|
@@ -186,6 +216,7 @@ export {
|
|
|
186
216
|
addSearchStrategy,
|
|
187
217
|
createEncryptedCustomFieldKeysResolver2 as createEncryptedCustomFieldKeysResolver,
|
|
188
218
|
createEncryptionMapResolver2 as createEncryptionMapResolver,
|
|
219
|
+
registerScopedSearchModule,
|
|
189
220
|
registerSearchModule
|
|
190
221
|
};
|
|
191
222
|
//# sourceMappingURL=di.js.map
|
package/dist/di.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/di.ts"],
|
|
4
|
-
"sourcesContent": ["import { resolveEntityIndexStrategies } from './lib/entity-strategy-policy'\nimport { asValue } from 'awilix'\nimport type { Kysely } from 'kysely'\nimport type { FullTextSearchDriver } from './fulltext/types'\nimport { SearchService } from './service'\nimport { TokenSearchStrategy } from './strategies/token.strategy'\nimport { VectorSearchStrategy, type EmbeddingService } from './strategies/vector.strategy'\nimport { FullTextSearchStrategy } from './strategies/fulltext.strategy'\nimport { createFulltextDriver } from './fulltext/drivers'\nimport { SearchIndexer } from './indexer/search-indexer'\nimport type {\n SearchStrategy,\n ResultMergeConfig,\n SearchModuleConfig,\n SearchFieldPolicy,\n SearchEntityConfig,\n PresenterEnricherFn,\n} from './types'\nimport type { VectorDriver } from './vector/types'\nimport type { QueryEngine } from '@open-mercato/shared/lib/query/types'\nimport type { EntityId } from '@open-mercato/shared/modules/entities'\nimport type { SearchStrategyId } from '@open-mercato/shared/modules/search'\nimport type { Queue } from '@open-mercato/queue'\nimport type { FulltextIndexJobPayload } from './queue/fulltext-indexing'\nimport type { VectorIndexJobPayload } from './queue/vector-indexing'\nimport type { EncryptionMapEntry } from './lib/field-policy'\nimport type { TenantDataEncryptionService } from '@open-mercato/shared/lib/encryption/tenantDataEncryptionService'\nimport { createPresenterEnricher } from './lib/presenter-enricher'\nimport { createEncryptionMapResolver } from './lib/encryption-map-resolver'\nimport {\n createEncryptedCustomFieldKeysResolver,\n type CustomFieldDefinitionsDatabase,\n type EncryptedCustomFieldKeysResolver,\n} from './lib/encrypted-custom-field-resolver'\nimport { reportSearchError } from './lib/debug'\n\nexport { createEncryptionMapResolver } from './lib/encryption-map-resolver'\nexport {\n createEncryptedCustomFieldKeysResolver,\n type EncryptedCustomFieldKeysResolver,\n} from './lib/encrypted-custom-field-resolver'\n\ntype KyselyEntityManager = {\n getKysely<Database>(): Kysely<Database>\n}\n\nconst FULLTEXT_DRIVER_KEY = '__omSearchFulltextDriver__'\n\ntype SearchModuleGlobals = {\n [FULLTEXT_DRIVER_KEY]?: FullTextSearchDriver\n}\n\nfunction getSearchModuleGlobals(): SearchModuleGlobals {\n return globalThis as unknown as SearchModuleGlobals\n}\n\n/**\n * Check if encrypted fields should be excluded from search indexing.\n * Controlled by SEARCH_EXCLUDE_ENCRYPTED_FIELDS environment variable.\n * Default: false (index all fields including decrypted data)\n */\nfunction shouldExcludeEncryptedFields(): boolean {\n const raw = (process.env.SEARCH_EXCLUDE_ENCRYPTED_FIELDS ?? '').toLowerCase()\n return raw === '1' || raw === 'true' || raw === 'yes' || raw === 'on'\n}\n\nfunction createUnavailableEncryptedCustomFieldKeysResolver(): EncryptedCustomFieldKeysResolver {\n return async () => {\n throw new Error('[internal] Custom-field definitions are unavailable; refusing to index potentially encrypted custom fields')\n }\n}\n\nfunction createUnavailableEncryptionMapResolver(): (entityId: EntityId) => Promise<EncryptionMapEntry[]> {\n return async () => {\n throw new Error('[internal] Encryption map lookup is unavailable; refusing to index potentially encrypted fields')\n }\n}\n\n/**\n * Container interface - minimal subset needed for registration.\n */\nexport interface SearchContainer {\n resolve<T = unknown>(name: string): T\n register(registrations: Record<string, unknown>): void\n}\n\n/**\n * Configuration options for search module registration.\n */\nexport type SearchModuleOptions = {\n /** Override default strategies to use */\n defaultStrategies?: SearchStrategyId[]\n /** Override merge configuration */\n mergeConfig?: ResultMergeConfig\n /** Skip token strategy registration */\n skipTokens?: boolean\n /** Skip vector strategy registration */\n skipVector?: boolean\n /** Skip fulltext strategy registration */\n skipFulltext?: boolean\n /** Module configurations (from generated/search.generated.ts) */\n moduleConfigs?: SearchModuleConfig[]\n}\n\n/**\n * Register the search module in the DI container.\n *\n * This creates and registers:\n * - SearchService instance\n * - All configured search strategies\n *\n * @param container - Awilix container\n * @param options - Optional configuration overrides\n */\nexport function registerSearchModule(\n container: SearchContainer,\n options?: SearchModuleOptions,\n): void {\n const moduleConfigs = (options?.moduleConfigs ?? []).map((moduleConfig) => ({\n ...moduleConfig,\n entities: moduleConfig.entities.map((entityConfig) => {\n const buildSource = entityConfig.buildSource\n return buildSource\n ? { ...entityConfig, buildSource: (ctx: Parameters<typeof buildSource>[0]) => buildSource.call(entityConfig, { ...ctx, container }) }\n : entityConfig\n }),\n }))\n // Resolve encryption service for decrypting presenter data\n let encryptionService: TenantDataEncryptionService | null = null\n try {\n encryptionService = container.resolve<TenantDataEncryptionService>('tenantEncryptionService')\n } catch {\n // Encryption service not available, presenters won't be decrypted\n }\n\n const strategies: SearchStrategy[] = []\n\n // Token strategy (always available unless explicitly skipped)\n if (!options?.skipTokens) {\n try {\n const em = container.resolve<any>('em')\n const db = em.getKysely() as Kysely<any>\n strategies.push(new TokenSearchStrategy(db))\n } catch {\n // Kysely not available via em, skipping TokenSearchStrategy\n }\n }\n\n // Vector strategy (requires embedding service and driver)\n // Note: We register even if not currently available - availability is checked at search time\n // via isAvailable(). The embedding config may be loaded later from the database.\n if (!options?.skipVector) {\n try {\n const embeddingService = container.resolve<EmbeddingService>('vectorEmbeddingService')\n const drivers = container.resolve<VectorDriver[]>('vectorDrivers')\n const primaryDriver = drivers?.[0]\n\n if (embeddingService && primaryDriver) {\n strategies.push(new VectorSearchStrategy(embeddingService, primaryDriver, { encryptionService }))\n }\n } catch {\n // Vector module not available, skipping VectorSearchStrategy\n }\n }\n\n // Build entity config map for field policy resolution\n const entityConfigMap = new Map<EntityId, SearchEntityConfig>()\n for (const moduleConfig of moduleConfigs) {\n for (const entityConfig of moduleConfig.entities) {\n if (entityConfig.enabled !== false) {\n entityConfigMap.set(entityConfig.entityId as EntityId, entityConfig)\n }\n }\n }\n\n // Fulltext strategy (requires driver configuration, e.g., MEILISEARCH_HOST)\n if (!options?.skipFulltext) {\n const excludeEncrypted = shouldExcludeEncryptedFields()\n const singletonCacheEnabled = process.env.SEARCH_DISABLE_SINGLETON_CACHE !== '1'\n const g = getSearchModuleGlobals()\n\n // The fulltext driver is safe to memoize only when SEARCH_EXCLUDE_ENCRYPTED_FIELDS is\n // OFF \u2014 in that case the driver holds no request-scoped Kysely reference. When the flag\n // is ON the encryptionMapResolver captures a per-request db handle and must stay\n // per-request to avoid cross-request data leaks.\n const canMemoize = singletonCacheEnabled && !excludeEncrypted\n\n let fulltextDriver: FullTextSearchDriver | null = canMemoize\n ? (g[FULLTEXT_DRIVER_KEY] ?? null)\n : null\n\n if (!fulltextDriver) {\n let encryptionMapResolver: ((entityId: EntityId) => Promise<EncryptionMapEntry[]>) | undefined\n if (excludeEncrypted) {\n try {\n const em = container.resolve<any>('em')\n const db = em.getKysely() as Kysely<any>\n encryptionMapResolver = createEncryptionMapResolver(db)\n } catch {\n encryptionMapResolver = createUnavailableEncryptionMapResolver()\n }\n }\n\n fulltextDriver = createFulltextDriver({\n fieldPolicyResolver: (entityId: EntityId): SearchFieldPolicy | undefined => {\n const config = entityConfigMap.get(entityId)\n return config?.fieldPolicy\n },\n encryptionMapResolver,\n })\n\n if (canMemoize && fulltextDriver) {\n g[FULLTEXT_DRIVER_KEY] = fulltextDriver\n }\n }\n\n if (fulltextDriver) {\n strategies.push(new FullTextSearchStrategy(fulltextDriver))\n }\n }\n\n // Determine default strategies based on what's available\n const defaultStrategies = options?.defaultStrategies ?? determineDefaultStrategies(strategies)\n\n // Try to resolve queryEngine for reindex support and presenter enrichment\n let queryEngine: QueryEngine | undefined\n try {\n queryEngine = container.resolve<QueryEngine>('queryEngine')\n } catch {\n // QueryEngine not available, reindex will be disabled\n }\n\n // Create presenter enricher for database-based presenter resolution\n let presenterEnricher: PresenterEnricherFn | undefined\n try {\n const em = container.resolve<any>('em')\n const db = em.getKysely() as Kysely<any>\n presenterEnricher = createPresenterEnricher(db, entityConfigMap, queryEngine, encryptionService)\n } catch {\n // Kysely not available, presenter enrichment disabled\n }\n\n // Create search service\n const searchService = new SearchService({\n strategies,\n resolveEntityStrategies: (entityId) => resolveEntityIndexStrategies(entityId, moduleConfigs),\n resolveResultFilter: (entityId) => entityConfigMap.get(entityId as EntityId)?.filterResults,\n defaultStrategies,\n fallbackStrategy: 'tokens',\n mergeConfig: options?.mergeConfig ?? {\n duplicateHandling: 'highest_score',\n strategyWeights: {\n fulltext: 1.2,\n vector: 1.0,\n tokens: 0.8,\n },\n },\n presenterEnricher,\n })\n\n // Try to resolve fulltextIndexQueue for queue-based reindexing\n let fulltextQueue: Queue<FulltextIndexJobPayload> | undefined\n try {\n fulltextQueue = container.resolve<Queue<FulltextIndexJobPayload>>('fulltextIndexQueue')\n } catch {\n // Queue not available, queue-based fulltext reindex will be disabled\n }\n\n // Try to resolve vectorIndexQueue for queue-based vector reindexing\n let vectorQueue: Queue<VectorIndexJobPayload> | undefined\n try {\n vectorQueue = container.resolve<Queue<VectorIndexJobPayload>>('vectorIndexQueue')\n } catch {\n // Queue not available, queue-based vector reindex will be disabled\n }\n\n let encryptedCustomFieldKeysResolver: EncryptedCustomFieldKeysResolver\n try {\n const em = container.resolve<KyselyEntityManager>('em')\n encryptedCustomFieldKeysResolver = createEncryptedCustomFieldKeysResolver(\n em.getKysely<CustomFieldDefinitionsDatabase>(),\n )\n } catch (error) {\n reportSearchError('SearchModule', error, 'search.custom_field_definitions_unavailable')\n encryptedCustomFieldKeysResolver = createUnavailableEncryptedCustomFieldKeysResolver()\n }\n\n const searchIndexer = new SearchIndexer(searchService, moduleConfigs, {\n queryEngine,\n fulltextQueue,\n vectorQueue,\n encryptedCustomFieldKeysResolver,\n })\n\n // Register in container\n container.register({\n searchService: asValue(searchService),\n searchStrategies: asValue(strategies),\n searchIndexer: asValue(searchIndexer),\n })\n}\n\n/**\n * Determine default strategy order based on available strategies.\n * Prefers fulltext > vector > tokens.\n */\nfunction determineDefaultStrategies(strategies: SearchStrategy[]): SearchStrategyId[] {\n const available = new Set(strategies.map((s) => s.id))\n const defaults: SearchStrategyId[] = []\n\n if (available.has('fulltext')) defaults.push('fulltext')\n if (available.has('vector')) defaults.push('vector')\n if (available.has('tokens')) defaults.push('tokens')\n\n return defaults.length > 0 ? defaults : ['tokens']\n}\n\n/**\n * Helper to add a custom strategy to an existing SearchService.\n *\n * @param container - DI container\n * @param strategy - Strategy to add\n */\nexport function addSearchStrategy(container: SearchContainer, strategy: SearchStrategy): void {\n const service = container.resolve<SearchService>('searchService')\n service.registerStrategy(strategy)\n\n const strategies = container.resolve<SearchStrategy[]>('searchStrategies')\n strategies.push(strategy)\n}\n"],
|
|
5
|
-
"mappings": "AAAA,SAAS,oCAAoC;AAC7C,SAAS,
|
|
4
|
+
"sourcesContent": ["import { resolveEntityIndexStrategies } from './lib/entity-strategy-policy'\nimport { asValue, type AwilixContainer } from 'awilix'\nimport { asScopedService } from '@open-mercato/shared/lib/di/process-registrar'\nimport type { Kysely } from 'kysely'\nimport type { FullTextSearchDriver } from './fulltext/types'\nimport { SearchService } from './service'\nimport { TokenSearchStrategy } from './strategies/token.strategy'\nimport { VectorSearchStrategy, type EmbeddingService } from './strategies/vector.strategy'\nimport { FullTextSearchStrategy } from './strategies/fulltext.strategy'\nimport { createFulltextDriver } from './fulltext/drivers'\nimport { SearchIndexer } from './indexer/search-indexer'\nimport type {\n SearchStrategy,\n ResultMergeConfig,\n SearchModuleConfig,\n SearchFieldPolicy,\n SearchEntityConfig,\n PresenterEnricherFn,\n} from './types'\nimport type { VectorDriver } from './vector/types'\nimport type { QueryEngine } from '@open-mercato/shared/lib/query/types'\nimport type { EntityId } from '@open-mercato/shared/modules/entities'\nimport type { SearchStrategyId } from '@open-mercato/shared/modules/search'\nimport type { Queue } from '@open-mercato/queue'\nimport type { FulltextIndexJobPayload } from './queue/fulltext-indexing'\nimport type { VectorIndexJobPayload } from './queue/vector-indexing'\nimport type { EncryptionMapEntry } from './lib/field-policy'\nimport type { TenantDataEncryptionService } from '@open-mercato/shared/lib/encryption/tenantDataEncryptionService'\nimport { createPresenterEnricher } from './lib/presenter-enricher'\nimport { createEncryptionMapResolver } from './lib/encryption-map-resolver'\nimport {\n createEncryptedCustomFieldKeysResolver,\n type CustomFieldDefinitionsDatabase,\n type EncryptedCustomFieldKeysResolver,\n} from './lib/encrypted-custom-field-resolver'\nimport { reportSearchError } from './lib/debug'\n\nexport { createEncryptionMapResolver } from './lib/encryption-map-resolver'\nexport {\n createEncryptedCustomFieldKeysResolver,\n type EncryptedCustomFieldKeysResolver,\n} from './lib/encrypted-custom-field-resolver'\n\ntype KyselyEntityManager = {\n getKysely<Database>(): Kysely<Database>\n}\n\nconst FULLTEXT_DRIVER_KEY = '__omSearchFulltextDriver__'\n\ntype SearchModuleGlobals = {\n [FULLTEXT_DRIVER_KEY]?: FullTextSearchDriver\n}\n\ntype GenerationFulltextDriverCache = SearchModuleGlobals & {\n fieldPolicyResolver: (entityId: EntityId) => SearchFieldPolicy | undefined\n}\n\nfunction getSearchModuleGlobals(): SearchModuleGlobals {\n return globalThis as unknown as SearchModuleGlobals\n}\n\n/**\n * Check if encrypted fields should be excluded from search indexing.\n * Controlled by SEARCH_EXCLUDE_ENCRYPTED_FIELDS environment variable.\n * Default: false (index all fields including decrypted data)\n */\nfunction shouldExcludeEncryptedFields(): boolean {\n const raw = (process.env.SEARCH_EXCLUDE_ENCRYPTED_FIELDS ?? '').toLowerCase()\n return raw === '1' || raw === 'true' || raw === 'yes' || raw === 'on'\n}\n\nfunction createUnavailableEncryptedCustomFieldKeysResolver(): EncryptedCustomFieldKeysResolver {\n return async () => {\n throw new Error('[internal] Custom-field definitions are unavailable; refusing to index potentially encrypted custom fields')\n }\n}\n\nfunction createUnavailableEncryptionMapResolver(): (entityId: EntityId) => Promise<EncryptionMapEntry[]> {\n return async () => {\n throw new Error('[internal] Encryption map lookup is unavailable; refusing to index potentially encrypted fields')\n }\n}\n\n/**\n * Container interface - minimal subset needed for registration.\n */\nexport interface SearchContainer {\n resolve<T = unknown>(name: string): T\n register(registrations: Record<string, unknown>): void\n}\n\n/**\n * Configuration options for search module registration.\n */\nexport type SearchModuleOptions = {\n /** Override default strategies to use */\n defaultStrategies?: SearchStrategyId[]\n /** Override merge configuration */\n mergeConfig?: ResultMergeConfig\n /** Skip token strategy registration */\n skipTokens?: boolean\n /** Skip vector strategy registration */\n skipVector?: boolean\n /** Skip fulltext strategy registration */\n skipFulltext?: boolean\n /** Module configurations (from generated/search.generated.ts) */\n moduleConfigs?: SearchModuleConfig[]\n}\n\n/**\n * Register the search module in the DI container.\n *\n * This creates and registers:\n * - SearchService instance\n * - All configured search strategies\n *\n * @param container - Awilix container\n * @param options - Optional configuration overrides\n */\nexport function registerSearchModule(\n container: SearchContainer,\n options?: SearchModuleOptions,\n): void {\n const services = createSearchServices(container, options)\n container.register({\n searchService: asValue(services.searchService),\n searchStrategies: asValue(services.searchStrategies),\n searchIndexer: asValue(services.searchIndexer),\n })\n}\n\nexport function registerScopedSearchModule(root: AwilixContainer, options?: SearchModuleOptions): void {\n const fieldPolicies = new Map<EntityId, SearchFieldPolicy | undefined>()\n for (const moduleConfig of options?.moduleConfigs ?? []) {\n for (const entityConfig of moduleConfig.entities) {\n if (entityConfig.enabled !== false) fieldPolicies.set(entityConfig.entityId as EntityId, entityConfig.fieldPolicy)\n }\n }\n const fulltextDriverCache: GenerationFulltextDriverCache = {\n fieldPolicyResolver: (entityId) => fieldPolicies.get(entityId),\n }\n const instances = new WeakMap<AwilixContainer, ReturnType<typeof createSearchServices>>()\n const resolveServices = (caller: AwilixContainer) => {\n let services = instances.get(caller)\n if (!services) {\n services = createSearchServices(caller, options, false, fulltextDriverCache)\n instances.set(caller, services)\n }\n return services\n }\n root.register({\n searchService: asScopedService((caller) => resolveServices(caller).searchService),\n searchStrategies: asScopedService((caller) => resolveServices(caller).searchStrategies),\n searchIndexer: asScopedService((caller) => resolveServices(caller).searchIndexer),\n })\n}\n\nfunction createSearchServices(\n container: SearchContainer,\n options?: SearchModuleOptions,\n cacheFulltextDriver = true,\n generationFulltextDriverCache?: GenerationFulltextDriverCache,\n) {\n const moduleConfigs = (options?.moduleConfigs ?? []).map((moduleConfig) => ({\n ...moduleConfig,\n entities: moduleConfig.entities.map((entityConfig) => {\n const buildSource = entityConfig.buildSource\n return buildSource\n ? { ...entityConfig, buildSource: (ctx: Parameters<typeof buildSource>[0]) => buildSource.call(entityConfig, { ...ctx, container }) }\n : entityConfig\n }),\n }))\n // Resolve encryption service for decrypting presenter data\n let encryptionService: TenantDataEncryptionService | null = null\n try {\n encryptionService = container.resolve<TenantDataEncryptionService>('tenantEncryptionService')\n } catch {\n // Encryption service not available, presenters won't be decrypted\n }\n\n const strategies: SearchStrategy[] = []\n\n // Token strategy (always available unless explicitly skipped)\n if (!options?.skipTokens) {\n try {\n const em = container.resolve<any>('em')\n const db = em.getKysely() as Kysely<any>\n strategies.push(new TokenSearchStrategy(db))\n } catch {\n // Kysely not available via em, skipping TokenSearchStrategy\n }\n }\n\n // Vector strategy (requires embedding service and driver)\n // Note: We register even if not currently available - availability is checked at search time\n // via isAvailable(). The embedding config may be loaded later from the database.\n if (!options?.skipVector) {\n try {\n const embeddingService = container.resolve<EmbeddingService>('vectorEmbeddingService')\n const drivers = container.resolve<VectorDriver[]>('vectorDrivers')\n const primaryDriver = drivers?.[0]\n\n if (embeddingService && primaryDriver) {\n strategies.push(new VectorSearchStrategy(embeddingService, primaryDriver, { encryptionService }))\n }\n } catch {\n // Vector module not available, skipping VectorSearchStrategy\n }\n }\n\n // Build entity config map for field policy resolution\n const entityConfigMap = new Map<EntityId, SearchEntityConfig>()\n for (const moduleConfig of moduleConfigs) {\n for (const entityConfig of moduleConfig.entities) {\n if (entityConfig.enabled !== false) {\n entityConfigMap.set(entityConfig.entityId as EntityId, entityConfig)\n }\n }\n }\n\n // Fulltext strategy (requires driver configuration, e.g., MEILISEARCH_HOST)\n if (!options?.skipFulltext) {\n const excludeEncrypted = shouldExcludeEncryptedFields()\n const singletonCacheEnabled = (cacheFulltextDriver || !!generationFulltextDriverCache)\n && process.env.SEARCH_DISABLE_SINGLETON_CACHE !== '1'\n const g = generationFulltextDriverCache ?? getSearchModuleGlobals()\n\n // The fulltext driver is safe to memoize only when SEARCH_EXCLUDE_ENCRYPTED_FIELDS is\n // OFF \u2014 in that case the driver holds no request-scoped Kysely reference. When the flag\n // is ON the encryptionMapResolver captures a per-request db handle and must stay\n // per-request to avoid cross-request data leaks.\n const canMemoize = singletonCacheEnabled && !excludeEncrypted\n\n let fulltextDriver: FullTextSearchDriver | null = canMemoize\n ? (g[FULLTEXT_DRIVER_KEY] ?? null)\n : null\n\n if (!fulltextDriver) {\n let encryptionMapResolver: ((entityId: EntityId) => Promise<EncryptionMapEntry[]>) | undefined\n if (excludeEncrypted) {\n try {\n const em = container.resolve<any>('em')\n const db = em.getKysely() as Kysely<any>\n encryptionMapResolver = createEncryptionMapResolver(db)\n } catch {\n encryptionMapResolver = createUnavailableEncryptionMapResolver()\n }\n }\n\n fulltextDriver = createFulltextDriver({\n fieldPolicyResolver: generationFulltextDriverCache?.fieldPolicyResolver ?? ((entityId: EntityId): SearchFieldPolicy | undefined => {\n const config = entityConfigMap.get(entityId)\n return config?.fieldPolicy\n }),\n encryptionMapResolver,\n })\n\n if (canMemoize && fulltextDriver) {\n g[FULLTEXT_DRIVER_KEY] = fulltextDriver\n }\n }\n\n if (fulltextDriver) {\n strategies.push(new FullTextSearchStrategy(fulltextDriver))\n }\n }\n\n // Determine default strategies based on what's available\n const defaultStrategies = options?.defaultStrategies ?? determineDefaultStrategies(strategies)\n\n // Try to resolve queryEngine for reindex support and presenter enrichment\n let queryEngine: QueryEngine | undefined\n try {\n queryEngine = container.resolve<QueryEngine>('queryEngine')\n } catch {\n // QueryEngine not available, reindex will be disabled\n }\n\n // Create presenter enricher for database-based presenter resolution\n let presenterEnricher: PresenterEnricherFn | undefined\n try {\n const em = container.resolve<any>('em')\n const db = em.getKysely() as Kysely<any>\n presenterEnricher = createPresenterEnricher(db, entityConfigMap, queryEngine, encryptionService)\n } catch {\n // Kysely not available, presenter enrichment disabled\n }\n\n // Create search service\n const searchService = new SearchService({\n strategies,\n resolveEntityStrategies: (entityId) => resolveEntityIndexStrategies(entityId, moduleConfigs),\n resolveResultFilter: (entityId) => entityConfigMap.get(entityId as EntityId)?.filterResults,\n defaultStrategies,\n fallbackStrategy: 'tokens',\n mergeConfig: options?.mergeConfig ?? {\n duplicateHandling: 'highest_score',\n strategyWeights: {\n fulltext: 1.2,\n vector: 1.0,\n tokens: 0.8,\n },\n },\n presenterEnricher,\n })\n\n // Try to resolve fulltextIndexQueue for queue-based reindexing\n let fulltextQueue: Queue<FulltextIndexJobPayload> | undefined\n try {\n fulltextQueue = container.resolve<Queue<FulltextIndexJobPayload>>('fulltextIndexQueue')\n } catch {\n // Queue not available, queue-based fulltext reindex will be disabled\n }\n\n // Try to resolve vectorIndexQueue for queue-based vector reindexing\n let vectorQueue: Queue<VectorIndexJobPayload> | undefined\n try {\n vectorQueue = container.resolve<Queue<VectorIndexJobPayload>>('vectorIndexQueue')\n } catch {\n // Queue not available, queue-based vector reindex will be disabled\n }\n\n let encryptedCustomFieldKeysResolver: EncryptedCustomFieldKeysResolver\n try {\n const em = container.resolve<KyselyEntityManager>('em')\n encryptedCustomFieldKeysResolver = createEncryptedCustomFieldKeysResolver(\n em.getKysely<CustomFieldDefinitionsDatabase>(),\n )\n } catch (error) {\n reportSearchError('SearchModule', error, 'search.custom_field_definitions_unavailable')\n encryptedCustomFieldKeysResolver = createUnavailableEncryptedCustomFieldKeysResolver()\n }\n\n const searchIndexer = new SearchIndexer(searchService, moduleConfigs, {\n queryEngine,\n fulltextQueue,\n vectorQueue,\n encryptedCustomFieldKeysResolver,\n })\n\n return { searchService, searchStrategies: strategies, searchIndexer }\n}\n\n/**\n * Determine default strategy order based on available strategies.\n * Prefers fulltext > vector > tokens.\n */\nfunction determineDefaultStrategies(strategies: SearchStrategy[]): SearchStrategyId[] {\n const available = new Set(strategies.map((s) => s.id))\n const defaults: SearchStrategyId[] = []\n\n if (available.has('fulltext')) defaults.push('fulltext')\n if (available.has('vector')) defaults.push('vector')\n if (available.has('tokens')) defaults.push('tokens')\n\n return defaults.length > 0 ? defaults : ['tokens']\n}\n\n/**\n * Helper to add a custom strategy to an existing SearchService.\n *\n * @param container - DI container\n * @param strategy - Strategy to add\n */\nexport function addSearchStrategy(container: SearchContainer, strategy: SearchStrategy): void {\n const service = container.resolve<SearchService>('searchService')\n service.registerStrategy(strategy)\n\n const strategies = container.resolve<SearchStrategy[]>('searchStrategies')\n strategies.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AA,IACA;AAAA,IACA;AAAA,EACF,CAAC;AAED,SAAO,EAAE,eAAe,kBAAkB,YAAY,cAAc;AACtE;AAMA,SAAS,2BAA2B,YAAkD;AACpF,QAAM,YAAY,IAAI,IAAI,WAAW,IAAI,CAAC,MAAM,EAAE,EAAE,CAAC;AACrD,QAAM,WAA+B,CAAC;AAEtC,MAAI,UAAU,IAAI,UAAU,EAAG,UAAS,KAAK,UAAU;AACvD,MAAI,UAAU,IAAI,QAAQ,EAAG,UAAS,KAAK,QAAQ;AACnD,MAAI,UAAU,IAAI,QAAQ,EAAG,UAAS,KAAK,QAAQ;AAEnD,SAAO,SAAS,SAAS,IAAI,WAAW,CAAC,QAAQ;AACnD;AAQO,SAAS,kBAAkB,WAA4B,UAAgC;AAC5F,QAAM,UAAU,UAAU,QAAuB,eAAe;AAChE,UAAQ,iBAAiB,QAAQ;AAEjC,QAAM,aAAa,UAAU,QAA0B,kBAAkB;AACzE,aAAW,KAAK,QAAQ;AAC1B;",
|
|
6
6
|
"names": ["createEncryptionMapResolver", "createEncryptedCustomFieldKeysResolver"]
|
|
7
7
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { defineProcessDiRegistrar, asScopedService } from "@open-mercato/shared/lib/di/process-registrar";
|
|
1
2
|
import { asValue } from "awilix";
|
|
2
3
|
import { Pool } from "pg";
|
|
3
4
|
import { getRedisUrlOrThrow } from "@open-mercato/shared/lib/redis/connection";
|
|
@@ -93,9 +94,10 @@ function register(container) {
|
|
|
93
94
|
vectorDrivers: asValue(drivers),
|
|
94
95
|
vectorIndexQueue: asValue(vectorIndexQueue),
|
|
95
96
|
fulltextIndexQueue: asValue(fulltextIndexQueue),
|
|
96
|
-
embeddingProviderProbe:
|
|
97
|
+
embeddingProviderProbe: asScopedService(createEmbeddingProviderProbe)
|
|
97
98
|
});
|
|
98
99
|
}
|
|
100
|
+
defineProcessDiRegistrar(register);
|
|
99
101
|
export {
|
|
100
102
|
register
|
|
101
103
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/modules/search/di.ts"],
|
|
4
|
-
"sourcesContent": ["import { asValue } from 'awilix'\nimport { Pool } from 'pg'\nimport type { AppContainer } from '@open-mercato/shared/lib/di/container'\nimport { getRedisUrlOrThrow } from '@open-mercato/shared/lib/redis/connection'\nimport { EmbeddingService, createPgVectorDriver, createChromaDbDriver, createQdrantDriver } from '../../vector'\nimport type { PgPool } from '../../vector/drivers/pgvector'\nimport { createVectorIndexingQueue, type VectorIndexJobPayload } from '../../queue/vector-indexing'\nimport { createFulltextIndexingQueue, type FulltextIndexJobPayload } from '../../queue/fulltext-indexing'\nimport { createEmbeddingProviderProbe } from './lib/provider-probe'\nimport type { Queue } from '@open-mercato/queue'\n\nconst EMBEDDING_SERVICE_KEY = '__omSearchEmbeddingService__'\nconst VECTOR_DRIVERS_KEY = '__omSearchVectorDrivers__'\nconst VECTOR_INDEX_QUEUE_KEY = '__omSearchVectorIndexQueue__'\nconst FULLTEXT_INDEX_QUEUE_KEY = '__omSearchFulltextIndexQueue__'\nconst PG_POOL_KEY = '__omSearchVectorPgPool__'\nconst SHUTDOWN_KEY = '__omSearchSingletonsShutdown__'\n\ntype SearchSingletonCache = {\n [EMBEDDING_SERVICE_KEY]?: EmbeddingService\n [VECTOR_DRIVERS_KEY]?: ReturnType<typeof createPgVectorDriver>[]\n [VECTOR_INDEX_QUEUE_KEY]?: Queue<VectorIndexJobPayload>\n [FULLTEXT_INDEX_QUEUE_KEY]?: Queue<FulltextIndexJobPayload>\n [PG_POOL_KEY]?: PgPool\n [SHUTDOWN_KEY]?: boolean\n}\n\nfunction getSearchGlobals(): SearchSingletonCache {\n // L1: these globals are NOT cleared on HMR \u2014 module re-evaluation hits the\n // cache-hit fast-path so no new connections are opened. Restart the dev server\n // (or set SEARCH_DISABLE_SINGLETON_CACHE=1) when a clean slate is needed.\n return globalThis as unknown as SearchSingletonCache\n}\n\nfunction isSingletonCacheEnabled(): boolean {\n return process.env.SEARCH_DISABLE_SINGLETON_CACHE !== '1'\n}\n\nfunction buildQueueOptions(): {\n queueStrategy: 'local' | 'async'\n queueConnection: { connection: { url: string } } | undefined\n} {\n const queueStrategy = (process.env.QUEUE_STRATEGY || 'local') as 'local' | 'async'\n const queueConnection = queueStrategy === 'async'\n ? { connection: { url: getRedisUrlOrThrow('QUEUE') } }\n : undefined\n return { queueStrategy, queueConnection }\n}\n\nfunction getOrCreateSingletons(): {\n embeddingService: EmbeddingService\n drivers: ReturnType<typeof createPgVectorDriver>[]\n vectorIndexQueue: Queue<VectorIndexJobPayload>\n fulltextIndexQueue: Queue<FulltextIndexJobPayload>\n} {\n const g = getSearchGlobals()\n\n if (\n g[EMBEDDING_SERVICE_KEY] &&\n g[VECTOR_DRIVERS_KEY] &&\n g[VECTOR_INDEX_QUEUE_KEY] &&\n g[FULLTEXT_INDEX_QUEUE_KEY]\n ) {\n return {\n embeddingService: g[EMBEDDING_SERVICE_KEY],\n drivers: g[VECTOR_DRIVERS_KEY],\n vectorIndexQueue: g[VECTOR_INDEX_QUEUE_KEY],\n fulltextIndexQueue: g[FULLTEXT_INDEX_QUEUE_KEY],\n }\n }\n\n const embeddingService = new EmbeddingService()\n\n // Create the pgvector pool separately so we can close it in the shutdown hook\n let pgPool: PgPool | undefined\n const dbUrl = process.env.DATABASE_URL\n if (dbUrl) {\n pgPool = new Pool({ connectionString: dbUrl }) as unknown as PgPool\n g[PG_POOL_KEY] = pgPool\n }\n\n const drivers = [\n createPgVectorDriver(pgPool ? { pool: pgPool } : {}),\n createChromaDbDriver(),\n createQdrantDriver(),\n ]\n\n const { queueStrategy, queueConnection } = buildQueueOptions()\n\n const vectorIndexQueue: Queue<VectorIndexJobPayload> = createVectorIndexingQueue(\n queueStrategy,\n queueConnection,\n )\n\n const fulltextIndexQueue: Queue<FulltextIndexJobPayload> = createFulltextIndexingQueue(\n queueStrategy,\n queueConnection,\n )\n\n g[EMBEDDING_SERVICE_KEY] = embeddingService\n g[VECTOR_DRIVERS_KEY] = drivers\n g[VECTOR_INDEX_QUEUE_KEY] = vectorIndexQueue\n g[FULLTEXT_INDEX_QUEUE_KEY] = fulltextIndexQueue\n\n if (!g[SHUTDOWN_KEY]) {\n const shutdown = () => {\n // L2: chromadb/qdrant drivers use stateless HTTP clients \u2014 no persistent\n // connection to close. Only the pg.Pool and BullMQ queues need teardown.\n g[PG_POOL_KEY]?.end().catch(() => {})\n g[VECTOR_INDEX_QUEUE_KEY]?.close().catch(() => {})\n g[FULLTEXT_INDEX_QUEUE_KEY]?.close().catch(() => {})\n }\n process.once('SIGTERM', shutdown)\n process.once('SIGINT', shutdown)\n g[SHUTDOWN_KEY] = true\n }\n\n return { embeddingService, drivers, vectorIndexQueue, fulltextIndexQueue }\n}\n\n/**\n * Register search module dependencies.\n *\n * This registers:\n * - vectorEmbeddingService: EmbeddingService for creating embeddings\n * - vectorDrivers: Array of vector database drivers (pgvector, chromadb, qdrant)\n * - vectorIndexQueue: Queue for vector indexing jobs\n * - fulltextIndexQueue: Queue for fulltext indexing jobs\n *\n * These four are process-scoped singletons (globalThis) to avoid per-request pg.Pool\n * creation, DDL re-runs, and BullMQ/ioredis connection leaks. Disable with\n * SEARCH_DISABLE_SINGLETON_CACHE=1.\n *\n * Note: VectorIndexService is no longer registered here. Use SearchIndexer instead,\n * which is registered in the main search module DI (packages/search/src/di.ts).\n */\nexport function register(container: AppContainer) {\n if (!isSingletonCacheEnabled()) {\n const { queueStrategy, queueConnection } = buildQueueOptions()\n\n container.register({\n vectorEmbeddingService: asValue(new EmbeddingService()),\n vectorDrivers: asValue([\n createPgVectorDriver(),\n createChromaDbDriver(),\n createQdrantDriver(),\n ]),\n vectorIndexQueue: asValue(createVectorIndexingQueue(queueStrategy, queueConnection)),\n fulltextIndexQueue: asValue(createFulltextIndexingQueue(queueStrategy, queueConnection)),\n })\n return\n }\n\n const { embeddingService, drivers, vectorIndexQueue, fulltextIndexQueue } = getOrCreateSingletons()\n\n container.register({\n vectorEmbeddingService: asValue(embeddingService),\n vectorDrivers: asValue(drivers),\n vectorIndexQueue: asValue(vectorIndexQueue),\n fulltextIndexQueue: asValue(fulltextIndexQueue),\n embeddingProviderProbe:
|
|
5
|
-
"mappings": "AAAA,SAAS,eAAe;AACxB,SAAS,YAAY;AAErB,SAAS,0BAA0B;AACnC,SAAS,kBAAkB,sBAAsB,sBAAsB,0BAA0B;AAEjG,SAAS,iCAA6D;AACtE,SAAS,mCAAiE;AAC1E,SAAS,oCAAoC;AAG7C,MAAM,wBAAwB;AAC9B,MAAM,qBAAqB;AAC3B,MAAM,yBAAyB;AAC/B,MAAM,2BAA2B;AACjC,MAAM,cAAc;AACpB,MAAM,eAAe;AAWrB,SAAS,mBAAyC;AAIhD,SAAO;AACT;AAEA,SAAS,0BAAmC;AAC1C,SAAO,QAAQ,IAAI,mCAAmC;AACxD;AAEA,SAAS,oBAGP;AACA,QAAM,gBAAiB,QAAQ,IAAI,kBAAkB;AACrD,QAAM,kBAAkB,kBAAkB,UACtC,EAAE,YAAY,EAAE,KAAK,mBAAmB,OAAO,EAAE,EAAE,IACnD;AACJ,SAAO,EAAE,eAAe,gBAAgB;AAC1C;AAEA,SAAS,wBAKP;AACA,QAAM,IAAI,iBAAiB;AAE3B,MACE,EAAE,qBAAqB,KACvB,EAAE,kBAAkB,KACpB,EAAE,sBAAsB,KACxB,EAAE,wBAAwB,GAC1B;AACA,WAAO;AAAA,MACL,kBAAkB,EAAE,qBAAqB;AAAA,MACzC,SAAS,EAAE,kBAAkB;AAAA,MAC7B,kBAAkB,EAAE,sBAAsB;AAAA,MAC1C,oBAAoB,EAAE,wBAAwB;AAAA,IAChD;AAAA,EACF;AAEA,QAAM,mBAAmB,IAAI,iBAAiB;AAG9C,MAAI;AACJ,QAAM,QAAQ,QAAQ,IAAI;AAC1B,MAAI,OAAO;AACT,aAAS,IAAI,KAAK,EAAE,kBAAkB,MAAM,CAAC;AAC7C,MAAE,WAAW,IA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C;AAAA,IACzF,CAAC;AACD;AAAA,EACF;AAEA,QAAM,EAAE,kBAAkB,SAAS,kBAAkB,mBAAmB,IAAI,sBAAsB;AAElG,YAAU,SAAS;AAAA,IACjB,wBAAwB,QAAQ,gBAAgB;AAAA,IAChD,eAAe,QAAQ,OAAO;AAAA,IAC9B,kBAAkB,QAAQ,gBAAgB;AAAA,IAC1C,oBAAoB,QAAQ,kBAAkB;AAAA,IAC9C,wBAAwB,
|
|
4
|
+
"sourcesContent": ["import { defineProcessDiRegistrar, asScopedService } from '@open-mercato/shared/lib/di/process-registrar'\nimport { asValue } from 'awilix'\nimport { Pool } from 'pg'\nimport type { AppContainer } from '@open-mercato/shared/lib/di/container'\nimport { getRedisUrlOrThrow } from '@open-mercato/shared/lib/redis/connection'\nimport { EmbeddingService, createPgVectorDriver, createChromaDbDriver, createQdrantDriver } from '../../vector'\nimport type { PgPool } from '../../vector/drivers/pgvector'\nimport { createVectorIndexingQueue, type VectorIndexJobPayload } from '../../queue/vector-indexing'\nimport { createFulltextIndexingQueue, type FulltextIndexJobPayload } from '../../queue/fulltext-indexing'\nimport { createEmbeddingProviderProbe } from './lib/provider-probe'\nimport type { Queue } from '@open-mercato/queue'\n\nconst EMBEDDING_SERVICE_KEY = '__omSearchEmbeddingService__'\nconst VECTOR_DRIVERS_KEY = '__omSearchVectorDrivers__'\nconst VECTOR_INDEX_QUEUE_KEY = '__omSearchVectorIndexQueue__'\nconst FULLTEXT_INDEX_QUEUE_KEY = '__omSearchFulltextIndexQueue__'\nconst PG_POOL_KEY = '__omSearchVectorPgPool__'\nconst SHUTDOWN_KEY = '__omSearchSingletonsShutdown__'\n\ntype SearchSingletonCache = {\n [EMBEDDING_SERVICE_KEY]?: EmbeddingService\n [VECTOR_DRIVERS_KEY]?: ReturnType<typeof createPgVectorDriver>[]\n [VECTOR_INDEX_QUEUE_KEY]?: Queue<VectorIndexJobPayload>\n [FULLTEXT_INDEX_QUEUE_KEY]?: Queue<FulltextIndexJobPayload>\n [PG_POOL_KEY]?: PgPool\n [SHUTDOWN_KEY]?: boolean\n}\n\nfunction getSearchGlobals(): SearchSingletonCache {\n // L1: these globals are NOT cleared on HMR \u2014 module re-evaluation hits the\n // cache-hit fast-path so no new connections are opened. Restart the dev server\n // (or set SEARCH_DISABLE_SINGLETON_CACHE=1) when a clean slate is needed.\n return globalThis as unknown as SearchSingletonCache\n}\n\nfunction isSingletonCacheEnabled(): boolean {\n return process.env.SEARCH_DISABLE_SINGLETON_CACHE !== '1'\n}\n\nfunction buildQueueOptions(): {\n queueStrategy: 'local' | 'async'\n queueConnection: { connection: { url: string } } | undefined\n} {\n const queueStrategy = (process.env.QUEUE_STRATEGY || 'local') as 'local' | 'async'\n const queueConnection = queueStrategy === 'async'\n ? { connection: { url: getRedisUrlOrThrow('QUEUE') } }\n : undefined\n return { queueStrategy, queueConnection }\n}\n\nfunction getOrCreateSingletons(): {\n embeddingService: EmbeddingService\n drivers: ReturnType<typeof createPgVectorDriver>[]\n vectorIndexQueue: Queue<VectorIndexJobPayload>\n fulltextIndexQueue: Queue<FulltextIndexJobPayload>\n} {\n const g = getSearchGlobals()\n\n if (\n g[EMBEDDING_SERVICE_KEY] &&\n g[VECTOR_DRIVERS_KEY] &&\n g[VECTOR_INDEX_QUEUE_KEY] &&\n g[FULLTEXT_INDEX_QUEUE_KEY]\n ) {\n return {\n embeddingService: g[EMBEDDING_SERVICE_KEY],\n drivers: g[VECTOR_DRIVERS_KEY],\n vectorIndexQueue: g[VECTOR_INDEX_QUEUE_KEY],\n fulltextIndexQueue: g[FULLTEXT_INDEX_QUEUE_KEY],\n }\n }\n\n const embeddingService = new EmbeddingService()\n\n // Create the pgvector pool separately so we can close it in the shutdown hook\n let pgPool: PgPool | undefined\n const dbUrl = process.env.DATABASE_URL\n if (dbUrl) {\n pgPool = new Pool({ connectionString: dbUrl }) as unknown as PgPool\n g[PG_POOL_KEY] = pgPool\n }\n\n const drivers = [\n createPgVectorDriver(pgPool ? { pool: pgPool } : {}),\n createChromaDbDriver(),\n createQdrantDriver(),\n ]\n\n const { queueStrategy, queueConnection } = buildQueueOptions()\n\n const vectorIndexQueue: Queue<VectorIndexJobPayload> = createVectorIndexingQueue(\n queueStrategy,\n queueConnection,\n )\n\n const fulltextIndexQueue: Queue<FulltextIndexJobPayload> = createFulltextIndexingQueue(\n queueStrategy,\n queueConnection,\n )\n\n g[EMBEDDING_SERVICE_KEY] = embeddingService\n g[VECTOR_DRIVERS_KEY] = drivers\n g[VECTOR_INDEX_QUEUE_KEY] = vectorIndexQueue\n g[FULLTEXT_INDEX_QUEUE_KEY] = fulltextIndexQueue\n\n if (!g[SHUTDOWN_KEY]) {\n const shutdown = () => {\n // L2: chromadb/qdrant drivers use stateless HTTP clients \u2014 no persistent\n // connection to close. Only the pg.Pool and BullMQ queues need teardown.\n g[PG_POOL_KEY]?.end().catch(() => {})\n g[VECTOR_INDEX_QUEUE_KEY]?.close().catch(() => {})\n g[FULLTEXT_INDEX_QUEUE_KEY]?.close().catch(() => {})\n }\n process.once('SIGTERM', shutdown)\n process.once('SIGINT', shutdown)\n g[SHUTDOWN_KEY] = true\n }\n\n return { embeddingService, drivers, vectorIndexQueue, fulltextIndexQueue }\n}\n\n/**\n * Register search module dependencies.\n *\n * This registers:\n * - vectorEmbeddingService: EmbeddingService for creating embeddings\n * - vectorDrivers: Array of vector database drivers (pgvector, chromadb, qdrant)\n * - vectorIndexQueue: Queue for vector indexing jobs\n * - fulltextIndexQueue: Queue for fulltext indexing jobs\n *\n * These four are process-scoped singletons (globalThis) to avoid per-request pg.Pool\n * creation, DDL re-runs, and BullMQ/ioredis connection leaks. Disable with\n * SEARCH_DISABLE_SINGLETON_CACHE=1.\n *\n * Note: VectorIndexService is no longer registered here. Use SearchIndexer instead,\n * which is registered in the main search module DI (packages/search/src/di.ts).\n */\nexport function register(container: AppContainer) {\n if (!isSingletonCacheEnabled()) {\n const { queueStrategy, queueConnection } = buildQueueOptions()\n\n container.register({\n vectorEmbeddingService: asValue(new EmbeddingService()),\n vectorDrivers: asValue([\n createPgVectorDriver(),\n createChromaDbDriver(),\n createQdrantDriver(),\n ]),\n vectorIndexQueue: asValue(createVectorIndexingQueue(queueStrategy, queueConnection)),\n fulltextIndexQueue: asValue(createFulltextIndexingQueue(queueStrategy, queueConnection)),\n })\n return\n }\n\n const { embeddingService, drivers, vectorIndexQueue, fulltextIndexQueue } = getOrCreateSingletons()\n\n container.register({\n vectorEmbeddingService: asValue(embeddingService),\n vectorDrivers: asValue(drivers),\n vectorIndexQueue: asValue(vectorIndexQueue),\n fulltextIndexQueue: asValue(fulltextIndexQueue),\n embeddingProviderProbe: asScopedService(createEmbeddingProviderProbe),\n })\n}\n\ndefineProcessDiRegistrar(register)\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,0BAA0B,uBAAuB;AAC1D,SAAS,eAAe;AACxB,SAAS,YAAY;AAErB,SAAS,0BAA0B;AACnC,SAAS,kBAAkB,sBAAsB,sBAAsB,0BAA0B;AAEjG,SAAS,iCAA6D;AACtE,SAAS,mCAAiE;AAC1E,SAAS,oCAAoC;AAG7C,MAAM,wBAAwB;AAC9B,MAAM,qBAAqB;AAC3B,MAAM,yBAAyB;AAC/B,MAAM,2BAA2B;AACjC,MAAM,cAAc;AACpB,MAAM,eAAe;AAWrB,SAAS,mBAAyC;AAIhD,SAAO;AACT;AAEA,SAAS,0BAAmC;AAC1C,SAAO,QAAQ,IAAI,mCAAmC;AACxD;AAEA,SAAS,oBAGP;AACA,QAAM,gBAAiB,QAAQ,IAAI,kBAAkB;AACrD,QAAM,kBAAkB,kBAAkB,UACtC,EAAE,YAAY,EAAE,KAAK,mBAAmB,OAAO,EAAE,EAAE,IACnD;AACJ,SAAO,EAAE,eAAe,gBAAgB;AAC1C;AAEA,SAAS,wBAKP;AACA,QAAM,IAAI,iBAAiB;AAE3B,MACE,EAAE,qBAAqB,KACvB,EAAE,kBAAkB,KACpB,EAAE,sBAAsB,KACxB,EAAE,wBAAwB,GAC1B;AACA,WAA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wB,GAAG;AAC9B,UAAM,EAAE,eAAe,gBAAgB,IAAI,kBAAkB;AAE7D,cAAU,SAAS;AAAA,MACjB,wBAAwB,QAAQ,IAAI,iBAAiB,CAAC;AAAA,MACtD,eAAe,QAAQ;AAAA,QACrB,qBAAqB;AAAA,QACrB,qBAAqB;AAAA,QACrB,mBAAmB;AAAA,MACrB,CAAC;AAAA,MACD,kBAAkB,QAAQ,0BAA0B,eAAe,eAAe,CAAC;AAAA,MACnF,oBAAoB,QAAQ,4BAA4B,eAAe,eAAe,CAAC;AAAA,IACzF,CAAC;AACD;AAAA,EACF;AAEA,QAAM,EAAE,kBAAkB,SAAS,kBAAkB,mBAAmB,IAAI,sBAAsB;AAElG,YAAU,SAAS;AAAA,IACjB,wBAAwB,QAAQ,gBAAgB;AAAA,IAChD,eAAe,QAAQ,OAAO;AAAA,IAC9B,kBAAkB,QAAQ,gBAAgB;AAAA,IAC1C,oBAAoB,QAAQ,kBAAkB;AAAA,IAC9C,wBAAwB,gBAAgB,4BAA4B;AAAA,EACtE,CAAC;AACH;AAEA,yBAAyB,QAAQ;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-mercato/search",
|
|
3
|
-
"version": "0.11.1-develop.
|
|
3
|
+
"version": "0.11.1-develop.8418.1.8ddb502575",
|
|
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.11.1-develop.
|
|
131
|
-
"@open-mercato/queue": "0.11.1-develop.
|
|
132
|
-
"@open-mercato/shared": "0.11.1-develop.
|
|
130
|
+
"@open-mercato/core": "0.11.1-develop.8418.1.8ddb502575",
|
|
131
|
+
"@open-mercato/queue": "0.11.1-develop.8418.1.8ddb502575",
|
|
132
|
+
"@open-mercato/shared": "0.11.1-develop.8418.1.8ddb502575"
|
|
133
133
|
},
|
|
134
134
|
"devDependencies": {
|
|
135
135
|
"@types/jest": "^30.0.0",
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
const mockCreateIndex = jest.fn(async () => undefined)
|
|
2
|
+
const mockUpdateSettings = jest.fn(async () => undefined)
|
|
3
|
+
const mockAddDocuments = jest.fn(async (_documents: unknown[]) => undefined)
|
|
4
|
+
|
|
5
|
+
jest.mock('meilisearch', () => ({
|
|
6
|
+
Meilisearch: jest.fn().mockImplementation(() => ({
|
|
7
|
+
createIndex: mockCreateIndex,
|
|
8
|
+
index: () => ({ updateSettings: mockUpdateSettings, addDocuments: mockAddDocuments }),
|
|
9
|
+
})),
|
|
10
|
+
}))
|
|
11
|
+
|
|
12
|
+
import { asValue, createContainer } from 'awilix'
|
|
13
|
+
import { registerScopedSearchModule } from '../di'
|
|
14
|
+
import type { SearchStrategy } from '../types'
|
|
15
|
+
|
|
16
|
+
const originalEnv = { ...process.env }
|
|
17
|
+
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
jest.clearAllMocks()
|
|
20
|
+
process.env = { ...originalEnv, MEILISEARCH_HOST: 'http://search.test', SEARCH_EXCLUDE_ENCRYPTED_FIELDS: 'off' }
|
|
21
|
+
delete process.env.SEARCH_DISABLE_SINGLETON_CACHE
|
|
22
|
+
})
|
|
23
|
+
afterEach(() => { process.env = originalEnv })
|
|
24
|
+
|
|
25
|
+
function database(encryptedField = 'secret') {
|
|
26
|
+
const query: Record<string, jest.Mock> = {}
|
|
27
|
+
for (const method of ['select', 'where', 'orderBy']) query[method] = jest.fn(() => query)
|
|
28
|
+
query.execute = jest.fn(async () => [{ fields_json: [{ field: encryptedField }] }])
|
|
29
|
+
return { selectFrom: jest.fn(() => query) }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function generation(excluded = 'secret') {
|
|
33
|
+
const root = createContainer()
|
|
34
|
+
registerScopedSearchModule(root, {
|
|
35
|
+
skipTokens: true, skipVector: true,
|
|
36
|
+
moduleConfigs: [{ entities: [{ entityId: 'probe:record', fieldPolicy: { excluded: [excluded] } }] }],
|
|
37
|
+
})
|
|
38
|
+
const firstDb = database('first_secret')
|
|
39
|
+
const secondDb = database('second_secret')
|
|
40
|
+
const first = root.createScope().register({ em: asValue({ getKysely: () => firstDb }) })
|
|
41
|
+
const second = root.createScope().register({ em: asValue({ getKysely: () => secondDb }) })
|
|
42
|
+
return { first, second, firstDb, secondDb }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const record = {
|
|
46
|
+
entityId: 'probe:record', recordId: 'record', tenantId: 'same-tenant',
|
|
47
|
+
fields: { title: 'Visible', secret: 'hidden', first_secret: 'first', second_secret: 'second' },
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
it('coalesces tenant index creation and settings across callers without retaining their SQL accessors', async () => {
|
|
51
|
+
const { first, second, firstDb, secondDb } = generation()
|
|
52
|
+
const firstStrategy = first.resolve<SearchStrategy[]>('searchStrategies')[0]
|
|
53
|
+
const secondStrategy = second.resolve<SearchStrategy[]>('searchStrategies')[0]
|
|
54
|
+
expect(firstStrategy).not.toBe(secondStrategy)
|
|
55
|
+
expect(first.resolve('searchIndexer')).not.toBe(second.resolve('searchIndexer'))
|
|
56
|
+
await Promise.all([firstStrategy.index(record), secondStrategy.index({ ...record, recordId: 'second' })])
|
|
57
|
+
await secondStrategy.index({ ...record, recordId: 'third' })
|
|
58
|
+
expect(mockCreateIndex).toHaveBeenCalledTimes(1)
|
|
59
|
+
expect(mockUpdateSettings).toHaveBeenCalledTimes(1)
|
|
60
|
+
expect(mockAddDocuments).toHaveBeenCalledTimes(3)
|
|
61
|
+
expect(mockAddDocuments.mock.calls[0][0][0]).not.toHaveProperty('secret')
|
|
62
|
+
expect(firstDb.selectFrom).not.toHaveBeenCalled()
|
|
63
|
+
expect(secondDb.selectFrom).not.toHaveBeenCalled()
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
it('owns driver initialization per generation and uses that generation field policy', async () => {
|
|
67
|
+
const oldGeneration = generation('secret')
|
|
68
|
+
const nextGeneration = generation('title')
|
|
69
|
+
await oldGeneration.first.resolve<SearchStrategy[]>('searchStrategies')[0].index(record)
|
|
70
|
+
await nextGeneration.first.resolve<SearchStrategy[]>('searchStrategies')[0].index(record)
|
|
71
|
+
expect(mockCreateIndex).toHaveBeenCalledTimes(2)
|
|
72
|
+
expect(mockUpdateSettings).toHaveBeenCalledTimes(2)
|
|
73
|
+
expect(mockAddDocuments.mock.calls[0][0][0]).toHaveProperty('title', 'Visible')
|
|
74
|
+
expect(mockAddDocuments.mock.calls[1][0][0]).not.toHaveProperty('title')
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it.each(['encrypted', 'disabled-cache'])('keeps fulltext drivers scoped for %s mode', async (mode) => {
|
|
78
|
+
if (mode === 'encrypted') process.env.SEARCH_EXCLUDE_ENCRYPTED_FIELDS = 'on'
|
|
79
|
+
else process.env.SEARCH_DISABLE_SINGLETON_CACHE = '1'
|
|
80
|
+
const { first, second, firstDb, secondDb } = generation()
|
|
81
|
+
await Promise.all([
|
|
82
|
+
first.resolve<SearchStrategy[]>('searchStrategies')[0].index(record),
|
|
83
|
+
second.resolve<SearchStrategy[]>('searchStrategies')[0].index(record),
|
|
84
|
+
])
|
|
85
|
+
expect(mockCreateIndex).toHaveBeenCalledTimes(2)
|
|
86
|
+
expect(mockUpdateSettings).toHaveBeenCalledTimes(2)
|
|
87
|
+
if (mode === 'encrypted') {
|
|
88
|
+
expect(firstDb.selectFrom).toHaveBeenCalledWith('encryption_maps')
|
|
89
|
+
expect(secondDb.selectFrom).toHaveBeenCalledWith('encryption_maps')
|
|
90
|
+
}
|
|
91
|
+
})
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
jest.mock('../fulltext/drivers', () => ({ createFulltextDriver: jest.fn(() => null) }))
|
|
2
|
+
|
|
3
|
+
import { asValue, createContainer } from 'awilix'
|
|
4
|
+
import { registerScopedSearchModule } from '../di'
|
|
5
|
+
import type { SearchIndexer } from '../indexer/search-indexer'
|
|
6
|
+
import type { SearchBuildContext } from '../types'
|
|
7
|
+
|
|
8
|
+
function definitionsDatabase(encryptedField: string) {
|
|
9
|
+
const query: Record<string, jest.Mock> = {}
|
|
10
|
+
for (const method of ['select', 'where']) query[method] = jest.fn(() => query)
|
|
11
|
+
query.execute = jest.fn(async () => [{ key: encryptedField, config_json: { encrypted: true } }])
|
|
12
|
+
return { selectFrom: jest.fn(() => query) }
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
it('indexes with each caller SQL policy and container even when its sibling resolves first', async () => {
|
|
16
|
+
const root = createContainer()
|
|
17
|
+
const contexts: SearchBuildContext[] = []
|
|
18
|
+
registerScopedSearchModule(root, {
|
|
19
|
+
skipTokens: true,
|
|
20
|
+
skipVector: true,
|
|
21
|
+
skipFulltext: true,
|
|
22
|
+
moduleConfigs: [{ entities: [{
|
|
23
|
+
entityId: 'probe:record',
|
|
24
|
+
enabled: true,
|
|
25
|
+
buildSource: async (context) => {
|
|
26
|
+
await Promise.resolve()
|
|
27
|
+
contexts.push(context)
|
|
28
|
+
return { text: 'safe text' }
|
|
29
|
+
},
|
|
30
|
+
}] }],
|
|
31
|
+
})
|
|
32
|
+
const firstDb = definitionsDatabase('secret_a')
|
|
33
|
+
const secondDb = definitionsDatabase('secret_b')
|
|
34
|
+
const first = root.createScope().register({ em: asValue({ getKysely: () => firstDb }), auth: asValue('first') })
|
|
35
|
+
const second = root.createScope().register({ em: asValue({ getKysely: () => secondDb }), auth: asValue('second') })
|
|
36
|
+
const secondIndexer = second.resolve<SearchIndexer>('searchIndexer')
|
|
37
|
+
const firstIndexer = first.resolve<SearchIndexer>('searchIndexer')
|
|
38
|
+
expect(firstIndexer).not.toBe(secondIndexer)
|
|
39
|
+
expect(first.resolve('searchService')).not.toBe(second.resolve('searchService'))
|
|
40
|
+
expect(first.resolve('searchIndexer')).toBe(firstIndexer)
|
|
41
|
+
const record = { id: 'record', 'cf:secret_a': 'private-a', 'cf:secret_b': 'private-b' }
|
|
42
|
+
await Promise.all([
|
|
43
|
+
firstIndexer.indexRecord({ entityId: 'probe:record', recordId: 'record', tenantId: 'tenant-a', record, customFields: { secret_a: 'private-a', secret_b: 'private-b' } }),
|
|
44
|
+
secondIndexer.indexRecord({ entityId: 'probe:record', recordId: 'record', tenantId: 'tenant-b', record, customFields: { secret_a: 'private-a', secret_b: 'private-b' } }),
|
|
45
|
+
])
|
|
46
|
+
const firstContext = contexts.find((context) => context.tenantId === 'tenant-a')!
|
|
47
|
+
const secondContext = contexts.find((context) => context.tenantId === 'tenant-b')!
|
|
48
|
+
expect(firstContext.container?.resolve('auth')).toBe('first')
|
|
49
|
+
expect(secondContext.container?.resolve('auth')).toBe('second')
|
|
50
|
+
expect(firstContext.customFields).toEqual({ secret_b: 'private-b' })
|
|
51
|
+
expect(secondContext.customFields).toEqual({ secret_a: 'private-a' })
|
|
52
|
+
expect(firstDb.selectFrom).toHaveBeenCalledWith('custom_field_defs')
|
|
53
|
+
expect(secondDb.selectFrom).toHaveBeenCalledWith('custom_field_defs')
|
|
54
|
+
})
|
package/src/di.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { resolveEntityIndexStrategies } from './lib/entity-strategy-policy'
|
|
2
|
-
import { asValue } from 'awilix'
|
|
2
|
+
import { asValue, type AwilixContainer } from 'awilix'
|
|
3
|
+
import { asScopedService } from '@open-mercato/shared/lib/di/process-registrar'
|
|
3
4
|
import type { Kysely } from 'kysely'
|
|
4
5
|
import type { FullTextSearchDriver } from './fulltext/types'
|
|
5
6
|
import { SearchService } from './service'
|
|
@@ -50,6 +51,10 @@ type SearchModuleGlobals = {
|
|
|
50
51
|
[FULLTEXT_DRIVER_KEY]?: FullTextSearchDriver
|
|
51
52
|
}
|
|
52
53
|
|
|
54
|
+
type GenerationFulltextDriverCache = SearchModuleGlobals & {
|
|
55
|
+
fieldPolicyResolver: (entityId: EntityId) => SearchFieldPolicy | undefined
|
|
56
|
+
}
|
|
57
|
+
|
|
53
58
|
function getSearchModuleGlobals(): SearchModuleGlobals {
|
|
54
59
|
return globalThis as unknown as SearchModuleGlobals
|
|
55
60
|
}
|
|
@@ -116,6 +121,46 @@ export function registerSearchModule(
|
|
|
116
121
|
container: SearchContainer,
|
|
117
122
|
options?: SearchModuleOptions,
|
|
118
123
|
): void {
|
|
124
|
+
const services = createSearchServices(container, options)
|
|
125
|
+
container.register({
|
|
126
|
+
searchService: asValue(services.searchService),
|
|
127
|
+
searchStrategies: asValue(services.searchStrategies),
|
|
128
|
+
searchIndexer: asValue(services.searchIndexer),
|
|
129
|
+
})
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function registerScopedSearchModule(root: AwilixContainer, options?: SearchModuleOptions): void {
|
|
133
|
+
const fieldPolicies = new Map<EntityId, SearchFieldPolicy | undefined>()
|
|
134
|
+
for (const moduleConfig of options?.moduleConfigs ?? []) {
|
|
135
|
+
for (const entityConfig of moduleConfig.entities) {
|
|
136
|
+
if (entityConfig.enabled !== false) fieldPolicies.set(entityConfig.entityId as EntityId, entityConfig.fieldPolicy)
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
const fulltextDriverCache: GenerationFulltextDriverCache = {
|
|
140
|
+
fieldPolicyResolver: (entityId) => fieldPolicies.get(entityId),
|
|
141
|
+
}
|
|
142
|
+
const instances = new WeakMap<AwilixContainer, ReturnType<typeof createSearchServices>>()
|
|
143
|
+
const resolveServices = (caller: AwilixContainer) => {
|
|
144
|
+
let services = instances.get(caller)
|
|
145
|
+
if (!services) {
|
|
146
|
+
services = createSearchServices(caller, options, false, fulltextDriverCache)
|
|
147
|
+
instances.set(caller, services)
|
|
148
|
+
}
|
|
149
|
+
return services
|
|
150
|
+
}
|
|
151
|
+
root.register({
|
|
152
|
+
searchService: asScopedService((caller) => resolveServices(caller).searchService),
|
|
153
|
+
searchStrategies: asScopedService((caller) => resolveServices(caller).searchStrategies),
|
|
154
|
+
searchIndexer: asScopedService((caller) => resolveServices(caller).searchIndexer),
|
|
155
|
+
})
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function createSearchServices(
|
|
159
|
+
container: SearchContainer,
|
|
160
|
+
options?: SearchModuleOptions,
|
|
161
|
+
cacheFulltextDriver = true,
|
|
162
|
+
generationFulltextDriverCache?: GenerationFulltextDriverCache,
|
|
163
|
+
) {
|
|
119
164
|
const moduleConfigs = (options?.moduleConfigs ?? []).map((moduleConfig) => ({
|
|
120
165
|
...moduleConfig,
|
|
121
166
|
entities: moduleConfig.entities.map((entityConfig) => {
|
|
@@ -176,8 +221,9 @@ export function registerSearchModule(
|
|
|
176
221
|
// Fulltext strategy (requires driver configuration, e.g., MEILISEARCH_HOST)
|
|
177
222
|
if (!options?.skipFulltext) {
|
|
178
223
|
const excludeEncrypted = shouldExcludeEncryptedFields()
|
|
179
|
-
const singletonCacheEnabled =
|
|
180
|
-
|
|
224
|
+
const singletonCacheEnabled = (cacheFulltextDriver || !!generationFulltextDriverCache)
|
|
225
|
+
&& process.env.SEARCH_DISABLE_SINGLETON_CACHE !== '1'
|
|
226
|
+
const g = generationFulltextDriverCache ?? getSearchModuleGlobals()
|
|
181
227
|
|
|
182
228
|
// The fulltext driver is safe to memoize only when SEARCH_EXCLUDE_ENCRYPTED_FIELDS is
|
|
183
229
|
// OFF — in that case the driver holds no request-scoped Kysely reference. When the flag
|
|
@@ -202,10 +248,10 @@ export function registerSearchModule(
|
|
|
202
248
|
}
|
|
203
249
|
|
|
204
250
|
fulltextDriver = createFulltextDriver({
|
|
205
|
-
fieldPolicyResolver: (entityId: EntityId): SearchFieldPolicy | undefined => {
|
|
251
|
+
fieldPolicyResolver: generationFulltextDriverCache?.fieldPolicyResolver ?? ((entityId: EntityId): SearchFieldPolicy | undefined => {
|
|
206
252
|
const config = entityConfigMap.get(entityId)
|
|
207
253
|
return config?.fieldPolicy
|
|
208
|
-
},
|
|
254
|
+
}),
|
|
209
255
|
encryptionMapResolver,
|
|
210
256
|
})
|
|
211
257
|
|
|
@@ -292,12 +338,7 @@ export function registerSearchModule(
|
|
|
292
338
|
encryptedCustomFieldKeysResolver,
|
|
293
339
|
})
|
|
294
340
|
|
|
295
|
-
|
|
296
|
-
container.register({
|
|
297
|
-
searchService: asValue(searchService),
|
|
298
|
-
searchStrategies: asValue(strategies),
|
|
299
|
-
searchIndexer: asValue(searchIndexer),
|
|
300
|
-
})
|
|
341
|
+
return { searchService, searchStrategies: strategies, searchIndexer }
|
|
301
342
|
}
|
|
302
343
|
|
|
303
344
|
/**
|
package/src/modules/search/di.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { defineProcessDiRegistrar, asScopedService } from '@open-mercato/shared/lib/di/process-registrar'
|
|
1
2
|
import { asValue } from 'awilix'
|
|
2
3
|
import { Pool } from 'pg'
|
|
3
4
|
import type { AppContainer } from '@open-mercato/shared/lib/di/container'
|
|
@@ -158,6 +159,8 @@ export function register(container: AppContainer) {
|
|
|
158
159
|
vectorDrivers: asValue(drivers),
|
|
159
160
|
vectorIndexQueue: asValue(vectorIndexQueue),
|
|
160
161
|
fulltextIndexQueue: asValue(fulltextIndexQueue),
|
|
161
|
-
embeddingProviderProbe:
|
|
162
|
+
embeddingProviderProbe: asScopedService(createEmbeddingProviderProbe),
|
|
162
163
|
})
|
|
163
164
|
}
|
|
165
|
+
|
|
166
|
+
defineProcessDiRegistrar(register)
|