@pol-studios/db 1.0.10 → 1.0.12

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.
Files changed (120) hide show
  1. package/dist/DataLayerContext-CL6alnkb.d.ts +755 -0
  2. package/dist/UserMetadataContext-B8gVWGMl.d.ts +35 -0
  3. package/dist/UserMetadataContext-DntmpK41.d.ts +33 -0
  4. package/dist/auth/context.d.ts +3 -2
  5. package/dist/auth/context.js +5 -4
  6. package/dist/auth/guards.js +2 -2
  7. package/dist/auth/hooks.d.ts +3 -3
  8. package/dist/auth/hooks.js +6 -5
  9. package/dist/auth/index.d.ts +3 -2
  10. package/dist/auth/index.js +8 -6
  11. package/dist/{canvas-UVNDA54X.node → canvas-C4TBBDUL.node} +0 -0
  12. package/dist/{canvas-75Y7XMF3.js → canvas-ZQNCL7JL.js} +2 -2
  13. package/dist/chunk-5EFDS7SR.js +205 -0
  14. package/dist/chunk-5EFDS7SR.js.map +1 -0
  15. package/dist/{chunk-BRTW7CO5.js → chunk-7SCJNYTE.js} +1 -9
  16. package/dist/chunk-7SCJNYTE.js.map +1 -0
  17. package/dist/chunk-DJ6VLEAL.js +247 -0
  18. package/dist/chunk-DJ6VLEAL.js.map +1 -0
  19. package/dist/{chunk-Y3INY2CS.js → chunk-GC3TBUWE.js} +1 -1
  20. package/dist/chunk-GC3TBUWE.js.map +1 -0
  21. package/dist/{chunk-7HG6G25H.js → chunk-H3LNH2NT.js} +169 -268
  22. package/dist/chunk-H3LNH2NT.js.map +1 -0
  23. package/dist/{chunk-USJYMRUO.js → chunk-HAWJTZCK.js} +2 -2
  24. package/dist/chunk-HAWJTZCK.js.map +1 -0
  25. package/dist/chunk-JAATANS3.js +429 -0
  26. package/dist/chunk-JAATANS3.js.map +1 -0
  27. package/dist/chunk-LNJ3WF7V.js +470 -0
  28. package/dist/chunk-LNJ3WF7V.js.map +1 -0
  29. package/dist/chunk-N26IEHZT.js +79 -0
  30. package/dist/chunk-N26IEHZT.js.map +1 -0
  31. package/dist/chunk-NSIAAYW3.js +1 -0
  32. package/dist/chunk-NSIAAYW3.js.map +1 -0
  33. package/dist/chunk-NZON56CB.js +3864 -0
  34. package/dist/chunk-NZON56CB.js.map +1 -0
  35. package/dist/{chunk-O3K7R32P.js → chunk-OQ7U6EQ3.js} +118 -123
  36. package/dist/chunk-OQ7U6EQ3.js.map +1 -0
  37. package/dist/chunk-SM73S2DY.js +11 -0
  38. package/dist/chunk-SM73S2DY.js.map +1 -0
  39. package/dist/{chunk-JUVE3DWY.js → chunk-TKWR5AAY.js} +47 -65
  40. package/dist/chunk-TKWR5AAY.js.map +1 -0
  41. package/dist/{chunk-2IFGILT3.js → chunk-U5UNPBKB.js} +76 -107
  42. package/dist/chunk-U5UNPBKB.js.map +1 -0
  43. package/dist/chunk-WGDJ4IXR.js +921 -0
  44. package/dist/chunk-WGDJ4IXR.js.map +1 -0
  45. package/dist/chunk-WVF7RUW5.js +186 -0
  46. package/dist/chunk-WVF7RUW5.js.map +1 -0
  47. package/dist/{chunk-ZTSBF536.js → chunk-X3HZLNBV.js} +637 -435
  48. package/dist/chunk-X3HZLNBV.js.map +1 -0
  49. package/dist/{chunk-EL45Z26M.js → chunk-XU3SBFAG.js} +1219 -208
  50. package/dist/chunk-XU3SBFAG.js.map +1 -0
  51. package/dist/chunk-ZVBHWU7O.js +1412 -0
  52. package/dist/chunk-ZVBHWU7O.js.map +1 -0
  53. package/dist/client/index.d.ts +1 -1
  54. package/dist/client/index.js +42 -23
  55. package/dist/client/index.js.map +1 -1
  56. package/dist/core/index.d.ts +19 -0
  57. package/dist/{index-BFu5_dS8.d.ts → database.types-ChFCG-4M.d.ts} +1 -177
  58. package/dist/gen/index.js +4 -2
  59. package/dist/hooks/index.d.ts +10 -3
  60. package/dist/hooks/index.js +8 -10
  61. package/dist/index-CQLyNG6A.d.ts +433 -0
  62. package/dist/index.d.ts +12 -8
  63. package/dist/index.js +92 -48
  64. package/dist/index.native.d.ts +373 -33
  65. package/dist/index.native.js +79 -47
  66. package/dist/index.web.d.ts +10 -7
  67. package/dist/index.web.js +83 -78
  68. package/dist/index.web.js.map +1 -1
  69. package/dist/mutation/index.d.ts +2 -2
  70. package/dist/mutation/index.js +307 -122
  71. package/dist/mutation/index.js.map +1 -1
  72. package/dist/parser/index.js +2 -2
  73. package/dist/{pdf-3TIGQRLA.js → pdf-PHXP7RHD.js} +2 -2
  74. package/dist/powersync-bridge/index.d.ts +284 -0
  75. package/dist/powersync-bridge/index.js +22 -0
  76. package/dist/powersync-bridge/index.js.map +1 -0
  77. package/dist/query/index.js +5 -5
  78. package/dist/realtime/index.js +252 -128
  79. package/dist/realtime/index.js.map +1 -1
  80. package/dist/{UserMetadataContext-BYYqA6LI.d.ts → setupAuthContext-Kv-THH-h.d.ts} +1 -29
  81. package/dist/types/index.d.ts +5 -1
  82. package/dist/types/index.js +10 -5
  83. package/dist/{useBatchUpsert-CSQVX7w8.d.ts → useBatchUpsert-9OYjibLh.d.ts} +1 -1
  84. package/dist/{useDbCount-RGCuHmHp.d.ts → useDbCount-BG356T9i.d.ts} +3 -719
  85. package/dist/{useReceiptAI-Bn0czE7C.d.ts → useReceiptAI-6HkRpRml.d.ts} +1 -1
  86. package/dist/{useResolveFeedback-CpZPP8Pw.d.ts → useResolveFeedback-BWmatBlE.d.ts} +26 -45
  87. package/dist/{useSupabase-pPhUZHcl.d.ts → useSupabase-DvWVuHHE.d.ts} +2 -1
  88. package/dist/with-auth/index.d.ts +704 -0
  89. package/dist/with-auth/index.js +1221 -0
  90. package/dist/with-auth/index.js.map +1 -0
  91. package/package.json +25 -10
  92. package/dist/chunk-2IFGILT3.js.map +0 -1
  93. package/dist/chunk-3M2U6TXH.js +0 -928
  94. package/dist/chunk-3M2U6TXH.js.map +0 -1
  95. package/dist/chunk-5ZYAEGCJ.js +0 -416
  96. package/dist/chunk-5ZYAEGCJ.js.map +0 -1
  97. package/dist/chunk-7HG6G25H.js.map +0 -1
  98. package/dist/chunk-7XT7K4QT.js +0 -2687
  99. package/dist/chunk-7XT7K4QT.js.map +0 -1
  100. package/dist/chunk-AWFMICFV.js +0 -158
  101. package/dist/chunk-AWFMICFV.js.map +0 -1
  102. package/dist/chunk-BRTW7CO5.js.map +0 -1
  103. package/dist/chunk-EL45Z26M.js.map +0 -1
  104. package/dist/chunk-ERGF2FCE.js +0 -903
  105. package/dist/chunk-ERGF2FCE.js.map +0 -1
  106. package/dist/chunk-GK7B66LY.js +0 -135
  107. package/dist/chunk-GK7B66LY.js.map +0 -1
  108. package/dist/chunk-GQI6WJGI.js +0 -172
  109. package/dist/chunk-GQI6WJGI.js.map +0 -1
  110. package/dist/chunk-JUVE3DWY.js.map +0 -1
  111. package/dist/chunk-O3K7R32P.js.map +0 -1
  112. package/dist/chunk-SEY5UO2T.js +0 -89
  113. package/dist/chunk-SEY5UO2T.js.map +0 -1
  114. package/dist/chunk-USJYMRUO.js.map +0 -1
  115. package/dist/chunk-XX3IWSPM.js +0 -189
  116. package/dist/chunk-XX3IWSPM.js.map +0 -1
  117. package/dist/chunk-Y3INY2CS.js.map +0 -1
  118. package/dist/chunk-ZTSBF536.js.map +0 -1
  119. /package/dist/{canvas-75Y7XMF3.js.map → canvas-ZQNCL7JL.js.map} +0 -0
  120. /package/dist/{pdf-3TIGQRLA.js.map → pdf-PHXP7RHD.js.map} +0 -0
