supastash 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.
Files changed (232) hide show
  1. package/README.md +250 -0
  2. package/dist/core/config/index.d.ts +90 -0
  3. package/dist/core/config/index.d.ts.map +1 -0
  4. package/dist/core/config/index.js +111 -0
  5. package/dist/core/schemaManager/index.d.ts +19 -0
  6. package/dist/core/schemaManager/index.d.ts.map +1 -0
  7. package/dist/core/schemaManager/index.js +53 -0
  8. package/dist/db/adapters/expo_sqlite.d.ts +3 -0
  9. package/dist/db/adapters/expo_sqlite.d.ts.map +1 -0
  10. package/dist/db/adapters/expo_sqlite.js +21 -0
  11. package/dist/db/adapters/rn_nitro.d.ts +3 -0
  12. package/dist/db/adapters/rn_nitro.d.ts.map +1 -0
  13. package/dist/db/adapters/rn_nitro.js +22 -0
  14. package/dist/db/adapters/rn_sqlite_storage.d.ts +3 -0
  15. package/dist/db/adapters/rn_sqlite_storage.d.ts.map +1 -0
  16. package/dist/db/adapters/rn_sqlite_storage.js +22 -0
  17. package/dist/db/dbErrorMsg.d.ts +2 -0
  18. package/dist/db/dbErrorMsg.d.ts.map +1 -0
  19. package/dist/db/dbErrorMsg.js +28 -0
  20. package/dist/db/dbInitializer.d.ts +7 -0
  21. package/dist/db/dbInitializer.d.ts.map +1 -0
  22. package/dist/db/dbInitializer.js +33 -0
  23. package/dist/hooks/supastashData/addPayloadToUI.d.ts +2 -0
  24. package/dist/hooks/supastashData/addPayloadToUI.d.ts.map +1 -0
  25. package/dist/hooks/supastashData/addPayloadToUI.js +36 -0
  26. package/dist/hooks/supastashData/dataState.d.ts +8 -0
  27. package/dist/hooks/supastashData/dataState.d.ts.map +1 -0
  28. package/dist/hooks/supastashData/dataState.js +7 -0
  29. package/dist/hooks/supastashData/eventQueues.d.ts +5 -0
  30. package/dist/hooks/supastashData/eventQueues.d.ts.map +1 -0
  31. package/dist/hooks/supastashData/eventQueues.js +54 -0
  32. package/dist/hooks/supastashData/fetchCalls.d.ts +9 -0
  33. package/dist/hooks/supastashData/fetchCalls.d.ts.map +1 -0
  34. package/dist/hooks/supastashData/fetchCalls.js +63 -0
  35. package/dist/hooks/supastashData/index.d.ts +53 -0
  36. package/dist/hooks/supastashData/index.d.ts.map +1 -0
  37. package/dist/hooks/supastashData/index.js +95 -0
  38. package/dist/hooks/supastashData/realtimeSubscription.d.ts +5 -0
  39. package/dist/hooks/supastashData/realtimeSubscription.d.ts.map +1 -0
  40. package/dist/hooks/supastashData/realtimeSubscription.js +95 -0
  41. package/dist/hooks/supastashData/registerSub.d.ts +4 -0
  42. package/dist/hooks/supastashData/registerSub.d.ts.map +1 -0
  43. package/dist/hooks/supastashData/registerSub.js +20 -0
  44. package/dist/hooks/supastashLogic.d.ts +19 -0
  45. package/dist/hooks/supastashLogic.d.ts.map +1 -0
  46. package/dist/hooks/supastashLogic.js +67 -0
  47. package/dist/hooks/syncEngine/index.d.ts +9 -0
  48. package/dist/hooks/syncEngine/index.d.ts.map +1 -0
  49. package/dist/hooks/syncEngine/index.js +66 -0
  50. package/dist/hooks/syncEngine/pullFromRemote/index.d.ts +5 -0
  51. package/dist/hooks/syncEngine/pullFromRemote/index.d.ts.map +1 -0
  52. package/dist/hooks/syncEngine/pullFromRemote/index.js +28 -0
  53. package/dist/hooks/syncEngine/pushLocal/index.d.ts +5 -0
  54. package/dist/hooks/syncEngine/pushLocal/index.d.ts.map +1 -0
  55. package/dist/hooks/syncEngine/pushLocal/index.js +36 -0
  56. package/dist/index.d.ts +14 -0
  57. package/dist/index.d.ts.map +1 -0
  58. package/dist/index.js +10 -0
  59. package/dist/store/tableFilters.d.ts +3 -0
  60. package/dist/store/tableFilters.d.ts.map +1 -0
  61. package/dist/store/tableFilters.js +2 -0
  62. package/dist/types/expoSqlite.types.d.ts +57 -0
  63. package/dist/types/index.d.ts +5 -0
  64. package/dist/types/index.d.ts.map +1 -0
  65. package/dist/types/index.js +1 -0
  66. package/dist/types/query.types.d.ts +153 -0
  67. package/dist/types/realtimeData.types.d.ts +186 -0
  68. package/dist/types/rnNitroSqlite.types.d.ts +101 -0
  69. package/dist/types/rnSqliteStorage.types.d.ts +67 -0
  70. package/dist/types/schemaManager.types.d.ts +44 -0
  71. package/dist/types/supastashConfig.types.d.ts +72 -0
  72. package/dist/types/syncEngine.types.d.ts +12 -0
  73. package/dist/utils/connection.d.ts +2 -0
  74. package/dist/utils/connection.d.ts.map +1 -0
  75. package/dist/utils/connection.js +7 -0
  76. package/dist/utils/events/eventBus.d.ts +3 -0
  77. package/dist/utils/events/eventBus.d.ts.map +1 -0
  78. package/dist/utils/events/eventBus.js +4 -0
  79. package/dist/utils/fetchData/buildFilter.d.ts +7 -0
  80. package/dist/utils/fetchData/buildFilter.d.ts.map +1 -0
  81. package/dist/utils/fetchData/buildFilter.js +13 -0
  82. package/dist/utils/fetchData/createTable.d.ts +8 -0
  83. package/dist/utils/fetchData/createTable.d.ts.map +1 -0
  84. package/dist/utils/fetchData/createTable.js +48 -0
  85. package/dist/utils/fetchData/deleteData.d.ts +3 -0
  86. package/dist/utils/fetchData/deleteData.d.ts.map +1 -0
  87. package/dist/utils/fetchData/deleteData.js +25 -0
  88. package/dist/utils/fetchData/fetchLocalData.d.ts +9 -0
  89. package/dist/utils/fetchData/fetchLocalData.d.ts.map +1 -0
  90. package/dist/utils/fetchData/fetchLocalData.js +51 -0
  91. package/dist/utils/fetchData/getKeyType.d.ts +7 -0
  92. package/dist/utils/fetchData/getKeyType.d.ts.map +1 -0
  93. package/dist/utils/fetchData/getKeyType.js +25 -0
  94. package/dist/utils/fetchData/initialFetch.d.ts +3 -0
  95. package/dist/utils/fetchData/initialFetch.d.ts.map +1 -0
  96. package/dist/utils/fetchData/initialFetch.js +20 -0
  97. package/dist/utils/fetchData/realTimeCall.d.ts +5 -0
  98. package/dist/utils/fetchData/realTimeCall.d.ts.map +1 -0
  99. package/dist/utils/fetchData/realTimeCall.js +43 -0
  100. package/dist/utils/fetchData/realTimeManager.d.ts +40 -0
  101. package/dist/utils/fetchData/realTimeManager.d.ts.map +1 -0
  102. package/dist/utils/fetchData/realTimeManager.js +262 -0
  103. package/dist/utils/fetchData/receiveData.d.ts +3 -0
  104. package/dist/utils/fetchData/receiveData.d.ts.map +1 -0
  105. package/dist/utils/fetchData/receiveData.js +38 -0
  106. package/dist/utils/fetchData/setDataInBatches.d.ts +3 -0
  107. package/dist/utils/fetchData/setDataInBatches.d.ts.map +1 -0
  108. package/dist/utils/fetchData/setDataInBatches.js +20 -0
  109. package/dist/utils/fetchData/validatePayload.d.ts +7 -0
  110. package/dist/utils/fetchData/validatePayload.d.ts.map +1 -0
  111. package/dist/utils/fetchData/validatePayload.js +18 -0
  112. package/dist/utils/genUUID.d.ts +2 -0
  113. package/dist/utils/genUUID.d.ts.map +1 -0
  114. package/dist/utils/genUUID.js +7 -0
  115. package/dist/utils/getTableSchema.d.ts +8 -0
  116. package/dist/utils/getTableSchema.d.ts.map +1 -0
  117. package/dist/utils/getTableSchema.js +28 -0
  118. package/dist/utils/logs.d.ts +7 -0
  119. package/dist/utils/logs.d.ts.map +1 -0
  120. package/dist/utils/logs.js +15 -0
  121. package/dist/utils/query/builder/crud.d.ts +50 -0
  122. package/dist/utils/query/builder/crud.d.ts.map +1 -0
  123. package/dist/utils/query/builder/crud.js +83 -0
  124. package/dist/utils/query/builder/filters.d.ts +145 -0
  125. package/dist/utils/query/builder/filters.d.ts.map +1 -0
  126. package/dist/utils/query/builder/filters.js +223 -0
  127. package/dist/utils/query/builder/index.d.ts +45 -0
  128. package/dist/utils/query/builder/index.d.ts.map +1 -0
  129. package/dist/utils/query/builder/index.js +60 -0
  130. package/dist/utils/query/builder/mainQuery.d.ts +10 -0
  131. package/dist/utils/query/builder/mainQuery.d.ts.map +1 -0
  132. package/dist/utils/query/builder/mainQuery.js +63 -0
  133. package/dist/utils/query/helpers/localDb/getLocalMethod.d.ts +15 -0
  134. package/dist/utils/query/helpers/localDb/getLocalMethod.d.ts.map +1 -0
  135. package/dist/utils/query/helpers/localDb/getLocalMethod.js +24 -0
  136. package/dist/utils/query/helpers/localDb/localQueryBuilder.d.ts +34 -0
  137. package/dist/utils/query/helpers/localDb/localQueryBuilder.d.ts.map +1 -0
  138. package/dist/utils/query/helpers/localDb/localQueryBuilder.js +52 -0
  139. package/dist/utils/query/helpers/mainQueryHelpers.d.ts +12 -0
  140. package/dist/utils/query/helpers/mainQueryHelpers.d.ts.map +1 -0
  141. package/dist/utils/query/helpers/mainQueryHelpers.js +164 -0
  142. package/dist/utils/query/helpers/queryValidator.d.ts +7 -0
  143. package/dist/utils/query/helpers/queryValidator.d.ts.map +1 -0
  144. package/dist/utils/query/helpers/queryValidator.js +27 -0
  145. package/dist/utils/query/helpers/remoteDb/queryFilterBuilder.d.ts +12 -0
  146. package/dist/utils/query/helpers/remoteDb/queryFilterBuilder.d.ts.map +1 -0
  147. package/dist/utils/query/helpers/remoteDb/queryFilterBuilder.js +41 -0
  148. package/dist/utils/query/helpers/remoteDb/queryUtils.d.ts +8 -0
  149. package/dist/utils/query/helpers/remoteDb/queryUtils.d.ts.map +1 -0
  150. package/dist/utils/query/helpers/remoteDb/queryUtils.js +30 -0
  151. package/dist/utils/query/localDbQuery/delete.d.ts +16 -0
  152. package/dist/utils/query/localDbQuery/delete.d.ts.map +1 -0
  153. package/dist/utils/query/localDbQuery/delete.js +54 -0
  154. package/dist/utils/query/localDbQuery/index.d.ts +8 -0
  155. package/dist/utils/query/localDbQuery/index.d.ts.map +1 -0
  156. package/dist/utils/query/localDbQuery/index.js +15 -0
  157. package/dist/utils/query/localDbQuery/insert.d.ts +10 -0
  158. package/dist/utils/query/localDbQuery/insert.d.ts.map +1 -0
  159. package/dist/utils/query/localDbQuery/insert.js +64 -0
  160. package/dist/utils/query/localDbQuery/select.d.ts +13 -0
  161. package/dist/utils/query/localDbQuery/select.d.ts.map +1 -0
  162. package/dist/utils/query/localDbQuery/select.js +41 -0
  163. package/dist/utils/query/localDbQuery/update.d.ts +11 -0
  164. package/dist/utils/query/localDbQuery/update.d.ts.map +1 -0
  165. package/dist/utils/query/localDbQuery/update.js +56 -0
  166. package/dist/utils/query/localDbQuery/upsert.d.ts +9 -0
  167. package/dist/utils/query/localDbQuery/upsert.d.ts.map +1 -0
  168. package/dist/utils/query/localDbQuery/upsert.js +67 -0
  169. package/dist/utils/query/remoteQuery/supabaseQuery.d.ts +8 -0
  170. package/dist/utils/query/remoteQuery/supabaseQuery.d.ts.map +1 -0
  171. package/dist/utils/query/remoteQuery/supabaseQuery.js +124 -0
  172. package/dist/utils/schema/createSyncStatus.d.ts +9 -0
  173. package/dist/utils/schema/createSyncStatus.d.ts.map +1 -0
  174. package/dist/utils/schema/createSyncStatus.js +23 -0
  175. package/dist/utils/schema/wipeTables.d.ts +66 -0
  176. package/dist/utils/schema/wipeTables.d.ts.map +1 -0
  177. package/dist/utils/schema/wipeTables.js +124 -0
  178. package/dist/utils/serializer.d.ts +8 -0
  179. package/dist/utils/serializer.d.ts.map +1 -0
  180. package/dist/utils/serializer.js +32 -0
  181. package/dist/utils/supabaseClientErr.d.ts +2 -0
  182. package/dist/utils/supabaseClientErr.d.ts.map +1 -0
  183. package/dist/utils/supabaseClientErr.js +15 -0
  184. package/dist/utils/sync/getAllTables.d.ts +2 -0
  185. package/dist/utils/sync/getAllTables.d.ts.map +1 -0
  186. package/dist/utils/sync/getAllTables.js +7 -0
  187. package/dist/utils/sync/pullFromRemote/getLastDeletedInfo.d.ts +13 -0
  188. package/dist/utils/sync/pullFromRemote/getLastDeletedInfo.d.ts.map +1 -0
  189. package/dist/utils/sync/pullFromRemote/getLastDeletedInfo.js +29 -0
  190. package/dist/utils/sync/pullFromRemote/getLastPulledInfo.d.ts +13 -0
  191. package/dist/utils/sync/pullFromRemote/getLastPulledInfo.d.ts.map +1 -0
  192. package/dist/utils/sync/pullFromRemote/getLastPulledInfo.js +29 -0
  193. package/dist/utils/sync/pullFromRemote/pullData.d.ts +9 -0
  194. package/dist/utils/sync/pullFromRemote/pullData.d.ts.map +1 -0
  195. package/dist/utils/sync/pullFromRemote/pullData.js +71 -0
  196. package/dist/utils/sync/pullFromRemote/pullDeletedData.d.ts +12 -0
  197. package/dist/utils/sync/pullFromRemote/pullDeletedData.d.ts.map +1 -0
  198. package/dist/utils/sync/pullFromRemote/pullDeletedData.js +69 -0
  199. package/dist/utils/sync/pullFromRemote/stringifyFields.d.ts +2 -0
  200. package/dist/utils/sync/pullFromRemote/stringifyFields.d.ts.map +1 -0
  201. package/dist/utils/sync/pullFromRemote/stringifyFields.js +11 -0
  202. package/dist/utils/sync/pullFromRemote/updateLocalDb.d.ts +14 -0
  203. package/dist/utils/sync/pullFromRemote/updateLocalDb.d.ts.map +1 -0
  204. package/dist/utils/sync/pullFromRemote/updateLocalDb.js +128 -0
  205. package/dist/utils/sync/pushLocal/deleteChunks.d.ts +8 -0
  206. package/dist/utils/sync/pushLocal/deleteChunks.d.ts.map +1 -0
  207. package/dist/utils/sync/pushLocal/deleteChunks.js +51 -0
  208. package/dist/utils/sync/pushLocal/getAllUnsyncedData.d.ts +14 -0
  209. package/dist/utils/sync/pushLocal/getAllUnsyncedData.d.ts.map +1 -0
  210. package/dist/utils/sync/pushLocal/getAllUnsyncedData.js +70 -0
  211. package/dist/utils/sync/pushLocal/parseFields.d.ts +7 -0
  212. package/dist/utils/sync/pushLocal/parseFields.d.ts.map +1 -0
  213. package/dist/utils/sync/pushLocal/parseFields.js +24 -0
  214. package/dist/utils/sync/pushLocal/sendUnsyncedToSupabase.d.ts +6 -0
  215. package/dist/utils/sync/pushLocal/sendUnsyncedToSupabase.d.ts.map +1 -0
  216. package/dist/utils/sync/pushLocal/sendUnsyncedToSupabase.js +43 -0
  217. package/dist/utils/sync/pushLocal/uploadChunk.d.ts +8 -0
  218. package/dist/utils/sync/pushLocal/uploadChunk.d.ts.map +1 -0
  219. package/dist/utils/sync/pushLocal/uploadChunk.js +117 -0
  220. package/dist/utils/sync/refreshTables.d.ts +12 -0
  221. package/dist/utils/sync/refreshTables.d.ts.map +1 -0
  222. package/dist/utils/sync/refreshTables.js +40 -0
  223. package/dist/utils/syncStatus.d.ts +67 -0
  224. package/dist/utils/syncStatus.d.ts.map +1 -0
  225. package/dist/utils/syncStatus.js +97 -0
  226. package/dist/utils/syncUpdate.d.ts +7 -0
  227. package/dist/utils/syncUpdate.d.ts.map +1 -0
  228. package/dist/utils/syncUpdate.js +19 -0
  229. package/dist/utils/tableValidator.d.ts +12 -0
  230. package/dist/utils/tableValidator.d.ts.map +1 -0
  231. package/dist/utils/tableValidator.js +23 -0
  232. package/package.json +60 -0
