feat: 🎸 Add sqlite support for auth methods (#2969)

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-17418
This commit is contained in:
Zhihe Li committed 2025-09-03 11:41:42 -04:00
1 parent ba7b8b8824
commit c2b0f321de
17 files changed
+227 -30

No files matched your search

+54 -3
View File
@@ -37,7 +37,7 @@ export default class SqliteHandler {
// Go through normal flow if we don't yet support the model
// or if we don't have a sqlite db instance
if (!supportedModels.includes(type) || !this.sqlite) {
if (!supportedModels.includes(type) || !this.sqlite.worker) {
return next(context.request);
}
@@ -60,6 +60,11 @@ export default class SqliteHandler {
}),
});
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
// from the API call and we do not want deleted items from showing in list view.
await this.sqlite.deleteResource(type);
}
do {
@@ -76,8 +81,15 @@ export default class SqliteHandler {
await writeToDbPromise;
}
} catch (err) {
console.error('Error retrieving data:', err);
throw err;
payload = await this.retryQueryFailure({
err,
type,
adapter,
store,
schema,
remainingQuery,
});
totalInsert += payload.items?.length ?? 0;
}
listToken = payload.list_token;
@@ -149,6 +161,45 @@ export default class SqliteHandler {
}
}
async retryQueryFailure({
err,
type,
adapter,
store,
schema,
remainingQuery,
}) {
// If we get an invalid list token, we'll delete the token and
// clear the DB and try again without a token.
if (
err?.errors[0].status === 400 &&
err?.errors[0].code === 'invalid list token'
) {
await this.sqlite.deleteResource(type);
// Delete all tokens of the same resource type so we don't keep clearing the DB.
// Even if other tokens may still be valid, we might have cleared data that they
// depended on so we should clear all tokens of the same type when clearing the DB.
const { sql, parameters } = generateSQLExpressions('token', {
filters: { id: [{ contains: type }] },
});
const tokenRows = await this.sqlite.fetchResource({
sql,
parameters,
});
const tokenIds = tokenRows.map((row) => row.id);
if (tokenIds.length > 0) {
await this.sqlite.deleteResource('token', tokenIds);
}
return await adapter.query(store, schema, {
...remainingQuery,
});
} else {
throw err;
}
}
async writeToDb(
payload,
storeToken,