feat(entities): Implement watch() method for live query subscriptions#86
feat(entities): Implement watch() method for live query subscriptions#86
Conversation
This adds a watch() method to EntityHandler that allows subscribing to
filtered subsets of entities with real-time change notifications.
Features:
- watch(options, callback) method with filter, sort, fields, limit options
- WatchEvent with changeType ('added', 'modified', 'removed')
- subscribeQuery/unsubscribeQuery socket events for server communication
- Proper cleanup on unsubscribe
The watch() method differs from subscribe() in that:
- It supports filter, sort, fields, and limit options
- It returns changeType indicating how the change affects the filtered results
- It uses dedicated subscribe_query socket events
Example usage:
```typescript
const unsubscribe = base44.entities.Task.watch(
{ filter: { status: 'active' }, sort: '-created_date', limit: 10 },
(event) => {
if (event.changeType === 'added') {
console.log('New matching task:', event.data);
}
}
);
```
Fixes #83
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
Claude finished @guyofeck's task —— View job Code Review for PR #86: feat(entities): Implement watch() method for live query subscriptionsI've thoroughly reviewed the implementation of the ✅ Code Quality and Best PracticesStrengths:
Minor Improvements:
✅ Potential Bugs or IssuesNo critical bugs found, but a few considerations:
|
|
@claude the callback should get the snapshot of all of the entities that returns from the query, and not a specific entity |
|
Claude finished @guyofeck's task —— View job Todo List:
Analysis of the watch() ImplementationI've reviewed the current implementation, and I understand the concern. Looking at the code and tests, the current Current Implementation BehaviorThe current implementation:
Architectural ImplicationsIf the callback should receive a snapshot of all entities matching the query (as you're suggesting), this would be a significant architectural change:
Potential Implementation ApproachesIf you want to implement snapshot-based updates, here are two approaches: Option 1: Full Snapshot on Every Change interface WatchSnapshot {
entities: any[]; // All entities matching the query
changeType: 'added' | 'modified' | 'removed';
changedEntityId: string; // Which entity triggered this update
timestamp: string;
}Option 2: Initial Snapshot + Individual Updates // Initial callback with full snapshot
callback({ type: 'snapshot', entities: [...] });
// Subsequent callbacks with individual changes
callback({ type: 'change', changeType: 'added', entity: {...} });Questions for Clarification
The current implementation follows a more traditional event-based pattern (similar to Firebase Realtime Database), while what you're describing sounds more like a reactive query pattern (similar to MongoDB Change Streams with full document). Would you like me to suggest specific implementation changes for the snapshot-based approach? |
Rewrite entities-watch.test.ts to match the current WatchSnapshot-based implementation and add new socket-utils-subscribeQuery.test.ts for testing the subscribeQuery socket functionality. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
Claude finished @guyofeck's task —— View job Code Review for PR #86: feat(entities): Implement watch() method for live query subscriptionsTodo List:
✅ Code Quality and Best PracticesStrengths:
Minor Improvements:
|
Summary
watch(options, callback)method toEntityHandlerfor live query subscriptionsWatchSnapshottype withtype('added', 'modified', 'removed'),entities, andtimestampWatchOptionstype for filter, sort, fields, limit configurationsubscribeQuery/unsubscribeQuerysocket events for server communicationUsage
Changes
watch()method with local snapshot state managementWatchSnapshot,WatchOptions,WatchChangeType,WatchSnapshotCallbacktypessubscribeQuery()for emitting subscribe/unsubscribe eventsTest plan
watch()method inentities-watch.test.tssubscribeQuery()insocket-utils-subscribeQuery.test.tsDepends on: Backend PRs #2894, #2895, #2896
Fixes #83
🤖 Generated with Claude Code