@ventlio/tanstack-query 0.2.63-beta.6.2 → 0.2.63-beta.7

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/dist/index.mjs CHANGED
@@ -139,7 +139,7 @@ class QueryModel {
139
139
  }
140
140
  return updatedRecord;
141
141
  }
142
- delete(id, path) {
142
+ remove(id, path) {
143
143
  const oldData = this.findAll(path) ?? [];
144
144
  const modelConfig = this.getModelConfig();
145
145
  if (!modelConfig?.idColumn) {
@@ -187,11 +187,88 @@ const useKeyTrackerModel = (keyTracker) => {
187
187
  return { refetchQuery, getQueryKey };
188
188
  };
189
189
 
190
- const useQueryModel = (keyTracker) => {
190
+ const useQueryModel = (keyTracker, exact = true) => {
191
191
  const queryClient = useQueryClient();
192
192
  const { getQueryKey } = useKeyTrackerModel(keyTracker);
193
193
  const queryKey = getQueryKey();
194
- return new QueryModel(queryKey, queryClient);
194
+ const findAll = (path) => {
195
+ const data = queryClient.getQueryData(queryKey, { exact });
196
+ if (!data) {
197
+ return [];
198
+ }
199
+ if (!path) {
200
+ return Array.isArray(data) ? data : [data];
201
+ }
202
+ return result(data, path, []);
203
+ };
204
+ const findMany = (selector, path) => {
205
+ const data = findAll(path) ?? [];
206
+ return data.filter(selector);
207
+ };
208
+ const find = (id, path) => {
209
+ const modelConfig = getModelConfig();
210
+ if (!modelConfig?.idColumn) {
211
+ return undefined;
212
+ }
213
+ const data = findAll(path) ?? [];
214
+ return data.find((record) => record[modelConfig.idColumn] === id);
215
+ };
216
+ const getModelConfig = () => {
217
+ const { options } = queryClient.getQueryData(['config']) ?? {};
218
+ const { modelConfig } = options ?? {};
219
+ return modelConfig;
220
+ };
221
+ const update = (id, data, path) => {
222
+ const oldData = findAll(path) ?? [];
223
+ const modelConfig = getModelConfig();
224
+ if (!modelConfig?.idColumn) {
225
+ return undefined;
226
+ }
227
+ const idColumn = modelConfig.idColumn;
228
+ let updatedRecord = undefined;
229
+ const newData = oldData.map((record) => {
230
+ let dataRecord = record;
231
+ if (dataRecord[idColumn] === id) {
232
+ dataRecord = { ...dataRecord, ...data };
233
+ updatedRecord = dataRecord;
234
+ }
235
+ return dataRecord;
236
+ });
237
+ if (!path) {
238
+ queryClient.setQueryData(queryKey, newData);
239
+ }
240
+ else {
241
+ const queryData = queryClient.getQueryData(queryKey, { exact }) ?? {};
242
+ queryClient.setQueryData(queryKey, set(queryData, path, newData));
243
+ }
244
+ return updatedRecord;
245
+ };
246
+ const remove = (id, path) => {
247
+ const oldData = findAll(path) ?? [];
248
+ const modelConfig = getModelConfig();
249
+ if (!modelConfig?.idColumn) {
250
+ return false;
251
+ }
252
+ const idColumn = modelConfig.idColumn;
253
+ let updated = false;
254
+ const newData = oldData.filter((record) => {
255
+ const dataRecord = record;
256
+ if (dataRecord[idColumn] === id) {
257
+ updated = true;
258
+ return false;
259
+ }
260
+ return true;
261
+ });
262
+ if (!path) {
263
+ queryClient.setQueryData(queryKey, newData);
264
+ }
265
+ else {
266
+ const queryData = queryClient.getQueryData(queryKey, { exact }) ?? {};
267
+ queryClient.setQueryData(queryKey, set(queryData, path, newData));
268
+ }
269
+ return updated;
270
+ };
271
+ return { find, findAll, findMany, remove, update };
195
272
  };
196
273
 
197
274
  const useRefetchQuery = async (queryKey) => {
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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 @@ export declare class QueryModel<T> implements QueryModelBuilder<T> {
9
9
  findMany(selector: (record: T) => boolean, path?: string): T[];
10
10
  find(id: string | number, path?: string): T | undefined;
11
11
  update(id: string | number, data: Partial<T>, path?: string): T | undefined;
12
- delete(id: number | string, path?: string): boolean;
12
+ remove(id: number | string, path?: string): boolean;
13
13
  private getModelConfig;
14
14
  }
@@ -57,7 +57,7 @@ class QueryModel {
57
57
  }
58
58
  return updatedRecord;
59
59
  }
60
- delete(id, path) {
60
+ remove(id, path) {
61
61
  const oldData = this.findAll(path) ?? [];
62
62
  const modelConfig = this.getModelConfig();
63
63
  if (!modelConfig?.idColumn) {
@@ -3,5 +3,5 @@ export interface QueryModelBuilder<T> {
3
3
  findMany: (selector: (record: T) => boolean, path?: string) => T[];
4
4
  find: (id: number | string, path?: string) => T | undefined;
5
5
  update: (id: number | string, data: Partial<T>, path?: string) => T | undefined;
6
- delete: (id: number, path?: string) => boolean;
6
+ remove: (id: number, path?: string) => boolean;
7
7
  }
@@ -1,2 +1,2 @@
1
- import { QueryModel } from './Model';
2
- export declare const useQueryModel: <T>(keyTracker: string) => QueryModel<T>;
1
+ import type { QueryModelBuilder } from './model.interface';
2
+ export declare const useQueryModel: <T>(keyTracker: string, exact?: boolean) => QueryModelBuilder<T>;
@@ -1,12 +1,90 @@
1
1
  import { useQueryClient } from '@tanstack/react-query';
2
- import { QueryModel } from './Model.js';
2
+ import result from 'lodash.result';
3
+ import set from 'lodash.set';
3
4
  import { useKeyTrackerModel } from './useKeyTrackerModel.js';
4
5
 
5
- const useQueryModel = (keyTracker) => {
6
+ const useQueryModel = (keyTracker, exact = true) => {
6
7
  const queryClient = useQueryClient();
7
8
  const { getQueryKey } = useKeyTrackerModel(keyTracker);
8
9
  const queryKey = getQueryKey();
9
- return new QueryModel(queryKey, queryClient);
10
+ const findAll = (path) => {
11
+ const data = queryClient.getQueryData(queryKey, { exact });
12
+ if (!data) {
13
+ return [];
14
+ }
15
+ if (!path) {
16
+ return Array.isArray(data) ? data : [data];
17
+ }
18
+ return result(data, path, []);
19
+ };
20
+ const findMany = (selector, path) => {
21
+ const data = findAll(path) ?? [];
22
+ return data.filter(selector);
23
+ };
24
+ const find = (id, path) => {
25
+ const modelConfig = getModelConfig();
26
+ if (!modelConfig?.idColumn) {
27
+ return undefined;
28
+ }
29
+ const data = findAll(path) ?? [];
30
+ return data.find((record) => record[modelConfig.idColumn] === id);
31
+ };
32
+ const getModelConfig = () => {
33
+ const { options } = queryClient.getQueryData(['config']) ?? {};
34
+ const { modelConfig } = options ?? {};
35
+ return modelConfig;
36
+ };
37
+ const update = (id, data, path) => {
38
+ const oldData = findAll(path) ?? [];
39
+ const modelConfig = getModelConfig();
40
+ if (!modelConfig?.idColumn) {
41
+ return undefined;
42
+ }
43
+ const idColumn = modelConfig.idColumn;
44
+ let updatedRecord = undefined;
45
+ const newData = oldData.map((record) => {
46
+ let dataRecord = record;
47
+ if (dataRecord[idColumn] === id) {
48
+ dataRecord = { ...dataRecord, ...data };
49
+ updatedRecord = dataRecord;
50
+ }
51
+ return dataRecord;
52
+ });
53
+ if (!path) {
54
+ queryClient.setQueryData(queryKey, newData);
55
+ }
56
+ else {
57
+ const queryData = queryClient.getQueryData(queryKey, { exact }) ?? {};
58
+ queryClient.setQueryData(queryKey, set(queryData, path, newData));
59
+ }
60
+ return updatedRecord;
61
+ };
62
+ const remove = (id, path) => {
63
+ const oldData = findAll(path) ?? [];
64
+ const modelConfig = getModelConfig();
65
+ if (!modelConfig?.idColumn) {
66
+ return false;
67
+ }
68
+ const idColumn = modelConfig.idColumn;
69
+ let updated = false;
70
+ const newData = oldData.filter((record) => {
71
+ const dataRecord = record;
72
+ if (dataRecord[idColumn] === id) {
73
+ updated = true;
74
+ return false;
75
+ }
76
+ return true;
77
+ });
78
+ if (!path) {
79
+ queryClient.setQueryData(queryKey, newData);
80
+ }
81
+ else {
82
+ const queryData = queryClient.getQueryData(queryKey, { exact }) ?? {};
83
+ queryClient.setQueryData(queryKey, set(queryData, path, newData));
84
+ }
85
+ return updated;
86
+ };
87
+ return { find, findAll, findMany, remove, update };
10
88
  };
11
89
 
12
90
  export { useQueryModel };
@@ -1 +1 @@
1
- {"version":3,"file":"useQueryModel.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;"}
1
+ {"version":3,"file":"useQueryModel.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ventlio/tanstack-query",
3
- "version": "0.2.63-beta.6.2",
3
+ "version": "0.2.63-beta.7",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "contributors": [
@@ -71,7 +71,7 @@ export class QueryModel<T> implements QueryModelBuilder<T> {
71
71
  return updatedRecord;
72
72
  }
73
73
 
74
- delete(id: number | string, path?: string): boolean {
74
+ remove(id: number | string, path?: string): boolean {
75
75
  const oldData = this.findAll(path) ?? [];
76
76
  const modelConfig = this.getModelConfig();
77
77
 
@@ -3,5 +3,5 @@ export interface QueryModelBuilder<T> {
3
3
  findMany: (selector: (record: T) => boolean, path?: string) => T[];
4
4
  find: (id: number | string, path?: string) => T | undefined;
5
5
  update: (id: number | string, data: Partial<T>, path?: string) => T | undefined;
6
- delete: (id: number, path?: string) => boolean;
6
+ remove: (id: number, path?: string) => boolean;
7
7
  }
@@ -1,10 +1,108 @@
1
1
  import { useQueryClient } from '@tanstack/react-query';
2
- import { QueryModel } from './Model';
2
+ import result from 'lodash.result';
3
+ import set from 'lodash.set';
4
+ import type { TanstackQueryConfig } from '../types';
5
+ import type { QueryModelBuilder } from './model.interface';
3
6
  import { useKeyTrackerModel } from './useKeyTrackerModel';
4
7
 
5
- export const useQueryModel = <T>(keyTracker: string): QueryModel<T> => {
8
+ export const useQueryModel = <T>(keyTracker: string, exact: boolean = true): QueryModelBuilder<T> => {
6
9
  const queryClient = useQueryClient();
7
10
  const { getQueryKey } = useKeyTrackerModel(keyTracker);
8
11
  const queryKey = getQueryKey() as any[];
9
- return new QueryModel<T>(queryKey, queryClient);
12
+
13
+ const findAll = (path?: string): T[] | undefined => {
14
+ const data = queryClient.getQueryData(queryKey, { exact });
15
+
16
+ if (!data) {
17
+ return [];
18
+ }
19
+
20
+ if (!path) {
21
+ return Array.isArray(data) ? data : [data];
22
+ }
23
+
24
+ return result<T[]>(data, path, []);
25
+ };
26
+
27
+ const findMany = (selector: (record: T) => boolean, path?: string): T[] => {
28
+ const data = findAll(path) ?? [];
29
+ return data.filter(selector);
30
+ };
31
+
32
+ const find = (id: string | number, path?: string): T | undefined => {
33
+ const modelConfig = getModelConfig();
34
+
35
+ if (!modelConfig?.idColumn) {
36
+ return undefined;
37
+ }
38
+ const data = findAll(path) ?? [];
39
+
40
+ return data.find((record) => (record as Record<string, any>)[modelConfig.idColumn] === id);
41
+ };
42
+
43
+ const getModelConfig = () => {
44
+ const { options } = queryClient.getQueryData<TanstackQueryConfig>(['config']) ?? {};
45
+ const { modelConfig } = options ?? {};
46
+
47
+ return modelConfig;
48
+ };
49
+
50
+ const update = (id: string | number, data: Partial<T>, path?: string): T | undefined => {
51
+ const oldData = findAll(path) ?? [];
52
+ const modelConfig = getModelConfig();
53
+
54
+ if (!modelConfig?.idColumn) {
55
+ return undefined;
56
+ }
57
+ const idColumn = modelConfig.idColumn;
58
+
59
+ let updatedRecord: T | undefined = undefined;
60
+ const newData = oldData.map((record) => {
61
+ let dataRecord = record as Record<string, any>;
62
+
63
+ if (dataRecord[idColumn] === id) {
64
+ dataRecord = { ...dataRecord, ...data };
65
+ updatedRecord = dataRecord as T;
66
+ }
67
+
68
+ return dataRecord;
69
+ });
70
+
71
+ if (!path) {
72
+ queryClient.setQueryData(queryKey, newData);
73
+ } else {
74
+ const queryData = queryClient.getQueryData(queryKey, { exact }) ?? {};
75
+ queryClient.setQueryData(queryKey, set(queryData, path, newData));
76
+ }
77
+ return updatedRecord;
78
+ };
79
+
80
+ const remove = (id: number | string, path?: string): boolean => {
81
+ const oldData = findAll(path) ?? [];
82
+ const modelConfig = getModelConfig();
83
+
84
+ if (!modelConfig?.idColumn) {
85
+ return false;
86
+ }
87
+ const idColumn = modelConfig.idColumn;
88
+ let updated = false;
89
+ const newData = oldData.filter((record) => {
90
+ const dataRecord = record as Record<string, any>;
91
+ if (dataRecord[idColumn] === id) {
92
+ updated = true;
93
+ return false;
94
+ }
95
+ return true;
96
+ });
97
+
98
+ if (!path) {
99
+ queryClient.setQueryData(queryKey, newData);
100
+ } else {
101
+ const queryData = queryClient.getQueryData(queryKey, { exact }) ?? {};
102
+ queryClient.setQueryData(queryKey, set(queryData, path, newData));
103
+ }
104
+ return updated;
105
+ };
106
+
107
+ return { find, findAll, findMany, remove, update };
10
108
  };