Skip to content

Commit 18ca138

Browse files
committed
feat: 🎸 Add ability to search other resources as well
1 parent c84ad0b commit 18ca138

File tree

6 files changed

+179
-32
lines changed

6 files changed

+179
-32
lines changed

‎addons/api/addon/utils/sqlite-query.js‎

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,25 +211,69 @@ function addSearchConditions({
211211
if (!search || !modelMapping[resource]) {
212212
return;
213213
}
214+
const searchSelect = search?.select ?? 'rowid';
215+
const searchSql = `SELECT ${searchSelect} FROM ${tableName}_fts WHERE ${tableName}_fts MATCH ?`;
216+
const getParameter = (fields, text) =>
217+
fields?.length > 0
218+
? or(fields.map((field) => `${field}:"${text}"*`))
219+
: `"${text}"*`;
214220

215221
// Use the special prefix indicator "*" for full-text search
216222
if (typeOf(search) === 'object') {
217-
if (!search?.text) {
223+
if (!search.text) {
218224
return;
219225
}
220226

221-
parameters.push(
222-
or(search.fields.map((field) => `${field}:"${search.text}"*`)),
223-
);
227+
const parameter = getParameter(search.fields, search.text);
228+
parameters.push(parameter);
224229
} else {
225230
parameters.push(`"${search}"*`);
226231
}
227232

233+
// If there are extra related searches on other tables, add them too
234+
if (search?.relatedSearches?.length > 0) {
235+
const relatedSearchQueries = [];
236+
237+
search.relatedSearches.forEach((relatedSearch) => {
238+
const { resource: relatedResource, fields, join } = relatedSearch;
239+
const relatedTableName = underscore(relatedResource);
240+
241+
const parameter = getParameter(fields, search.text);
242+
parameters.push(parameter);
243+
244+
// Build the related FTS query with optional join
245+
let relatedQuery = [];
246+
relatedQuery.push(
247+
`SELECT "${tableName}".${searchSelect} FROM ${relatedTableName}_fts`,
248+
);
249+
250+
if (join) {
251+
const { joinFrom = 'id', joinOn } = join;
252+
relatedQuery.push(
253+
`JOIN "${tableName}" ON "${tableName}".${joinFrom} = ${relatedResource}_fts.${joinOn}`,
254+
);
255+
}
256+
relatedQuery.push(`WHERE ${relatedTableName}_fts MATCH ?`);
257+
258+
relatedSearchQueries.push(relatedQuery.join(' '));
259+
});
260+
261+
// Add the original search first since we've already added the original parameter first
262+
const conditionStatement = [searchSql, ...relatedSearchQueries].join(
263+
'\nUNION ',
264+
);
265+
conditions.push(
266+
`"${tableName}".${searchSelect} IN (${conditionStatement})`,
267+
);
268+
269+
return;
270+
}
271+
228272
// Use a subquery to match against the FTS table with rowids as SQLite is
229273
// much more efficient with FTS queries when using rowids or MATCH (or both).
230274
// We could have also used a join here but a subquery is simpler.
231275
conditions.push(
232-
`"${tableName}".rowid IN (SELECT rowid FROM ${tableName}_fts WHERE ${tableName}_fts MATCH ?)`,
276+
`"${tableName}".${search?.select ?? 'rowid'} IN (${searchSql})`,
233277
);
234278
}
235279

‎addons/api/addon/workers/sqlite-worker.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
// See "Maximum Number Of Host Parameters In A Single SQL Statement" in
2929
// https://www.sqlite.org/limits.html
3030
const MAX_HOST_PARAMETERS = 32766;
31-
const SCHEMA_VERSION = 1;
31+
const SCHEMA_VERSION = 2;
3232

3333
// Some browsers do not allow calling getDirectory in private browsing modes even
3434
// if we're in a secure context. This will cause the SQLite setup to fail so we should

‎addons/api/addon/workers/utils/schema.js‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ CREATE VIRTUAL TABLE IF NOT EXISTS target_fts USING fts5(
3535
address,
3636
scope_id,
3737
created_time,
38-
content='',
38+
content='target',
3939
);
4040
4141
-- Create triggers to keep the FTS table in sync with the target table
@@ -68,6 +68,7 @@ CREATE TABLE IF NOT EXISTS alias (
6868
data TEXT NOT NULL
6969
);
7070
CREATE INDEX IF NOT EXISTS idx_alias_created_time ON alias(created_time DESC);
71+
CREATE INDEX IF NOT EXISTS idx_alias_destination_id ON alias(destination_id);
7172
7273
CREATE VIRTUAL TABLE IF NOT EXISTS alias_fts USING fts5(
7374
id,
@@ -78,7 +79,7 @@ CREATE VIRTUAL TABLE IF NOT EXISTS alias_fts USING fts5(
7879
value,
7980
scope_id,
8081
created_time,
81-
content='',
82+
content='alias',
8283
);
8384
8485
CREATE TRIGGER IF NOT EXISTS alias_ai AFTER INSERT ON alias BEGIN
@@ -111,7 +112,7 @@ CREATE VIRTUAL TABLE IF NOT EXISTS group_fts USING fts5(
111112
description,
112113
scope_id,
113114
created_time,
114-
content='',
115+
content='group',
115116
);
116117
117118
CREATE TRIGGER IF NOT EXISTS group_ai AFTER INSERT ON "group" BEGIN
@@ -144,7 +145,7 @@ CREATE VIRTUAL TABLE IF NOT EXISTS role_fts USING fts5(
144145
description,
145146
scope_id,
146147
created_time,
147-
content='',
148+
content='role',
148149
);
149150
150151
CREATE TRIGGER IF NOT EXISTS role_ai AFTER INSERT ON role BEGIN
@@ -177,7 +178,7 @@ CREATE VIRTUAL TABLE IF NOT EXISTS user_fts USING fts5(
177178
description,
178179
scope_id,
179180
created_time,
180-
content='',
181+
content='user',
181182
);
182183
183184
CREATE TRIGGER IF NOT EXISTS user_ai AFTER INSERT ON user BEGIN
@@ -212,7 +213,7 @@ CREATE VIRTUAL TABLE IF NOT EXISTS credential_store_fts USING fts5(
212213
description,
213214
scope_id,
214215
created_time,
215-
content='',
216+
content='credential_store',
216217
);
217218
218219
CREATE TRIGGER IF NOT EXISTS credential_store_ai AFTER INSERT ON credential_store BEGIN
@@ -247,7 +248,7 @@ CREATE VIRTUAL TABLE IF NOT EXISTS scope_fts USING fts5(
247248
description,
248249
scope_id,
249250
created_time,
250-
content='',
251+
content='scope',
251252
);
252253
253254
CREATE TRIGGER IF NOT EXISTS scope_ai AFTER INSERT ON scope BEGIN
@@ -284,7 +285,7 @@ CREATE VIRTUAL TABLE IF NOT EXISTS auth_method_fts USING fts5(
284285
is_primary,
285286
scope_id,
286287
created_time,
287-
content='',
288+
content='auth_method',
288289
);
289290
290291
CREATE TRIGGER IF NOT EXISTS auth_method_ai AFTER INSERT ON auth_method BEGIN
@@ -321,7 +322,7 @@ CREATE VIRTUAL TABLE IF NOT EXISTS host_catalog_fts USING fts5(
321322
plugin_name,
322323
scope_id,
323324
created_time,
324-
content='',
325+
content='host_catalog',
325326
);
326327
327328
CREATE TRIGGER IF NOT EXISTS host_catalog_ai AFTER INSERT ON host_catalog BEGIN
@@ -374,7 +375,7 @@ CREATE VIRTUAL TABLE IF NOT EXISTS session_recording_fts USING fts5(
374375
target_scope_name,
375376
target_scope_parent_scope_id,
376377
created_time,
377-
content='',
378+
content='session_recording',
378379
);
379380
380381
CREATE TRIGGER IF NOT EXISTS session_recording_ai AFTER INSERT ON session_recording BEGIN
@@ -413,7 +414,7 @@ CREATE VIRTUAL TABLE IF NOT EXISTS session_fts USING fts5(
413414
user_id,
414415
scope_id,
415416
created_time,
416-
content='',
417+
content='session',
417418
);
418419
419420
CREATE TRIGGER IF NOT EXISTS session_ai AFTER INSERT ON session BEGIN

‎addons/api/tests/unit/utils/sqlite-query-test.js‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,64 @@ module('Unit | Utility | sqlite-query', function (hooks) {
419419
ORDER BY "target".created_time DESC`,
420420
expectedParams: ['name:"favorite"* OR description:"favorite"*'],
421421
},
422+
'search with related searches': {
423+
query: {
424+
search: {
425+
text: 'dev',
426+
select: 'id',
427+
relatedSearches: [
428+
{
429+
resource: 'alias',
430+
fields: ['name', 'description'],
431+
join: {
432+
joinOn: 'destination_id',
433+
joinFrom: 'id',
434+
},
435+
},
436+
],
437+
},
438+
},
439+
expectedSql: `
440+
SELECT * FROM "target"
441+
WHERE "target".id IN (SELECT id FROM target_fts WHERE target_fts MATCH ?
442+
UNION SELECT "target".id FROM alias_fts JOIN "target" ON "target".id = alias_fts.destination_id WHERE alias_fts MATCH ?)
443+
ORDER BY "target".created_time DESC`,
444+
expectedParams: ['"dev"*', 'name:"dev"* OR description:"dev"*'],
445+
},
446+
'search with multiple related searches': {
447+
query: {
448+
search: {
449+
text: 'dev',
450+
relatedSearches: [
451+
{
452+
resource: 'alias',
453+
fields: ['name', 'description'],
454+
join: {
455+
joinOn: 'destination_id',
456+
},
457+
},
458+
{
459+
resource: 'session',
460+
fields: ['name'],
461+
join: {
462+
joinOn: 'target_id',
463+
},
464+
},
465+
],
466+
},
467+
},
468+
expectedSql: `
469+
SELECT * FROM "target"
470+
WHERE "target".rowid IN (SELECT rowid FROM target_fts WHERE target_fts MATCH ?
471+
UNION SELECT "target".rowid FROM alias_fts JOIN "target" ON "target".id = alias_fts.destination_id WHERE alias_fts MATCH ?
472+
UNION SELECT "target".rowid FROM session_fts JOIN "target" ON "target".id = session_fts.target_id WHERE session_fts MATCH ?)
473+
ORDER BY "target".created_time DESC`,
474+
expectedParams: [
475+
'"dev"*',
476+
'name:"dev"* OR description:"dev"*',
477+
'name:"dev"*',
478+
],
479+
},
422480
},
423481
function (assert, { query, expectedSql, expectedParams }) {
424482
const { sql, parameters } = generateSQLExpressions('target', query);

‎ui/admin/app/routes/scopes/scope/aliases/index.js‎

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import Route from '@ember/routing/route';
77
import { service } from '@ember/service';
8-
import { hash } from 'rsvp';
98
import { restartableTask, timeout } from 'ember-concurrency';
109

1110
export default class ScopesScopeAliasesIndexRoute extends Route {
@@ -79,28 +78,69 @@ export default class ScopesScopeAliasesIndexRoute extends Route {
7978
direction: sortDirection,
8079
};
8180

81+
const searchOptions = {
82+
text: search,
83+
relatedSearches: [
84+
{
85+
resource: 'target',
86+
text: search,
87+
fields: ['name'],
88+
join: {
89+
joinFrom: 'destination_id',
90+
joinOn: 'id',
91+
},
92+
},
93+
],
94+
};
95+
96+
const targetPromise = this.store.query(
97+
'target',
98+
{
99+
scope_id,
100+
recursive: true,
101+
page: 1,
102+
pageSize: 1,
103+
},
104+
{ pushToStore: false },
105+
);
106+
82107
aliases = await this.store.query('alias', {
83108
scope_id,
84-
query: { search, sort },
109+
query: { search: searchOptions, sort },
85110
page,
86111
pageSize,
87112
});
88113

89114
totalItems = aliases.meta?.totalItems;
90-
// since we don't receive target info from aliases list API,
91-
// we query the store to fetch target information based on the destination id
92-
aliases = await Promise.all(
93-
aliases.map((alias) =>
94-
hash({
95-
alias,
96-
target: alias.destination_id
97-
? this.store.findRecord('target', alias.destination_id, {
98-
backgroundReload: false,
99-
})
100-
: null,
101-
}),
115+
116+
await targetPromise;
117+
// All the targets should have been retrieved just before this so we don't need to make another API request
118+
// Check for actual aliases with destinations as an empty array will bring back all targets which we don't want
119+
const associatedTargets = aliases.some((alias) => alias.destination_id)
120+
? await this.store.query(
121+
'target',
122+
{
123+
query: {
124+
filters: {
125+
id: aliases
126+
.filter((alias) => alias.destination_id)
127+
.map((alias) => ({
128+
equals: alias.destination_id,
129+
})),
130+
},
131+
},
132+
},
133+
{ pushToStore: true, peekDb: true },
134+
)
135+
: [];
136+
137+
aliases = aliases.map((alias) => ({
138+
alias,
139+
target: associatedTargets.find(
140+
(target) => target.id === alias.destination_id,
102141
),
103-
);
142+
}));
143+
104144
doAliasesExist = await this.getDoAliasesExist(scope_id, totalItems);
105145
}
106146

‎ui/admin/tests/acceptance/aliases/update-test.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ module('Acceptance | aliases | update', function (hooks) {
4242
type: 'org',
4343
scope: { id: 'global', type: 'global' },
4444
});
45+
instances.scopes.project = this.server.create('scope', {
46+
type: 'project',
47+
scope: { id: instances.scopes.org.id, type: 'org' },
48+
});
4549
instances.target = this.server.create('target', {
4650
scope: instances.scopes.project,
4751
});

0 commit comments

Comments
 (0)