@@ -0,0 +1,66 @@
1
+ import { useRef } from "react";
2
+ import { AppState } from "react-native";
3
+ import { getSupastashConfig } from "../../core/config";
4
+ import { isOnline } from "../../utils/connection";
5
+ import log from "../../utils/logs";
6
+ import { pullFromRemote } from "./pullFromRemote";
7
+ import { pushLocalData } from "./pushLocal";
8
+ let isSyncing = false;
9
+ let lastFullSync = 0;
10
+ const syncPollingInterval = getSupastashConfig().pollingInterval?.push || 30000;
11
+ /**
12
+ * Syncs the local data to the remote database
13
+ */
14
+ export async function syncEngine(force = false) {
15
+ if (isSyncing)
16
+ return;
17
+ if (!(await isOnline()))
18
+ return;
19
+ try {
20
+ isSyncing = true;
21
+ await pushLocalData();
22
+ if (!getSupastashConfig().syncEngine?.pull)
23
+ return;
24
+ const now = Date.now();
25
+ const shouldPull = force ||
26
+ now - lastFullSync >
27
+ (getSupastashConfig().pollingInterval?.pull || 30000);
28
+ if (shouldPull) {
29
+ await pullFromRemote();
30
+ lastFullSync = now;
31
+ }
32
+ }
33
+ catch (error) {
34
+ log(`[Supastash] Error syncing: ${error}`);
35
+ }
36
+ finally {
37
+ isSyncing = false;
38
+ }
39
+ }
40
+ export function useSyncEngine() {
41
+ const isSyncingRef = useRef(false);
42
+ const intervalRef = useRef(null);
43
+ const appStateRef = useRef(null);
44
+ function startSync() {
45
+ if (isSyncingRef.current)
46
+ return;
47
+ isSyncingRef.current = true;
48
+ const config = getSupastashConfig();
49
+ const syncPollingInterval = config.pollingInterval?.push ?? 30000;
50
+ intervalRef.current = setInterval(() => {
51
+ syncEngine();
52
+ }, syncPollingInterval);
53
+ appStateRef.current = AppState.addEventListener("change", (state) => {
54
+ if (state === "active") {
55
+ syncEngine(true);
56
+ }
57
+ });
58
+ }
59
+ function stopSync() {
60
+ if (intervalRef.current)
61
+ clearInterval(intervalRef.current);
62
+ appStateRef.current?.remove?.();
63
+ isSyncingRef.current = false;
64
+ }
65
+ return { startSync, stopSync };
66
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Pulls the data from the remote database to the local database
3
+ */
4
+ export declare function pullFromRemote(): Promise<void>;
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/hooks/syncEngine/pullFromRemote/index.ts"],"names":[],"mappings":"AAMA;;GAEG;AACH,wBAAsB,cAAc,kBAyBnC"}
@@ -0,0 +1,28 @@
1
+ import { getSupastashConfig } from "../../../core/config";
2
+ import { tableFilters } from "../../../store/tableFilters";
3
+ import log from "../../../utils/logs";
4
+ import { getAllTables } from "../../../utils/sync/getAllTables";
5
+ import { updateLocalDb } from "../../../utils/sync/pullFromRemote/updateLocalDb";
6
+ /**
7
+ * Pulls the data from the remote database to the local database
8
+ */
9
+ export async function pullFromRemote() {
10
+ const config = getSupastashConfig();
11
+ const { useFiltersFromStore = true } = config?.syncEngine || {};
12
+ const filter = (table) => useFiltersFromStore ? tableFilters.get(table) : undefined;
13
+ try {
14
+ const tables = await getAllTables();
15
+ if (!tables) {
16
+ log(`[Supastash] No tables found`);
17
+ return;
18
+ }
19
+ const excludeTables = getSupastashConfig()?.excludeTables?.pull || [];
20
+ const tablesToPull = tables.filter((table) => !excludeTables?.includes(table));
21
+ for (const table of tablesToPull) {
22
+ await updateLocalDb(table, filter(table));
23
+ }
24
+ }
25
+ catch (error) {
26
+ log(`[Supastash] Error pulling from remote: ${error}`);
27
+ }
28
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Pushes the local data to the remote database
3
+ */
4
+ export declare function pushLocalData(): Promise<void>;
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/hooks/syncEngine/pushLocal/index.ts"],"names":[],"mappings":"AAQA;;GAEG;AACH,wBAAsB,aAAa,kBAmClC"}
@@ -0,0 +1,36 @@
1
+ import { getSupastashConfig } from "../../../core/config";
2
+ import log from "../../../utils/logs";
3
+ import { getAllTables } from "../../../utils/sync/getAllTables";
4
+ import { pushLocalDataToRemote } from "../../../utils/sync/pushLocal/sendUnsyncedToSupabase";
5
+ let timesPushed = 0;
6
+ let lastPushed = 0;
7
+ /**
8
+ * Pushes the local data to the remote database
9
+ */
10
+ export async function pushLocalData() {
11
+ try {
12
+ const tables = await getAllTables();
13
+ if (!tables) {
14
+ log("No tables found");
15
+ return;
16
+ }
17
+ const excludeTables = getSupastashConfig()?.excludeTables?.push || [];
18
+ const tablesToPush = tables.filter((table) => !excludeTables?.includes(table));
19
+ const noSync = [];
20
+ for (const table of tablesToPush) {
21
+ await pushLocalDataToRemote(table, undefined, noSync);
22
+ }
23
+ if (noSync.length > 0) {
24
+ timesPushed++;
25
+ if (timesPushed >= 30) {
26
+ const timeSinceLastPush = Date.now() - lastPushed;
27
+ lastPushed = Date.now();
28
+ log(`[Supastash] No sync data found for tables: ${noSync.join(", ")} (times pushed: ${timesPushed}) in the last ${timeSinceLastPush}ms`);
29
+ timesPushed = 0;
30
+ }
31
+ }
32
+ }
33
+ catch (error) {
34
+ log(`[Supastash] Error pushing local data to remote database: ${error}`);
35
+ }
36
+ }
@@ -0,0 +1,14 @@
1
+ export { configureSupastash, getSupastashConfig } from "./core/config";
2
+ export { defineLocalSchema } from "./core/schemaManager";
3
+ export { getSupastashDb } from "./db/dbInitializer";
4
+ export { useSupastashData } from "./hooks/supastashData";
5
+ export { useSupastash } from "./hooks/supastashLogic";
6
+ export { supastash } from "./utils/query/builder";
7
+ export { wipeAllTables, wipeOldDataForAllTables, wipeOldDataForATable, wipeTable, } from "./utils/schema/wipeTables";
8
+ export { getAllTables } from "./utils/sync/getAllTables";
9
+ export { refreshAllTables, refreshTable } from "./utils/sync/refreshTables";
10
+ export { clearAllLocalDeleteLog, clearAllLocalSyncLog, clearLocalDeleteLog, clearLocalSyncLog, getLocalDeleteLog, getLocalSyncLog, setLocalDeleteLog, setLocalSyncLog, } from "./utils/syncStatus";
11
+ export type { CrudMethods } from "./types/query.types";
12
+ export type { RealtimeOptions, SupastashDataResult, } from "./types/realtimeData.types";
13
+ export type { SupastashConfig, SupastashHookReturn, SupastashSQLiteClientTypes, SupastashSQLiteDatabase, } from "./types/supastashConfig.types";
14
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACvE,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAClD,OAAO,EACL,aAAa,EACb,uBAAuB,EACvB,oBAAoB,EACpB,SAAS,GACV,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC5E,OAAO,EACL,sBAAsB,EACtB,oBAAoB,EACpB,mBAAmB,EACnB,iBAAiB,EACjB,iBAAiB,EACjB,eAAe,EACf,iBAAiB,EACjB,eAAe,GAChB,MAAM,oBAAoB,CAAC;AAE5B,YAAY,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AACvD,YAAY,EACV,eAAe,EACf,mBAAmB,GACpB,MAAM,4BAA4B,CAAC;AACpC,YAAY,EACV,eAAe,EACf,mBAAmB,EACnB,0BAA0B,EAC1B,uBAAuB,GACxB,MAAM,+BAA+B,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,10 @@
1
+ export { configureSupastash, getSupastashConfig } from "./core/config";
2
+ export { defineLocalSchema } from "./core/schemaManager";
3
+ export { getSupastashDb } from "./db/dbInitializer";
4
+ export { useSupastashData } from "./hooks/supastashData";
5
+ export { useSupastash } from "./hooks/supastashLogic";
6
+ export { supastash } from "./utils/query/builder";
7
+ export { wipeAllTables, wipeOldDataForAllTables, wipeOldDataForATable, wipeTable, } from "./utils/schema/wipeTables";
8
+ export { getAllTables } from "./utils/sync/getAllTables";
9
+ export { refreshAllTables, refreshTable } from "./utils/sync/refreshTables";
10
+ export { clearAllLocalDeleteLog, clearAllLocalSyncLog, clearLocalDeleteLog, clearLocalSyncLog, getLocalDeleteLog, getLocalSyncLog, setLocalDeleteLog, setLocalSyncLog, } from "./utils/syncStatus";
@@ -0,0 +1,3 @@
1
+ import { RealtimeFilter } from "../types/realtimeData.types";
2
+ export declare const tableFilters: Map<string, RealtimeFilter>;
3
+ //# sourceMappingURL=tableFilters.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tableFilters.d.ts","sourceRoot":"","sources":["../../src/store/tableFilters.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAE7D,eAAO,MAAM,YAAY,6BAAoC,CAAC"}
@@ -0,0 +1,2 @@
1
+ // Store for table filters
2
+ export const tableFilters = new Map();
@@ -0,0 +1,57 @@
1
+ export interface ExpoSQLiteDatabase {
2
+ runAsync<T = any>(
3
+ statement: string,
4
+ params: SQLiteBindParams
5
+ ): Promise<SQLiteRunResult>;
6
+ runAsync<T = any>(
7
+ statement: string,
8
+ ...params: SQLiteVariadicBindParams
9
+ ): Promise<SQLiteRunResult>;
10
+
11
+ getFirstAsync<T = any>(
12
+ statement: string,
13
+ params: SQLiteBindParams
14
+ ): Promise<T | null>;
15
+ getFirstAsync<T = any>(
16
+ statement: string,
17
+ ...params: SQLiteVariadicBindParams
18
+ ): Promise<T | null>;
19
+
20
+ getAllAsync<T = any>(
21
+ statement: string,
22
+ params: SQLiteBindParams
23
+ ): Promise<T[]>;
24
+ getAllAsync<T = any>(
25
+ statement: string,
26
+ ...params: SQLiteVariadicBindParams
27
+ ): Promise<T[]>;
28
+
29
+ execAsync(statement: string): Promise<void>;
30
+ }
31
+
32
+ export type SQLiteBindValue = string | number | null | boolean | Uint8Array;
33
+ export type SQLiteBindParams =
34
+ | Record<string, SQLiteBindValue>
35
+ | SQLiteBindValue[];
36
+ export type SQLiteVariadicBindParams = SQLiteBindValue[];
37
+
38
+ export type SQLiteBindPrimitiveParams = Record<
39
+ string,
40
+ Exclude<SQLiteBindValue, Uint8Array>
41
+ >;
42
+ export type SQLiteBindBlobParams = Record<string, Uint8Array>;
43
+ export type SQLiteColumnNames = string[];
44
+ export type SQLiteColumnValues = any[];
45
+ export type SQLiteAnyDatabase = any;
46
+
47
+ export interface SQLiteRunResult {
48
+ /**
49
+ * The last inserted row ID. Returned from the [`sqlite3_last_insert_rowid()`](https://www.sqlite.org/c3ref/last_insert_rowid.html) function.
50
+ */
51
+ lastInsertRowId: number;
52
+
53
+ /**
54
+ * The number of rows affected. Returned from the [`sqlite3_changes()`](https://www.sqlite.org/c3ref/changes.html) function.
55
+ */
56
+ changes: number;
57
+ }
@@ -0,0 +1,5 @@
1
+ export type { SupastashQueryResult, SupastashResult } from "./query.types";
2
+ export type { SupastashDataHook, SupastashDataResult, } from "./realtimeData.types";
3
+ export type { SupastashConfig, SupastashSQLiteClientTypes, } from "./supastashConfig.types";
4
+ export type { SyncResult, TableSchema } from "./syncEngine.types";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,oBAAoB,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAC3E,YAAY,EACV,iBAAiB,EACjB,mBAAmB,GACpB,MAAM,sBAAsB,CAAC;AAC9B,YAAY,EACV,eAAe,EACf,0BAA0B,GAC3B,MAAM,yBAAyB,CAAC;AACjC,YAAY,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,153 @@
1
+ import { PostgrestError, PostgrestSingleResponse } from "@supabase/supabase-js";
2
+
3
+ type SupabaseResult<T> = PostgrestSingleResponse<T>;
4
+ export type PayloadData = any;
5
+
6
+ export type SupabaseQueryReturn<U extends boolean, R> = U extends true
7
+ ? PostgrestSingleResponse<R>
8
+ : PostgrestSingleResponse<R[]>;
9
+
10
+ type FilterOperations =
11
+ | "="
12
+ | "!="
13
+ | ">"
14
+ | "<"
15
+ | ">="
16
+ | "<="
17
+ | "LIKE"
18
+ | "IS"
19
+ | "IN";
20
+
21
+ export type CrudMethods =
22
+ | "select"
23
+ | "insert"
24
+ | "update"
25
+ | "delete"
26
+ | "upsert"
27
+ | "none";
28
+
29
+ export type FilterCalls = {
30
+ column: string;
31
+ operator: FilterOperations;
32
+ value: any;
33
+ };
34
+
35
+ export interface SupastashQuery<T extends CrudMethods, U extends boolean, R> {
36
+ id: string;
37
+ table: string;
38
+ method: T;
39
+ payload: R | R[] | null;
40
+ filters: FilterCalls[] | null;
41
+ limit: number | null;
42
+ select: string | null;
43
+ isSingle: U;
44
+ type: SyncMode;
45
+ runSelected: boolean;
46
+ viewRemoteResult: boolean;
47
+ }
48
+
49
+ export interface CrudReturnValue {
50
+ error;
51
+ }
52
+
53
+ export type SupastashResult<R> = {
54
+ data: R | null;
55
+ error: {
56
+ message: string;
57
+ name: string;
58
+ supabaseError?: PostgrestError;
59
+ } | null;
60
+ };
61
+
62
+ export type PayloadResult<R> = SupastashResult<R>;
63
+ export type PayloadListResult<R> = SupastashResult<R[]>;
64
+ export type SupatashDeleteResult<R> = {
65
+ data?: R[] | null;
66
+ error: {
67
+ message: string;
68
+ name: string;
69
+ supabaseError?: PostgrestError;
70
+ } | null;
71
+ };
72
+
73
+ export type MethodReturnTypeMap<U extends boolean, R> = {
74
+ select: U extends true ? SupastashResult<R> : SupastashResult<R[]>;
75
+ insert: U extends true ? SupastashResult<R> : SupastashResult<R[]>;
76
+ update: U extends true ? SupastashResult<R> : SupastashResult<R[]>;
77
+ delete: SupatashDeleteResult<R>;
78
+ upsert: U extends true ? SupastashResult<R> : SupastashResult<R[]>;
79
+ none: null;
80
+ };
81
+
82
+ export type HandlerMap<U extends boolean, R> = {
83
+ [K in CrudMethods]: () => Promise<MethodReturnTypeMap<U, R>[K]>;
84
+ };
85
+
86
+ export type SyncMode =
87
+ | "localFirst"
88
+ | "remoteFirst"
89
+ | "localOnly"
90
+ | "remoteOnly";
91
+
92
+ export type SupastashQueryResult<
93
+ T extends CrudMethods,
94
+ U extends boolean,
95
+ V extends boolean,
96
+ R
97
+ > = V extends true
98
+ ? Promise<{
99
+ remote: SupabaseQueryReturn<U, R> | null;
100
+ local: MethodReturnTypeMap<U, R>[T] | null;
101
+ success: boolean;
102
+ }>
103
+ : MethodReturnTypeMap<U, R>[T] extends { data: infer D; error: infer E }
104
+ ? Promise<{ data: D; error: E; success: boolean }>
105
+ : Promise<{
106
+ data: null;
107
+ error: { message: string } | null;
108
+ success: boolean;
109
+ }>;
110
+
111
+ export type QueryBuilder<T extends CrudMethods, U extends boolean> = {
112
+ eq(column: string, value: any): QueryBuilder<T, U>;
113
+ neq(column: string, value: any): QueryBuilder<T, U>;
114
+ gt(column: string, value: any): QueryBuilder<T, U>;
115
+ lt(column: string, value: any): QueryBuilder<T, U>;
116
+ gte(column: string, value: any): QueryBuilder<T, U>;
117
+ lte(column: string, value: any): QueryBuilder<T, U>;
118
+ like(column: string, value: any): QueryBuilder<T, U>;
119
+ is(column: string, value: any): QueryBuilder<T, U>;
120
+ in(column: string, value: any[]): QueryBuilder<T, U>;
121
+ limit(n: number): QueryBuilder<T, U>;
122
+ syncMode(mode: SyncMode): QueryBuilder<T, U>;
123
+
124
+ // Transitions to U = true when called
125
+ single(): QueryBuilder<T, true>;
126
+
127
+ // Executes the query
128
+ execute(): SupastashQueryResult<T, U>;
129
+ run(): SupastashQueryResult<T, U>;
130
+ go(): SupastashQueryResult<T, U>;
131
+ };
132
+
133
+ export interface ExecuteOptions {
134
+ /**
135
+ * The number of times to retry the remote query if it fails
136
+ */
137
+ remoteRetry?: number;
138
+
139
+ /**
140
+ * Whether to view the remote result
141
+ */
142
+ viewRemoteResult?: boolean;
143
+
144
+ /**
145
+ * The delay between remote retries
146
+ */
147
+ remoteRetryDelay?: number;
148
+
149
+ /**
150
+ * Whether to enable debug logging
151
+ */
152
+ debug?: boolean;
153
+ }
@@ -0,0 +1,186 @@
1
+ export type FilterOperator =
2
+ | "eq"
3
+ | "neq"
4
+ | "gt"
5
+ | "lt"
6
+ | "gte"
7
+ | "lte"
8
+ | "in"
9
+ | "is";
10
+
11
+ export type RealtimeFilter = {
12
+ column: string;
13
+ operator: FilterOperator;
14
+ value: string | number | null | (string | number)[];
15
+ };
16
+
17
+ export interface RealtimeOptions {
18
+ /**
19
+ * Whether to fetch local data automatically when the hook mounts.
20
+ * @default true
21
+ *
22
+ * @example
23
+ * const { user } = useAuth();
24
+ * const { data } = useSupastashData("messages", {
25
+ * shouldFetch: !!user
26
+ * });
27
+ */
28
+ shouldFetch?: boolean;
29
+
30
+ /**
31
+ * Whether to use the filter while syncing.
32
+ * @default true
33
+ *
34
+ * @example
35
+ * useFilterWhileSyncing: true
36
+ */
37
+ useFilterWhileSyncing?: boolean;
38
+
39
+ /**
40
+ * Filter condition applied to the Supabase realtime subscription stream.
41
+ *
42
+ * @example
43
+ * filter: {
44
+ * column: "user_id",
45
+ * operator: "eq",
46
+ * value: "123"
47
+ * }
48
+ */
49
+ filter?: RealtimeFilter;
50
+
51
+ /**
52
+ * Maximum number of records to fetch initially from the local database.
53
+ * @default 1000
54
+ *
55
+ * @example
56
+ * limit: 400
57
+ */
58
+ limit?: number;
59
+
60
+ /**
61
+ * Called when a new record is inserted into the table.
62
+ *
63
+ * @example
64
+ * onInsert: (payload) => {
65
+ * console.log("Inserted", payload);
66
+ * }
67
+ */
68
+ onInsert?: (payload: any) => void;
69
+
70
+ /**
71
+ * Called when an existing record is updated.
72
+ *
73
+ * @example
74
+ * onUpdate: (payload) => {
75
+ * console.log("Updated", payload);
76
+ * }
77
+ */
78
+ onUpdate?: (payload: any) => void;
79
+
80
+ /**
81
+ * Called when a record is deleted.
82
+ *
83
+ * @example
84
+ * onDelete: (payload) => {
85
+ * console.log("Deleted", payload);
86
+ * }
87
+ */
88
+ onDelete?: (payload: any) => void;
89
+
90
+ /**
91
+ * Called on both insert and update events.
92
+ * Useful when insert/update are handled the same way.
93
+ *
94
+ * @example
95
+ * onInsertAndUpdate: async (payload) => {
96
+ * const { data: localMessage } = await supastash
97
+ * .from("messages")
98
+ * .select("*")
99
+ * .eq("id", payload.id)
100
+ * .run();
101
+ *
102
+ * if (!localMessage || localMessage.is_received) return;
103
+ *
104
+ * await supastash
105
+ * .from("messages")
106
+ * .upsert({ ...localMessage, is_received: true })
107
+ * .run();
108
+ * }
109
+ */
110
+ onInsertAndUpdate?: (payload: any) => Promise<void>;
111
+
112
+ /**
113
+ * Called to push unsynced local records to the remote database.
114
+ * Must return `true` if push was successful; otherwise, it will be retried later.
115
+ *
116
+ * @example
117
+ * onPushToRemote: async (payload) => {
118
+ * const result = await supabase.from("messages").upsert(payload);
119
+ * return !result.error;
120
+ * }
121
+ */
122
+ onPushToRemote?: (payload: any[]) => Promise<boolean>;
123
+
124
+ /**
125
+ * Whether to use realtime subscription.
126
+ * @default true
127
+ *
128
+ * @example
129
+ * realtime: true
130
+ */
131
+ realtime?: boolean;
132
+
133
+ /**
134
+ * If true, delays the initial fetch until `trigger()` is manually called.
135
+ *
136
+ * @example
137
+ * lazy: true
138
+ *
139
+ * const { data, trigger } = useSupastashData("messages", { lazy: true });
140
+ *
141
+ * useEffect(() => {
142
+ * if (loaded) trigger();
143
+ * }, []);
144
+ */
145
+ lazy?: boolean;
146
+
147
+ /**
148
+ * Interval in milliseconds to batch UI updates for performance.
149
+ * @default 100
150
+ *
151
+ * @example
152
+ * flushIntervalMs: 200
153
+ */
154
+ flushIntervalMs?: number;
155
+ }
156
+
157
+ export type SupastashDataResult<R = any> = {
158
+ data: R[];
159
+ dataMap: Map<string, R>;
160
+ trigger: () => void;
161
+ cancel: () => void;
162
+ realtimeStatus: RealtimeStatus;
163
+ };
164
+
165
+ export type SupastashDataHook = (
166
+ table: string,
167
+ options: RealtimeOptions
168
+ ) => SupastashDataResult;
169
+
170
+ // Realtime manager types
171
+ export type EventHandler = (eventType: string, data: any) => void;
172
+ export type SubscriptionKey = string;
173
+ export type HookId = string;
174
+
175
+ export interface TableSubscription {
176
+ table: string;
177
+ filterString?: string;
178
+ handlers: Map<HookId, EventHandler>;
179
+ isActive: boolean;
180
+ }
181
+
182
+ export interface RealtimeStatus {
183
+ status: "disconnected" | "connecting" | "connected" | "error";
184
+ error?: string;
185
+ isNetworkConnected: boolean;
186
+ }