@open-mercato/search 0.5.1-develop.2691.d8a0934b37 → 0.5.1-develop.2694.732417c5ec

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.
Files changed (46) hide show
  1. package/dist/di.js +9 -9
  2. package/dist/di.js.map +2 -2
  3. package/dist/lib/presenter-enricher.js +14 -14
  4. package/dist/lib/presenter-enricher.js.map +2 -2
  5. package/dist/modules/search/api/embeddings/reindex/cancel/route.js +2 -2
  6. package/dist/modules/search/api/embeddings/reindex/cancel/route.js.map +2 -2
  7. package/dist/modules/search/api/embeddings/reindex/route.js +3 -3
  8. package/dist/modules/search/api/embeddings/reindex/route.js.map +2 -2
  9. package/dist/modules/search/api/reindex/cancel/route.js +2 -2
  10. package/dist/modules/search/api/reindex/cancel/route.js.map +2 -2
  11. package/dist/modules/search/api/reindex/route.js +4 -4
  12. package/dist/modules/search/api/reindex/route.js.map +2 -2
  13. package/dist/modules/search/api/settings/route.js +3 -3
  14. package/dist/modules/search/api/settings/route.js.map +2 -2
  15. package/dist/modules/search/lib/reindex-lock.js +20 -17
  16. package/dist/modules/search/lib/reindex-lock.js.map +2 -2
  17. package/dist/modules/search/subscribers/fulltext_upsert.js +2 -2
  18. package/dist/modules/search/subscribers/fulltext_upsert.js.map +2 -2
  19. package/dist/modules/search/subscribers/vector_delete.js +2 -2
  20. package/dist/modules/search/subscribers/vector_delete.js.map +2 -2
  21. package/dist/modules/search/subscribers/vector_upsert.js +2 -2
  22. package/dist/modules/search/subscribers/vector_upsert.js.map +2 -2
  23. package/dist/modules/search/workers/fulltext-index.worker.js +7 -7
  24. package/dist/modules/search/workers/fulltext-index.worker.js.map +2 -2
  25. package/dist/modules/search/workers/vector-index.worker.js +7 -7
  26. package/dist/modules/search/workers/vector-index.worker.js.map +2 -2
  27. package/dist/strategies/token.strategy.js +15 -10
  28. package/dist/strategies/token.strategy.js.map +2 -2
  29. package/jest.config.cjs +4 -2
  30. package/package.json +4 -4
  31. package/src/__tests__/presenter-enricher.test.ts +17 -60
  32. package/src/__tests__/workers.test.ts +20 -21
  33. package/src/di.ts +22 -21
  34. package/src/lib/presenter-enricher.ts +21 -20
  35. package/src/modules/search/api/embeddings/reindex/cancel/route.ts +4 -3
  36. package/src/modules/search/api/embeddings/reindex/route.ts +5 -4
  37. package/src/modules/search/api/reindex/cancel/route.ts +4 -3
  38. package/src/modules/search/api/reindex/route.ts +5 -5
  39. package/src/modules/search/api/settings/route.ts +5 -4
  40. package/src/modules/search/lib/reindex-lock.ts +50 -32
  41. package/src/modules/search/subscribers/fulltext_upsert.ts +6 -2
  42. package/src/modules/search/subscribers/vector_delete.ts +6 -2
  43. package/src/modules/search/subscribers/vector_upsert.ts +6 -2
  44. package/src/modules/search/workers/fulltext-index.worker.ts +10 -9
  45. package/src/modules/search/workers/vector-index.worker.ts +10 -9
  46. package/src/strategies/token.strategy.ts +25 -19