@@ -0,0 +1,1412 @@
1
+ import {
2
+ getSupabaseUrl
3
+ } from "./chunk-GC3TBUWE.js";
4
+ import {
5
+ omit
6
+ } from "./chunk-OQ7U6EQ3.js";
7
+ import {
8
+ useSupabase
9
+ } from "./chunk-5EFDS7SR.js";
10
+
11
+ // src/hooks/useDataLayer.ts
12
+ import { useContext } from "react";
13
+
14
+ // src/providers/DataLayerContext.ts
15
+ import { createContext } from "react";
16
+ var DataLayerContext = createContext(null);
17
+ DataLayerContext.displayName = "DataLayerContext";
18
+
19
+ // src/hooks/useDataLayer.ts
20
+ function useDataLayer() {
21
+ const context = useContext(DataLayerContext);
22
+ if (!context) {
23
+ throw new Error("useDataLayer must be used within a DataLayerProvider. Make sure you have wrapped your app with <DataLayerProvider>.");
24
+ }
25
+ return context;
26
+ }
27
+ function useDataLayerOptional() {
28
+ return useContext(DataLayerContext);
29
+ }
30
+
31
+ // src/hooks/useDbQuery.ts
32
+ import { useMemo, useEffect, useCallback } from "react";
33
+ import { useQuery } from "@tanstack/react-query";
34
+ function buildQueryKey(table, options) {
35
+ return ["v3", "query", table, options.select ?? "*", JSON.stringify(options.where ?? {}), JSON.stringify(options.orderBy ?? []), options.limit, options.offset];
36
+ }
37
+ function serializeQueryOptions(options) {
38
+ return JSON.stringify({
39
+ select: options.select,
40
+ where: options.where,
41
+ orderBy: options.orderBy,
42
+ limit: options.limit,
43
+ offset: options.offset
44
+ });
45
+ }
46
+ function resolveTableName(table) {
47
+ if (typeof table === "string") {
48
+ return table;
49
+ }
50
+ return `${table.schema}.${table.table}`;
51
+ }
52
+ function useDbQuery(table, options = {}) {
53
+ const tableName = typeof table === "string" ? table : resolveTableName(table);
54
+ const {
55
+ registry,
56
+ status,
57
+ queryClient
58
+ } = useDataLayer();
59
+ const isPowerSync = status.currentBackend === "powersync";
60
+ const {
61
+ enabled = true,
62
+ staleTime = isPowerSync ? 0 : 3e4,
63
+ gcTime = 3e5,
64
+ // 5 minutes - keep in memory for instant display while refetching
65
+ refetchOnWindowFocus = true,
66
+ refetchOnMount = isPowerSync ? "always" : true,
67
+ realtime = isPowerSync,
68
+ // Enable real-time subscriptions by default for PowerSync
69
+ ...queryOptions
70
+ } = options;
71
+ const adapter = useMemo(() => {
72
+ if (!status.isInitialized) return null;
73
+ try {
74
+ return registry.getAdapter(tableName);
75
+ } catch {
76
+ return null;
77
+ }
78
+ }, [registry, tableName, status.isInitialized, status.currentBackend]);
79
+ const queryKey = useMemo(() => buildQueryKey(tableName, options), [tableName, serializeQueryOptions(options)]);
80
+ const memoizedQueryOptions = useMemo(() => ({
81
+ select: queryOptions.select,
82
+ where: queryOptions.where,
83
+ orderBy: queryOptions.orderBy,
84
+ limit: queryOptions.limit,
85
+ offset: queryOptions.offset
86
+ }), [serializeQueryOptions(queryOptions)]);
87
+ const queryFn = useCallback(async () => {
88
+ if (!adapter) {
89
+ throw new Error(`Adapter not available for table: ${tableName}`);
90
+ }
91
+ const result = await adapter.query(tableName, memoizedQueryOptions);
92
+ return result;
93
+ }, [adapter, tableName, memoizedQueryOptions]);
94
+ const query = useQuery({
95
+ queryKey,
96
+ queryFn,
97
+ enabled: enabled && status.isInitialized && adapter !== null,
98
+ staleTime,
99
+ gcTime,
100
+ refetchOnWindowFocus,
101
+ refetchOnMount
102
+ });
103
+ useEffect(() => {
104
+ if (!realtime || !adapter?.subscribe || !status.isInitialized) {
105
+ return;
106
+ }
107
+ const unsubscribe = adapter.subscribe(tableName, memoizedQueryOptions, (data) => {
108
+ const hasRelations = memoizedQueryOptions.select?.includes("(");
109
+ if (hasRelations) {
110
+ queryClient.invalidateQueries({
111
+ queryKey
112
+ });
113
+ } else {
114
+ queryClient.setQueryData(queryKey, {
115
+ data,
116
+ count: data.length
117
+ });
118
+ }
119
+ });
120
+ return () => {
121
+ unsubscribe();
122
+ };
123
+ }, [realtime, adapter, tableName, memoizedQueryOptions, queryClient, queryKey, status.isInitialized]);
124
+ const refetch = useCallback(async () => {
125
+ await query.refetch();
126
+ }, [query]);
127
+ return {
128
+ data: query.data?.data,
129
+ isLoading: query.isLoading,
130
+ isPending: query.isPending,
131
+ isFetching: query.isFetching,
132
+ isRefetching: query.isFetching,
133
+ // Alias for V2 compatibility
134
+ isSuccess: query.isSuccess,
135
+ isError: query.isError,
136
+ error: query.error,
137
+ refetch,
138
+ count: query.data?.count,
139
+ isStale: query.isStale,
140
+ dataUpdatedAt: query.dataUpdatedAt ?? null
141
+ };
142
+ }
143
+
144
+ // src/hooks/useDbQueryById.ts
145
+ import { useCallback as useCallback2, useMemo as useMemo2 } from "react";
146
+ import { useQuery as useQuery2 } from "@tanstack/react-query";
147
+ function buildQueryKey2(table, id, select) {
148
+ return ["v3", "queryById", table, id, select ?? "*"];
149
+ }
150
+ function useDbQueryById(table, id, options = {}) {
151
+ const {
152
+ registry,
153
+ status
154
+ } = useDataLayer();
155
+ const {
156
+ select,
157
+ enabled = id != null,
158
+ staleTime = 3e4
159
+ } = options;
160
+ const adapter = useMemo2(() => {
161
+ if (!status.isInitialized) return null;
162
+ try {
163
+ return registry.getAdapter(table);
164
+ } catch {
165
+ return null;
166
+ }
167
+ }, [registry, table, status.isInitialized, status.currentBackend]);
168
+ const queryKey = useMemo2(() => buildQueryKey2(table, id, select), [table, id, select]);
169
+ const queryFn = useCallback2(async () => {
170
+ if (!adapter) {
171
+ throw new Error(`Adapter not available for table: ${table}`);
172
+ }
173
+ if (id == null) {
174
+ return null;
175
+ }
176
+ if (adapter.queryById) {
177
+ return adapter.queryById(table, String(id), {
178
+ select
179
+ });
180
+ }
181
+ const result = await adapter.query(table, {
182
+ select,
183
+ where: {
184
+ id
185
+ },
186
+ limit: 1
187
+ });
188
+ return result.data[0] ?? null;
189
+ }, [adapter, table, id, select]);
190
+ const query = useQuery2({
191
+ queryKey,
192
+ queryFn,
193
+ enabled: enabled && status.isInitialized && adapter !== null && id != null,
194
+ staleTime
195
+ });
196
+ const refetch = useCallback2(async () => {
197
+ await query.refetch();
198
+ }, [query]);
199
+ return {
200
+ data: query.data,
201
+ isLoading: query.isLoading,
202
+ isPending: query.isPending,
203
+ isFetching: query.isFetching,
204
+ error: query.error,
205
+ refetch
206
+ };
207
+ }
208
+
209
+ // src/hooks/useAdvanceQuery.ts
210
+ import { useCallback as useCallback3, useMemo as useMemo3, useState, useEffect as useEffect2 } from "react";
211
+ import { useQuery as useQuery3 } from "@tanstack/react-query";
212
+ import { useSessionStorageState } from "@pol-studios/hooks/storage";
213
+ var createDefaultFilterState = () => ({
214
+ id: "root",
215
+ op: "AND",
216
+ filters: [],
217
+ pagination: void 0,
218
+ sort: [],
219
+ isReady: true
220
+ });
221
+ function useAdvanceQuery(table, options) {
222
+ const tableName = typeof table === "string" ? table : `${table.schema}.${table.table}`;
223
+ const {
224
+ registry,
225
+ status,
226
+ queryClient
227
+ } = useDataLayer();
228
+ const supabase = useSupabase();
229
+ const {
230
+ filterKey,
231
+ initialFilters,
232
+ enabled = true,
233
+ timeout = 15e3,
234
+ count = "exact",
235
+ realtime,
236
+ select,
237
+ where,
238
+ orderBy,
239
+ limit,
240
+ offset,
241
+ ...restOptions
242
+ } = options;
243
+ const isPowerSync = status.currentBackend === "powersync";
244
+ const realtimeEnabled = realtime ?? isPowerSync;
245
+ const defaultFilterState = useMemo3(
246
+ () => initialFilters ?? createDefaultFilterState(),
247
+ // eslint-disable-next-line react-hooks/exhaustive-deps
248
+ []
249
+ // Only compute once
250
+ );
251
+ const [filtersRaw, setFiltersRaw] = useSessionStorageState(filterKey, defaultFilterState);
252
+ const filters = filtersRaw ?? defaultFilterState;
253
+ const setFilters = useCallback3((action) => {
254
+ if (typeof action === "function") {
255
+ setFiltersRaw((prev) => action(prev ?? defaultFilterState));
256
+ } else {
257
+ setFiltersRaw(action);
258
+ }
259
+ }, [setFiltersRaw, defaultFilterState]);
260
+ const hasAdvancedFilters = useMemo3(() => (filters?.filters?.length ?? 0) > 0 || !!filters?.naturalLanguageQuery, [filters?.filters?.length, filters?.naturalLanguageQuery]);
261
+ const usePowerSyncPath = !hasAdvancedFilters;
262
+ const powerSyncOrderBy = useMemo3(() => {
263
+ if (filters?.sort && filters.sort.length > 0) {
264
+ return filters.sort.map((s) => ({
265
+ field: s.field,
266
+ direction: s.direction
267
+ }));
268
+ }
269
+ return orderBy;
270
+ }, [filters?.sort, orderBy]);
271
+ const powerSyncResult = useDbQuery(table, {
272
+ select,
273
+ where,
274
+ orderBy: powerSyncOrderBy,
275
+ limit: filters?.pagination?.limit ?? limit,
276
+ offset: filters?.pagination?.offset ?? offset,
277
+ enabled: enabled && usePowerSyncPath && filters?.isReady !== false,
278
+ realtime: realtimeEnabled
279
+ // Enable watch() subscriptions for reactive updates
280
+ });
281
+ const [extraData, setExtraData] = useState({});
282
+ const edgeFunctionQueryKey = useMemo3(() => ["v3", "advance-query", tableName, select, JSON.stringify(where), JSON.stringify(filters), status.currentBackend], [tableName, select, where, filters, status.currentBackend]);
283
+ const edgeFunctionResult = useQuery3({
284
+ queryKey: edgeFunctionQueryKey,
285
+ queryFn: async ({
286
+ signal
287
+ }) => {
288
+ const {
289
+ data: {
290
+ session
291
+ }
292
+ } = await supabase.auth.getSession();
293
+ if (!session?.access_token) {
294
+ throw new Error("No active session");
295
+ }
296
+ const controller = new AbortController();
297
+ signal.addEventListener("abort", () => controller.abort());
298
+ const timeoutId = setTimeout(() => controller.abort(), timeout);
299
+ try {
300
+ const filterBody = {
301
+ id: filters.id || "root",
302
+ op: filters.op || "AND",
303
+ not: filters.not,
304
+ filters: filters.filters || []
305
+ };
306
+ const baseFilters = [];
307
+ if (where) {
308
+ Object.entries(where).forEach(([field, value]) => {
309
+ if (value !== void 0 && value !== null) {
310
+ if (typeof value === "object" && "in" in value) {
311
+ baseFilters.push({
312
+ id: `base-${field}`,
313
+ field,
314
+ op: "in",
315
+ value: value.in
316
+ });
317
+ } else {
318
+ baseFilters.push({
319
+ id: `base-${field}`,
320
+ field,
321
+ op: "=",
322
+ value
323
+ });
324
+ }
325
+ }
326
+ });
327
+ }
328
+ const combinedFilters = [...baseFilters, ...filterBody.filters];
329
+ const currentKey = `${tableName}${select}${JSON.stringify(omit(filters, ["pagination", "isReady"]))}`;
330
+ const res = await fetch(`${getSupabaseUrl()}/functions/v1/query?forceDenoVersion=2`, {
331
+ method: "POST",
332
+ headers: {
333
+ "Content-Type": "application/json",
334
+ Authorization: `Bearer ${session.access_token}`
335
+ },
336
+ body: JSON.stringify({
337
+ table: tableName,
338
+ schema: "public",
339
+ select: select ?? "*",
340
+ filters: {
341
+ id: filterBody.id,
342
+ op: filterBody.op,
343
+ not: filterBody.not,
344
+ filters: combinedFilters
345
+ },
346
+ pagination: filters.pagination,
347
+ sort: filters.sort || orderBy?.map((o) => ({
348
+ field: o.field,
349
+ direction: o.direction
350
+ })),
351
+ distinctOn: filters.distinctOn,
352
+ naturalLanguageQuery: filters.naturalLanguageQuery,
353
+ count: currentKey === extraData.key ? "" : count
354
+ }),
355
+ signal: controller.signal
356
+ });
357
+ if (!res.ok) {
358
+ const errorData = await res.json();
359
+ const errorMessage = typeof errorData?.error === "string" ? errorData.error : errorData?.error?.message || errorData?.message || "Query failed";
360
+ throw new Error(errorMessage);
361
+ }
362
+ const result2 = await res.json();
363
+ if (result2.clarification) {
364
+ return {
365
+ data: [],
366
+ count: 0,
367
+ clarification: result2.clarification
368
+ };
369
+ }
370
+ setExtraData((prev) => ({
371
+ count: prev.key === currentKey ? prev.count : result2.count,
372
+ key: currentKey
373
+ }));
374
+ return {
375
+ data: result2.data,
376
+ count: result2.count
377
+ };
378
+ } finally {
379
+ clearTimeout(timeoutId);
380
+ }
381
+ },
382
+ enabled: enabled && !usePowerSyncPath && filters?.isReady !== false,
383
+ staleTime: 3e4,
384
+ gcTime: 3e5,
385
+ refetchOnMount: true,
386
+ refetchOnWindowFocus: false
387
+ });
388
+ useEffect2(() => {
389
+ if (!realtimeEnabled || usePowerSyncPath || !enabled) {
390
+ return;
391
+ }
392
+ const channel = supabase.channel(`advance-query-${tableName}-${filterKey}`).on("postgres_changes", {
393
+ event: "*",
394
+ // Listen to INSERT, UPDATE, DELETE
395
+ schema: "public",
396
+ table: tableName
397
+ // Note: We can't easily filter by complex where clauses,
398
+ // so we invalidate on any change to the table
399
+ }, () => {
400
+ queryClient.invalidateQueries({
401
+ queryKey: edgeFunctionQueryKey
402
+ });
403
+ }).subscribe();
404
+ return () => {
405
+ supabase.removeChannel(channel);
406
+ };
407
+ }, [realtimeEnabled, usePowerSyncPath, enabled, supabase, tableName, filterKey, queryClient, edgeFunctionQueryKey]);
408
+ const result = useMemo3(() => {
409
+ if (usePowerSyncPath) {
410
+ return {
411
+ data: powerSyncResult.data,
412
+ isLoading: powerSyncResult.isLoading,
413
+ isPending: powerSyncResult.isPending,
414
+ isFetching: powerSyncResult.isFetching,
415
+ isRefetching: powerSyncResult.isRefetching,
416
+ isSuccess: powerSyncResult.isSuccess,
417
+ isError: powerSyncResult.isError,
418
+ error: powerSyncResult.error,
419
+ refetch: powerSyncResult.refetch,
420
+ count: powerSyncResult.count ?? powerSyncResult.data?.length
421
+ };
422
+ }
423
+ return {
424
+ data: edgeFunctionResult.data?.data,
425
+ isLoading: edgeFunctionResult.isLoading,
426
+ isPending: edgeFunctionResult.isPending,
427
+ isFetching: edgeFunctionResult.isFetching,
428
+ isRefetching: edgeFunctionResult.isFetching,
429
+ isSuccess: edgeFunctionResult.isSuccess,
430
+ isError: edgeFunctionResult.isError,
431
+ error: edgeFunctionResult.error,
432
+ refetch: async () => {
433
+ await edgeFunctionResult.refetch();
434
+ },
435
+ count: edgeFunctionResult.data?.count ?? extraData.count,
436
+ clarification: edgeFunctionResult.data?.clarification
437
+ };
438
+ }, [usePowerSyncPath, powerSyncResult, edgeFunctionResult, extraData.count]);
439
+ return [result, filters, setFilters];
440
+ }
441
+
442
+ // src/hooks/useDbInsert.ts
443
+ import { c as _c } from "react/compiler-runtime";
444
+ import { useMutation, useQueryClient } from "@tanstack/react-query";
445
+ function useDbInsert(table, t0) {
446
+ const $ = _c(28);
447
+ let t1;
448
+ if ($[0] !== t0) {
449
+ t1 = t0 === void 0 ? {} : t0;
450
+ $[0] = t0;
451
+ $[1] = t1;
452
+ } else {
453
+ t1 = $[1];
454
+ }
455
+ const options = t1;
456
+ const {
457
+ registry,
458
+ status
459
+ } = useDataLayer();
460
+ const queryClient = useQueryClient();
461
+ const {
462
+ onSuccess,
463
+ onError,
464
+ invalidateTables: t2
465
+ } = options;
466
+ let t3;
467
+ if ($[2] !== t2 || $[3] !== table) {
468
+ t3 = t2 === void 0 ? [table] : t2;
469
+ $[2] = t2;
470
+ $[3] = table;
471
+ $[4] = t3;
472
+ } else {
473
+ t3 = $[4];
474
+ }
475
+ const invalidateTables = t3;
476
+ let t4;
477
+ bb0: {
478
+ if (!status.isInitialized) {
479
+ t4 = null;
480
+ break bb0;
481
+ }
482
+ try {
483
+ let t52;
484
+ if ($[5] !== registry || $[6] !== table) {
485
+ t52 = registry.getAdapter(table);
486
+ $[5] = registry;
487
+ $[6] = table;
488
+ $[7] = t52;
489
+ } else {
490
+ t52 = $[7];
491
+ }
492
+ t4 = t52;
493
+ } catch {
494
+ t4 = null;
495
+ }
496
+ }
497
+ const adapter = t4;
498
+ let t5;
499
+ if ($[8] !== adapter || $[9] !== table) {
500
+ t5 = async (data) => {
501
+ if (!adapter) {
502
+ throw new Error(`Adapter not available for table: ${table}`);
503
+ }
504
+ return adapter.insert(table, data);
505
+ };
506
+ $[8] = adapter;
507
+ $[9] = table;
508
+ $[10] = t5;
509
+ } else {
510
+ t5 = $[10];
511
+ }
512
+ const mutationFn = t5;
513
+ let t6;
514
+ if ($[11] !== invalidateTables || $[12] !== onSuccess || $[13] !== queryClient) {
515
+ t6 = (data_0) => {
516
+ invalidateTables.forEach((t) => {
517
+ queryClient.invalidateQueries({
518
+ predicate: (query) => query.queryKey[0] === "v3" && query.queryKey[2] === t
519
+ });
520
+ });
521
+ onSuccess?.(data_0);
522
+ };
523
+ $[11] = invalidateTables;
524
+ $[12] = onSuccess;
525
+ $[13] = queryClient;
526
+ $[14] = t6;
527
+ } else {
528
+ t6 = $[14];
529
+ }
530
+ let t7;
531
+ if ($[15] !== onError) {
532
+ t7 = (error) => {
533
+ onError?.(error instanceof Error ? error : new Error(String(error)));
534
+ };
535
+ $[15] = onError;
536
+ $[16] = t7;
537
+ } else {
538
+ t7 = $[16];
539
+ }
540
+ let t8;
541
+ if ($[17] !== mutationFn || $[18] !== t6 || $[19] !== t7) {
542
+ t8 = {
543
+ mutationFn,
544
+ onSuccess: t6,
545
+ onError: t7
546
+ };
547
+ $[17] = mutationFn;
548
+ $[18] = t6;
549
+ $[19] = t7;
550
+ $[20] = t8;
551
+ } else {
552
+ t8 = $[20];
553
+ }
554
+ const mutation = useMutation(t8);
555
+ const t9 = mutation.error;
556
+ let t10;
557
+ if ($[21] !== mutation.data || $[22] !== mutation.isPending || $[23] !== mutation.mutate || $[24] !== mutation.mutateAsync || $[25] !== mutation.reset || $[26] !== t9) {
558
+ t10 = {
559
+ mutate: mutation.mutate,
560
+ mutateAsync: mutation.mutateAsync,
561
+ isPending: mutation.isPending,
562
+ error: t9,
563
+ reset: mutation.reset,
564
+ data: mutation.data
565
+ };
566
+ $[21] = mutation.data;
567
+ $[22] = mutation.isPending;
568
+ $[23] = mutation.mutate;
569
+ $[24] = mutation.mutateAsync;
570
+ $[25] = mutation.reset;
571
+ $[26] = t9;
572
+ $[27] = t10;
573
+ } else {
574
+ t10 = $[27];
575
+ }
576
+ return t10;
577
+ }
578
+
579
+ // src/hooks/useDbUpdate.ts
580
+ import { c as _c2 } from "react/compiler-runtime";
581
+ import { useMutation as useMutation2, useQueryClient as useQueryClient2 } from "@tanstack/react-query";
582
+ function useDbUpdate(table, t0) {
583
+ const $ = _c2(35);
584
+ let t1;
585
+ if ($[0] !== t0) {
586
+ t1 = t0 === void 0 ? {} : t0;
587
+ $[0] = t0;
588
+ $[1] = t1;
589
+ } else {
590
+ t1 = $[1];
591
+ }
592
+ const options = t1;
593
+ const {
594
+ registry,
595
+ status
596
+ } = useDataLayer();
597
+ const queryClient = useQueryClient2();
598
+ const {
599
+ onSuccess,
600
+ onError,
601
+ invalidateTables: t2,
602
+ optimistic: t3
603
+ } = options;
604
+ let t4;
605
+ if ($[2] !== t2 || $[3] !== table) {
606
+ t4 = t2 === void 0 ? [table] : t2;
607
+ $[2] = t2;
608
+ $[3] = table;
609
+ $[4] = t4;
610
+ } else {
611
+ t4 = $[4];
612
+ }
613
+ const invalidateTables = t4;
614
+ const optimistic = t3 === void 0 ? false : t3;
615
+ let t5;
616
+ bb0: {
617
+ if (!status.isInitialized) {
618
+ t5 = null;
619
+ break bb0;
620
+ }
621
+ try {
622
+ let t62;
623
+ if ($[5] !== registry || $[6] !== table) {
624
+ t62 = registry.getAdapter(table);
625
+ $[5] = registry;
626
+ $[6] = table;
627
+ $[7] = t62;
628
+ } else {
629
+ t62 = $[7];
630
+ }
631
+ t5 = t62;
632
+ } catch {
633
+ t5 = null;
634
+ }
635
+ }
636
+ const adapter = t5;
637
+ let t6;
638
+ if ($[8] !== adapter || $[9] !== table) {
639
+ t6 = async (params) => {
640
+ if (!adapter) {
641
+ throw new Error(`Adapter not available for table: ${table}`);
642
+ }
643
+ return adapter.update(table, params.id, params.data);
644
+ };
645
+ $[8] = adapter;
646
+ $[9] = table;
647
+ $[10] = t6;
648
+ } else {
649
+ t6 = $[10];
650
+ }
651
+ const mutationFn = t6;
652
+ let t7;
653
+ if ($[11] !== optimistic || $[12] !== queryClient || $[13] !== table) {
654
+ t7 = optimistic ? async () => {
655
+ await queryClient.cancelQueries({
656
+ predicate: (query) => query.queryKey[0] === "v3" && query.queryKey[2] === table
657
+ });
658
+ const previousData = queryClient.getQueriesData({
659
+ predicate: (query_0) => query_0.queryKey[0] === "v3" && query_0.queryKey[2] === table
660
+ });
661
+ return {
662
+ previousData
663
+ };
664
+ } : void 0;
665
+ $[11] = optimistic;
666
+ $[12] = queryClient;
667
+ $[13] = table;
668
+ $[14] = t7;
669
+ } else {
670
+ t7 = $[14];
671
+ }
672
+ let t8;
673
+ if ($[15] !== invalidateTables || $[16] !== onSuccess || $[17] !== queryClient) {
674
+ t8 = (data) => {
675
+ invalidateTables.forEach((t) => {
676
+ queryClient.invalidateQueries({
677
+ predicate: (query_1) => query_1.queryKey[0] === "v3" && query_1.queryKey[2] === t
678
+ });
679
+ });
680
+ onSuccess?.(data);
681
+ };
682
+ $[15] = invalidateTables;
683
+ $[16] = onSuccess;
684
+ $[17] = queryClient;
685
+ $[18] = t8;
686
+ } else {
687
+ t8 = $[18];
688
+ }
689
+ let t9;
690
+ if ($[19] !== onError || $[20] !== optimistic || $[21] !== queryClient) {
691
+ t9 = (error, _variables, context) => {
692
+ if (optimistic && context?.previousData) {
693
+ context.previousData.forEach((t102) => {
694
+ const [queryKey, data_0] = t102;
695
+ queryClient.setQueryData(queryKey, data_0);
696
+ });
697
+ }
698
+ onError?.(error instanceof Error ? error : new Error(String(error)));
699
+ };
700
+ $[19] = onError;
701
+ $[20] = optimistic;
702
+ $[21] = queryClient;
703
+ $[22] = t9;
704
+ } else {
705
+ t9 = $[22];
706
+ }
707
+ let t10;
708
+ if ($[23] !== mutationFn || $[24] !== t7 || $[25] !== t8 || $[26] !== t9) {
709
+ t10 = {
710
+ mutationFn,
711
+ onMutate: t7,
712
+ onSuccess: t8,
713
+ onError: t9
714
+ };
715
+ $[23] = mutationFn;
716
+ $[24] = t7;
717
+ $[25] = t8;
718
+ $[26] = t9;
719
+ $[27] = t10;
720
+ } else {
721
+ t10 = $[27];
722
+ }
723
+ const mutation = useMutation2(t10);
724
+ const t11 = mutation.error;
725
+ let t12;
726
+ if ($[28] !== mutation.data || $[29] !== mutation.isPending || $[30] !== mutation.mutate || $[31] !== mutation.mutateAsync || $[32] !== mutation.reset || $[33] !== t11) {
727
+ t12 = {
728
+ mutate: mutation.mutate,
729
+ mutateAsync: mutation.mutateAsync,
730
+ isPending: mutation.isPending,
731
+ error: t11,
732
+ reset: mutation.reset,
733
+ data: mutation.data
734
+ };
735
+ $[28] = mutation.data;
736
+ $[29] = mutation.isPending;
737
+ $[30] = mutation.mutate;
738
+ $[31] = mutation.mutateAsync;
739
+ $[32] = mutation.reset;
740
+ $[33] = t11;
741
+ $[34] = t12;
742
+ } else {
743
+ t12 = $[34];
744
+ }
745
+ return t12;
746
+ }
747
+
748
+ // src/hooks/useDbUpsert.ts
749
+ import { useCallback as useCallback4, useMemo as useMemo4 } from "react";
750
+ import { useMutation as useMutation3, useQueryClient as useQueryClient3 } from "@tanstack/react-query";
751
+ function useDbUpsert(table, options = {}) {
752
+ const {
753
+ registry,
754
+ status
755
+ } = useDataLayer();
756
+ const queryClient = useQueryClient3();
757
+ const {
758
+ onSuccess,
759
+ onError,
760
+ invalidateTables = [table]
761
+ } = options;
762
+ const adapter = useMemo4(() => {
763
+ if (!status.isInitialized) return null;
764
+ try {
765
+ const resolvedAdapter = registry.getAdapter(table);
766
+ if (__DEV__) {
767
+ console.log(`[useDbUpsert] Adapter resolved for table "${table}":`, {
768
+ adapterName: resolvedAdapter?.name ?? "unknown",
769
+ backend: status.currentBackend
770
+ });
771
+ }
772
+ return resolvedAdapter;
773
+ } catch (error) {
774
+ if (__DEV__) {
775
+ console.warn(`[useDbUpsert] Failed to get adapter for table "${table}":`, error);
776
+ }
777
+ return null;
778
+ }
779
+ }, [registry, table, status.isInitialized, status.currentBackend]);
780
+ const mutationFn = useCallback4(async (data) => {
781
+ if (__DEV__) {
782
+ console.log(`[useDbUpsert] mutationFn called for table "${table}":`, {
783
+ adapterName: adapter?.name ?? "null",
784
+ dataKeys: Object.keys(data),
785
+ hasId: "id" in data
786
+ });
787
+ }
788
+ if (!adapter) {
789
+ throw new Error(`Adapter not available for table: ${table}`);
790
+ }
791
+ const result = await adapter.upsert(table, data);
792
+ if (__DEV__) {
793
+ console.log(`[useDbUpsert] upsert completed for table "${table}":`, {
794
+ adapterName: adapter.name,
795
+ resultId: result?.id ?? "unknown"
796
+ });
797
+ }
798
+ return result;
799
+ }, [adapter, table]);
800
+ const mutation = useMutation3({
801
+ mutationFn,
802
+ onSuccess: (data_0) => {
803
+ invalidateTables.forEach((t) => {
804
+ queryClient.invalidateQueries({
805
+ predicate: (query) => query.queryKey[0] === "v3" && query.queryKey[2] === t
806
+ });
807
+ });
808
+ onSuccess?.(data_0);
809
+ },
810
+ onError: (error_0) => {
811
+ onError?.(error_0 instanceof Error ? error_0 : new Error(String(error_0)));
812
+ }
813
+ });
814
+ return {
815
+ mutate: mutation.mutate,
816
+ mutateAsync: mutation.mutateAsync,
817
+ isPending: mutation.isPending,
818
+ error: mutation.error,
819
+ reset: mutation.reset,
820
+ data: mutation.data
821
+ };
822
+ }
823
+
824
+ // src/hooks/useDbDelete.ts
825
+ import { c as _c3 } from "react/compiler-runtime";
826
+ import { useMutation as useMutation4, useQueryClient as useQueryClient4 } from "@tanstack/react-query";
827
+ function useDbDelete(table, t0) {
828
+ const $ = _c3(34);
829
+ let t1;
830
+ if ($[0] !== t0) {
831
+ t1 = t0 === void 0 ? {} : t0;
832
+ $[0] = t0;
833
+ $[1] = t1;
834
+ } else {
835
+ t1 = $[1];
836
+ }
837
+ const options = t1;
838
+ const {
839
+ registry,
840
+ status
841
+ } = useDataLayer();
842
+ const queryClient = useQueryClient4();
843
+ const {
844
+ onSuccess,
845
+ onError,
846
+ invalidateTables: t2,
847
+ optimistic: t3
848
+ } = options;
849
+ let t4;
850
+ if ($[2] !== t2 || $[3] !== table) {
851
+ t4 = t2 === void 0 ? [table] : t2;
852
+ $[2] = t2;
853
+ $[3] = table;
854
+ $[4] = t4;
855
+ } else {
856
+ t4 = $[4];
857
+ }
858
+ const invalidateTables = t4;
859
+ const optimistic = t3 === void 0 ? false : t3;
860
+ let t5;
861
+ bb0: {
862
+ if (!status.isInitialized) {
863
+ t5 = null;
864
+ break bb0;
865
+ }
866
+ try {
867
+ let t62;
868
+ if ($[5] !== registry || $[6] !== table) {
869
+ t62 = registry.getAdapter(table);
870
+ $[5] = registry;
871
+ $[6] = table;
872
+ $[7] = t62;
873
+ } else {
874
+ t62 = $[7];
875
+ }
876
+ t5 = t62;
877
+ } catch {
878
+ t5 = null;
879
+ }
880
+ }
881
+ const adapter = t5;
882
+ let t6;
883
+ if ($[8] !== adapter || $[9] !== table) {
884
+ t6 = async (id) => {
885
+ if (!adapter) {
886
+ throw new Error(`Adapter not available for table: ${table}`);
887
+ }
888
+ await adapter.delete(table, id);
889
+ };
890
+ $[8] = adapter;
891
+ $[9] = table;
892
+ $[10] = t6;
893
+ } else {
894
+ t6 = $[10];
895
+ }
896
+ const mutationFn = t6;
897
+ let t7;
898
+ if ($[11] !== optimistic || $[12] !== queryClient || $[13] !== table) {
899
+ t7 = optimistic ? async (id_0) => {
900
+ await queryClient.cancelQueries({
901
+ predicate: (query) => query.queryKey[0] === "v3" && query.queryKey[2] === table
902
+ });
903
+ const previousData = queryClient.getQueriesData({
904
+ predicate: (query_0) => query_0.queryKey[0] === "v3" && query_0.queryKey[2] === table
905
+ });
906
+ queryClient.setQueriesData({
907
+ predicate: (query_1) => query_1.queryKey[0] === "v3" && query_1.queryKey[2] === table
908
+ }, (old) => {
909
+ if (!old?.data) {
910
+ return old;
911
+ }
912
+ return {
913
+ ...old,
914
+ data: old.data.filter((item) => item.id !== id_0)
915
+ };
916
+ });
917
+ return {
918
+ previousData
919
+ };
920
+ } : void 0;
921
+ $[11] = optimistic;
922
+ $[12] = queryClient;
923
+ $[13] = table;
924
+ $[14] = t7;
925
+ } else {
926
+ t7 = $[14];
927
+ }
928
+ let t8;
929
+ if ($[15] !== invalidateTables || $[16] !== onSuccess || $[17] !== queryClient) {
930
+ t8 = () => {
931
+ invalidateTables.forEach((t) => {
932
+ queryClient.invalidateQueries({
933
+ predicate: (query_2) => query_2.queryKey[0] === "v3" && query_2.queryKey[2] === t
934
+ });
935
+ });
936
+ onSuccess?.();
937
+ };
938
+ $[15] = invalidateTables;
939
+ $[16] = onSuccess;
940
+ $[17] = queryClient;
941
+ $[18] = t8;
942
+ } else {
943
+ t8 = $[18];
944
+ }
945
+ let t9;
946
+ if ($[19] !== onError || $[20] !== optimistic || $[21] !== queryClient) {
947
+ t9 = (error, _id, context) => {
948
+ if (optimistic && context?.previousData) {
949
+ context.previousData.forEach((t102) => {
950
+ const [queryKey, data] = t102;
951
+ queryClient.setQueryData(queryKey, data);
952
+ });
953
+ }
954
+ onError?.(error instanceof Error ? error : new Error(String(error)));
955
+ };
956
+ $[19] = onError;
957
+ $[20] = optimistic;
958
+ $[21] = queryClient;
959
+ $[22] = t9;
960
+ } else {
961
+ t9 = $[22];
962
+ }
963
+ let t10;
964
+ if ($[23] !== mutationFn || $[24] !== t7 || $[25] !== t8 || $[26] !== t9) {
965
+ t10 = {
966
+ mutationFn,
967
+ onMutate: t7,
968
+ onSuccess: t8,
969
+ onError: t9
970
+ };
971
+ $[23] = mutationFn;
972
+ $[24] = t7;
973
+ $[25] = t8;
974
+ $[26] = t9;
975
+ $[27] = t10;
976
+ } else {
977
+ t10 = $[27];
978
+ }
979
+ const mutation = useMutation4(t10);
980
+ const t11 = mutation.error;
981
+ let t12;
982
+ if ($[28] !== mutation.isPending || $[29] !== mutation.mutate || $[30] !== mutation.mutateAsync || $[31] !== mutation.reset || $[32] !== t11) {
983
+ t12 = {
984
+ mutate: mutation.mutate,
985
+ mutateAsync: mutation.mutateAsync,
986
+ isPending: mutation.isPending,
987
+ error: t11,
988
+ reset: mutation.reset
989
+ };
990
+ $[28] = mutation.isPending;
991
+ $[29] = mutation.mutate;
992
+ $[30] = mutation.mutateAsync;
993
+ $[31] = mutation.reset;
994
+ $[32] = t11;
995
+ $[33] = t12;
996
+ } else {
997
+ t12 = $[33];
998
+ }
999
+ return t12;
1000
+ }
1001
+
1002
+ // src/hooks/useDbInfiniteQuery.ts
1003
+ import { useMemo as useMemo5, useCallback as useCallback5 } from "react";
1004
+ import { useInfiniteQuery } from "@tanstack/react-query";
1005
+ function buildInfiniteQueryKey(table, options) {
1006
+ return ["v3", "infinite-query", table, options.select ?? "*", JSON.stringify(options.where ?? {}), JSON.stringify(options.orderBy ?? []), options.pageSize ?? 50, options.searchText ?? "", JSON.stringify(options.searchFields ?? [])];
1007
+ }
1008
+ function serializeInfiniteQueryOptions(options) {
1009
+ return JSON.stringify({
1010
+ select: options.select,
1011
+ where: options.where,
1012
+ orderBy: options.orderBy,
1013
+ pageSize: options.pageSize,
1014
+ searchText: options.searchText,
1015
+ searchFields: options.searchFields
1016
+ });
1017
+ }
1018
+ function resolveTableName2(table) {
1019
+ if (typeof table === "string") {
1020
+ return table;
1021
+ }
1022
+ return `${table.schema}.${table.table}`;
1023
+ }
1024
+ function buildSearchWhereClause(searchText, searchFields, existingWhere) {
1025
+ const searchWhere = {};
1026
+ if (searchFields.length === 1) {
1027
+ searchWhere[searchFields[0]] = {
1028
+ like: `%${searchText}%`
1029
+ };
1030
+ } else if (searchFields.length > 1) {
1031
+ searchWhere[searchFields[0]] = {
1032
+ like: `%${searchText}%`
1033
+ };
1034
+ }
1035
+ return {
1036
+ ...existingWhere,
1037
+ ...searchWhere
1038
+ };
1039
+ }
1040
+ function useDbInfiniteQuery(table, options = {}) {
1041
+ const tableName = resolveTableName2(table);
1042
+ const {
1043
+ registry,
1044
+ status
1045
+ } = useDataLayer();
1046
+ const {
1047
+ enabled = true,
1048
+ staleTime = 3e4,
1049
+ refetchOnWindowFocus = true,
1050
+ pageSize = 50,
1051
+ searchText,
1052
+ searchFields,
1053
+ ...queryOptions
1054
+ } = options;
1055
+ const adapter = useMemo5(() => {
1056
+ if (!status.isInitialized) return null;
1057
+ try {
1058
+ return registry.getAdapter(tableName);
1059
+ } catch {
1060
+ return null;
1061
+ }
1062
+ }, [registry, tableName, status.isInitialized, status.currentBackend]);
1063
+ const queryKey = useMemo5(() => buildInfiniteQueryKey(tableName, options), [tableName, serializeInfiniteQueryOptions(options)]);
1064
+ const effectiveWhere = useMemo5(() => {
1065
+ if (searchText && searchFields && searchFields.length > 0) {
1066
+ return buildSearchWhereClause(searchText, searchFields, queryOptions.where);
1067
+ }
1068
+ return queryOptions.where;
1069
+ }, [searchText, searchFields, queryOptions.where]);
1070
+ const memoizedQueryOptions = useMemo5(() => ({
1071
+ select: queryOptions.select,
1072
+ where: effectiveWhere,
1073
+ orderBy: queryOptions.orderBy
1074
+ }), [queryOptions.select, effectiveWhere, queryOptions.orderBy]);
1075
+ const infiniteQuery = useInfiniteQuery({
1076
+ queryKey,
1077
+ queryFn: async ({
1078
+ pageParam
1079
+ }) => {
1080
+ if (!adapter) {
1081
+ throw new Error(`Adapter not available for table: ${tableName}`);
1082
+ }
1083
+ const offset = (pageParam - 1) * pageSize;
1084
+ const result = await adapter.query(tableName, {
1085
+ ...memoizedQueryOptions,
1086
+ limit: pageSize,
1087
+ offset
1088
+ });
1089
+ return {
1090
+ data: result.data ?? [],
1091
+ count: result.count
1092
+ };
1093
+ },
1094
+ initialPageParam: 1,
1095
+ getNextPageParam: (lastPage, allPages) => {
1096
+ const totalLoaded = allPages.reduce((sum, page) => sum + page.data.length, 0);
1097
+ const totalCount = lastPage.count;
1098
+ if (totalCount !== void 0 && totalLoaded >= totalCount) {
1099
+ return void 0;
1100
+ }
1101
+ if (lastPage.data.length < pageSize) {
1102
+ return void 0;
1103
+ }
1104
+ return allPages.length + 1;
1105
+ },
1106
+ enabled: enabled && status.isInitialized && adapter !== null,
1107
+ staleTime,
1108
+ refetchOnWindowFocus
1109
+ });
1110
+ const flattenedData = useMemo5(() => {
1111
+ if (!infiniteQuery.data?.pages) return void 0;
1112
+ return infiniteQuery.data.pages.flatMap((page_0) => page_0.data);
1113
+ }, [infiniteQuery.data?.pages]);
1114
+ const count = useMemo5(() => {
1115
+ if (!infiniteQuery.data?.pages || infiniteQuery.data.pages.length === 0) {
1116
+ return void 0;
1117
+ }
1118
+ return infiniteQuery.data.pages[infiniteQuery.data.pages.length - 1].count;
1119
+ }, [infiniteQuery.data?.pages]);
1120
+ const fetchNextPage = useCallback5(async () => {
1121
+ await infiniteQuery.fetchNextPage();
1122
+ }, [infiniteQuery]);
1123
+ const refetch = useCallback5(async () => {
1124
+ await infiniteQuery.refetch();
1125
+ }, [infiniteQuery]);
1126
+ return {
1127
+ data: flattenedData,
1128
+ isLoading: infiniteQuery.isLoading,
1129
+ isPending: infiniteQuery.isPending,
1130
+ isFetching: infiniteQuery.isFetching,
1131
+ error: infiniteQuery.error,
1132
+ fetchNextPage,
1133
+ hasNextPage: infiniteQuery.hasNextPage ?? false,
1134
+ isFetchingNextPage: infiniteQuery.isFetchingNextPage,
1135
+ count,
1136
+ refetch
1137
+ };
1138
+ }
1139
+
1140
+ // src/hooks/useDbCount.ts
1141
+ import { c as _c4 } from "react/compiler-runtime";
1142
+ import { useQuery as useQuery4 } from "@tanstack/react-query";
1143
+ function useDbCount(table, t0) {
1144
+ const $ = _c4(27);
1145
+ let t1;
1146
+ if ($[0] !== t0) {
1147
+ t1 = t0 === void 0 ? {} : t0;
1148
+ $[0] = t0;
1149
+ $[1] = t1;
1150
+ } else {
1151
+ t1 = $[1];
1152
+ }
1153
+ const options = t1;
1154
+ const {
1155
+ registry,
1156
+ status
1157
+ } = useDataLayer();
1158
+ const {
1159
+ enabled: t2,
1160
+ staleTime: t3,
1161
+ where
1162
+ } = options;
1163
+ const enabled = t2 === void 0 ? true : t2;
1164
+ const staleTime = t3 === void 0 ? 3e4 : t3;
1165
+ let t4;
1166
+ bb0: {
1167
+ if (!status.isInitialized) {
1168
+ t4 = null;
1169
+ break bb0;
1170
+ }
1171
+ try {
1172
+ let t52;
1173
+ if ($[2] !== registry || $[3] !== table) {
1174
+ t52 = registry.getAdapter(table);
1175
+ $[2] = registry;
1176
+ $[3] = table;
1177
+ $[4] = t52;
1178
+ } else {
1179
+ t52 = $[4];
1180
+ }
1181
+ t4 = t52;
1182
+ } catch {
1183
+ t4 = null;
1184
+ }
1185
+ }
1186
+ const adapter = t4;
1187
+ let t5;
1188
+ if ($[5] !== where) {
1189
+ t5 = JSON.stringify(where ?? {});
1190
+ $[5] = where;
1191
+ $[6] = t5;
1192
+ } else {
1193
+ t5 = $[6];
1194
+ }
1195
+ let t6;
1196
+ if ($[7] !== t5 || $[8] !== table) {
1197
+ t6 = ["v3", "count", table, t5];
1198
+ $[7] = t5;
1199
+ $[8] = table;
1200
+ $[9] = t6;
1201
+ } else {
1202
+ t6 = $[9];
1203
+ }
1204
+ const queryKey = t6;
1205
+ let t7;
1206
+ if ($[10] !== adapter || $[11] !== table || $[12] !== where) {
1207
+ t7 = async () => {
1208
+ if (!adapter) {
1209
+ throw new Error(`Adapter not available for table: ${table}`);
1210
+ }
1211
+ const result = await adapter.query(table, {
1212
+ where,
1213
+ select: "id"
1214
+ });
1215
+ return result.count ?? result.data.length;
1216
+ };
1217
+ $[10] = adapter;
1218
+ $[11] = table;
1219
+ $[12] = where;
1220
+ $[13] = t7;
1221
+ } else {
1222
+ t7 = $[13];
1223
+ }
1224
+ const queryFn = t7;
1225
+ const t8 = enabled && status.isInitialized && adapter !== null;
1226
+ let t9;
1227
+ if ($[14] !== queryFn || $[15] !== queryKey || $[16] !== staleTime || $[17] !== t8) {
1228
+ t9 = {
1229
+ queryKey,
1230
+ queryFn,
1231
+ enabled: t8,
1232
+ staleTime
1233
+ };
1234
+ $[14] = queryFn;
1235
+ $[15] = queryKey;
1236
+ $[16] = staleTime;
1237
+ $[17] = t8;
1238
+ $[18] = t9;
1239
+ } else {
1240
+ t9 = $[18];
1241
+ }
1242
+ const query = useQuery4(t9);
1243
+ let t10;
1244
+ if ($[19] !== query) {
1245
+ t10 = async () => {
1246
+ await query.refetch();
1247
+ };
1248
+ $[19] = query;
1249
+ $[20] = t10;
1250
+ } else {
1251
+ t10 = $[20];
1252
+ }
1253
+ const refetch = t10;
1254
+ const t11 = query.error;
1255
+ let t12;
1256
+ if ($[21] !== query.data || $[22] !== query.isFetching || $[23] !== query.isLoading || $[24] !== refetch || $[25] !== t11) {
1257
+ t12 = {
1258
+ count: query.data,
1259
+ isLoading: query.isLoading,
1260
+ isFetching: query.isFetching,
1261
+ error: t11,
1262
+ refetch
1263
+ };
1264
+ $[21] = query.data;
1265
+ $[22] = query.isFetching;
1266
+ $[23] = query.isLoading;
1267
+ $[24] = refetch;
1268
+ $[25] = t11;
1269
+ $[26] = t12;
1270
+ } else {
1271
+ t12 = $[26];
1272
+ }
1273
+ return t12;
1274
+ }
1275
+
1276
+ // src/hooks/useSyncStatus.ts
1277
+ import { c as _c5 } from "react/compiler-runtime";
1278
+ import { useState as useState2, useEffect as useEffect3 } from "react";
1279
+ var defaultSyncStatus = {
1280
+ isConnected: true,
1281
+ // Supabase-only mode is always "connected" when online
1282
+ isSyncing: false,
1283
+ lastSyncedAt: null,
1284
+ pendingUploads: 0,
1285
+ error: null
1286
+ };
1287
+ function useSyncStatus() {
1288
+ const $ = _c5(6);
1289
+ const {
1290
+ syncStatus,
1291
+ status,
1292
+ powerSync
1293
+ } = useDataLayer();
1294
+ const [currentStatus, setCurrentStatus] = useState2(syncStatus);
1295
+ let t0;
1296
+ let t1;
1297
+ if ($[0] !== powerSync || $[1] !== status.currentBackend || $[2] !== status.isOnline || $[3] !== syncStatus) {
1298
+ t0 = () => {
1299
+ if (powerSync && status.currentBackend === "powersync") {
1300
+ setCurrentStatus(syncStatus);
1301
+ } else {
1302
+ setCurrentStatus({
1303
+ ...defaultSyncStatus,
1304
+ isConnected: status.isOnline
1305
+ });
1306
+ }
1307
+ };
1308
+ t1 = [syncStatus, status.currentBackend, status.isOnline, powerSync];
1309
+ $[0] = powerSync;
1310
+ $[1] = status.currentBackend;
1311
+ $[2] = status.isOnline;
1312
+ $[3] = syncStatus;
1313
+ $[4] = t0;
1314
+ $[5] = t1;
1315
+ } else {
1316
+ t0 = $[4];
1317
+ t1 = $[5];
1318
+ }
1319
+ useEffect3(t0, t1);
1320
+ return currentStatus;
1321
+ }
1322
+
1323
+ // src/hooks/useSyncControl.ts
1324
+ import { c as _c6 } from "react/compiler-runtime";
1325
+ function useSyncControl() {
1326
+ const $ = _c6(1);
1327
+ const {
1328
+ syncControl,
1329
+ status,
1330
+ powerSync
1331
+ } = useDataLayer();
1332
+ let t0;
1333
+ if ($[0] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel")) {
1334
+ t0 = {
1335
+ triggerSync: _temp,
1336
+ startLiveSync: _temp2,
1337
+ stopLiveSync: _temp3,
1338
+ setScope: _temp4
1339
+ };
1340
+ $[0] = t0;
1341
+ } else {
1342
+ t0 = $[0];
1343
+ }
1344
+ const noOpControls = t0;
1345
+ let t1;
1346
+ bb0: {
1347
+ if (powerSync && status.currentBackend === "powersync") {
1348
+ t1 = syncControl;
1349
+ break bb0;
1350
+ }
1351
+ t1 = noOpControls;
1352
+ }
1353
+ const controls = t1;
1354
+ return controls;
1355
+ }
1356
+ async function _temp4(_scopeName, _values) {
1357
+ console.warn("[useSyncControl] setScope called but PowerSync is not available");
1358
+ }
1359
+ function _temp3() {
1360
+ console.warn("[useSyncControl] stopLiveSync called but PowerSync is not available");
1361
+ }
1362
+ async function _temp2() {
1363
+ console.warn("[useSyncControl] startLiveSync called but PowerSync is not available");
1364
+ }
1365
+ async function _temp() {
1366
+ console.warn("[useSyncControl] triggerSync called but PowerSync is not available");
1367
+ }
1368
+
1369
+ // src/hooks/useOnlineStatus.ts
1370
+ import { c as _c7 } from "react/compiler-runtime";
1371
+ function useOnlineStatus() {
1372
+ const $ = _c7(4);
1373
+ const {
1374
+ status,
1375
+ powerSync
1376
+ } = useDataLayer();
1377
+ const t0 = powerSync !== null && status.powerSyncStatus === "available";
1378
+ let t1;
1379
+ if ($[0] !== status.currentBackend || $[1] !== status.isOnline || $[2] !== t0) {
1380
+ t1 = {
1381
+ isOnline: status.isOnline,
1382
+ isConnected: t0,
1383
+ backend: status.currentBackend
1384
+ };
1385
+ $[0] = status.currentBackend;
1386
+ $[1] = status.isOnline;
1387
+ $[2] = t0;
1388
+ $[3] = t1;
1389
+ } else {
1390
+ t1 = $[3];
1391
+ }
1392
+ return t1;
1393
+ }
1394
+
1395
+ export {
1396
+ DataLayerContext,
1397
+ useDataLayer,
1398
+ useDataLayerOptional,
1399
+ useDbQuery,
1400
+ useDbQueryById,
1401
+ useAdvanceQuery,
1402
+ useDbInsert,
1403
+ useDbUpdate,
1404
+ useDbUpsert,
1405
+ useDbDelete,
1406
+ useDbInfiniteQuery,
1407
+ useDbCount,
1408
+ useSyncStatus,
1409
+ useSyncControl,
1410
+ useOnlineStatus
1411
+ };
1412
+ //# sourceMappingURL=chunk-ZVBHWU7O.js.map