nuxt-backend 0.1.0
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/LICENSE +21 -0
- package/README.md +744 -0
- package/dist/convex/auth.config.d.ts +9 -0
- package/dist/convex/auth.config.d.ts.map +1 -0
- package/dist/convex/auth.config.js +14 -0
- package/dist/convex/auth.config.js.map +1 -0
- package/dist/convex/client/index.d.ts +12784 -0
- package/dist/convex/client/index.d.ts.map +1 -0
- package/dist/convex/client/index.js +230 -0
- package/dist/convex/client/index.js.map +1 -0
- package/dist/convex/component/_generated/api.d.ts +36 -0
- package/dist/convex/component/_generated/api.d.ts.map +1 -0
- package/dist/convex/component/_generated/api.js +31 -0
- package/dist/convex/component/_generated/api.js.map +1 -0
- package/dist/convex/component/_generated/component.d.ts +549 -0
- package/dist/convex/component/_generated/component.d.ts.map +1 -0
- package/dist/convex/component/_generated/component.js +11 -0
- package/dist/convex/component/_generated/component.js.map +1 -0
- package/dist/convex/component/_generated/dataModel.d.ts +46 -0
- package/dist/convex/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/convex/component/_generated/dataModel.js +11 -0
- package/dist/convex/component/_generated/dataModel.js.map +1 -0
- package/dist/convex/component/_generated/server.d.ts +121 -0
- package/dist/convex/component/_generated/server.d.ts.map +1 -0
- package/dist/convex/component/_generated/server.js +78 -0
- package/dist/convex/component/_generated/server.js.map +1 -0
- package/dist/convex/component/adapter.d.ts +3 -0
- package/dist/convex/component/adapter.d.ts.map +1 -0
- package/dist/convex/component/adapter.js +23 -0
- package/dist/convex/component/adapter.js.map +1 -0
- package/dist/convex/component/constants.d.ts +3 -0
- package/dist/convex/component/constants.d.ts.map +1 -0
- package/dist/convex/component/constants.js +3 -0
- package/dist/convex/component/constants.js.map +1 -0
- package/dist/convex/component/convex.config.d.ts +14 -0
- package/dist/convex/component/convex.config.d.ts.map +1 -0
- package/dist/convex/component/convex.config.js +15 -0
- package/dist/convex/component/convex.config.js.map +1 -0
- package/dist/convex/component/schema.d.ts +278 -0
- package/dist/convex/component/schema.d.ts.map +1 -0
- package/dist/convex/component/schema.js +91 -0
- package/dist/convex/component/schema.js.map +1 -0
- package/dist/module.d.ts +47 -0
- package/dist/module.js +275 -0
- package/dist/module.json +9 -0
- package/dist/runtime/nuxt/auth/middleware.d.ts +12 -0
- package/dist/runtime/nuxt/auth/middleware.js +31 -0
- package/dist/runtime/nuxt/auth/proxy.d.ts +6 -0
- package/dist/runtime/nuxt/auth/proxy.js +11 -0
- package/dist/runtime/nuxt/auth/server.d.ts +86 -0
- package/dist/runtime/nuxt/auth/server.js +110 -0
- package/dist/runtime/nuxt/config.d.ts +7 -0
- package/dist/runtime/nuxt/config.js +12 -0
- package/dist/runtime/nuxt/index.d.ts +102 -0
- package/dist/runtime/nuxt/index.js +77 -0
- package/dist/runtime/vue/auth/client.d.ts +14 -0
- package/dist/runtime/vue/auth/client.js +7 -0
- package/dist/runtime/vue/auth/cross-domain.d.ts +8 -0
- package/dist/runtime/vue/auth/cross-domain.js +34 -0
- package/dist/runtime/vue/auth/helpers.d.ts +52 -0
- package/dist/runtime/vue/auth/helpers.js +35 -0
- package/dist/runtime/vue/auth/hydration.d.ts +14 -0
- package/dist/runtime/vue/auth/hydration.js +23 -0
- package/dist/runtime/vue/auth/index.d.ts +79 -0
- package/dist/runtime/vue/auth/index.js +81 -0
- package/dist/runtime/vue/auth/plugin.client.d.ts +6 -0
- package/dist/runtime/vue/auth/plugin.client.js +30 -0
- package/dist/runtime/vue/auth/plugin.server.d.ts +17 -0
- package/dist/runtime/vue/auth/plugin.server.js +29 -0
- package/dist/runtime/vue/auth/use-auth.d.ts +84 -0
- package/dist/runtime/vue/auth/use-auth.js +77 -0
- package/dist/runtime/vue/client.d.ts +245 -0
- package/dist/runtime/vue/client.js +341 -0
- package/dist/runtime/vue/composables/use-action.d.ts +45 -0
- package/dist/runtime/vue/composables/use-action.js +10 -0
- package/dist/runtime/vue/composables/use-connection-state.d.ts +25 -0
- package/dist/runtime/vue/composables/use-connection-state.js +13 -0
- package/dist/runtime/vue/composables/use-mutation.d.ts +60 -0
- package/dist/runtime/vue/composables/use-mutation.js +36 -0
- package/dist/runtime/vue/composables/use-paginated-query.d.ts +101 -0
- package/dist/runtime/vue/composables/use-paginated-query.js +508 -0
- package/dist/runtime/vue/composables/use-queries.d.ts +45 -0
- package/dist/runtime/vue/composables/use-queries.js +32 -0
- package/dist/runtime/vue/composables/use-query.d.ts +82 -0
- package/dist/runtime/vue/composables/use-query.js +63 -0
- package/dist/runtime/vue/composables/use-subscription.d.ts +14 -0
- package/dist/runtime/vue/composables/use-subscription.js +15 -0
- package/dist/runtime/vue/hydration.d.ts +42 -0
- package/dist/runtime/vue/hydration.js +18 -0
- package/dist/runtime/vue/index.d.ts +29 -0
- package/dist/runtime/vue/index.js +38 -0
- package/dist/runtime/vue/queries-observer.d.ts +38 -0
- package/dist/runtime/vue/queries-observer.js +127 -0
- package/dist/types.d.ts +3 -0
- package/package.json +157 -0
- package/src/convex/auth.config.ts +21 -0
- package/src/convex/client/index.ts +329 -0
- package/src/convex/component/_generated/api.ts +52 -0
- package/src/convex/component/_generated/component.ts +1338 -0
- package/src/convex/component/_generated/dataModel.ts +60 -0
- package/src/convex/component/_generated/server.ts +156 -0
- package/src/convex/component/adapter.ts +42 -0
- package/src/convex/component/constants.ts +2 -0
- package/src/convex/component/convex.config.ts +16 -0
- package/src/convex/component/schema.ts +93 -0
- package/src/convex/test.ts +34 -0
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
import { getFunctionName } from "convex/server";
|
|
2
|
+
import { ConvexError, compareValues, convexToJson } from "convex/values";
|
|
3
|
+
import { computed, shallowRef, toValue } from "vue";
|
|
4
|
+
import { useConvex } from "../client.js";
|
|
5
|
+
import { useConvexQueries } from "./use-queries.js";
|
|
6
|
+
function isPaginatedQueryValue(value) {
|
|
7
|
+
return typeof value === "object" && value !== null && "page" in value && Array.isArray(value.page) && "isDone" in value && "continueCursor" in value;
|
|
8
|
+
}
|
|
9
|
+
const INTERNAL_ERROR_STATUS = "Error";
|
|
10
|
+
const SPLIT_PAGE_STATUSES = ["SplitRecommended", "SplitRequired"];
|
|
11
|
+
let paginationId = 0;
|
|
12
|
+
function nextPaginationId() {
|
|
13
|
+
return ++paginationId;
|
|
14
|
+
}
|
|
15
|
+
export function resetPaginationId() {
|
|
16
|
+
paginationId = 0;
|
|
17
|
+
}
|
|
18
|
+
export function usePaginatedQuery(query, args, options) {
|
|
19
|
+
validateInitialNumItems(options?.initialNumItems);
|
|
20
|
+
const internal = usePaginatedQueryInternal(
|
|
21
|
+
() => query,
|
|
22
|
+
args,
|
|
23
|
+
() => options,
|
|
24
|
+
() => true
|
|
25
|
+
);
|
|
26
|
+
return internal;
|
|
27
|
+
}
|
|
28
|
+
function validateInitialNumItems(value) {
|
|
29
|
+
if (typeof value !== "number" || value < 0) {
|
|
30
|
+
throw new Error(
|
|
31
|
+
`\`options.initialNumItems\` must be a positive number. Received \`${String(value)}\`.`
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function reshapeToObjectForm(internal) {
|
|
36
|
+
const { results, loadMore } = internal;
|
|
37
|
+
if (internal.status === INTERNAL_ERROR_STATUS) {
|
|
38
|
+
return {
|
|
39
|
+
data: results,
|
|
40
|
+
status: "error",
|
|
41
|
+
canLoadMore: false,
|
|
42
|
+
isLoading: false,
|
|
43
|
+
error: internal.error,
|
|
44
|
+
loadMore
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
if (internal.status === "LoadingFirstPage" || internal.status === "LoadingMore") {
|
|
48
|
+
return {
|
|
49
|
+
data: internal.status === "LoadingFirstPage" ? void 0 : results,
|
|
50
|
+
status: "pending",
|
|
51
|
+
canLoadMore: false,
|
|
52
|
+
isLoading: true,
|
|
53
|
+
error: void 0,
|
|
54
|
+
loadMore
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
data: results,
|
|
59
|
+
status: "success",
|
|
60
|
+
canLoadMore: internal.status === "CanLoadMore",
|
|
61
|
+
isLoading: false,
|
|
62
|
+
error: void 0,
|
|
63
|
+
loadMore
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function usePaginatedQueryInternal(query, args, options, throwOnError) {
|
|
67
|
+
const convex = useConvex();
|
|
68
|
+
const logger = convex.logger;
|
|
69
|
+
function buildInitialState(q, currentArgs, skip, initialNumItems) {
|
|
70
|
+
const id = nextPaginationId();
|
|
71
|
+
return {
|
|
72
|
+
query: q,
|
|
73
|
+
args: currentArgs,
|
|
74
|
+
id,
|
|
75
|
+
nextPageKey: 1,
|
|
76
|
+
pageKeys: skip ? [] : [0],
|
|
77
|
+
queries: skip ? {} : {
|
|
78
|
+
0: {
|
|
79
|
+
query: q,
|
|
80
|
+
args: {
|
|
81
|
+
...currentArgs,
|
|
82
|
+
paginationOpts: {
|
|
83
|
+
numItems: initialNumItems,
|
|
84
|
+
cursor: null,
|
|
85
|
+
id
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
ongoingSplits: {},
|
|
91
|
+
skip
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
const initialQuery = toValue(query);
|
|
95
|
+
const initialRawArgs = toValue(args);
|
|
96
|
+
const initialSkip = initialRawArgs === "skip";
|
|
97
|
+
const initialArgsObject = initialSkip ? {} : initialRawArgs;
|
|
98
|
+
const { initialNumItems: initialInitialNumItems } = toValue(options);
|
|
99
|
+
const state = shallowRef(
|
|
100
|
+
buildInitialState(initialQuery, initialArgsObject, initialSkip, initialInitialNumItems)
|
|
101
|
+
);
|
|
102
|
+
const queriesInput = computed(() => {
|
|
103
|
+
const q = toValue(query);
|
|
104
|
+
const rawArgs = toValue(args);
|
|
105
|
+
const skip = rawArgs === "skip";
|
|
106
|
+
const argsObject = skip ? {} : rawArgs;
|
|
107
|
+
const { initialNumItems } = toValue(options);
|
|
108
|
+
const currentState = state.value;
|
|
109
|
+
const needsReset = getFunctionName(q) !== getFunctionName(currentState.query) || JSON.stringify(convexToJson(argsObject)) !== JSON.stringify(convexToJson(currentState.args)) || skip !== currentState.skip;
|
|
110
|
+
if (needsReset) {
|
|
111
|
+
state.value = buildInitialState(q, argsObject, skip, initialNumItems);
|
|
112
|
+
}
|
|
113
|
+
return state.value.queries;
|
|
114
|
+
});
|
|
115
|
+
const allResults = useConvexQueries(queriesInput);
|
|
116
|
+
function splitPage(key, splitCursor, continueCursor) {
|
|
117
|
+
const prev = state.value;
|
|
118
|
+
const currentQuery = prev.queries[key];
|
|
119
|
+
if (currentQuery === void 0) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const queries = { ...prev.queries };
|
|
123
|
+
const splitKey1 = prev.nextPageKey;
|
|
124
|
+
const splitKey2 = prev.nextPageKey + 1;
|
|
125
|
+
const currentArgs = currentQuery.args;
|
|
126
|
+
const basePaginationOpts = currentArgs.paginationOpts;
|
|
127
|
+
queries[splitKey1] = {
|
|
128
|
+
query: prev.query,
|
|
129
|
+
args: {
|
|
130
|
+
...currentArgs,
|
|
131
|
+
paginationOpts: {
|
|
132
|
+
...basePaginationOpts,
|
|
133
|
+
endCursor: splitCursor
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
queries[splitKey2] = {
|
|
138
|
+
query: prev.query,
|
|
139
|
+
args: {
|
|
140
|
+
...currentArgs,
|
|
141
|
+
paginationOpts: {
|
|
142
|
+
...basePaginationOpts,
|
|
143
|
+
cursor: splitCursor,
|
|
144
|
+
endCursor: continueCursor
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
const ongoingSplits = { ...prev.ongoingSplits };
|
|
149
|
+
ongoingSplits[key] = [splitKey1, splitKey2];
|
|
150
|
+
state.value = {
|
|
151
|
+
...prev,
|
|
152
|
+
nextPageKey: prev.nextPageKey + 2,
|
|
153
|
+
queries,
|
|
154
|
+
ongoingSplits
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
function completeSplit(key) {
|
|
158
|
+
const prev = state.value;
|
|
159
|
+
const completedSplit = prev.ongoingSplits[key];
|
|
160
|
+
if (completedSplit === void 0) return;
|
|
161
|
+
const queries = { ...prev.queries };
|
|
162
|
+
const ongoingSplits = { ...prev.ongoingSplits };
|
|
163
|
+
for (const k of Object.keys(queries)) {
|
|
164
|
+
if (Number(k) === key) Reflect.deleteProperty(queries, k);
|
|
165
|
+
}
|
|
166
|
+
for (const k of Object.keys(ongoingSplits)) {
|
|
167
|
+
if (Number(k) === key) Reflect.deleteProperty(ongoingSplits, k);
|
|
168
|
+
}
|
|
169
|
+
let pageKeys = prev.pageKeys.slice();
|
|
170
|
+
const pageIndex = prev.pageKeys.findIndex((v) => v === key);
|
|
171
|
+
if (pageIndex >= 0) {
|
|
172
|
+
pageKeys = [
|
|
173
|
+
...prev.pageKeys.slice(0, pageIndex),
|
|
174
|
+
...completedSplit,
|
|
175
|
+
...prev.pageKeys.slice(pageIndex + 1)
|
|
176
|
+
];
|
|
177
|
+
}
|
|
178
|
+
state.value = {
|
|
179
|
+
...prev,
|
|
180
|
+
queries,
|
|
181
|
+
pageKeys,
|
|
182
|
+
ongoingSplits
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
function resetState() {
|
|
186
|
+
const q = toValue(query);
|
|
187
|
+
const rawArgs = toValue(args);
|
|
188
|
+
const skip = rawArgs === "skip";
|
|
189
|
+
const argsObject = skip ? {} : rawArgs;
|
|
190
|
+
const { initialNumItems } = toValue(options);
|
|
191
|
+
state.value = buildInitialState(q, argsObject, skip, initialNumItems);
|
|
192
|
+
}
|
|
193
|
+
return computed(() => {
|
|
194
|
+
const currState = state.value;
|
|
195
|
+
const { initialNumItems } = toValue(options);
|
|
196
|
+
const shouldThrow = toValue(throwOnError);
|
|
197
|
+
const resultsObject = allResults.value;
|
|
198
|
+
let lastResult;
|
|
199
|
+
let maybeError;
|
|
200
|
+
const allItems = [];
|
|
201
|
+
for (const pageKey of currState.pageKeys) {
|
|
202
|
+
const currResult = resultsObject[pageKey];
|
|
203
|
+
if (currResult === void 0) {
|
|
204
|
+
lastResult = void 0;
|
|
205
|
+
break;
|
|
206
|
+
}
|
|
207
|
+
if (currResult instanceof Error) {
|
|
208
|
+
const isInvalidCursor = currResult.message.includes("InvalidCursor") || currResult instanceof ConvexError && typeof currResult.data === "object" && currResult.data?.isConvexSystemError === true && currResult.data?.paginationError === "InvalidCursor";
|
|
209
|
+
if (isInvalidCursor) {
|
|
210
|
+
logger.warn?.(
|
|
211
|
+
"usePaginatedQuery hit error, resetting pagination state: " + currResult.message
|
|
212
|
+
);
|
|
213
|
+
resetState();
|
|
214
|
+
return {
|
|
215
|
+
results: [],
|
|
216
|
+
status: "LoadingFirstPage",
|
|
217
|
+
isLoading: true,
|
|
218
|
+
loadMore: noopLoadMore
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
if (shouldThrow) {
|
|
222
|
+
throw currResult;
|
|
223
|
+
}
|
|
224
|
+
maybeError = currResult;
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
if (!isPaginatedQueryValue(currResult)) {
|
|
228
|
+
throw new Error("Paginated query returned a non-pagination result.");
|
|
229
|
+
}
|
|
230
|
+
const result = currResult;
|
|
231
|
+
const ongoingSplit = currState.ongoingSplits[pageKey];
|
|
232
|
+
if (ongoingSplit !== void 0) {
|
|
233
|
+
if (resultsObject[ongoingSplit[0]] !== void 0 && resultsObject[ongoingSplit[1]] !== void 0) {
|
|
234
|
+
completeSplit(pageKey);
|
|
235
|
+
}
|
|
236
|
+
} else if (result.splitCursor && (SPLIT_PAGE_STATUSES.includes(result.pageStatus) || result.page.length > initialNumItems * 2)) {
|
|
237
|
+
splitPage(pageKey, result.splitCursor, result.continueCursor);
|
|
238
|
+
}
|
|
239
|
+
if (result.pageStatus === SPLIT_PAGE_STATUSES[1]) {
|
|
240
|
+
lastResult = void 0;
|
|
241
|
+
break;
|
|
242
|
+
}
|
|
243
|
+
allItems.push(...result.page);
|
|
244
|
+
lastResult = result;
|
|
245
|
+
}
|
|
246
|
+
if (maybeError !== void 0) {
|
|
247
|
+
return {
|
|
248
|
+
results: allItems,
|
|
249
|
+
status: INTERNAL_ERROR_STATUS,
|
|
250
|
+
isLoading: false,
|
|
251
|
+
error: maybeError,
|
|
252
|
+
loadMore: noopLoadMore
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
if (lastResult === void 0) {
|
|
256
|
+
if (currState.nextPageKey === 1) {
|
|
257
|
+
return {
|
|
258
|
+
results: allItems,
|
|
259
|
+
status: "LoadingFirstPage",
|
|
260
|
+
isLoading: true,
|
|
261
|
+
loadMore: noopLoadMore
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
return {
|
|
265
|
+
results: allItems,
|
|
266
|
+
status: "LoadingMore",
|
|
267
|
+
isLoading: true,
|
|
268
|
+
loadMore: noopLoadMore
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
if (lastResult.isDone) {
|
|
272
|
+
return {
|
|
273
|
+
results: allItems,
|
|
274
|
+
status: "Exhausted",
|
|
275
|
+
isLoading: false,
|
|
276
|
+
loadMore: noopLoadMore
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
const continueCursor = lastResult.continueCursor;
|
|
280
|
+
let alreadyLoadingMore = false;
|
|
281
|
+
return {
|
|
282
|
+
results: allItems,
|
|
283
|
+
status: "CanLoadMore",
|
|
284
|
+
isLoading: false,
|
|
285
|
+
loadMore: (numItems) => {
|
|
286
|
+
if (alreadyLoadingMore) return;
|
|
287
|
+
alreadyLoadingMore = true;
|
|
288
|
+
const prev = state.value;
|
|
289
|
+
const rawArgs = toValue(args);
|
|
290
|
+
const argsObject = rawArgs === "skip" ? {} : rawArgs;
|
|
291
|
+
const pageKeys = [...prev.pageKeys, prev.nextPageKey];
|
|
292
|
+
const queries = { ...prev.queries };
|
|
293
|
+
queries[prev.nextPageKey] = {
|
|
294
|
+
query: prev.query,
|
|
295
|
+
args: {
|
|
296
|
+
...argsObject,
|
|
297
|
+
paginationOpts: {
|
|
298
|
+
numItems,
|
|
299
|
+
cursor: continueCursor,
|
|
300
|
+
id: prev.id
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
state.value = {
|
|
305
|
+
...prev,
|
|
306
|
+
nextPageKey: prev.nextPageKey + 1,
|
|
307
|
+
pageKeys,
|
|
308
|
+
queries
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
function noopLoadMore(_numItems) {
|
|
315
|
+
}
|
|
316
|
+
export const useConvexPaginatedQuery = usePaginatedQuery;
|
|
317
|
+
export function usePaginatedQuery_experimental(queryOrOptions, args, options) {
|
|
318
|
+
const firstValue = toValue(queryOrOptions);
|
|
319
|
+
const isObjectOptions = typeof firstValue === "object" && firstValue !== null && "query" in firstValue;
|
|
320
|
+
if (isObjectOptions) {
|
|
321
|
+
const optsGetter = () => toValue(
|
|
322
|
+
queryOrOptions
|
|
323
|
+
);
|
|
324
|
+
const initial = optsGetter();
|
|
325
|
+
validateInitialNumItems(initial.initialNumItems);
|
|
326
|
+
const internal2 = usePaginatedQueryInternal(
|
|
327
|
+
() => optsGetter().query,
|
|
328
|
+
() => optsGetter().args,
|
|
329
|
+
() => ({ initialNumItems: optsGetter().initialNumItems }),
|
|
330
|
+
() => optsGetter().throwOnError ?? false
|
|
331
|
+
);
|
|
332
|
+
return computed(
|
|
333
|
+
() => reshapeToObjectForm(internal2.value)
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
validateInitialNumItems(options?.initialNumItems);
|
|
337
|
+
const query = queryOrOptions;
|
|
338
|
+
const internal = usePaginatedQueryInternal(
|
|
339
|
+
() => query,
|
|
340
|
+
args,
|
|
341
|
+
() => options,
|
|
342
|
+
() => true
|
|
343
|
+
);
|
|
344
|
+
return internal;
|
|
345
|
+
}
|
|
346
|
+
export function optimisticallyUpdateValueInPaginatedQuery(localStore, query, args, updateValue) {
|
|
347
|
+
const expectedArgs = JSON.stringify(convexToJson(args));
|
|
348
|
+
for (const queryResult of localStore.getAllQueries(query)) {
|
|
349
|
+
if (queryResult.value !== void 0) {
|
|
350
|
+
const { paginationOpts: _, ...innerArgs } = queryResult.args;
|
|
351
|
+
if (JSON.stringify(convexToJson(innerArgs)) === expectedArgs) {
|
|
352
|
+
const value = queryResult.value;
|
|
353
|
+
if (typeof value === "object" && value !== null && Array.isArray(value.page)) {
|
|
354
|
+
localStore.setQuery(query, queryResult.args, {
|
|
355
|
+
...value,
|
|
356
|
+
page: value.page.map(updateValue)
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
export function insertAtTop(options) {
|
|
364
|
+
const { paginatedQuery, argsToMatch, localQueryStore, item } = options;
|
|
365
|
+
const queries = localQueryStore.getAllQueries(paginatedQuery);
|
|
366
|
+
const queriesThatMatch = queries.filter((q) => matchesArgs(q.args, argsToMatch));
|
|
367
|
+
const firstPage = queriesThatMatch.find(
|
|
368
|
+
(q) => q.args.paginationOpts.cursor === null
|
|
369
|
+
);
|
|
370
|
+
if (firstPage === void 0 || firstPage.value === void 0) {
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
const value = firstPage.value;
|
|
374
|
+
localQueryStore.setQuery(paginatedQuery, firstPage.args, {
|
|
375
|
+
...value,
|
|
376
|
+
page: [item, ...value.page]
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
export function insertAtBottomIfLoaded(options) {
|
|
380
|
+
const { paginatedQuery, argsToMatch, localQueryStore, item } = options;
|
|
381
|
+
const queries = localQueryStore.getAllQueries(paginatedQuery);
|
|
382
|
+
const queriesThatMatch = queries.filter((q) => matchesArgs(q.args, argsToMatch));
|
|
383
|
+
const lastPage = queriesThatMatch.find(
|
|
384
|
+
(q) => q.value !== void 0 && q.value.isDone
|
|
385
|
+
);
|
|
386
|
+
if (lastPage === void 0) return;
|
|
387
|
+
const value = lastPage.value;
|
|
388
|
+
localQueryStore.setQuery(paginatedQuery, lastPage.args, {
|
|
389
|
+
...value,
|
|
390
|
+
page: [...value.page, item]
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
export function insertAtPosition(options) {
|
|
394
|
+
const {
|
|
395
|
+
paginatedQuery,
|
|
396
|
+
argsToMatch,
|
|
397
|
+
sortOrder,
|
|
398
|
+
sortKeyFromItem,
|
|
399
|
+
localQueryStore,
|
|
400
|
+
item
|
|
401
|
+
} = options;
|
|
402
|
+
const queries = localQueryStore.getAllQueries(paginatedQuery);
|
|
403
|
+
const queryGroups = {};
|
|
404
|
+
for (const q of queries) {
|
|
405
|
+
if (!matchesArgs(q.args, argsToMatch)) continue;
|
|
406
|
+
const key = JSON.stringify(
|
|
407
|
+
Object.fromEntries(
|
|
408
|
+
Object.entries(q.args).map(([k, v]) => [
|
|
409
|
+
k,
|
|
410
|
+
k === "paginationOpts" ? v.id : v
|
|
411
|
+
])
|
|
412
|
+
)
|
|
413
|
+
);
|
|
414
|
+
queryGroups[key] ??= [];
|
|
415
|
+
queryGroups[key].push(q);
|
|
416
|
+
}
|
|
417
|
+
for (const pageQueries of Object.values(queryGroups)) {
|
|
418
|
+
insertAtPositionInPages({
|
|
419
|
+
pageQueries,
|
|
420
|
+
paginatedQuery,
|
|
421
|
+
sortOrder,
|
|
422
|
+
sortKeyFromItem,
|
|
423
|
+
localQueryStore,
|
|
424
|
+
item
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
function insertAtPositionInPages(options) {
|
|
429
|
+
const {
|
|
430
|
+
pageQueries,
|
|
431
|
+
sortOrder,
|
|
432
|
+
sortKeyFromItem,
|
|
433
|
+
localQueryStore,
|
|
434
|
+
item,
|
|
435
|
+
paginatedQuery
|
|
436
|
+
} = options;
|
|
437
|
+
const insertedKey = sortKeyFromItem(item);
|
|
438
|
+
const loadedPages = pageQueries.filter(
|
|
439
|
+
(q) => q.value !== void 0 && q.value.page.length > 0
|
|
440
|
+
);
|
|
441
|
+
const sortedPages = loadedPages.slice().sort((a, b) => {
|
|
442
|
+
const aKey = sortKeyFromItem(
|
|
443
|
+
a.value.page[0]
|
|
444
|
+
);
|
|
445
|
+
const bKey = sortKeyFromItem(
|
|
446
|
+
b.value.page[0]
|
|
447
|
+
);
|
|
448
|
+
return sortOrder === "asc" ? compareValues(aKey, bKey) : compareValues(bKey, aKey);
|
|
449
|
+
});
|
|
450
|
+
const firstLoadedPage = sortedPages[0];
|
|
451
|
+
if (firstLoadedPage === void 0) return;
|
|
452
|
+
const firstValue = firstLoadedPage.value;
|
|
453
|
+
const firstPageKey = sortKeyFromItem(firstValue.page[0]);
|
|
454
|
+
const isBeforeFirstPage = sortOrder === "asc" ? compareValues(insertedKey, firstPageKey) <= 0 : compareValues(insertedKey, firstPageKey) >= 0;
|
|
455
|
+
if (isBeforeFirstPage) {
|
|
456
|
+
if (firstLoadedPage.args.paginationOpts.cursor === null) {
|
|
457
|
+
localQueryStore.setQuery(paginatedQuery, firstLoadedPage.args, {
|
|
458
|
+
...firstValue,
|
|
459
|
+
page: [item, ...firstValue.page]
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
const lastLoadedPage = sortedPages[sortedPages.length - 1];
|
|
465
|
+
if (lastLoadedPage === void 0) return;
|
|
466
|
+
const lastValue = lastLoadedPage.value;
|
|
467
|
+
const lastPageKey = sortKeyFromItem(
|
|
468
|
+
lastValue.page[lastValue.page.length - 1]
|
|
469
|
+
);
|
|
470
|
+
const isAfterLastPage = sortOrder === "asc" ? compareValues(insertedKey, lastPageKey) >= 0 : compareValues(insertedKey, lastPageKey) <= 0;
|
|
471
|
+
if (isAfterLastPage) {
|
|
472
|
+
if (lastValue.isDone) {
|
|
473
|
+
localQueryStore.setQuery(paginatedQuery, lastLoadedPage.args, {
|
|
474
|
+
...lastValue,
|
|
475
|
+
page: [...lastValue.page, item]
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
const successorPageIndex = sortedPages.findIndex((p) => {
|
|
481
|
+
const headKey = sortKeyFromItem(
|
|
482
|
+
p.value.page[0]
|
|
483
|
+
);
|
|
484
|
+
return sortOrder === "asc" ? compareValues(headKey, insertedKey) > 0 : compareValues(headKey, insertedKey) < 0;
|
|
485
|
+
});
|
|
486
|
+
const pageToUpdate = successorPageIndex === -1 ? sortedPages[sortedPages.length - 1] : sortedPages[successorPageIndex - 1];
|
|
487
|
+
if (pageToUpdate === void 0) return;
|
|
488
|
+
const pageValue = pageToUpdate.value;
|
|
489
|
+
const indexWithinPage = pageValue.page.findIndex((e) => {
|
|
490
|
+
const k = sortKeyFromItem(e);
|
|
491
|
+
return sortOrder === "asc" ? compareValues(k, insertedKey) >= 0 : compareValues(k, insertedKey) <= 0;
|
|
492
|
+
});
|
|
493
|
+
const newPage = indexWithinPage === -1 ? [...pageValue.page, item] : [
|
|
494
|
+
...pageValue.page.slice(0, indexWithinPage),
|
|
495
|
+
item,
|
|
496
|
+
...pageValue.page.slice(indexWithinPage)
|
|
497
|
+
];
|
|
498
|
+
localQueryStore.setQuery(paginatedQuery, pageToUpdate.args, {
|
|
499
|
+
...pageValue,
|
|
500
|
+
page: newPage
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
function matchesArgs(args, argsToMatch) {
|
|
504
|
+
if (argsToMatch === void 0) return true;
|
|
505
|
+
return Object.keys(argsToMatch).every(
|
|
506
|
+
(k) => compareValues(argsToMatch[k], args[k]) === 0
|
|
507
|
+
);
|
|
508
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Value } from 'convex/values';
|
|
2
|
+
import { type MaybeRefOrGetter, type ShallowRef } from 'vue';
|
|
3
|
+
import { type CreateWatch } from '../queries-observer.js';
|
|
4
|
+
import type { RequestForQueries } from 'convex/react';
|
|
5
|
+
export type { RequestForQueries };
|
|
6
|
+
export type QueriesResults = Record<string, Value | undefined | Error | unknown>;
|
|
7
|
+
/**
|
|
8
|
+
* Load multiple reactive Convex queries at once.
|
|
9
|
+
*
|
|
10
|
+
* Unlike calling {@link useQuery} multiple times, `useConvexQueries`
|
|
11
|
+
* accepts a reactive map of query descriptors so you can add or remove queries
|
|
12
|
+
* at runtime without breaking the rules of composables.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```vue
|
|
16
|
+
* <script setup lang="ts">
|
|
17
|
+
* import { useConvexQueries } from '#imports'
|
|
18
|
+
* import { api } from '#backend/api'
|
|
19
|
+
*
|
|
20
|
+
* const results = useConvexQueries({
|
|
21
|
+
* tasks: { query: api.tasks.list, args: { completed: false } },
|
|
22
|
+
* user: { query: api.users.me, args: {} },
|
|
23
|
+
* })
|
|
24
|
+
* // results.value.tasks — Task[] | undefined
|
|
25
|
+
* // results.value.user — User | undefined
|
|
26
|
+
* </script>
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @param queries - A reactive (ref/computed/getter) or plain object mapping
|
|
30
|
+
* identifiers to `{ query, args }` descriptors.
|
|
31
|
+
* @returns A shallow ref whose value maps each identifier to its query result,
|
|
32
|
+
* `undefined` while loading, or an `Error` if the query threw.
|
|
33
|
+
*
|
|
34
|
+
* @public
|
|
35
|
+
*/
|
|
36
|
+
export declare function useConvexQueries(queries: MaybeRefOrGetter<RequestForQueries>): ShallowRef<QueriesResults>;
|
|
37
|
+
/**
|
|
38
|
+
* Testable core of {@link useConvexQueries} that accepts a custom
|
|
39
|
+
* `createWatch` factory instead of reading from `inject`.
|
|
40
|
+
*
|
|
41
|
+
* @internal
|
|
42
|
+
*/
|
|
43
|
+
export declare function useQueriesHelper(queries: MaybeRefOrGetter<RequestForQueries>, createWatch: CreateWatch): ShallowRef<QueriesResults>;
|
|
44
|
+
/** @public */
|
|
45
|
+
export declare const useQueries: typeof useConvexQueries;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { onScopeDispose, shallowRef, toValue, watchEffect } from "vue";
|
|
2
|
+
import { useConvex } from "../client.js";
|
|
3
|
+
import { QueriesObserver } from "../queries-observer.js";
|
|
4
|
+
export function useConvexQueries(queries) {
|
|
5
|
+
const convex = useConvex();
|
|
6
|
+
const createWatch = (query, args, {
|
|
7
|
+
journal,
|
|
8
|
+
paginationOptions
|
|
9
|
+
}) => {
|
|
10
|
+
if (paginationOptions) {
|
|
11
|
+
return convex.watchPaginatedQuery(query, args, paginationOptions);
|
|
12
|
+
}
|
|
13
|
+
return convex.watchQuery(query, args, journal ? { journal } : {});
|
|
14
|
+
};
|
|
15
|
+
return useQueriesHelper(queries, createWatch);
|
|
16
|
+
}
|
|
17
|
+
export function useQueriesHelper(queries, createWatch) {
|
|
18
|
+
const observer = new QueriesObserver(createWatch);
|
|
19
|
+
const results = shallowRef({});
|
|
20
|
+
watchEffect((onCleanup) => {
|
|
21
|
+
const currentQueries = toValue(queries);
|
|
22
|
+
observer.setQueries(currentQueries);
|
|
23
|
+
results.value = observer.getLocalResults(currentQueries);
|
|
24
|
+
const unsubscribe = observer.subscribe(() => {
|
|
25
|
+
results.value = observer.getLocalResults(toValue(queries));
|
|
26
|
+
});
|
|
27
|
+
onCleanup(unsubscribe);
|
|
28
|
+
});
|
|
29
|
+
onScopeDispose(() => observer.destroy());
|
|
30
|
+
return results;
|
|
31
|
+
}
|
|
32
|
+
export const useQueries = useConvexQueries;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { FunctionArgs, FunctionReference, FunctionReturnType } from 'convex/server';
|
|
2
|
+
import { type MaybeRefOrGetter, type ShallowRef } from 'vue';
|
|
3
|
+
import type { UseQueryResult as ConvexUseQueryResult } from 'convex/react';
|
|
4
|
+
type OptionalRestArgsOrSkip<FuncRef extends FunctionReference<'query'>> = FuncRef['_args'] extends Record<string, never> ? [args?: MaybeRefOrGetter<Record<string, never> | 'skip'>] : [args: MaybeRefOrGetter<FuncRef['_args'] | 'skip'>];
|
|
5
|
+
export type { OptionalRestArgsOrSkip };
|
|
6
|
+
export type UseQueryResult<QueryResult, ThrowOnError extends boolean = false> = ConvexUseQueryResult<QueryResult, ThrowOnError>;
|
|
7
|
+
interface UseQueryOptions<Query extends FunctionReference<'query'>, ThrowOnError extends boolean> {
|
|
8
|
+
query: Query;
|
|
9
|
+
args: MaybeRefOrGetter<FunctionArgs<Query> | 'skip'>;
|
|
10
|
+
throwOnError?: ThrowOnError;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Load a reactive query within a Vue component.
|
|
14
|
+
*
|
|
15
|
+
* Subscribes to a Convex query and returns a shallow ref that updates
|
|
16
|
+
* automatically whenever the server sends a new result. The subscription is
|
|
17
|
+
* started when the component mounts and cleaned up on unmount.
|
|
18
|
+
*
|
|
19
|
+
* Pass `'skip'` (or a reactive getter that returns `'skip'`) to conditionally
|
|
20
|
+
* disable the subscription without breaking the rules of composables.
|
|
21
|
+
*
|
|
22
|
+
* Returns `undefined` while the first result is loading. Query errors are
|
|
23
|
+
* thrown and propagate to the nearest `errorCaptured` boundary — use
|
|
24
|
+
* {@link useQuery_experimental} if you want errors returned in the result.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```vue
|
|
28
|
+
* <script setup lang="ts">
|
|
29
|
+
* import { useQuery } from '#imports'
|
|
30
|
+
* import { api } from '#backend/api'
|
|
31
|
+
*
|
|
32
|
+
* const tasks = useQuery(api.tasks.list, { completed: false })
|
|
33
|
+
* // tasks.value is Task[] | undefined while the first result is loading
|
|
34
|
+
*
|
|
35
|
+
* // Conditionally disable with 'skip'
|
|
36
|
+
* const profile = useQuery(
|
|
37
|
+
* api.users.get,
|
|
38
|
+
* computed(() => userId.value ? { userId: userId.value } : 'skip'),
|
|
39
|
+
* )
|
|
40
|
+
* </script>
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* @param query - A `FunctionReference` for the public query to run.
|
|
44
|
+
* @param args - Arguments for the query, or `'skip'` to pause the subscription.
|
|
45
|
+
* Accepts a ref, computed, or getter for reactive args.
|
|
46
|
+
* @returns A shallow ref containing the latest query result, or `undefined`
|
|
47
|
+
* while the first result is loading.
|
|
48
|
+
*
|
|
49
|
+
* @public
|
|
50
|
+
*/
|
|
51
|
+
export declare function useQuery<Query extends FunctionReference<'query'>>(query: Query, ...args: OptionalRestArgsOrSkip<Query>): ShallowRef<FunctionReturnType<Query> | undefined>;
|
|
52
|
+
/**
|
|
53
|
+
* Load a reactive query within a Vue component using an options object.
|
|
54
|
+
*
|
|
55
|
+
* This is an experimental form of {@link useQuery} that accepts a single
|
|
56
|
+
* {@link UseQueryOptions} object instead of positional arguments and returns a
|
|
57
|
+
* discriminated-union {@link UseQueryResult} as a shallow ref.
|
|
58
|
+
*
|
|
59
|
+
* Inspect the returned `status` field to use the result. If an error occurs it
|
|
60
|
+
* is present in the result object unless `throwOnError` is `true`, in which case
|
|
61
|
+
* the error is thrown instead.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```vue
|
|
65
|
+
* <script setup lang="ts">
|
|
66
|
+
* import { useQuery_experimental as useQuery } from '#imports'
|
|
67
|
+
* import { api } from '#backend/api'
|
|
68
|
+
*
|
|
69
|
+
* const state = useQuery({ query: api.tasks.list, args: { completed: false } })
|
|
70
|
+
* // state.value.status: 'pending' | 'success' | 'error'
|
|
71
|
+
* </script>
|
|
72
|
+
* ```
|
|
73
|
+
*
|
|
74
|
+
* @param options - Query options. Pass `args: 'skip'` to disable the query.
|
|
75
|
+
* @returns A shallow ref containing the current query state as a
|
|
76
|
+
* {@link UseQueryResult} object.
|
|
77
|
+
*
|
|
78
|
+
* @public
|
|
79
|
+
*/
|
|
80
|
+
export declare function useQuery_experimental<Query extends FunctionReference<'query'>, ThrowOnError extends boolean = false>(options: UseQueryOptions<Query, ThrowOnError>): ShallowRef<UseQueryResult<FunctionReturnType<Query>, ThrowOnError>>;
|
|
81
|
+
/** @public */
|
|
82
|
+
export declare const useConvexQuery: typeof useQuery;
|