@@ -1,4 +1,4 @@
1
- import type { Knex } from 'knex'
1
+ import { type Kysely, sql, type SqlBool } from 'kysely'
2
2
  import type {
3
3
  SearchStrategy,
4
4
  SearchStrategyId,
@@ -34,7 +34,7 @@ export class TokenSearchStrategy implements SearchStrategy {
34
34
  private readonly defaultLimit: number
35
35
 
36
36
  constructor(
37
- private readonly knex: Knex,
37
+ private readonly db: Kysely<any>,
38
38
  config?: TokenStrategyConfig,
39
39
  ) {
40
40
  this.minMatchRatio = config?.minMatchRatio ?? 0.5
@@ -63,25 +63,29 @@ export class TokenSearchStrategy implements SearchStrategy {
63
63
  const minMatches = Math.max(1, Math.ceil(hashes.length * this.minMatchRatio))
64
64
  const limit = options.limit ?? this.defaultLimit
65
65
 
66
- let queryBuilder = this.knex('search_tokens')
67
- .select('entity_type', 'entity_id')
68
- .count('* as match_count')
69
- .whereIn('token_hash', hashes)
70
- .where('tenant_id', options.tenantId)
71
- .groupBy('entity_type', 'entity_id')
72
- .havingRaw('COUNT(DISTINCT token_hash) >= ?', [minMatches])
73
- .orderByRaw('COUNT(DISTINCT token_hash) DESC')
66
+ let queryBuilder = this.db
67
+ .selectFrom('search_tokens' as any)
68
+ .select([
69
+ 'entity_type' as any,
70
+ 'entity_id' as any,
71
+ sql<string>`count(*)`.as('match_count'),
72
+ ])
73
+ .where('token_hash' as any, 'in', hashes)
74
+ .where('tenant_id' as any, '=', options.tenantId)
75
+ .groupBy(['entity_type' as any, 'entity_id' as any])
76
+ .having(sql<SqlBool>`count(distinct token_hash) >= ${minMatches}`)
77
+ .orderBy(sql`count(distinct token_hash) desc`)
74
78
  .limit(limit)
75
79
 
76
80
  if (options.organizationId) {
77
- queryBuilder = queryBuilder.where('organization_id', options.organizationId)
81
+ queryBuilder = queryBuilder.where('organization_id' as any, '=', options.organizationId)
78
82
  }
79
83
 
80
84
  if (options.entityTypes?.length) {
81
- queryBuilder = queryBuilder.whereIn('entity_type', options.entityTypes)
85
+ queryBuilder = queryBuilder.where('entity_type' as any, 'in', options.entityTypes)
82
86
  }
83
87
 
84
- const rows = await queryBuilder as Array<{ entity_type: string; entity_id: string; match_count: string | number }>
88
+ const rows = await queryBuilder.execute() as Array<{ entity_type: string; entity_id: string; match_count: string | number }>
85
89
 
86
90
  return rows.map((row) => {
87
91
  const matchCount = typeof row.match_count === 'string'
@@ -105,7 +109,7 @@ export class TokenSearchStrategy implements SearchStrategy {
105
109
  '@open-mercato/core/modules/query_index/lib/search-tokens'
106
110
  )
107
111
 
108
- await replaceSearchTokensForRecord(this.knex, {
112
+ await replaceSearchTokensForRecord(this.db, {
109
113
  entityType: record.entityId,
110
114
  recordId: record.recordId,
111
115
  tenantId: record.tenantId,
@@ -120,7 +124,7 @@ export class TokenSearchStrategy implements SearchStrategy {
120
124
  '@open-mercato/core/modules/query_index/lib/search-tokens'
121
125
  )
122
126
 
123
- await deleteSearchTokensForRecord(this.knex, {
127
+ await deleteSearchTokensForRecord(this.db, {
124
128
  entityType: entityId,
125
129
  recordId,
126
130
  tenantId,
@@ -142,12 +146,14 @@ export class TokenSearchStrategy implements SearchStrategy {
142
146
  doc: record.fields as Record<string, unknown>,
143
147
  }))
144
148
 
145
- await replaceSearchTokensForBatch(this.knex, payloads)
149
+ await replaceSearchTokensForBatch(this.db, payloads)
146
150
  }
147
151
 
148
152
  async purge(entityId: EntityId, tenantId: string): Promise<void> {
149
- await this.knex('search_tokens')
150
- .where({ entity_type: entityId, tenant_id: tenantId })
151
- .del()
153
+ await this.db
154
+ .deleteFrom('search_tokens' as any)
155
+ .where('entity_type' as any, '=', entityId)
156
+ .where('tenant_id' as any, '=', tenantId)
157
+ .execute()
152
158
  }
153
159
  }