@codeleap/query 5.8.8 → 5.8.10
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.
- package/package.json +4 -4
- package/package.json.bak +1 -1
- package/src/lib/Mutations.ts +10 -4
- package/src/lib/QueryKeys.ts +13 -0
- package/src/lib/QueryManager.ts +36 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codeleap/query",
|
|
3
|
-
"version": "5.8.
|
|
3
|
+
"version": "5.8.10",
|
|
4
4
|
"main": "src/index.ts",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"repository": {
|
|
@@ -9,15 +9,15 @@
|
|
|
9
9
|
"directory": "packages/query"
|
|
10
10
|
},
|
|
11
11
|
"devDependencies": {
|
|
12
|
-
"@codeleap/config": "5.8.
|
|
13
|
-
"@codeleap/types": "5.8.
|
|
12
|
+
"@codeleap/config": "5.8.10",
|
|
13
|
+
"@codeleap/types": "5.8.10",
|
|
14
14
|
"ts-node-dev": "1.1.8"
|
|
15
15
|
},
|
|
16
16
|
"scripts": {
|
|
17
17
|
"build": "echo 'No build needed'"
|
|
18
18
|
},
|
|
19
19
|
"peerDependencies": {
|
|
20
|
-
"@codeleap/types": "5.8.
|
|
20
|
+
"@codeleap/types": "5.8.10",
|
|
21
21
|
"typescript": "5.5.2",
|
|
22
22
|
"@tanstack/react-query": "5.89.0"
|
|
23
23
|
},
|
package/package.json.bak
CHANGED
package/src/lib/Mutations.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { InfiniteData } from '@tanstack/query-core'
|
|
|
2
2
|
import { QueryKeys } from './QueryKeys'
|
|
3
3
|
import { ItemPosition, ListPaginationResponse, PageParam, QueryClient, QueryItem, RemovedItemMap, WithTempId } from '../types'
|
|
4
4
|
import deepEqual from 'fast-deep-equal'
|
|
5
|
+
import { TypeGuards } from '@codeleap/types'
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Class for managing mutations and cache updates for React Query list data
|
|
@@ -115,24 +116,29 @@ export class Mutations<T extends QueryItem, F> {
|
|
|
115
116
|
}
|
|
116
117
|
|
|
117
118
|
/**
|
|
118
|
-
* Removes an item from all cached list
|
|
119
|
+
* Removes an item from all or specific cached list query and returns the positions where it was found
|
|
119
120
|
* @param itemId - The ID of the item to remove
|
|
120
|
-
* @
|
|
121
|
+
* @param listFilters - Optional filters to target a specific list query. If omitted, removes from all list queries
|
|
122
|
+
* @returns A RemovedItemMap containing the query keys and positions where the item was found, or null if not found in any query
|
|
121
123
|
*
|
|
122
124
|
* @example
|
|
123
125
|
* ```typescript
|
|
126
|
+
* // Remove from all list queries
|
|
124
127
|
* const removedPositions = mutations.removeItem('user-123')
|
|
125
128
|
*
|
|
129
|
+
* // Remove from a specific filtered list
|
|
130
|
+
* const removedPositions = mutations.removeItem('user-123', { status: 'active' })
|
|
131
|
+
*
|
|
126
132
|
* // Later, restore the item to its original positions
|
|
127
133
|
* if (removedPositions) {
|
|
128
134
|
* mutations.addItem(restoredUser, removedPositions)
|
|
129
135
|
* }
|
|
130
136
|
* ```
|
|
131
137
|
*/
|
|
132
|
-
removeItem(itemId: QueryItem['id']): RemovedItemMap | null {
|
|
138
|
+
removeItem(itemId: QueryItem['id'], listFilters?: F): RemovedItemMap | null {
|
|
133
139
|
this.queryKeys.removeRetrieveQueryData(itemId)
|
|
134
140
|
|
|
135
|
-
const listQueries = this.queryKeys.getAllListQueries()
|
|
141
|
+
const listQueries = TypeGuards.isNil(listFilters) ? this.queryKeys.getAllListQueries() : [this.queryKeys.getListQuery(listFilters)]
|
|
136
142
|
|
|
137
143
|
const removedItemMap: RemovedItemMap = []
|
|
138
144
|
|
package/src/lib/QueryKeys.ts
CHANGED
|
@@ -336,6 +336,19 @@ export class QueryKeys<T extends QueryItem, F> {
|
|
|
336
336
|
|
|
337
337
|
return queries as Query<ListPaginationResponse<T>, Error, Omit<ListSelector<T>, 'allItems'>, QueryKey>[]
|
|
338
338
|
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Gets a specific list query from the query cache
|
|
342
|
+
* @param listFilters - Optional filters to identify a specific list query. If omitted, returns the unfiltered list query
|
|
343
|
+
* @returns The matching list query, or undefined if not found
|
|
344
|
+
*/
|
|
345
|
+
getListQuery(listFilters?: F) {
|
|
346
|
+
const query = this.queryClient.getQueryCache().find({
|
|
347
|
+
queryKey: this.listKeyWithFilters(listFilters)
|
|
348
|
+
})
|
|
349
|
+
|
|
350
|
+
return query as Query<ListPaginationResponse<T>, Error, Omit<ListSelector<T>, 'allItems'>, QueryKey>
|
|
351
|
+
}
|
|
339
352
|
}
|
|
340
353
|
|
|
341
354
|
/**
|
package/src/lib/QueryManager.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FetchQueryOptions, InfiniteData, MutationFunctionContext, QueryKey, useInfiniteQuery, useMutation, useQuery } from '@tanstack/react-query'
|
|
1
|
+
import { FetchInfiniteQueryOptions, FetchQueryOptions, InfiniteData, MutationFunctionContext, QueryKey, useInfiniteQuery, useMutation, useQuery } from '@tanstack/react-query'
|
|
2
2
|
import { useCallback } from 'react'
|
|
3
3
|
import { createQueryKeys, QueryKeys } from './QueryKeys'
|
|
4
4
|
import { createMutations, Mutations } from './Mutations'
|
|
@@ -485,4 +485,39 @@ export class QueryManager<T extends QueryItem, F> {
|
|
|
485
485
|
queryFn: () => this.options.retrieveFn(id),
|
|
486
486
|
})
|
|
487
487
|
}
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* Prefetches a paginated list with filters for improved performance
|
|
491
|
+
* @param filters - Filter parameters to apply to the list query
|
|
492
|
+
* @param options - Prefetch options compatible with React Query's infinite queries
|
|
493
|
+
* @param options.initialOffset - Starting offset for pagination (default: 0)
|
|
494
|
+
* @returns Promise that resolves when prefetch is complete
|
|
495
|
+
*
|
|
496
|
+
* @description
|
|
497
|
+
* Use this method to preload paginated list data that users are likely to need soon.
|
|
498
|
+
*
|
|
499
|
+
* @example
|
|
500
|
+
* ```typescript
|
|
501
|
+
* const handle = () => {
|
|
502
|
+
* queryManager.prefetchList(
|
|
503
|
+
* { category: 'electronics' },
|
|
504
|
+
* { staleTime: 5 * 60 * 1000 }
|
|
505
|
+
* )
|
|
506
|
+
* }
|
|
507
|
+
*
|
|
508
|
+
* ```
|
|
509
|
+
*/
|
|
510
|
+
prefetchList(
|
|
511
|
+
filters?: F,
|
|
512
|
+
options: Omit<FetchInfiniteQueryOptions<ListPaginationResponse<T>, Error, ListPaginationResponse<T>, QueryKey, number>, 'queryKey' | 'queryFn' | 'initialPageParam'> & { initialOffset?: number } = {}
|
|
513
|
+
) {
|
|
514
|
+
const { initialOffset = 0, ...prefetchOptions } = options
|
|
515
|
+
|
|
516
|
+
return this.options.queryClient.prefetchInfiniteQuery({
|
|
517
|
+
...prefetchOptions as any,
|
|
518
|
+
initialPageParam: initialOffset,
|
|
519
|
+
queryKey: this.queryKeys.listKeyWithFilters(filters),
|
|
520
|
+
queryFn: () => this.options.listFn(this.options.listLimit ?? 10, initialOffset, filters),
|
|
521
|
+
})
|
|
522
|
+
}
|
|
488
523
|
}
|