query-optimistic 0.3.4 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,188 @@
1
+ /**
2
+ * Core types for query-toolkit
3
+ * Framework-agnostic definitions
4
+ */
5
+ /** Extract the ID from an entity */
6
+ type IdGetter<T> = (item: T) => string;
7
+ /** Optimistic status for pending mutations */
8
+ type OptimisticStatus = 'pending' | 'success' | 'error';
9
+ /** Wraps an entity with optimistic metadata */
10
+ type Optimistic<T> = T & {
11
+ _optimistic?: {
12
+ id: string;
13
+ status: OptimisticStatus;
14
+ error?: string;
15
+ };
16
+ };
17
+ /** Collection definition for arrays of items */
18
+ interface CollectionDef<TData, TParams = void> {
19
+ readonly _type: 'collection';
20
+ readonly name: string;
21
+ readonly id: IdGetter<TData>;
22
+ readonly fetch: (params: TParams) => Promise<TData[]>;
23
+ }
24
+ /** Entity definition for single items */
25
+ interface EntityDef<TData, TParams = void> {
26
+ readonly _type: 'entity';
27
+ readonly name: string;
28
+ readonly fetch: (params: TParams) => Promise<TData>;
29
+ }
30
+ /** Mutation definition */
31
+ interface MutationDef<TParams, TResponse = void> {
32
+ readonly _type: 'mutation';
33
+ readonly name?: string;
34
+ readonly mutate: (params: TParams) => Promise<TResponse>;
35
+ }
36
+ /** Unified definition type */
37
+ type AnyDef = CollectionDef<any, any> | EntityDef<any, any>;
38
+ /** Optimistic action types */
39
+ type OptimisticAction = 'prepend' | 'append' | 'update' | 'delete' | 'replace';
40
+ /** Optimistic update instruction */
41
+ interface OptimisticInstruction<T = any> {
42
+ target: CollectionDef<T, any> | EntityDef<T, any>;
43
+ action: OptimisticAction;
44
+ data?: Partial<T>;
45
+ id?: string;
46
+ where?: (item: T) => boolean;
47
+ update?: (item: T) => T;
48
+ }
49
+ /** Options for query execution */
50
+ interface QueryOptions {
51
+ enabled?: boolean;
52
+ staleTime?: number;
53
+ cacheTime?: number;
54
+ refetchOnMount?: boolean;
55
+ refetchOnWindowFocus?: boolean;
56
+ refetchInterval?: number | false;
57
+ }
58
+ /** Options for paginated queries */
59
+ interface PaginatedOptions extends QueryOptions {
60
+ getNextPageParam?: (lastPage: any[], allPages: any[][]) => unknown | undefined;
61
+ }
62
+
63
+ /** Transaction returned from channel methods */
64
+ interface OptimisticTransaction {
65
+ target: CollectionDef<any, any> | EntityDef<any, any>;
66
+ action: 'prepend' | 'append' | 'update' | 'delete' | 'replace';
67
+ data?: any;
68
+ id?: string;
69
+ where?: (item: any) => boolean;
70
+ update?: (item: any) => any;
71
+ sync?: boolean;
72
+ rollback: () => void;
73
+ }
74
+ /** Options for channel operations */
75
+ interface ChannelOptions {
76
+ /**
77
+ * Only apply updates to queries whose params partially match this object.
78
+ * All key-value pairs in scope must exist in the query's params.
79
+ * @example
80
+ * channel(ordersCollection, { scope: { chain: 'solana' } }).delete(id)
81
+ * // Only affects queries with params containing chain: 'solana'
82
+ */
83
+ scope?: Record<string, unknown>;
84
+ }
85
+ /** Channel for a collection - provides typed optimistic mutation methods */
86
+ declare class CollectionChannel<TEntity> {
87
+ private readonly target;
88
+ private readonly options?;
89
+ private readonly optimisticId;
90
+ constructor(target: CollectionDef<TEntity, any>, options?: ChannelOptions | undefined);
91
+ /**
92
+ * Prepend an item to the collection
93
+ * @returns Rollback function to undo the change
94
+ */
95
+ prepend(data: TEntity, options?: {
96
+ sync?: boolean;
97
+ }): () => void;
98
+ /**
99
+ * Append an item to the collection
100
+ * @returns Rollback function to undo the change
101
+ */
102
+ append(data: TEntity, options?: {
103
+ sync?: boolean;
104
+ }): () => void;
105
+ /**
106
+ * Update an item in the collection by ID
107
+ * @returns Rollback function to undo the change
108
+ */
109
+ update(id: string, updateFn: (item: TEntity) => TEntity, options?: {
110
+ sync?: boolean;
111
+ }): () => void;
112
+ /**
113
+ * Update items matching a predicate
114
+ * @returns Rollback function to undo the change
115
+ */
116
+ updateWhere(where: (item: TEntity) => boolean, updateFn: (item: TEntity) => TEntity): () => void;
117
+ /**
118
+ * Delete an item from the collection by ID
119
+ * @returns Rollback function to undo the change
120
+ */
121
+ delete(id: string): () => void;
122
+ /**
123
+ * Delete items matching a predicate
124
+ * @returns Rollback function to undo the change
125
+ */
126
+ deleteWhere(where: (item: TEntity) => boolean): () => void;
127
+ }
128
+ /** Channel for an entity - provides typed optimistic mutation methods */
129
+ declare class EntityChannel<TEntity> {
130
+ private readonly target;
131
+ constructor(target: EntityDef<TEntity, any>);
132
+ /**
133
+ * Update the entity
134
+ * @returns Rollback function to undo the change
135
+ */
136
+ update(updateFn: (item: TEntity) => TEntity, options?: {
137
+ sync?: boolean;
138
+ }): () => void;
139
+ /**
140
+ * Replace the entity with new data
141
+ * @returns Rollback function to undo the change
142
+ */
143
+ replace(data: TEntity, options?: {
144
+ sync?: boolean;
145
+ }): () => void;
146
+ }
147
+ /**
148
+ * Channel function for optimistic mutations.
149
+ * Call with a collection or entity to get typed mutation methods.
150
+ *
151
+ * @example
152
+ * // Standalone usage
153
+ * const rollback = channel(usersCollection).prepend({ id: '1', name: 'John' });
154
+ * // Later, to undo:
155
+ * rollback();
156
+ *
157
+ * @example
158
+ * // Update an entity
159
+ * channel(userEntity).update(user => ({ ...user, name: 'Jane' }));
160
+ *
161
+ * @example
162
+ * // Scoped update
163
+ * channel(ordersCollection, { scope: { chain: 'solana' } }).delete(id);
164
+ * // Only affects queries with params containing chain: 'solana'
165
+ */
166
+ interface Channel {
167
+ <TEntity>(target: CollectionDef<TEntity, any>, options?: ChannelOptions): CollectionChannel<TEntity>;
168
+ <TEntity>(target: EntityDef<TEntity, any>): EntityChannel<TEntity>;
169
+ }
170
+ /**
171
+ * Create a channel for optimistic mutations.
172
+ * Use this to apply immediate UI updates that can be rolled back.
173
+ *
174
+ * @example
175
+ * const rollback = channel(usersCollection).prepend(newUser);
176
+ * try {
177
+ * await api.createUser(newUser);
178
+ * } catch (error) {
179
+ * rollback(); // Undo the optimistic update
180
+ * }
181
+ *
182
+ * @example
183
+ * // Scoped update - only affects queries with matching params
184
+ * channel(ordersCollection, { scope: { chain: 'solana', status: 'pending' } }).delete(id);
185
+ */
186
+ declare const channel: Channel;
187
+
188
+ export { type AnyDef as A, type Channel as C, EntityChannel as E, type IdGetter as I, type MutationDef as M, type Optimistic as O, type PaginatedOptions as P, type QueryOptions as Q, type ChannelOptions as a, CollectionChannel as b, type CollectionDef as c, type EntityDef as d, type OptimisticAction as e, type OptimisticInstruction as f, type OptimisticStatus as g, type OptimisticTransaction as h, channel as i };
@@ -0,0 +1,188 @@
1
+ /**
2
+ * Core types for query-toolkit
3
+ * Framework-agnostic definitions
4
+ */
5
+ /** Extract the ID from an entity */
6
+ type IdGetter<T> = (item: T) => string;
7
+ /** Optimistic status for pending mutations */
8
+ type OptimisticStatus = 'pending' | 'success' | 'error';
9
+ /** Wraps an entity with optimistic metadata */
10
+ type Optimistic<T> = T & {
11
+ _optimistic?: {
12
+ id: string;
13
+ status: OptimisticStatus;
14
+ error?: string;
15
+ };
16
+ };
17
+ /** Collection definition for arrays of items */
18
+ interface CollectionDef<TData, TParams = void> {
19
+ readonly _type: 'collection';
20
+ readonly name: string;
21
+ readonly id: IdGetter<TData>;
22
+ readonly fetch: (params: TParams) => Promise<TData[]>;
23
+ }
24
+ /** Entity definition for single items */
25
+ interface EntityDef<TData, TParams = void> {
26
+ readonly _type: 'entity';
27
+ readonly name: string;
28
+ readonly fetch: (params: TParams) => Promise<TData>;
29
+ }
30
+ /** Mutation definition */
31
+ interface MutationDef<TParams, TResponse = void> {
32
+ readonly _type: 'mutation';
33
+ readonly name?: string;
34
+ readonly mutate: (params: TParams) => Promise<TResponse>;
35
+ }
36
+ /** Unified definition type */
37
+ type AnyDef = CollectionDef<any, any> | EntityDef<any, any>;
38
+ /** Optimistic action types */
39
+ type OptimisticAction = 'prepend' | 'append' | 'update' | 'delete' | 'replace';
40
+ /** Optimistic update instruction */
41
+ interface OptimisticInstruction<T = any> {
42
+ target: CollectionDef<T, any> | EntityDef<T, any>;
43
+ action: OptimisticAction;
44
+ data?: Partial<T>;
45
+ id?: string;
46
+ where?: (item: T) => boolean;
47
+ update?: (item: T) => T;
48
+ }
49
+ /** Options for query execution */
50
+ interface QueryOptions {
51
+ enabled?: boolean;
52
+ staleTime?: number;
53
+ cacheTime?: number;
54
+ refetchOnMount?: boolean;
55
+ refetchOnWindowFocus?: boolean;
56
+ refetchInterval?: number | false;
57
+ }
58
+ /** Options for paginated queries */
59
+ interface PaginatedOptions extends QueryOptions {
60
+ getNextPageParam?: (lastPage: any[], allPages: any[][]) => unknown | undefined;
61
+ }
62
+
63
+ /** Transaction returned from channel methods */
64
+ interface OptimisticTransaction {
65
+ target: CollectionDef<any, any> | EntityDef<any, any>;
66
+ action: 'prepend' | 'append' | 'update' | 'delete' | 'replace';
67
+ data?: any;
68
+ id?: string;
69
+ where?: (item: any) => boolean;
70
+ update?: (item: any) => any;
71
+ sync?: boolean;
72
+ rollback: () => void;
73
+ }
74
+ /** Options for channel operations */
75
+ interface ChannelOptions {
76
+ /**
77
+ * Only apply updates to queries whose params partially match this object.
78
+ * All key-value pairs in scope must exist in the query's params.
79
+ * @example
80
+ * channel(ordersCollection, { scope: { chain: 'solana' } }).delete(id)
81
+ * // Only affects queries with params containing chain: 'solana'
82
+ */
83
+ scope?: Record<string, unknown>;
84
+ }
85
+ /** Channel for a collection - provides typed optimistic mutation methods */
86
+ declare class CollectionChannel<TEntity> {
87
+ private readonly target;
88
+ private readonly options?;
89
+ private readonly optimisticId;
90
+ constructor(target: CollectionDef<TEntity, any>, options?: ChannelOptions | undefined);
91
+ /**
92
+ * Prepend an item to the collection
93
+ * @returns Rollback function to undo the change
94
+ */
95
+ prepend(data: TEntity, options?: {
96
+ sync?: boolean;
97
+ }): () => void;
98
+ /**
99
+ * Append an item to the collection
100
+ * @returns Rollback function to undo the change
101
+ */
102
+ append(data: TEntity, options?: {
103
+ sync?: boolean;
104
+ }): () => void;
105
+ /**
106
+ * Update an item in the collection by ID
107
+ * @returns Rollback function to undo the change
108
+ */
109
+ update(id: string, updateFn: (item: TEntity) => TEntity, options?: {
110
+ sync?: boolean;
111
+ }): () => void;
112
+ /**
113
+ * Update items matching a predicate
114
+ * @returns Rollback function to undo the change
115
+ */
116
+ updateWhere(where: (item: TEntity) => boolean, updateFn: (item: TEntity) => TEntity): () => void;
117
+ /**
118
+ * Delete an item from the collection by ID
119
+ * @returns Rollback function to undo the change
120
+ */
121
+ delete(id: string): () => void;
122
+ /**
123
+ * Delete items matching a predicate
124
+ * @returns Rollback function to undo the change
125
+ */
126
+ deleteWhere(where: (item: TEntity) => boolean): () => void;
127
+ }
128
+ /** Channel for an entity - provides typed optimistic mutation methods */
129
+ declare class EntityChannel<TEntity> {
130
+ private readonly target;
131
+ constructor(target: EntityDef<TEntity, any>);
132
+ /**
133
+ * Update the entity
134
+ * @returns Rollback function to undo the change
135
+ */
136
+ update(updateFn: (item: TEntity) => TEntity, options?: {
137
+ sync?: boolean;
138
+ }): () => void;
139
+ /**
140
+ * Replace the entity with new data
141
+ * @returns Rollback function to undo the change
142
+ */
143
+ replace(data: TEntity, options?: {
144
+ sync?: boolean;
145
+ }): () => void;
146
+ }
147
+ /**
148
+ * Channel function for optimistic mutations.
149
+ * Call with a collection or entity to get typed mutation methods.
150
+ *
151
+ * @example
152
+ * // Standalone usage
153
+ * const rollback = channel(usersCollection).prepend({ id: '1', name: 'John' });
154
+ * // Later, to undo:
155
+ * rollback();
156
+ *
157
+ * @example
158
+ * // Update an entity
159
+ * channel(userEntity).update(user => ({ ...user, name: 'Jane' }));
160
+ *
161
+ * @example
162
+ * // Scoped update
163
+ * channel(ordersCollection, { scope: { chain: 'solana' } }).delete(id);
164
+ * // Only affects queries with params containing chain: 'solana'
165
+ */
166
+ interface Channel {
167
+ <TEntity>(target: CollectionDef<TEntity, any>, options?: ChannelOptions): CollectionChannel<TEntity>;
168
+ <TEntity>(target: EntityDef<TEntity, any>): EntityChannel<TEntity>;
169
+ }
170
+ /**
171
+ * Create a channel for optimistic mutations.
172
+ * Use this to apply immediate UI updates that can be rolled back.
173
+ *
174
+ * @example
175
+ * const rollback = channel(usersCollection).prepend(newUser);
176
+ * try {
177
+ * await api.createUser(newUser);
178
+ * } catch (error) {
179
+ * rollback(); // Undo the optimistic update
180
+ * }
181
+ *
182
+ * @example
183
+ * // Scoped update - only affects queries with matching params
184
+ * channel(ordersCollection, { scope: { chain: 'solana', status: 'pending' } }).delete(id);
185
+ */
186
+ declare const channel: Channel;
187
+
188
+ export { type AnyDef as A, type Channel as C, EntityChannel as E, type IdGetter as I, type MutationDef as M, type Optimistic as O, type PaginatedOptions as P, type QueryOptions as Q, type ChannelOptions as a, CollectionChannel as b, type CollectionDef as c, type EntityDef as d, type OptimisticAction as e, type OptimisticInstruction as f, type OptimisticStatus as g, type OptimisticTransaction as h, channel as i };
@@ -1,5 +1,6 @@
1
- import { I as IdGetter, C as CollectionDef, E as EntityDef, M as MutationDef, O as Optimistic } from '../types-BOq5W_Qm.mjs';
2
- export { A as AnyDef, a as OptimisticAction, b as OptimisticInstruction, c as OptimisticStatus, P as PaginatedOptions, Q as QueryOptions } from '../types-BOq5W_Qm.mjs';
1
+ import { I as IdGetter, c as CollectionDef, d as EntityDef, M as MutationDef, O as Optimistic } from '../channel-OFFEKFI1.mjs';
2
+ export { A as AnyDef, C as Channel, a as ChannelOptions, b as CollectionChannel, E as EntityChannel, e as OptimisticAction, f as OptimisticInstruction, g as OptimisticStatus, h as OptimisticTransaction, P as PaginatedOptions, Q as QueryOptions, i as channel } from '../channel-OFFEKFI1.mjs';
3
+ import { QueryClient } from '@tanstack/react-query';
3
4
 
