3.0 KiB
3.0 KiB
Create a smart cache store for an Odoo model with offline-first capabilities.
What this command does:
- Creates a framework-specific cache store (Svelte store, React Context, or Vue Pinia)
- Implements dual storage (localStorage + IndexedDB)
- Adds smart caching with stale detection (5-minute validity)
- Implements background sync (3-minute intervals)
- Includes incremental fetching (only new records)
- Adds partner name resolution and caching
- Implements optimistic updates
Required Information:
Before starting, gather:
- Current working directory - Must be inside an Odoo PWA project
- Framework - Detect from project files (SvelteKit/React/Vue)
- Model name (without
x_prefix, e.g., "expense", "task") - Model display name (human-readable, e.g., "Expense", "Task")
- Fields to fetch - Array of Odoo fields (e.g., ["x_studio_name", "x_studio_amount"])
Store Features:
The generated cache store will include:
records- Reactive array of cached recordsisLoading- Loading state indicatorerror- Error statelastSync- Timestamp of last successful syncload()- Load from cache and trigger background synccreate(data)- Create new record with optimistic updateupdate(id, data)- Update record with optimistic updatedelete(id)- Delete record with optimistic updaterefresh()- Force refresh from OdooclearCache()- Clear all cached data
Storage Strategy:
- localStorage: Stores metadata (lastSyncTime, lastRecordId, version)
- IndexedDB: Stores master data (all records)
- Stale detection: Cache considered stale after 5 minutes
- Background sync: Automatic sync every 3 minutes
- Incremental fetch: Only fetches records with
id > lastRecordId
Steps:
- Verify the current directory is an Odoo PWA project
- Detect the framework from project structure
- Ask the user for model details and fields
- Create cache store file in appropriate location:
- SvelteKit:
src/lib/stores/{model}Cache.js - React:
src/contexts/{Model}Context.jsx - Vue:
src/stores/{model}Store.js
- SvelteKit:
- Import and register the store in the appropriate location
- Provide usage examples and documentation
Example prompts to use this command:
/create-cache-store- Interactive mode- User: "Create a cache store for the task model"
- User: "Add caching for product catalog"
After creation:
Remind the user to:
- Import the store in components that need it
- Call the
load()method when the component mounts - Use reactive data bindings to display records
- Test create, update, and delete operations
- Verify offline functionality works correctly
Usage Examples:
SvelteKit
import { taskCache } from '$lib/stores/taskCache';
// In +page.svelte
$effect(() => {
taskCache.load();
});
React
import { useTask } from './contexts/TaskContext';
const { records, create, update } = useTask();
Vue
import { useTaskStore } from '@/stores/taskStore';
const taskStore = useTaskStore();
taskStore.load();