Skip to content

Commit 2a805e1

Browse files
committed
chore(cleanup): share escapeLikePattern from @sim/utils/string
1 parent c484e43 commit 2a805e1

14 files changed

Lines changed: 29 additions & 45 deletions

File tree

‎apps/sim/ee/access-requests/lib/repository.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { permissionAccessRequest, user } from '@sim/db/schema'
2+
import { escapeLikePattern } from '@sim/utils/string'
23
import { and, count, desc, eq, ilike, or, type SQL } from 'drizzle-orm'
34
import {
4-
escapeLikePattern,
55
keysetColumns,
66
keysetPage,
77
listOrderBy,

‎apps/sim/lib/api/list-query.ts‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ export type ListSortOrder = (typeof LIST_SORT_ORDERS)[number]
3232
* Escapes LIKE/ILIKE wildcards so `%`, `_`, and `\` in a caller's term match
3333
* themselves. Postgres treats `\` as the default LIKE escape character, so no
3434
* explicit `ESCAPE` clause is needed.
35-
*
36-
* `lib/table/sql.ts` carries its own copy for the JSONB predicate engine; the
37-
* two are worth folding together, but that module is table-specific and pulls
38-
* the whole column-type registry with it.
3935
*/
4036
export function escapeLikePattern(value: string): string {
4137
return value.replace(/[\\%_]/g, '\\$&')

‎apps/sim/lib/knowledge/application/connectors.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
knowledgeConnectorSyncLog,
1111
} from '@sim/db/schema'
1212
import { toError } from '@sim/utils/errors'
13+
import { escapeLikePattern } from '@sim/utils/string'
1314
import { and, asc, desc, eq, inArray, isNull, lt, or, sql } from 'drizzle-orm'
1415
import type { ConnectorDocumentFilter } from '@/lib/api/contracts/knowledge/connectors'
1516
import type { BillingAttributionSnapshot } from '@/lib/billing/core/billing-attribution'
@@ -105,7 +106,6 @@ import type {
105106
KnowledgeOperationSource,
106107
KnowledgeOrchestrationResult,
107108
} from '@/lib/knowledge/orchestration/shared'
108-
import { escapeLikePattern } from '@/lib/knowledge/tags/utils'
109109
import { credentialProviderMatchesService, type ServiceProviderIdentity } from '@/lib/oauth'
110110
import { ServiceAccountTokenError } from '@/lib/oauth/credential-service'
111111
import { CAPABILITY_RULES, refuseCapability } from '@/lib/permission-groups/capabilities'

‎apps/sim/lib/knowledge/documents/tag-filter.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { document } from '@sim/db/schema'
2+
import { escapeLikePattern } from '@sim/utils/string'
23
import { and, eq, gt, gte, lt, lte, ne, type SQL, sql } from 'drizzle-orm'
3-
import {
4-
buildDateTagCondition,
5-
coerceTagFilterValue,
6-
escapeLikePattern,
7-
} from '@/lib/knowledge/tags/utils'
4+
import { buildDateTagCondition, coerceTagFilterValue } from '@/lib/knowledge/tags/utils'
85

96
/**
107
* A single tag filter applied to a document list query.

‎apps/sim/lib/knowledge/search/tag-filters.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { document, embedding } from '@sim/db/schema'
2+
import { escapeLikePattern } from '@sim/utils/string'
23
import { and, eq, inArray, type SQL, sql } from 'drizzle-orm'
34
import { knowledgeAccessCondition } from '@/lib/knowledge/access/predicate'
45
import { runSearchQuery } from '@/lib/knowledge/search/budget'
@@ -15,7 +16,6 @@ import {
1516
import {
1617
buildDateTagCondition,
1718
coerceTagFilterValue,
18-
escapeLikePattern,
1919
uncompilableTagFilterError,
2020
} from '@/lib/knowledge/tags/utils'
2121
import type { StructuredFilter } from '@/lib/knowledge/types'

‎apps/sim/lib/knowledge/tags/utils.ts‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,6 @@ export function coerceTagFilterValue(
6161
}
6262
}
6363

64-
/**
65-
* Escapes the LIKE metacharacters in a tag filter value so a `%` or `_` a
66-
* caller typed matches itself instead of acting as a wildcard. Both filter
67-
* builders pair this with `ESCAPE '\'`.
68-
*/
69-
export function escapeLikePattern(value: string): string {
70-
return value.replace(/\\/g, '\\\\').replace(/%/g, '\\%').replace(/_/g, '\\_')
71-
}
72-
7364
/**
7465
* Compiles a date tag filter as a half-open range on the raw column, which a
7566
* btree on the slot serves; a `column::date` comparison never can. Date slots

‎apps/sim/lib/mothership/docs/docs-search.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { db } from '@sim/db'
22
import { docsEmbeddings } from '@sim/db/schema'
33
import { createLogger } from '@sim/logger'
4+
import { escapeLikePattern } from '@sim/utils/string'
45
import { and, eq, like, ne, notLike, or, sql } from 'drizzle-orm'
5-
import { escapeLikePattern } from '@/lib/api/list-query'
66
import { DOCS_EMBEDDING_DIMENSIONS } from '@/lib/chunkers/constants'
77
import { OrchestrationError } from '@/lib/core/orchestration/types'
88
import { DEFAULT_EMBEDDING_MODEL } from '@/lib/knowledge/embedding-models'

‎apps/sim/lib/table/rows/service.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { escapeLikePattern } from '@sim/utils/string'
12
/**
23
* Row CRUD + query operations for the table service layer.
34
*
@@ -77,7 +78,6 @@ import {
7778
buildFilterClause,
7879
buildPredicateClause,
7980
buildSortClause,
80-
escapeLikePattern,
8181
uniqueValuePredicate,
8282
} from '@/lib/table/sql'
8383
import { fireTableTrigger } from '@/lib/table/trigger'

‎apps/sim/lib/table/sql.ts‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import { isRecordLike } from '@sim/utils/object'
9-
import { truncate } from '@sim/utils/string'
9+
import { escapeLikePattern, truncate } from '@sim/utils/string'
1010
import type { SQL } from 'drizzle-orm'
1111
import { sql } from 'drizzle-orm'
1212
import { getColumnId } from '@/lib/table/column-keys'
@@ -959,11 +959,6 @@ function buildComparisonClause(
959959
: sql`${cell} ${sql.raw(operator)} ${value}`
960960
}
961961

962-
/** Escapes LIKE/ILIKE wildcard characters so they match literally */
963-
export function escapeLikePattern(value: string): string {
964-
return value.replace(/[\\%_]/g, '\\$&')
965-
}
966-
967962
/**
968963
* General LIKE/ILIKE pattern match (the `like`/`ilike` ops). The caller's `*`
969964
* is the only wildcard — it maps to SQL `%`; any literal `%`/`_`/`\` in the
@@ -981,9 +976,7 @@ function buildPatternClause(
981976
options: { caseInsensitive: boolean; negate?: boolean }
982977
): SQL {
983978
const escapedField = field.replace(/'/g, "''")
984-
const pattern = String(value)
985-
.replace(/[\\%_]/g, '\\$&')
986-
.replace(/\*/g, '%')
979+
const pattern = escapeLikePattern(String(value)).replace(/\*/g, '%')
987980
const cell = sql.raw(`${tableName}.data->>'${escapedField}'`)
988981
const match = options.caseInsensitive
989982
? sql`${cell} ILIKE ${pattern}`

‎apps/sim/lib/webhooks/quickbooks-credentials.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { db } from '@sim/db'
22
import { account, credential } from '@sim/db/schema'
3+
import { escapeLikePattern } from '@sim/utils/string'
34
import { and, eq, like } from 'drizzle-orm'
4-
import { escapeLikePattern } from '@/lib/api/list-query'
55
import {
66
normalizeQuickBooksRealmId,
77
parseQuickBooksAccountId,

0 commit comments

Comments
 (0)