4
5
  /**
5
6
  * Define a collection query for fetching arrays of items
@@ -86,126 +87,35 @@ type RegisteredEntry = RegisteredCollection | RegisteredEntity | RegisteredPagin
86
87
  */
87
88
  declare class QueryRegistry {
88
89
  private entries;
90
+ private queryClient;
91
+ private collectionDefs;
92
+ /** Set the query client for direct cache access */
93
+ setQueryClient(client: QueryClient): void;
94
+ /** Register a collection definition for direct cache updates */
95
+ registerDef(def: CollectionDef<any, any>): void;
89
96
  /** Register an active query */
90
97
  register(entry: RegisteredEntry): void;
91
98
  /** Unregister a query when component unmounts */
92
99
  unregister(entry: RegisteredEntry): void;
93
100
  /** Get all registered entries for a query name */
94
101
  getByName(name: string): RegisteredEntry[];
102
+ /**
103
+ * Check if params partially match the given scope object.
104
+ * Returns true if all key-value pairs in scope exist in params.
105
+ */
106
+ private matchesScope;
95
107
  /** Apply an optimistic update to all queries with given name */
96
108
  applyUpdate<T>(name: string, action: 'prepend' | 'append' | 'update' | 'delete' | 'replace', payload: {
97
109
  data?: Partial<Optimistic<T>>;
98
110
  id?: string;
99
111
  where?: (item: T) => boolean;
100
112
  update?: (item: T) => T;
101
- }): (() => void)[];
113
+ }, scope?: Record<string, unknown>): (() => void)[];
114
+ /** Apply update directly to query cache (used when scope is provided) */
115
+ private applyDirectCacheUpdate;
102
116
  private applyCollectionUpdate;
