Icu 18506 load resource IDs for autocomplete (#3247)
* feat: 🎸 Add id auto completion * style: 💄 Fix code editor styling for dark mode * feat: 🎸 Add optimization for caching sqlite data ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-18506
This commit is contained in:
1 parent
1d126dc074
commit
e5657a8d4c
11 files changed
+860
-128
No files matched your search
@@ -63,14 +63,35 @@ export default class SqliteHandler {
|
||||
rateLimitWarning;
|
||||
|
||||
if (!peekDb) {
|
||||
const tokenKey = `${type}-${hashCode(remainingQuery)}`;
|
||||
let tokenKey = `${type}-${hashCode(remainingQuery)}`;
|
||||
// The global token key represents a recursive query from global scope,
|
||||
// which pulls back every resource of this type.
|
||||
const globalTokenKey = `${type}-${hashCode({ scope_id: 'global', recursive: true })}`;
|
||||
if (storeToken) {
|
||||
const [tokenObj] = await this.sqlite.fetchResource({
|
||||
...generateSQLExpressions('token', {
|
||||
filters: { id: [{ equals: tokenKey }] },
|
||||
}),
|
||||
});
|
||||
listToken = tokenObj?.token;
|
||||
// Always prefer the global token when it exists for
|
||||
// any more fine-grained query. The global query already fetched every
|
||||
// item for this resource type, so we should just re-use it.
|
||||
if (tokenKey !== globalTokenKey) {
|
||||
const [globalTokenObj] = await this.sqlite.fetchResource({
|
||||
...generateSQLExpressions('token', {
|
||||
filters: { id: [{ equals: globalTokenKey }] },
|
||||
}),
|
||||
});
|
||||
if (globalTokenObj?.token) {
|
||||
tokenKey = globalTokenKey;
|
||||
listToken = globalTokenObj.token;
|
||||
}
|
||||
}
|
||||
|
||||
// Only look up the specific token if no global token was found.
|
||||
if (!listToken) {
|
||||
const [tokenObj] = await this.sqlite.fetchResource({
|
||||
...generateSQLExpressions('token', {
|
||||
filters: { id: [{ equals: tokenKey }] },
|
||||
}),
|
||||
});
|
||||
listToken = tokenObj?.token;
|
||||
}
|
||||
} else {
|
||||
// This is a temporary fix of clearing the DB (specifically for auth-methods)
|
||||
// since we are not storing the token we do not get back a list of removed_ids
|
||||
|
||||
@@ -470,6 +470,62 @@ module('Unit | Handler | sqlite-handler', function (hooks) {
|
||||
assert.strictEqual(results[0].name, 'specific-target');
|
||||
});
|
||||
|
||||
test('it reuses the global token for a more specific query when the global token exists', async function (assert) {
|
||||
const targets = this.server.createList('target', 3, { scope });
|
||||
let capturedListToken;
|
||||
let responseToken = 'specific-list-token';
|
||||
|
||||
this.server.get('targets', (_schema, request) => {
|
||||
capturedListToken = request.queryParams.list_token;
|
||||
return {
|
||||
items: targets.map((t) =>
|
||||
this.server.serializerOrRegistry.serialize(t),
|
||||
),
|
||||
list_token: responseToken,
|
||||
response_type: 'complete',
|
||||
};
|
||||
});
|
||||
|
||||
// Run the specific query first
|
||||
await store.query('target', { scope_id: 'some-specific-scope' });
|
||||
|
||||
// Switch the response token and run the global query
|
||||
responseToken = 'global-list-token';
|
||||
await store.query('target', { scope_id: 'global', recursive: true });
|
||||
|
||||
capturedListToken = undefined;
|
||||
|
||||
// Re-run the specific query which has a cached token but should prefer the global token since it exists
|
||||
await store.query('target', { scope_id: 'some-specific-scope' });
|
||||
|
||||
assert.strictEqual(capturedListToken, 'global-list-token');
|
||||
});
|
||||
|
||||
test('it uses the specific token when no global token exists', async function (assert) {
|
||||
const targets = this.server.createList('target', 3, { scope });
|
||||
let capturedListToken;
|
||||
|
||||
this.server.get('targets', (_schema, request) => {
|
||||
capturedListToken = request.queryParams.list_token;
|
||||
return {
|
||||
items: targets.map((t) =>
|
||||
this.server.serializerOrRegistry.serialize(t),
|
||||
),
|
||||
list_token: 'specific-list-token',
|
||||
response_type: 'complete',
|
||||
};
|
||||
});
|
||||
|
||||
await store.query('target', { scope_id: 'some-specific-scope' });
|
||||
|
||||
capturedListToken = undefined;
|
||||
|
||||
// Re-run the specific query which should just use the normal specific token since no global token exists
|
||||
await store.query('target', { scope_id: 'some-specific-scope' });
|
||||
|
||||
assert.strictEqual(capturedListToken, 'specific-list-token');
|
||||
});
|
||||
|
||||
test('it retries query when invalid list token error occurs', async function (assert) {
|
||||
let targets = this.server.createList('target', 5, { scope });
|
||||
|
||||
|
||||
Reference in new issue
Block a user