@ventlio/tanstack-query 0.2.64 → 0.2.65

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.js CHANGED
@@ -5,7 +5,6 @@ export { useQueryHeaders } from './config/useQueryHeaders.js';
5
5
  export { useReactNativeEnv } from './config/useReactNativeEnv.js';
6
6
  export { scrollToTop } from './helpers/scrollToTop.js';
7
7
  export { getDateInFuture } from './helpers/timeFuncs.js';
8
- export { QueryModel } from './model/Model.js';
9
8
  export { useKeyTrackerModel } from './model/useKeyTrackerModel.js';
10
9
  export { useQueryModel } from './model/useQueryModel.js';
11
10
  export { useRefetchQuery } from './model/useRefetchQuery.js';
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;"}
package/dist/index.mjs CHANGED
@@ -83,94 +83,6 @@ const useQueryHeaders = () => {
83
83
  return { setQueryHeaders, getHeaders };
84
84
  };
85
85
 
86
- class QueryModel {
87
- queryKey;
88
- queryClient;
89
- exact;
90
- constructor(queryKey, queryClient, exact = true) {
91
- this.queryKey = queryKey;
92
- this.queryClient = queryClient;
93
- this.exact = exact;
94
- }
95
- findAll(path) {
96
- const data = this.queryClient.getQueryData(this.queryKey, { exact: this.exact });
97
- if (!data) {
98
- return [];
99
- }
100
- if (!path) {
101
- return Array.isArray(data) ? data : [data];
102
- }
103
- return result(data, path, []);
104
- }
105
- findMany(selector, path) {
106
- const data = this.findAll(path) ?? [];
107
- return data.filter(selector);
108
- }
109
- find(id, path) {
110
- const modelConfig = this.getModelConfig();
111
- if (!modelConfig?.idColumn) {
112
- return undefined;
113
- }
114
- const data = this.findAll(path) ?? [];
115
- return data.find((record) => record[modelConfig.idColumn] === id);
116
- }
117
- update(id, data, path) {
118
- const oldData = this.findAll(path) ?? [];
119
- const modelConfig = this.getModelConfig();
120
- if (!modelConfig?.idColumn) {
121
- return undefined;
122
- }
123
- const idColumn = modelConfig.idColumn;
124
- let updatedRecord = undefined;
125
- const newData = oldData.map((record) => {
126
- let dataRecord = record;
127
- if (dataRecord[idColumn] === id) {
128
- dataRecord = { ...dataRecord, ...data };
129
- updatedRecord = dataRecord;
130
- }
131
- return dataRecord;
132
- });
133
- if (!path) {
134
- this.queryClient.setQueryData(this.queryKey, newData);
135
- }
136
- else {
137
- const queryData = this.queryClient.getQueryData(this.queryKey, { exact: this.exact }) ?? {};
138
- this.queryClient.setQueryData(this.queryKey, set(queryData, path, newData));
139
- }
140
- return updatedRecord;
141
- }
142
- remove(id, path) {
143
- const oldData = this.findAll(path) ?? [];
144
- const modelConfig = this.getModelConfig();
145
- if (!modelConfig?.idColumn) {
146
- return false;
147
- }
148
- const idColumn = modelConfig.idColumn;
149
- let updated = false;
150
- const newData = oldData.filter((record) => {
151
- const dataRecord = record;
152
- if (dataRecord[idColumn] === id) {
153
- updated = true;
154
- return false;
155
- }
156
- return true;
157
- });
158
- if (!path) {
159
- this.queryClient.setQueryData(this.queryKey, newData);
160
- }
161
- else {
162
- const queryData = this.queryClient.getQueryData(this.queryKey, { exact: this.exact }) ?? {};
163
- this.queryClient.setQueryData(this.queryKey, set(queryData, path, newData));
164
- }
165
- return updated;
166
- }
167
- getModelConfig() {
168
- const { options } = this.queryClient.getQueryData(['config']) ?? {};
169
- const { modelConfig } = options ?? {};
170
- return modelConfig;
171
- }
172
- }
173
-
174
86
  const useKeyTrackerModel = (keyTracker) => {
175
87
  const queryClient = useQueryClient();
176
88
  const getQueryKey = (innerKeyTracker) => {
@@ -191,6 +103,24 @@ const useQueryModel = (keyTracker, exact = true) => {
191
103
  const queryClient = useQueryClient();
192
104
  const { getQueryKey } = useKeyTrackerModel(keyTracker);
193
105
  const queryKey = getQueryKey();
106
+ const add = (data, position, path) => {
107
+ let records = findAll(path) ?? [];
108
+ if (!position || position === 'end') {
109
+ records = [...records, data];
110
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
111
+ }
112
+ else if (position === 'start') {
113
+ records = [data, ...records];
114
+ }
115
+ if (!path) {
116
+ queryClient.setQueryData(queryKey, records);
117
+ }
118
+ else {
119
+ const queryData = queryClient.getQueryData(queryKey, { exact }) ?? {};
120
+ queryClient.setQueryData(queryKey, set(queryData, path, records));
121
+ }
122
+ return data;
123
+ };
194
124
  const findAll = (path) => {
195
125
  const data = queryClient.getQueryData(queryKey, { exact });
196
126
  if (!data) {
@@ -268,7 +198,7 @@ const useQueryModel = (keyTracker, exact = true) => {
268
198
  }
269
199
  return updated;
270
200
  };
271
- return { find, findAll, findMany, remove, update };
201
+ return { find, findAll, findMany, remove, update, add };
272
202
  };
273
203
 
274
204
  const useRefetchQuery = async (queryKey) => {
@@ -668,5 +598,5 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
668
598
  return { post, ...mutation };
669
599
  };
670
600
 
671
- export { ContentType, HttpMethod, QueryModel, axiosInstance, bootstrapQueryRequest, buildFormData, errorTransformer, getDateInFuture, makeRequest, scrollToTop, successTransformer, useDeleteRequest, useEnvironmentVariables, useGetRequest, useKeyTrackerModel, usePatchRequest, usePostRequest, useQueryConfig, useQueryHeaders, useQueryModel, useReactNativeEnv, useRefetchQuery };
601
+ export { ContentType, HttpMethod, axiosInstance, bootstrapQueryRequest, buildFormData, errorTransformer, getDateInFuture, makeRequest, scrollToTop, successTransformer, useDeleteRequest, useEnvironmentVariables, useGetRequest, useKeyTrackerModel, usePatchRequest, usePostRequest, useQueryConfig, useQueryHeaders, useQueryModel, useReactNativeEnv, useRefetchQuery };
672
602
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,4 +1,3 @@
1
- export * from './Model';
2
1
  export * from './model.interface';
3
2
  export * from './useKeyTrackerModel';
4
3
  export * from './useQueryModel';
@@ -1,7 +1,9 @@
1
1
  export interface QueryModelBuilder<T> {
2
+ add: (data: T, position?: QueryModelAddPosition, path?: string) => T | undefined;
2
3
  findAll: (path?: string) => T[] | undefined;
3
4
  findMany: (selector: (record: T) => boolean, path?: string) => T[];
4
5
  find: (id: number | string, path?: string) => T | undefined;
5
6
  update: (id: number | string, data: Partial<T>, path?: string) => T | undefined;
6
7
  remove: (id: number, path?: string) => boolean;
7
8
  }
9
+ export type QueryModelAddPosition = 'start' | 'end';
@@ -7,6 +7,24 @@ const useQueryModel = (keyTracker, exact = true) => {
7
7
  const queryClient = useQueryClient();
8
8
  const { getQueryKey } = useKeyTrackerModel(keyTracker);
9
9
  const queryKey = getQueryKey();
10
+ const add = (data, position, path) => {
11
+ let records = findAll(path) ?? [];
12
+ if (!position || position === 'end') {
13
+ records = [...records, data];
14
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
15
+ }
16
+ else if (position === 'start') {
17
+ records = [data, ...records];
18
+ }
19
+ if (!path) {
20
+ queryClient.setQueryData(queryKey, records);
21
+ }
22
+ else {
23
+ const queryData = queryClient.getQueryData(queryKey, { exact }) ?? {};
24
+ queryClient.setQueryData(queryKey, set(queryData, path, records));
25
+ }
26
+ return data;
27
+ };
10
28
  const findAll = (path) => {
11
29
  const data = queryClient.getQueryData(queryKey, { exact });
12
30
  if (!data) {
@@ -84,7 +102,7 @@ const useQueryModel = (keyTracker, exact = true) => {
84
102
  }
85
103
  return updated;
86
104
  };
87
- return { find, findAll, findMany, remove, update };
105
+ return { find, findAll, findMany, remove, update, add };
88
106
  };
89
107
 
90
108
  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.64",
3
+ "version": "0.2.65",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "contributors": [
@@ -1,4 +1,3 @@
1
- export * from './Model';
2
1
  export * from './model.interface';
3
2
  export * from './useKeyTrackerModel';
4
3
  export * from './useQueryModel';
@@ -1,14 +0,0 @@
1
- import type { QueryClient } from '@tanstack/react-query';
2
- import type { QueryModelBuilder } from './model.interface';
3
- export declare class QueryModel<T> implements QueryModelBuilder<T> {
4
- private readonly queryKey;
5
- private readonly queryClient;
6
- private readonly exact;
7
- constructor(queryKey: any[], queryClient: QueryClient, exact?: boolean);
8
- findAll(path?: string): T[] | undefined;
9
- findMany(selector: (record: T) => boolean, path?: string): T[];
10
- find(id: string | number, path?: string): T | undefined;
11
- update(id: string | number, data: Partial<T>, path?: string): T | undefined;
12
- remove(id: number | string, path?: string): boolean;
13
- private getModelConfig;
14
- }
@@ -1,93 +0,0 @@
1
- import result from 'lodash.result';
2
- import set from 'lodash.set';
3
-
4
- class QueryModel {
5
- queryKey;
6
- queryClient;
7
- exact;
8
- constructor(queryKey, queryClient, exact = true) {
9
- this.queryKey = queryKey;
10
- this.queryClient = queryClient;
11
- this.exact = exact;
12
- }
13
- findAll(path) {
14
- const data = this.queryClient.getQueryData(this.queryKey, { exact: this.exact });
15
- if (!data) {
16
- return [];
17
- }
18
- if (!path) {
19
- return Array.isArray(data) ? data : [data];
20
- }
21
- return result(data, path, []);
22
- }
23
- findMany(selector, path) {
24
- const data = this.findAll(path) ?? [];
25
- return data.filter(selector);
26
- }
27
- find(id, path) {
28
- const modelConfig = this.getModelConfig();
29
- if (!modelConfig?.idColumn) {
30
- return undefined;
31
- }
32
- const data = this.findAll(path) ?? [];
33
- return data.find((record) => record[modelConfig.idColumn] === id);
34
- }
35
- update(id, data, path) {
36
- const oldData = this.findAll(path) ?? [];
37
- const modelConfig = this.getModelConfig();
38
- if (!modelConfig?.idColumn) {
39
- return undefined;
40
- }
41
- const idColumn = modelConfig.idColumn;
42
- let updatedRecord = undefined;
43
- const newData = oldData.map((record) => {
44
- let dataRecord = record;
45
- if (dataRecord[idColumn] === id) {
46
- dataRecord = { ...dataRecord, ...data };
47
- updatedRecord = dataRecord;
48
- }
49
- return dataRecord;
50
- });
51
- if (!path) {
52
- this.queryClient.setQueryData(this.queryKey, newData);
53
- }
54
- else {
55
- const queryData = this.queryClient.getQueryData(this.queryKey, { exact: this.exact }) ?? {};
56
- this.queryClient.setQueryData(this.queryKey, set(queryData, path, newData));
57
- }
58
- return updatedRecord;
59
- }
60
- remove(id, path) {
61
- const oldData = this.findAll(path) ?? [];
62
- const modelConfig = this.getModelConfig();
63
- if (!modelConfig?.idColumn) {
64
- return false;
65
- }
66
- const idColumn = modelConfig.idColumn;
67
- let updated = false;
68
- const newData = oldData.filter((record) => {
69
- const dataRecord = record;
70
- if (dataRecord[idColumn] === id) {
71
- updated = true;
72
- return false;
73
- }
74
- return true;
75
- });
76
- if (!path) {
77
- this.queryClient.setQueryData(this.queryKey, newData);
78
- }
79
- else {
80
- const queryData = this.queryClient.getQueryData(this.queryKey, { exact: this.exact }) ?? {};
81
- this.queryClient.setQueryData(this.queryKey, set(queryData, path, newData));
82
- }
83
- return updated;
84
- }
85
- getModelConfig() {
86
- const { options } = this.queryClient.getQueryData(['config']) ?? {};
87
- const { modelConfig } = options ?? {};
88
- return modelConfig;
89
- }
90
- }
91
-
92
- export { QueryModel };
93
- //# sourceMappingURL=Model.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Model.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,107 +0,0 @@
1
- import type { QueryClient } from '@tanstack/react-query';
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';
6
-
7
- export class QueryModel<T> implements QueryModelBuilder<T> {
8
- constructor(
9
- private readonly queryKey: any[],
10
- private readonly queryClient: QueryClient,
11
- private readonly exact: boolean = true
12
- ) {}
13
-
14
- public findAll(path?: string): T[] | undefined {
15
- const data = this.queryClient.getQueryData(this.queryKey, { exact: this.exact });
16
-
17
- if (!data) {
18
- return [];
19
- }
20
-
21
- if (!path) {
22
- return Array.isArray(data) ? data : [data];
23
- }
24
-
25
- return result<T[]>(data, path, []);
26
- }
27
-
28
- public findMany(selector: (record: T) => boolean, path?: string): T[] {
29
- const data = this.findAll(path) ?? [];
30
- return data.filter(selector);
31
- }
32
-
33
- find(id: string | number, path?: string): T | undefined {
34
- const modelConfig = this.getModelConfig();
35
-
36
- if (!modelConfig?.idColumn) {
37
- return undefined;
38
- }
39
- const data = this.findAll(path) ?? [];
40
-
41
- return data.find((record) => (record as Record<string, any>)[modelConfig.idColumn] === id);
42
- }
43
-
44
- update(id: string | number, data: Partial<T>, path?: string): T | undefined {
45
- const oldData = this.findAll(path) ?? [];
46
- const modelConfig = this.getModelConfig();
47
-
48
- if (!modelConfig?.idColumn) {
49
- return undefined;
50
- }
51
- const idColumn = modelConfig.idColumn;
52
-
53
- let updatedRecord: T | undefined = undefined;
54
- const newData = oldData.map((record) => {
55
- let dataRecord = record as Record<string, any>;
56
-
57
- if (dataRecord[idColumn] === id) {
58
- dataRecord = { ...dataRecord, ...data };
59
- updatedRecord = dataRecord as T;
60
- }
61
-
62
- return dataRecord;
63
- });
64
-
65
- if (!path) {
66
- this.queryClient.setQueryData(this.queryKey, newData);
67
- } else {
68
- const queryData = this.queryClient.getQueryData(this.queryKey, { exact: this.exact }) ?? {};
69
- this.queryClient.setQueryData(this.queryKey, set(queryData, path, newData));
70
- }
71
- return updatedRecord;
72
- }
73
-
74
- remove(id: number | string, path?: string): boolean {
75
- const oldData = this.findAll(path) ?? [];
76
- const modelConfig = this.getModelConfig();
77
-
78
- if (!modelConfig?.idColumn) {
79
- return false;
80
- }
81
- const idColumn = modelConfig.idColumn;
82
- let updated = false;
83
- const newData = oldData.filter((record) => {
84
- const dataRecord = record as Record<string, any>;
85
- if (dataRecord[idColumn] === id) {
86
- updated = true;
87
- return false;
88
- }
89
- return true;
90
- });
91
-
92
- if (!path) {
93
- this.queryClient.setQueryData(this.queryKey, newData);
94
- } else {
95
- const queryData = this.queryClient.getQueryData(this.queryKey, { exact: this.exact }) ?? {};
96
- this.queryClient.setQueryData(this.queryKey, set(queryData, path, newData));
97
- }
98
- return updated;
99
- }
100
-
101
- private getModelConfig() {
102
- const { options } = this.queryClient.getQueryData<TanstackQueryConfig>(['config']) ?? {};
103
- const { modelConfig } = options ?? {};
104
-
105
- return modelConfig;
106
- }
107
- }