103
117
  }
104
118
  /** Singleton registry instance */
105
119
  declare const registry: QueryRegistry;
106
120
 
107
- /** Transaction returned from channel methods */
108
- interface OptimisticTransaction {
109
- target: CollectionDef<any, any> | EntityDef<any, any>;
110
- action: 'prepend' | 'append' | 'update' | 'delete' | 'replace';
111
- data?: any;
112
- id?: string;
113
- where?: (item: any) => boolean;
114
- update?: (item: any) => any;
115
- sync?: boolean;
116
- rollback: () => void;
117
- }
118
- /** Channel for a collection - provides typed optimistic mutation methods */
119
- declare class CollectionChannel<TEntity> {
120
- private readonly target;
121
- private readonly optimisticId;
122
- constructor(target: CollectionDef<TEntity, any>);
123
- /**
124
- * Prepend an item to the collection
125
- * @returns Rollback function to undo the change
126
- */
127
- prepend(data: TEntity, options?: {
128
- sync?: boolean;
129
- }): () => void;
130
- /**
131
- * Append an item to the collection
132
- * @returns Rollback function to undo the change
133
- */
134
- append(data: TEntity, options?: {
135
- sync?: boolean;
136
- }): () => void;
137
- /**
138
- * Update an item in the collection by ID
139
- * @returns Rollback function to undo the change
140
- */
141
- update(id: string, updateFn: (item: TEntity) => TEntity, options?: {
142
- sync?: boolean;
143
- }): () => void;
144
- /**
145
- * Update items matching a predicate
146
- * @returns Rollback function to undo the change
147
- */
148
- updateWhere(where: (item: TEntity) => boolean, updateFn: (item: TEntity) => TEntity): () => void;
149
- /**
150
- * Delete an item from the collection by ID
151
- * @returns Rollback function to undo the change
152
- */
153
- delete(id: string): () => void;
154
- /**
155
- * Delete items matching a predicate
156
- * @returns Rollback function to undo the change
157
- */
158
- deleteWhere(where: (item: TEntity) => boolean): () => void;
159
- }
160
- /** Channel for an entity - provides typed optimistic mutation methods */
161
- declare class EntityChannel<TEntity> {
162
- private readonly target;
163
- constructor(target: EntityDef<TEntity, any>);
164
- /**
165
- * Update the entity
166
- * @returns Rollback function to undo the change
167
- */
168
- update(updateFn: (item: TEntity) => TEntity, options?: {
169
- sync?: boolean;
170
- }): () => void;
171
- /**
172
- * Replace the entity with new data
173
- * @returns Rollback function to undo the change
174
- */
175
- replace(data: TEntity, options?: {
176
- sync?: boolean;
177
- }): () => void;
178
- }
179
- /**
180
- * Channel function for optimistic mutations.
181
- * Call with a collection or entity to get typed mutation methods.
182
- *
183
- * @example
184
- * // Standalone usage
185
- * const rollback = channel(usersCollection).prepend({ id: '1', name: 'John' });
186
- * // Later, to undo:
187
- * rollback();
188
- *
189
- * @example
190
- * // Update an entity
191
- * channel(userEntity).update(user => ({ ...user, name: 'Jane' }));
192
- */
193
- interface Channel {
194
- <TEntity>(target: CollectionDef<TEntity, any>): CollectionChannel<TEntity>;
195
- <TEntity>(target: EntityDef<TEntity, any>): EntityChannel<TEntity>;
196
- }
197
- /**
198
- * Create a channel for optimistic mutations.
199
- * Use this to apply immediate UI updates that can be rolled back.
200
- *
201
- * @example
202
- * const rollback = channel(usersCollection).prepend(newUser);
203
- * try {
204
- * await api.createUser(newUser);
205
- * } catch (error) {
206
- * rollback(); // Undo the optimistic update
207
- * }
208
- */
209
- declare const channel: Channel;
210
-
211
- export { type Channel, CollectionChannel, CollectionDef, EntityChannel, EntityDef, IdGetter, MutationDef, Optimistic, type OptimisticTransaction, type RegisteredCollection, type RegisteredEntity, type RegisteredEntry, type RegisteredPaginatedCollection, channel, defineCollection, defineEntity, defineMutation, registry };
121
+ export { CollectionDef, EntityDef, IdGetter, MutationDef, Optimistic, type RegisteredCollection, type RegisteredEntity, type RegisteredEntry, type RegisteredPaginatedCollection, defineCollection, defineEntity, defineMutation, registry };
@@ -1,5 +1,6 @@
1
- import { I as IdGetter, C as CollectionDef, E as EntityDef, M as MutationDef, O as Optimistic } from '../types-BOq5W_Qm.js';
2
- export { A as AnyDef, a as OptimisticAction, b as OptimisticInstruction, c as OptimisticStatus, P as PaginatedOptions, Q as QueryOptions } from '../types-BOq5W_Qm.js';
1
+ import { I as IdGetter, c as CollectionDef, d as EntityDef, M as MutationDef, O as Optimistic } from '../channel-OFFEKFI1.js';
2
+ export { A as AnyDef, C as Channel, a as ChannelOptions, b as CollectionChannel, E as EntityChannel, e as OptimisticAction, f as OptimisticInstruction, g as OptimisticStatus, h as OptimisticTransaction, P as PaginatedOptions, Q as QueryOptions, i as channel } from '../channel-OFFEKFI1.js';
3
+ import { QueryClient } from '@tanstack/react-query';
3
4
 
