@teleporthq/teleport-plugin-next-data-source 0.44.11 → 0.44.13
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/__tests__/browser-row-policy.test.ts +6 -6
- package/__tests__/fetchers.test.ts +1 -1
- package/__tests__/items-path.test.ts +3 -2
- package/__tests__/search-relevance.test.ts +407 -0
- package/dist/cjs/fetchers/csv-file.d.ts.map +1 -1
- package/dist/cjs/fetchers/csv-file.js +4 -3
- package/dist/cjs/fetchers/csv-file.js.map +1 -1
- package/dist/cjs/fetchers/google-sheets.d.ts.map +1 -1
- package/dist/cjs/fetchers/google-sheets.js +2 -1
- package/dist/cjs/fetchers/google-sheets.js.map +1 -1
- package/dist/cjs/fetchers/javascript.d.ts.map +1 -1
- package/dist/cjs/fetchers/javascript.js +2 -1
- package/dist/cjs/fetchers/javascript.js.map +1 -1
- package/dist/cjs/fetchers/mariadb.d.ts.map +1 -1
- package/dist/cjs/fetchers/mariadb.js +4 -3
- package/dist/cjs/fetchers/mariadb.js.map +1 -1
- package/dist/cjs/fetchers/mysql.d.ts.map +1 -1
- package/dist/cjs/fetchers/mysql.js +3 -2
- package/dist/cjs/fetchers/mysql.js.map +1 -1
- package/dist/cjs/fetchers/postgresql.d.ts.map +1 -1
- package/dist/cjs/fetchers/postgresql.js +4 -3
- package/dist/cjs/fetchers/postgresql.js.map +1 -1
- package/dist/cjs/fetchers/redshift.d.ts.map +1 -1
- package/dist/cjs/fetchers/redshift.js +3 -2
- package/dist/cjs/fetchers/redshift.js.map +1 -1
- package/dist/cjs/fetchers/rest-api.d.ts.map +1 -1
- package/dist/cjs/fetchers/rest-api.js +3 -2
- package/dist/cjs/fetchers/rest-api.js.map +1 -1
- package/dist/cjs/fetchers/supabase.d.ts.map +1 -1
- package/dist/cjs/fetchers/supabase.js +3 -2
- package/dist/cjs/fetchers/supabase.js.map +1 -1
- package/dist/cjs/fetchers/teleport.d.ts.map +1 -1
- package/dist/cjs/fetchers/teleport.js +5 -4
- package/dist/cjs/fetchers/teleport.js.map +1 -1
- package/dist/cjs/fetchers/turso.d.ts.map +1 -1
- package/dist/cjs/fetchers/turso.js +2 -1
- package/dist/cjs/fetchers/turso.js.map +1 -1
- package/dist/cjs/search-relevance.d.ts +81 -0
- package/dist/cjs/search-relevance.d.ts.map +1 -0
- package/dist/cjs/search-relevance.js +119 -0
- package/dist/cjs/search-relevance.js.map +1 -0
- package/dist/cjs/tsconfig.tsbuildinfo +1 -1
- package/dist/esm/fetchers/csv-file.d.ts.map +1 -1
- package/dist/esm/fetchers/csv-file.js +4 -3
- package/dist/esm/fetchers/csv-file.js.map +1 -1
- package/dist/esm/fetchers/google-sheets.d.ts.map +1 -1
- package/dist/esm/fetchers/google-sheets.js +2 -1
- package/dist/esm/fetchers/google-sheets.js.map +1 -1
- package/dist/esm/fetchers/javascript.d.ts.map +1 -1
- package/dist/esm/fetchers/javascript.js +2 -1
- package/dist/esm/fetchers/javascript.js.map +1 -1
- package/dist/esm/fetchers/mariadb.d.ts.map +1 -1
- package/dist/esm/fetchers/mariadb.js +4 -3
- package/dist/esm/fetchers/mariadb.js.map +1 -1
- package/dist/esm/fetchers/mysql.d.ts.map +1 -1
- package/dist/esm/fetchers/mysql.js +3 -2
- package/dist/esm/fetchers/mysql.js.map +1 -1
- package/dist/esm/fetchers/postgresql.d.ts.map +1 -1
- package/dist/esm/fetchers/postgresql.js +4 -3
- package/dist/esm/fetchers/postgresql.js.map +1 -1
- package/dist/esm/fetchers/redshift.d.ts.map +1 -1
- package/dist/esm/fetchers/redshift.js +3 -2
- package/dist/esm/fetchers/redshift.js.map +1 -1
- package/dist/esm/fetchers/rest-api.d.ts.map +1 -1
- package/dist/esm/fetchers/rest-api.js +3 -2
- package/dist/esm/fetchers/rest-api.js.map +1 -1
- package/dist/esm/fetchers/supabase.d.ts.map +1 -1
- package/dist/esm/fetchers/supabase.js +3 -2
- package/dist/esm/fetchers/supabase.js.map +1 -1
- package/dist/esm/fetchers/teleport.d.ts.map +1 -1
- package/dist/esm/fetchers/teleport.js +5 -4
- package/dist/esm/fetchers/teleport.js.map +1 -1
- package/dist/esm/fetchers/turso.d.ts.map +1 -1
- package/dist/esm/fetchers/turso.js +2 -1
- package/dist/esm/fetchers/turso.js.map +1 -1
- package/dist/esm/search-relevance.d.ts +81 -0
- package/dist/esm/search-relevance.d.ts.map +1 -0
- package/dist/esm/search-relevance.js +113 -0
- package/dist/esm/search-relevance.js.map +1 -0
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/fetchers/csv-file.ts +17 -11
- package/src/fetchers/google-sheets.ts +15 -12
- package/src/fetchers/javascript.ts +12 -12
- package/src/fetchers/mariadb.ts +21 -26
- package/src/fetchers/mysql.ts +21 -26
- package/src/fetchers/postgresql.ts +31 -26
- package/src/fetchers/redshift.ts +18 -17
- package/src/fetchers/rest-api.ts +11 -11
- package/src/fetchers/supabase.ts +27 -20
- package/src/fetchers/teleport.ts +31 -26
- package/src/fetchers/turso.ts +18 -17
- package/src/search-relevance.ts +230 -0
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How a list's search box narrows and orders the rows of a generated route.
|
|
3
|
+
*
|
|
4
|
+
* The typed text is split into terms at white space, letter case aside, and
|
|
5
|
+
* the punctuation around a term is dropped ("form?" looks for "form",
|
|
6
|
+
* "\"create" for "create"). Every term must appear in one of the searched
|
|
7
|
+
* columns, so "create form" finds "How to create a form".
|
|
8
|
+
*
|
|
9
|
+
* Where the stored text is read the same way as the typed text (the
|
|
10
|
+
* TeleportHQ database and other Postgres sources, and the routes that search
|
|
11
|
+
* rows in memory), punctuation inside a term counts as a space on both sides
|
|
12
|
+
* ("e-commerce" finds "E-Commerce", "what's" finds "what’s"), and HTML tags
|
|
13
|
+
* and entities count as spaces too, so a word that is only in a rich-text
|
|
14
|
+
* body's markup ("strong", "href", "amp") never matches.
|
|
15
|
+
*
|
|
16
|
+
* The matches are ordered column by column, in the order the list searches
|
|
17
|
+
* them: by how much of the typed text the first column (a title) holds, then
|
|
18
|
+
* the second (a summary), and so on. A term counts 3 as a whole word, 2 at
|
|
19
|
+
* the start of a word ("form" in "forms") and 1 inside one ("form" in
|
|
20
|
+
* "platform"). The list's own sort orders the rows that score alike.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Characters a search reads as a space: punctuation, and line breaks and
|
|
25
|
+
* tabs, so a word at the end of a line is still a whole word.
|
|
26
|
+
*/
|
|
27
|
+
export const SEARCH_PUNCTUATION =
|
|
28
|
+
'\t\n\v\f\r!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~\u00a0\u00a1\u00ab\u00b7\u00bb\u00bf' +
|
|
29
|
+
'\u2010\u2011\u2012\u2013\u2014\u2015\u2018\u2019\u201a\u201b\u201c\u201d\u201e\u2022\u2026\u2039\u203a'
|
|
30
|
+
|
|
31
|
+
/** A JavaScript string literal that survives being pasted into any generated file. */
|
|
32
|
+
const jsString = (value: string): string =>
|
|
33
|
+
JSON.stringify(value).replace(
|
|
34
|
+
/[\u007f-\uffff`$]/g,
|
|
35
|
+
(char) => '\\u' + ('0000' + char.charCodeAt(0).toString(16)).slice(-4)
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* One Postgres regular expression, as a string literal, for everything a
|
|
40
|
+
* normalised search reads as a space: a tag, an entity, or one of
|
|
41
|
+
* SEARCH_PUNCTUATION. The bracket expression puts `]` first and `-` last,
|
|
42
|
+
* escapes the backslash, and puts `[` before `-` so it never opens a class.
|
|
43
|
+
* One pass: `translate()` is several times slower over long multibyte text.
|
|
44
|
+
*/
|
|
45
|
+
const PG_SPACES_REGEX_SQL = `'${(
|
|
46
|
+
'<[^>]*>|&[a-z0-9#]+;|[]' +
|
|
47
|
+
SEARCH_PUNCTUATION.split('')
|
|
48
|
+
.filter((char) => !']-\\['.includes(char))
|
|
49
|
+
.join('') +
|
|
50
|
+
'\\\\[-]'
|
|
51
|
+
).replace(/'/g, "''")}'`
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* `searchTerms(query)`: the typed terms, lowercased, each once. `normalised`
|
|
55
|
+
* also reads the punctuation inside a term as a space, for the routes whose
|
|
56
|
+
* stored text is read the same way; otherwise only the punctuation around a
|
|
57
|
+
* term is dropped, so "e-commerce" still finds the raw text "e-commerce".
|
|
58
|
+
*/
|
|
59
|
+
export const generateSearchTermsCode = (
|
|
60
|
+
normalised: boolean
|
|
61
|
+
): string => `const SEARCH_PUNCTUATION = ${jsString(SEARCH_PUNCTUATION)}
|
|
62
|
+
const searchPunctuationToSpaces = (text) => {
|
|
63
|
+
let out = ''
|
|
64
|
+
for (const char of text) out += SEARCH_PUNCTUATION.indexOf(char) === -1 ? char : ' '
|
|
65
|
+
return out
|
|
66
|
+
}
|
|
67
|
+
const searchTerms = (query) => {
|
|
68
|
+
const terms = []
|
|
69
|
+
String(query == null ? '' : query).toLowerCase().split(/\\s+/).forEach((token) => {
|
|
70
|
+
const spaced = searchPunctuationToSpaces(token)
|
|
71
|
+
${
|
|
72
|
+
normalised
|
|
73
|
+
? ` const term = spaced.trim().replace(/\\s+/g, ' ')`
|
|
74
|
+
: ` const start = spaced.length - spaced.trimStart().length
|
|
75
|
+
const term = token.slice(start, start + spaced.trim().length)`
|
|
76
|
+
}
|
|
77
|
+
if (term && terms.indexOf(term) === -1) terms.push(term)
|
|
78
|
+
})
|
|
79
|
+
return terms
|
|
80
|
+
}`
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* How one SQL dialect reads a column for a search. `textSql` is the source
|
|
84
|
+
* of `(column) => sql`, the column's text as the search reads it: lower case,
|
|
85
|
+
* with a space at each end so a whole word and a word start can be told
|
|
86
|
+
* apart. `likeSql` is the source of `(text, placeholder, before, after) =>
|
|
87
|
+
* sql`, testing `text LIKE before + term + after` with the term bound to the
|
|
88
|
+
* placeholder. `normalised` dialects also read tags, entities and punctuation
|
|
89
|
+
* in the stored text as spaces; `lateral` ones work each column's text out
|
|
90
|
+
* once per row, in a LATERAL subquery the route joins after its table.
|
|
91
|
+
*/
|
|
92
|
+
export interface SearchSqlDialect {
|
|
93
|
+
textSql: string
|
|
94
|
+
likeSql: string
|
|
95
|
+
normalised: boolean
|
|
96
|
+
lateral: boolean
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const PG_LIKE_SQL = `(text, placeholder, before, after) => text + " LIKE ('" + before + "' || " + placeholder + "::text || '" + after + "') ESCAPE '|'"`
|
|
100
|
+
|
|
101
|
+
export const PG_SEARCH_SQL: SearchSqlDialect = {
|
|
102
|
+
textSql: `(column) => "(' ' || regexp_replace(lower(\\"" + column + "\\"::text), " + SEARCH_SPACES_REGEX_SQL + ", ' ', 'g') || ' ')"`,
|
|
103
|
+
likeSql: PG_LIKE_SQL,
|
|
104
|
+
normalised: true,
|
|
105
|
+
lateral: true,
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export const REDSHIFT_SEARCH_SQL: SearchSqlDialect = {
|
|
109
|
+
textSql: `(column) => "(' ' || lower(\\"" + column + "\\"::text) || ' ')"`,
|
|
110
|
+
likeSql: PG_LIKE_SQL,
|
|
111
|
+
normalised: false,
|
|
112
|
+
lateral: false,
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const CONCAT_LIKE_SQL = `(text, placeholder, before, after) => text + " LIKE CONCAT('" + before + "', " + placeholder + ", '" + after + "') ESCAPE '|'"`
|
|
116
|
+
|
|
117
|
+
export const MYSQL_SEARCH_SQL: SearchSqlDialect = {
|
|
118
|
+
textSql: `(column) => "CONCAT(' ', LOWER(CAST(" + mysql.escapeId(column) + " AS CHAR)), ' ')"`,
|
|
119
|
+
likeSql: CONCAT_LIKE_SQL,
|
|
120
|
+
normalised: false,
|
|
121
|
+
lateral: false,
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export const MARIADB_SEARCH_SQL: SearchSqlDialect = {
|
|
125
|
+
textSql: `(column) => "CONCAT(' ', LOWER(CAST(\\\`" + column + "\\\` AS CHAR)), ' ')"`,
|
|
126
|
+
likeSql: CONCAT_LIKE_SQL,
|
|
127
|
+
normalised: false,
|
|
128
|
+
lateral: false,
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export const SQLITE_SEARCH_SQL: SearchSqlDialect = {
|
|
132
|
+
textSql: `(column) => "(' ' || LOWER(CAST(\\"" + column + "\\" AS TEXT)) || ' ')"`,
|
|
133
|
+
likeSql: `(text, placeholder, before, after) => text + " LIKE ('" + before + "' || " + placeholder + " || '" + after + "') ESCAPE '|'"`,
|
|
134
|
+
normalised: false,
|
|
135
|
+
lateral: false,
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* `buildSearchSql(query, columns, firstIndex)` for the SQL routes:
|
|
140
|
+
* `{ where, params, from, rank, rankParams, nextIndex }`, or null when there
|
|
141
|
+
* is nothing to search. `from` is the LATERAL join a `lateral` dialect's
|
|
142
|
+
* route appends after its table (selecting `table.*`, so the worked-out
|
|
143
|
+
* texts never reach a row), '' otherwise. `rank` is the ORDER BY keys, one
|
|
144
|
+
* per searched column, best first.
|
|
145
|
+
*
|
|
146
|
+
* With `firstIndex` the placeholders are numbered (`$n`) and each term is
|
|
147
|
+
* bound once, the rank reusing it; without it they are positional (`?`),
|
|
148
|
+
* every occurrence binds its own copy, and `rankParams` go after the
|
|
149
|
+
* parameters of the whole WHERE, as the ORDER BY comes after it.
|
|
150
|
+
*
|
|
151
|
+
* Uses `escapeLikePattern` and `sanitizeSearchIdentifier`
|
|
152
|
+
* (`generateSearchEscapeHelpersCode`).
|
|
153
|
+
*/
|
|
154
|
+
export const generateSearchSqlHelpersCode = (
|
|
155
|
+
dialect: SearchSqlDialect
|
|
156
|
+
): string => `${generateSearchTermsCode(dialect.normalised)}
|
|
157
|
+
${
|
|
158
|
+
dialect.normalised ? `const SEARCH_SPACES_REGEX_SQL = ${jsString(PG_SPACES_REGEX_SQL)}\n` : ''
|
|
159
|
+
}const searchTextSql = ${dialect.textSql}
|
|
160
|
+
const searchLikeSql = ${dialect.likeSql}
|
|
161
|
+
const buildSearchSql = (query, columns, firstIndex) => {
|
|
162
|
+
const terms = searchTerms(query)
|
|
163
|
+
const searched = []
|
|
164
|
+
;(Array.isArray(columns) ? columns : []).forEach((column) => {
|
|
165
|
+
const name = sanitizeSearchIdentifier(column)
|
|
166
|
+
if (searched.indexOf(name) === -1) searched.push(name)
|
|
167
|
+
})
|
|
168
|
+
if (terms.length === 0 || searched.length === 0) return null
|
|
169
|
+
const patterns = terms.map((term) => escapeLikePattern(term))
|
|
170
|
+
const numbered = typeof firstIndex === 'number'
|
|
171
|
+
const params = numbered ? patterns.slice() : []
|
|
172
|
+
const rankParams = []
|
|
173
|
+
const placeholder = (termIndex, bound) => {
|
|
174
|
+
if (numbered) return '$' + (firstIndex + termIndex)
|
|
175
|
+
bound.push(patterns[termIndex])
|
|
176
|
+
return '?'
|
|
177
|
+
}
|
|
178
|
+
const texts = searched.map((column, position) => ${
|
|
179
|
+
dialect.lateral ? `'"__tq_search"."__tq_search_' + position + '"'` : 'searchTextSql(column)'
|
|
180
|
+
})
|
|
181
|
+
const from = ${
|
|
182
|
+
dialect.lateral
|
|
183
|
+
? `' CROSS JOIN LATERAL (SELECT ' + searched.map((column, position) => searchTextSql(column) + ' AS "__tq_search_' + position + '"').join(', ') + ' OFFSET 0) AS "__tq_search"'`
|
|
184
|
+
: `''`
|
|
185
|
+
}
|
|
186
|
+
const where = '(' + patterns.map((pattern, termIndex) =>
|
|
187
|
+
'(' + texts.map((text) => searchLikeSql(text, placeholder(termIndex, params), '%', '%')).join(' OR ') + ')'
|
|
188
|
+
).join(' AND ') + ')'
|
|
189
|
+
const rank = texts.map((text) => '(' + patterns.map((pattern, termIndex) =>
|
|
190
|
+
'CASE WHEN ' + searchLikeSql(text, placeholder(termIndex, rankParams), '% ', ' %') + ' THEN 3' +
|
|
191
|
+
' WHEN ' + searchLikeSql(text, placeholder(termIndex, rankParams), '% ', '%') + ' THEN 2' +
|
|
192
|
+
' WHEN ' + searchLikeSql(text, placeholder(termIndex, rankParams), '%', '%') + ' THEN 1 ELSE 0 END'
|
|
193
|
+
).join(' + ') + ') DESC').join(', ')
|
|
194
|
+
return { where, params, from, rank, rankParams, nextIndex: numbered ? firstIndex + patterns.length : firstIndex }
|
|
195
|
+
}`
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* For the routes that search rows in memory: `matchesSearch(row, query,
|
|
199
|
+
* columns, valueOf)`, true when every term is in one of the columns, and
|
|
200
|
+
* `rankBySearch(rows, query, columns, valueOf)`, the same order as the SQL
|
|
201
|
+
* routes' rank, applied after the list's sort (a stable sort keeps it among
|
|
202
|
+
* rows that score alike) and before the page is cut. Stored text is read
|
|
203
|
+
* the normalised way: tags, entities and punctuation as spaces.
|
|
204
|
+
*/
|
|
205
|
+
export const generateSearchRankHelperCode = (): string => `${generateSearchTermsCode(true)}
|
|
206
|
+
const searchText = (value) => {
|
|
207
|
+
const text = value === null || value === undefined ? '' : typeof value === 'object' ? JSON.stringify(value) : String(value)
|
|
208
|
+
return ' ' + searchPunctuationToSpaces(text.toLowerCase().replace(/<[^>]*>|&[a-z0-9#]+;/g, ' ')) + ' '
|
|
209
|
+
}
|
|
210
|
+
const searchScore = (text, terms) => terms.reduce((score, term) =>
|
|
211
|
+
score + (text.indexOf(' ' + term + ' ') !== -1 ? 3 : text.indexOf(' ' + term) !== -1 ? 2 : text.indexOf(term) !== -1 ? 1 : 0), 0)
|
|
212
|
+
const matchesSearch = (row, query, columns, valueOf) => {
|
|
213
|
+
const terms = searchTerms(query)
|
|
214
|
+
if (terms.length === 0) return true
|
|
215
|
+
const texts = (Array.isArray(columns) ? columns : []).map((column) => searchText(valueOf(row, column)))
|
|
216
|
+
return terms.every((term) => texts.some((text) => text.indexOf(term) !== -1))
|
|
217
|
+
}
|
|
218
|
+
const rankBySearch = (rows, query, columns, valueOf) => {
|
|
219
|
+
const terms = searchTerms(query)
|
|
220
|
+
if (!Array.isArray(columns) || columns.length === 0 || terms.length === 0) return rows
|
|
221
|
+
return rows
|
|
222
|
+
.map((row, index) => ({ row, index, scores: columns.map((column) => searchScore(searchText(valueOf(row, column)), terms)) }))
|
|
223
|
+
.sort((a, b) => {
|
|
224
|
+
for (let position = 0; position < a.scores.length; position++) {
|
|
225
|
+
if (a.scores[position] !== b.scores[position]) return b.scores[position] - a.scores[position]
|
|
226
|
+
}
|
|
227
|
+
return a.index - b.index
|
|
228
|
+
})
|
|
229
|
+
.map((entry) => entry.row)
|
|
230
|
+
}`
|