veza/apps/web/src/utils/persistWithMigration.ts
senke 75c027c5bd feat(web): add Zustand store migration strategy
- Document migration approach in ZUSTAND_MIGRATION_STRATEGY.md
- Add persistWithMigration utility for future stores
- Add version and migrate to authStore, library, ui, cartStore, playerStore
2026-02-14 22:43:06 +01:00

30 lines
710 B
TypeScript

/**
* Helper to create persist config with version and migrate.
* Use when adding new persisted stores to ensure future schema changes are handled.
*
* @example
* ```ts
* persist(
* (set) => ({ ... }),
* persistWithMigration('my-storage', 1, (state, version) => state ?? defaultState)
* )
* ```
*/
export function persistWithMigration<T>(
name: string,
version: number,
migrate: (state: unknown, version: number) => T,
partialize?: (state: T) => Partial<T>,
): {
name: string;
version: number;
migrate: (state: unknown, version: number) => T;
partialize?: (state: T) => Partial<T>;
} {
return {
name,
version,
migrate,
...(partialize && { partialize }),
};
}