4
5
  /**
5
6
  * Define a collection query for fetching arrays of items
@@ -86,126 +87,35 @@ type RegisteredEntry = RegisteredCollection | RegisteredEntity | RegisteredPagin
86
87
  */
87
88
  declare class QueryRegistry {
88
89
  private entries;
90
+ private queryClient;
91
+ private collectionDefs;
92
+ /** Set the query client for direct cache access */
93
+ setQueryClient(client: QueryClient): void;
94
+ /** Register a collection definition for direct cache updates */
95
+ registerDef(def: CollectionDef<any, any>): void;
89
96
  /** Register an active query */
90
97
  register(entry: RegisteredEntry): void;
91
98
  /** Unregister a query when component unmounts */
92
99
  unregister(entry: RegisteredEntry): void;
93
100
  /** Get all registered entries for a query name */
94
101
  getByName(name: string): RegisteredEntry[];
102
+ /**
103
+ * Check if params partially match the given scope object.
104
+ * Returns true if all key-value pairs in scope exist in params.
105
+ */
106
+ private matchesScope;
95
107
  /** Apply an optimistic update to all queries with given name */
96
108
  applyUpdate<T>(name: string, action: 'prepend' | 'append' | 'update' | 'delete' | 'replace', payload: {
97
109
  data?: Partial<Optimistic<T>>;
98
110
  id?: string;
99
111
  where?: (item: T) => boolean;
100
112
  update?: (item: T) => T;
101
- }): (() => void)[];
113
+ }, scope?: Record<string, unknown>): (() => void)[];
114
+ /** Apply update directly to query cache (used when scope is provided) */
115
+ private applyDirectCacheUpdate;
102
116
  private applyCollectionUpdate;
