zova-module-a-model 5.0.9 → 5.0.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
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zova-module-a-model",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.10",
|
|
4
4
|
"title": "a-model",
|
|
5
5
|
"zovaModule": {
|
|
6
6
|
"capabilities": {
|
|
@@ -63,5 +63,5 @@
|
|
|
63
63
|
"js-cookie": "^3.0.5",
|
|
64
64
|
"localforage": "^1.10.0"
|
|
65
65
|
},
|
|
66
|
-
"gitHead": "
|
|
66
|
+
"gitHead": "6b24f8fbc3857e405882791f096d19d146675077"
|
|
67
67
|
}
|
|
@@ -70,6 +70,24 @@ export class BeanModelPersister<TScopeModule = unknown> extends BeanModelLast<TS
|
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
+
$persisterRemove(queryKey: QueryKey) {
|
|
74
|
+
const query = this.self.$queryFind({ queryKey });
|
|
75
|
+
if (!query) return;
|
|
76
|
+
const options = this._adjustPersisterOptions(query.meta?.persister);
|
|
77
|
+
if (!options) return;
|
|
78
|
+
const storage = this._getPersisterStorage(options);
|
|
79
|
+
if (!storage) return;
|
|
80
|
+
const storageKey = this._getPersisterStorageKey(options, query);
|
|
81
|
+
if (options.sync === true) {
|
|
82
|
+
storage.removeItem(storageKey);
|
|
83
|
+
} else {
|
|
84
|
+
// Persist if we have storage defined, we use timeout to get proper state to be persisted
|
|
85
|
+
setTimeout(() => {
|
|
86
|
+
storage.removeItem(storageKey);
|
|
87
|
+
}, 0);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
73
91
|
protected _createPersister(options?: QueryMetaPersister | boolean) {
|
|
74
92
|
options = this._adjustPersisterOptions(options);
|
|
75
93
|
if (!options) return undefined;
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
SetDataOptions,
|
|
10
10
|
Updater,
|
|
11
11
|
} from '@tanstack/vue-query';
|
|
12
|
+
import localforage from 'localforage';
|
|
12
13
|
import { MaybeRefDeep, NoUnknown } from '../../common/types.js';
|
|
13
14
|
import { Cast } from 'zova';
|
|
14
15
|
import { BeanModelCookie } from './bean.model.cookie.js';
|
|
@@ -33,8 +34,15 @@ export class BeanModelQuery<TScopeModule = unknown> extends BeanModelCookie<TSco
|
|
|
33
34
|
$setQueryData(queryKey, updater, persisterSave, options) {
|
|
34
35
|
queryKey = this._forceQueryKeyPrefix(queryKey);
|
|
35
36
|
const data = this.$queryClient.setQueryData(queryKey, updater, options);
|
|
36
|
-
if (
|
|
37
|
-
|
|
37
|
+
if (data === undefined) {
|
|
38
|
+
if (persisterSave) {
|
|
39
|
+
this.$persisterRemove(queryKey);
|
|
40
|
+
}
|
|
41
|
+
this.$setQueryDataDirect(queryKey, data);
|
|
42
|
+
} else {
|
|
43
|
+
if (persisterSave) {
|
|
44
|
+
this.$persisterSave(queryKey);
|
|
45
|
+
}
|
|
38
46
|
}
|
|
39
47
|
return data;
|
|
40
48
|
}
|
|
@@ -56,4 +64,18 @@ export class BeanModelQuery<TScopeModule = unknown> extends BeanModelCookie<TSco
|
|
|
56
64
|
filters = { ...filters, queryKey };
|
|
57
65
|
return this.$queryClient.invalidateQueries(filters, options);
|
|
58
66
|
}
|
|
67
|
+
|
|
68
|
+
$setQueryDataDirect(queryKey: QueryKey, value: any) {
|
|
69
|
+
const query = this.$queryFind({ queryKey, exact: true });
|
|
70
|
+
query?.setData(value);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async $clear() {
|
|
74
|
+
const queries = this.$queryClient.getQueryCache().getAll();
|
|
75
|
+
for (const query of queries) {
|
|
76
|
+
query?.setData(undefined);
|
|
77
|
+
}
|
|
78
|
+
// remove all db cache
|
|
79
|
+
await localforage.clear();
|
|
80
|
+
}
|
|
59
81
|
}
|
|
@@ -1,15 +1,22 @@
|
|
|
1
1
|
import {
|
|
2
2
|
DefaultError,
|
|
3
|
+
MutationKey,
|
|
3
4
|
MutationObserverOptions,
|
|
4
5
|
QueryClient,
|
|
5
6
|
UseMutationReturnType,
|
|
7
|
+
hashKey,
|
|
6
8
|
useMutation,
|
|
7
9
|
} from '@tanstack/vue-query';
|
|
8
|
-
import { BeanModelUseQuery } from './bean.model.useQuery.js';
|
|
9
10
|
import { MaybeRefDeep } from '../../common/types.js';
|
|
10
11
|
import { UnwrapNestedRefs } from 'vue';
|
|
12
|
+
import { Cast } from 'zova';
|
|
13
|
+
import { BeanModelUseQuery } from './bean.model.useQuery.js';
|
|
14
|
+
|
|
15
|
+
const SymbolUseMutations = Symbol('SymbolUseMutations');
|
|
11
16
|
|
|
12
17
|
export class BeanModelUseMutation<TScopeModule = unknown> extends BeanModelUseQuery<TScopeModule> {
|
|
18
|
+
private [SymbolUseMutations]: Record<string, unknown> = {};
|
|
19
|
+
|
|
13
20
|
$useMutation<TData = unknown, TVariables = void, TContext = unknown>(
|
|
14
21
|
mutationOptions: MaybeRefDeep<MutationObserverOptions<TData, DefaultError, TVariables, TContext>>,
|
|
15
22
|
queryClient?: QueryClient,
|
|
@@ -18,4 +25,21 @@ export class BeanModelUseMutation<TScopeModule = unknown> extends BeanModelUseQu
|
|
|
18
25
|
return useMutation(mutationOptions, queryClient) as any;
|
|
19
26
|
});
|
|
20
27
|
}
|
|
28
|
+
|
|
29
|
+
$useMutationExisting<TData = unknown, TVariables = void, TContext = unknown>(
|
|
30
|
+
mutationOptions: MaybeRefDeep<MutationObserverOptions<TData, DefaultError, TVariables, TContext>>,
|
|
31
|
+
queryClient?: QueryClient,
|
|
32
|
+
): UnwrapNestedRefs<UseMutationReturnType<TData, DefaultError, TVariables, TContext>> {
|
|
33
|
+
let mutationKey: MutationKey = Cast(mutationOptions).mutationKey;
|
|
34
|
+
if (!mutationKey || mutationKey.length === 0) throw new Error('should specify mutationKey');
|
|
35
|
+
mutationKey = this.self._forceQueryKeyPrefix(mutationKey);
|
|
36
|
+
const mutationHash = hashKey(mutationKey);
|
|
37
|
+
if (!this[SymbolUseMutations][mutationHash]) {
|
|
38
|
+
mutationOptions = { ...mutationOptions, mutationKey };
|
|
39
|
+
this[SymbolUseMutations][mutationHash] = this.$useMutation(mutationOptions, queryClient);
|
|
40
|
+
}
|
|
41
|
+
return this[SymbolUseMutations][mutationHash] as UnwrapNestedRefs<
|
|
42
|
+
UseMutationReturnType<TData, DefaultError, TVariables, TContext>
|
|
43
|
+
>;
|
|
44
|
+
}
|
|
21
45
|
}
|
|
@@ -17,7 +17,7 @@ import { BeanModelQuery } from './bean.model.query.js';
|
|
|
17
17
|
const SymbolUseQueries = Symbol('SymbolUseQueries');
|
|
18
18
|
|
|
19
19
|
export class BeanModelUseQuery<TScopeModule = unknown> extends BeanModelQuery<TScopeModule> {
|
|
20
|
-
[SymbolUseQueries]: Record<string, unknown> = {};
|
|
20
|
+
private [SymbolUseQueries]: Record<string, unknown> = {};
|
|
21
21
|
|
|
22
22
|
$useQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(
|
|
23
23
|
options: UndefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
@@ -40,42 +40,6 @@ export class BeanModelUseQuery<TScopeModule = unknown> extends BeanModelQuery<TS
|
|
|
40
40
|
});
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
$useQueryExisting<
|
|
44
|
-
TQueryFnData = unknown,
|
|
45
|
-
TError = DefaultError,
|
|
46
|
-
TData = TQueryFnData,
|
|
47
|
-
TQueryKey extends QueryKey = QueryKey,
|
|
48
|
-
>(
|
|
49
|
-
options: UndefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
50
|
-
queryClient?: QueryClient,
|
|
51
|
-
): UnwrapNestedRefs<UseQueryReturnType<TData, TError>>;
|
|
52
|
-
$useQueryExisting<
|
|
53
|
-
TQueryFnData = unknown,
|
|
54
|
-
TError = DefaultError,
|
|
55
|
-
TData = TQueryFnData,
|
|
56
|
-
TQueryKey extends QueryKey = QueryKey,
|
|
57
|
-
>(
|
|
58
|
-
options: DefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
59
|
-
queryClient?: QueryClient,
|
|
60
|
-
): UnwrapNestedRefs<UseQueryDefinedReturnType<TData, TError>>;
|
|
61
|
-
$useQueryExisting<
|
|
62
|
-
TQueryFnData = unknown,
|
|
63
|
-
TError = DefaultError,
|
|
64
|
-
TData = TQueryFnData,
|
|
65
|
-
TQueryKey extends QueryKey = QueryKey,
|
|
66
|
-
>(
|
|
67
|
-
options: UseQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>,
|
|
68
|
-
queryClient?: QueryClient,
|
|
69
|
-
): UnwrapNestedRefs<UseQueryReturnType<TData, TError>>;
|
|
70
|
-
$useQueryExisting(options, queryClient) {
|
|
71
|
-
const queryKey = this.self._forceQueryKeyPrefix(options.queryKey);
|
|
72
|
-
const queryHash = hashKey(queryKey);
|
|
73
|
-
if (!this[SymbolUseQueries][queryHash]) {
|
|
74
|
-
this[SymbolUseQueries][queryHash] = this.$useQuery(options, queryClient);
|
|
75
|
-
}
|
|
76
|
-
return this[SymbolUseQueries][queryHash];
|
|
77
|
-
}
|
|
78
|
-
|
|
79
43
|
$useQueryLocal<
|
|
80
44
|
TQueryFnData = unknown,
|
|
81
45
|
TError = DefaultError,
|
|
@@ -106,8 +70,8 @@ export class BeanModelUseQuery<TScopeModule = unknown> extends BeanModelQuery<TS
|
|
|
106
70
|
const self = this;
|
|
107
71
|
return useComputed({
|
|
108
72
|
get() {
|
|
109
|
-
const query = self.$
|
|
110
|
-
if (query.data
|
|
73
|
+
const query = self.$useQueryExisting(options, queryClient) as any;
|
|
74
|
+
if (query.data === undefined) {
|
|
111
75
|
const data = self.$persisterLoad(queryKey);
|
|
112
76
|
if (data !== undefined) {
|
|
113
77
|
self.$setQueryData(queryKey, data);
|
|
@@ -116,7 +80,9 @@ export class BeanModelUseQuery<TScopeModule = unknown> extends BeanModelQuery<TS
|
|
|
116
80
|
return query.data;
|
|
117
81
|
},
|
|
118
82
|
set(value) {
|
|
83
|
+
const query = self.$useQueryExisting(options, queryClient) as any;
|
|
119
84
|
self.$setQueryData(queryKey, value, true);
|
|
85
|
+
return query;
|
|
120
86
|
},
|
|
121
87
|
});
|
|
122
88
|
}
|
|
@@ -166,8 +132,8 @@ export class BeanModelUseQuery<TScopeModule = unknown> extends BeanModelQuery<TS
|
|
|
166
132
|
const self = this;
|
|
167
133
|
return useComputed({
|
|
168
134
|
get() {
|
|
169
|
-
const query = self.$
|
|
170
|
-
if (query.data
|
|
135
|
+
const query = self.$useQueryExisting(options, queryClient) as any;
|
|
136
|
+
if (query.data === undefined) {
|
|
171
137
|
const data = self.$persisterLoad(queryKey);
|
|
172
138
|
if (data !== undefined) {
|
|
173
139
|
self.$setQueryData(queryKey, data);
|
|
@@ -176,7 +142,9 @@ export class BeanModelUseQuery<TScopeModule = unknown> extends BeanModelQuery<TS
|
|
|
176
142
|
return query.data;
|
|
177
143
|
},
|
|
178
144
|
set(value) {
|
|
145
|
+
const query = self.$useQueryExisting(options, queryClient) as any;
|
|
179
146
|
self.$setQueryData(queryKey, value, true);
|
|
147
|
+
return query;
|
|
180
148
|
},
|
|
181
149
|
});
|
|
182
150
|
}
|
|
@@ -211,12 +179,50 @@ export class BeanModelUseQuery<TScopeModule = unknown> extends BeanModelQuery<TS
|
|
|
211
179
|
const self = this;
|
|
212
180
|
return useComputed({
|
|
213
181
|
get() {
|
|
214
|
-
const query = self.$
|
|
182
|
+
const query = self.$useQueryExisting(options, queryClient) as any;
|
|
215
183
|
return query.data;
|
|
216
184
|
},
|
|
217
185
|
set(value) {
|
|
186
|
+
const query = self.$useQueryExisting(options, queryClient) as any;
|
|
218
187
|
self.$setQueryData(queryKey, value, false);
|
|
188
|
+
return query;
|
|
219
189
|
},
|
|
220
190
|
});
|
|
221
191
|
}
|
|
192
|
+
|
|
193
|
+
$useQueryExisting<
|
|
194
|
+
TQueryFnData = unknown,
|
|
195
|
+
TError = DefaultError,
|
|
196
|
+
TData = TQueryFnData,
|
|
197
|
+
TQueryKey extends QueryKey = QueryKey,
|
|
198
|
+
>(
|
|
199
|
+
options: UndefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
200
|
+
queryClient?: QueryClient,
|
|
201
|
+
): UnwrapNestedRefs<UseQueryReturnType<TData, TError>>;
|
|
202
|
+
$useQueryExisting<
|
|
203
|
+
TQueryFnData = unknown,
|
|
204
|
+
TError = DefaultError,
|
|
205
|
+
TData = TQueryFnData,
|
|
206
|
+
TQueryKey extends QueryKey = QueryKey,
|
|
207
|
+
>(
|
|
208
|
+
options: DefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
209
|
+
queryClient?: QueryClient,
|
|
210
|
+
): UnwrapNestedRefs<UseQueryDefinedReturnType<TData, TError>>;
|
|
211
|
+
$useQueryExisting<
|
|
212
|
+
TQueryFnData = unknown,
|
|
213
|
+
TError = DefaultError,
|
|
214
|
+
TData = TQueryFnData,
|
|
215
|
+
TQueryKey extends QueryKey = QueryKey,
|
|
216
|
+
>(
|
|
217
|
+
options: UseQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>,
|
|
218
|
+
queryClient?: QueryClient,
|
|
219
|
+
): UnwrapNestedRefs<UseQueryReturnType<TData, TError>>;
|
|
220
|
+
$useQueryExisting(options, queryClient) {
|
|
221
|
+
const queryKey = this.self._forceQueryKeyPrefix(options.queryKey);
|
|
222
|
+
const queryHash = hashKey(queryKey);
|
|
223
|
+
if (!this[SymbolUseQueries][queryHash]) {
|
|
224
|
+
this[SymbolUseQueries][queryHash] = this.$useQuery(options, queryClient);
|
|
225
|
+
}
|
|
226
|
+
return this[SymbolUseQueries][queryHash];
|
|
227
|
+
}
|
|
222
228
|
}
|