refactor: Opt stores into persistence via a persistable property

Stores now declare persistence with `persistable = true` and RootStore
sweeps all persistable stores, deriving each database name from the
store's apiEndpoint, instead of RootStore hard-coding the policies
store. The deleteAllDatabases fallback uses the same sweep.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XisV6sRdexfnPSiehurKoV
This commit is contained in:
Claude authored and Tom Moor committed 2026-08-16 11:56:11 -04:00
1 parent 6455506edc
commit 9a3b075bcc
4 files changed
+36 -18

No files matched your search

+2
View File
@@ -7,6 +7,8 @@ import Store from "./base/Store";
export default class PoliciesStore extends Store<Policy> {
actions = [];
persistable = true;
constructor(rootStore: RootStore) {
super(rootStore, Policy);
}
+9 -8
View File
@@ -35,8 +35,7 @@ import UserMembershipsStore from "./UserMembershipsStore";
import UsersStore from "./UsersStore";
import ViewsStore from "./ViewsStore";
import WebhookSubscriptionsStore from "./WebhookSubscriptionStore";
import type Store from "./base/Store";
import StorePersistence from "./base/StorePersistence";
import Store from "./base/Store";
export default class RootStore {
apiKeys: ApiKeysStore;
@@ -121,9 +120,9 @@ export default class RootStore {
}
/**
* Enable IndexedDB persistence for eligible stores, scoped to the current
* team. A no-op when there is no authenticated team or persistence is
* already enabled.
* Enable IndexedDB persistence for all stores marked as persistable, scoped
* to the current team. A no-op when there is no authenticated team or
* persistence is already enabled.
*/
public enablePersistence() {
const teamId = this.auth.currentTeamId;
@@ -131,9 +130,11 @@ export default class RootStore {
return;
}
void this.policies.enablePersistence(
StorePersistence.databaseName("policies", teamId)
);
Object.values(this).forEach((store) => {
if (store instanceof Store && store.persistable) {
void store.enablePersistence(teamId);
}
});
}
/**
+15 -6
View File
@@ -76,6 +76,12 @@ export default abstract class Store<T extends Model> {
apiEndpoint: string;
/**
* Whether the store's data should be persisted to IndexedDB, and restored
* at boot, once persistence is enabled for the authenticated team.
*/
persistable = false;
rootStore: RootStore;
protected persistence?: StorePersistence<T>;
@@ -105,19 +111,22 @@ export default abstract class Store<T extends Model> {
}
/**
* Enables persistence of this store's data to IndexedDB and hydrates any
* previously persisted records into the store. Safe to call multiple times,
* subsequent calls are a no-op.
* Enables persistence of this store's data to IndexedDB, scoped to the
* given team, and hydrates any previously persisted records into the store.
* Safe to call multiple times, subsequent calls are a no-op.
*
* @param databaseName the name of the IndexedDB database to persist to.
* @param teamId the ID of the team the persisted data belongs to.
* @returns a promise that resolves when hydration is complete.
*/
async enablePersistence(databaseName: string): Promise<void> {
async enablePersistence(teamId: string): Promise<void> {
if (this.persistence || !StorePersistence.isSupported) {
return;
}
this.persistence = new StorePersistence<T>(this, databaseName);
this.persistence = new StorePersistence<T>(
this,
StorePersistence.databaseName(this.apiEndpoint, teamId)
);
await this.persistence.hydrate();
}
+10 -4
View File
@@ -1,5 +1,6 @@
import { flatten } from "es-toolkit/compat";
import stores from "~/stores";
import Store from "~/stores/base/Store";
import StorePersistence from "~/stores/base/StorePersistence";
import { flattenTree } from "@shared/utils/tree";
@@ -13,10 +14,15 @@ export async function deleteAllDatabases() {
return;
}
if (stores.auth.currentTeamId) {
window.indexedDB.deleteDatabase(
StorePersistence.databaseName("policies", stores.auth.currentTeamId)
);
const teamId = stores.auth.currentTeamId;
if (teamId) {
Object.values(stores).forEach((store) => {
if (store instanceof Store && store.persistable) {
window.indexedDB.deleteDatabase(
StorePersistence.databaseName(store.apiEndpoint, teamId)
);
}
});
}
if ("databases" in window.indexedDB) {