103
117
  }
104
118
  /** Singleton registry instance */
105
119
  declare const registry: QueryRegistry;
106
120
 
107
- /** Transaction returned from channel methods */
108
- interface OptimisticTransaction {
109
- target: CollectionDef<any, any> | EntityDef<any, any>;
110
- action: 'prepend' | 'append' | 'update' | 'delete' | 'replace';
111
- data?: any;
112
- id?: string;
113
- where?: (item: any) => boolean;
114
- update?: (item: any) => any;
115
- sync?: boolean;
116
- rollback: () => void;
117
- }
118
- /** Channel for a collection - provides typed optimistic mutation methods */
119
- declare class CollectionChannel<TEntity> {
120
- private readonly target;
121
- private readonly optimisticId;
122
- constructor(target: CollectionDef<TEntity, any>);
123
- /**
124
- * Prepend an item to the collection
125
- * @returns Rollback function to undo the change
126
- */
127
- prepend(data: TEntity, options?: {
128
- sync?: boolean;
129
- }): () => void;
130
- /**
131
- * Append an item to the collection
132
- * @returns Rollback function to undo the change
133
- */
134
- append(data: TEntity, options?: {
135
- sync?: boolean;
136
- }): () => void;
137
- /**
138
- * Update an item in the collection by ID
139
- * @returns Rollback function to undo the change
140
- */
141
- update(id: string, updateFn: (item: TEntity) => TEntity, options?: {
142
- sync?: boolean;
143
- }): () => void;
144
- /**
145
- * Update items matching a predicate
146
- * @returns Rollback function to undo the change
147
- */
148
- updateWhere(where: (item: TEntity) => boolean, updateFn: (item: TEntity) => TEntity): () => void;
149
- /**
150
- * Delete an item from the collection by ID
151
- * @returns Rollback function to undo the change
152
- */
153
- delete(id: string): () => void;
154
- /**
155
- * Delete items matching a predicate
156
- * @returns Rollback function to undo the change
157
- */
158
- deleteWhere(where: (item: TEntity) => boolean): () => void;
159
- }
160
- /** Channel for an entity - provides typed optimistic mutation methods */
161
- declare class EntityChannel<TEntity> {
162
- private readonly target;
163
- constructor(target: EntityDef<TEntity, any>);
164
- /**
165
- * Update the entity
166
- * @returns Rollback function to undo the change
167
- */
168
- update(updateFn: (item: TEntity) => TEntity, options?: {
169
- sync?: boolean;
170
- }): () => void;
171
- /**
172
- * Replace the entity with new data
173
- * @returns Rollback function to undo the change
174
- */
175
- replace(data: TEntity, options?: {
176
- sync?: boolean;
177
- }): () => void;
178
- }
179
- /**
180
- * Channel function for optimistic mutations.
181
- * Call with a collection or entity to get typed mutation methods.
182
- *
183
- * @example
184
- * // Standalone usage
185
- * const rollback = channel(usersCollection).prepend({ id: '1', name: 'John' });
186
- * // Later, to undo:
187
- * rollback();
188
- *
189
- * @example
190
- * // Update an entity
191
- * channel(userEntity).update(user => ({ ...user, name: 'Jane' }));
192
- */
193
- interface Channel {
194
- <TEntity>(target: CollectionDef<TEntity, any>): CollectionChannel<TEntity>;
195
- <TEntity>(target: EntityDef<TEntity, any>): EntityChannel<TEntity>;
196
- }
197
- /**
198
- * Create a channel for optimistic mutations.
199
- * Use this to apply immediate UI updates that can be rolled back.
200
- *
201
- * @example
202
- * const rollback = channel(usersCollection).prepend(newUser);
203
- * try {
204
- * await api.createUser(newUser);
205
- * } catch (error) {
206
- * rollback(); // Undo the optimistic update
207
- * }
208
- */
209
- declare const channel: Channel;
210
-
211
- export { type Channel, CollectionChannel, CollectionDef, EntityChannel, EntityDef, IdGetter, MutationDef, Optimistic, type OptimisticTransaction, type RegisteredCollection, type RegisteredEntity, type RegisteredEntry, type RegisteredPaginatedCollection, channel, defineCollection, defineEntity, defineMutation, registry };
121
+ export { CollectionDef, EntityDef, IdGetter, MutationDef, Optimistic, type RegisteredCollection, type RegisteredEntity, type RegisteredEntry, type RegisteredPaginatedCollection, defineCollection, defineEntity, defineMutation, registry };