@powersync/common 0.0.0-dev-20260504100448 → 0.0.0-dev-20260630141119

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 (263) hide show
  1. package/lib/attachments/AttachmentContext.d.ts +9 -8
  2. package/lib/attachments/AttachmentContext.js +8 -3
  3. package/lib/attachments/AttachmentContext.js.map +1 -1
  4. package/lib/attachments/AttachmentErrorHandler.d.ts +6 -6
  5. package/lib/attachments/AttachmentQueue.d.ts +85 -36
  6. package/lib/attachments/AttachmentQueue.js +19 -21
  7. package/lib/attachments/AttachmentQueue.js.map +1 -1
  8. package/lib/attachments/AttachmentService.d.ts +3 -3
  9. package/lib/attachments/AttachmentService.js +4 -3
  10. package/lib/attachments/AttachmentService.js.map +1 -1
  11. package/lib/attachments/LocalStorageAdapter.d.ts +14 -8
  12. package/lib/attachments/LocalStorageAdapter.js +3 -0
  13. package/lib/attachments/LocalStorageAdapter.js.map +1 -1
  14. package/lib/attachments/RemoteStorageAdapter.d.ts +4 -4
  15. package/lib/attachments/Schema.d.ts +14 -6
  16. package/lib/attachments/Schema.js +8 -3
  17. package/lib/attachments/Schema.js.map +1 -1
  18. package/lib/attachments/SyncingService.d.ts +2 -2
  19. package/lib/attachments/SyncingService.js +8 -3
  20. package/lib/attachments/SyncingService.js.map +1 -1
  21. package/lib/attachments/WatchedAttachmentItem.d.ts +3 -1
  22. package/lib/client/{AbstractPowerSyncDatabase.d.ts → CommonPowerSyncDatabase.d.ts} +112 -238
  23. package/lib/client/CommonPowerSyncDatabase.js +2 -0
  24. package/lib/client/CommonPowerSyncDatabase.js.map +1 -0
  25. package/lib/client/Query.d.ts +9 -0
  26. package/lib/client/SQLOpenFactory.d.ts +44 -10
  27. package/lib/client/SQLOpenFactory.js +1 -19
  28. package/lib/client/SQLOpenFactory.js.map +1 -1
  29. package/lib/client/compilableQueryWatch.d.ts +8 -2
  30. package/lib/client/compilableQueryWatch.js +4 -2
  31. package/lib/client/compilableQueryWatch.js.map +1 -1
  32. package/lib/client/connection/PowerSyncBackendConnector.d.ts +6 -3
  33. package/lib/client/connection/PowerSyncCredentials.d.ts +3 -0
  34. package/lib/client/runOnSchemaChange.d.ts +5 -2
  35. package/lib/client/runOnSchemaChange.js +3 -0
  36. package/lib/client/runOnSchemaChange.js.map +1 -1
  37. package/lib/client/sync/bucket/CrudBatch.d.ts +2 -0
  38. package/lib/client/sync/bucket/CrudBatch.js +2 -0
  39. package/lib/client/sync/bucket/CrudBatch.js.map +1 -1
  40. package/lib/client/sync/bucket/CrudEntry.d.ts +9 -31
  41. package/lib/client/sync/bucket/CrudEntry.js +2 -97
  42. package/lib/client/sync/bucket/CrudEntry.js.map +1 -1
  43. package/lib/client/sync/bucket/CrudTransaction.d.ts +3 -0
  44. package/lib/client/sync/bucket/CrudTransaction.js +3 -0
  45. package/lib/client/sync/bucket/CrudTransaction.js.map +1 -1
  46. package/lib/client/sync/options.d.ts +65 -0
  47. package/lib/client/sync/options.js +25 -0
  48. package/lib/client/sync/options.js.map +1 -0
  49. package/lib/client/sync/stream/JsonValue.d.ts +3 -0
  50. package/lib/client/sync/sync-streams.d.ts +23 -8
  51. package/lib/client/triggers/TriggerManager.d.ts +28 -56
  52. package/lib/client/triggers/TriggerManager.js +2 -1
  53. package/lib/client/triggers/TriggerManager.js.map +1 -1
  54. package/lib/client/triggers/sanitizeSQL.d.ts +4 -0
  55. package/lib/client/triggers/sanitizeSQL.js +4 -0
  56. package/lib/client/triggers/sanitizeSQL.js.map +1 -1
  57. package/lib/client/watched/GetAllQuery.d.ts +7 -3
  58. package/lib/client/watched/GetAllQuery.js +3 -1
  59. package/lib/client/watched/GetAllQuery.js.map +1 -1
  60. package/lib/client/watched/WatchedQuery.d.ts +20 -6
  61. package/lib/client/watched/WatchedQuery.js +3 -5
  62. package/lib/client/watched/WatchedQuery.js.map +1 -1
  63. package/lib/client/watched/processors/DifferentialQueryProcessor.d.ts +15 -45
  64. package/lib/client/watched/processors/DifferentialQueryProcessor.js +1 -172
  65. package/lib/client/watched/processors/DifferentialQueryProcessor.js.map +1 -1
  66. package/lib/client/watched/processors/OnChangeQueryProcessor.d.ts +4 -22
  67. package/lib/client/watched/processors/OnChangeQueryProcessor.js +1 -82
  68. package/lib/client/watched/processors/OnChangeQueryProcessor.js.map +1 -1
  69. package/lib/client/watched/processors/comparators.d.ts +8 -0
  70. package/lib/client/watched/processors/comparators.js +4 -0
  71. package/lib/client/watched/processors/comparators.js.map +1 -1
  72. package/lib/db/DBAdapter.d.ts +90 -113
  73. package/lib/db/DBAdapter.js +70 -89
  74. package/lib/db/DBAdapter.js.map +1 -1
  75. package/lib/db/QueryResult.d.ts +104 -0
  76. package/lib/db/QueryResult.js +96 -0
  77. package/lib/db/QueryResult.js.map +1 -0
  78. package/lib/db/crud/SyncProgress.d.ts +7 -14
  79. package/lib/db/crud/SyncProgress.js +1 -60
  80. package/lib/db/crud/SyncProgress.js.map +1 -1
  81. package/lib/db/crud/SyncStatus.d.ts +54 -91
  82. package/lib/db/crud/SyncStatus.js +1 -245
  83. package/lib/db/crud/SyncStatus.js.map +1 -1
  84. package/lib/db/crud/UploadQueueStatus.d.ts +3 -0
  85. package/lib/db/crud/UploadQueueStatus.js +3 -0
  86. package/lib/db/crud/UploadQueueStatus.js.map +1 -1
  87. package/lib/db/schema/Column.d.ts +22 -1
  88. package/lib/db/schema/Column.js +10 -4
  89. package/lib/db/schema/Column.js.map +1 -1
  90. package/lib/db/schema/Index.d.ts +8 -3
  91. package/lib/db/schema/Index.js +4 -1
  92. package/lib/db/schema/Index.js.map +1 -1
  93. package/lib/db/schema/IndexedColumn.d.ts +8 -3
  94. package/lib/db/schema/IndexedColumn.js +4 -1
  95. package/lib/db/schema/IndexedColumn.js.map +1 -1
  96. package/lib/db/schema/RawTable.d.ts +7 -1
  97. package/lib/db/schema/Schema.d.ts +10 -29
  98. package/lib/db/schema/Schema.js +3 -1
  99. package/lib/db/schema/Schema.js.map +1 -1
  100. package/lib/db/schema/Table.d.ts +69 -91
  101. package/lib/db/schema/Table.js +88 -87
  102. package/lib/db/schema/Table.js.map +1 -1
  103. package/lib/index.d.ts +6 -22
  104. package/lib/index.js +5 -22
  105. package/lib/index.js.map +1 -1
  106. package/lib/types/types.d.ts +6 -0
  107. package/lib/utils/BaseObserver.d.ts +12 -0
  108. package/lib/utils/BaseObserver.js +3 -0
  109. package/lib/utils/BaseObserver.js.map +1 -1
  110. package/lib/utils/Logger.d.ts +60 -22
  111. package/lib/utils/Logger.js +38 -30
  112. package/lib/utils/Logger.js.map +1 -1
  113. package/lib/utils/MetaBaseObserver.d.ts +11 -11
  114. package/lib/utils/MetaBaseObserver.js +1 -50
  115. package/lib/utils/MetaBaseObserver.js.map +1 -1
  116. package/lib/utils/mutex.d.ts +3 -45
  117. package/lib/utils/mutex.js +1 -153
  118. package/lib/utils/mutex.js.map +1 -1
  119. package/package.json +9 -46
  120. package/src/attachments/AttachmentContext.ts +15 -13
  121. package/src/attachments/AttachmentErrorHandler.ts +6 -6
  122. package/src/attachments/AttachmentQueue.ts +99 -42
  123. package/src/attachments/AttachmentService.ts +7 -6
  124. package/src/attachments/LocalStorageAdapter.ts +14 -8
  125. package/src/attachments/README.md +2 -0
  126. package/src/attachments/RemoteStorageAdapter.ts +4 -4
  127. package/src/attachments/Schema.ts +14 -6
  128. package/src/attachments/SyncingService.ts +10 -6
  129. package/src/attachments/WatchedAttachmentItem.ts +3 -1
  130. package/src/client/CommonPowerSyncDatabase.ts +540 -0
  131. package/src/client/Query.ts +9 -0
  132. package/src/client/SQLOpenFactory.ts +47 -19
  133. package/src/client/compilableQueryWatch.ts +9 -4
  134. package/src/client/connection/PowerSyncBackendConnector.ts +6 -3
  135. package/src/client/connection/PowerSyncCredentials.ts +3 -0
  136. package/src/client/runOnSchemaChange.ts +5 -2
  137. package/src/client/sync/bucket/CrudBatch.ts +2 -0
  138. package/src/client/sync/bucket/CrudEntry.ts +10 -101
  139. package/src/client/sync/bucket/CrudTransaction.ts +3 -0
  140. package/src/client/sync/options.ts +77 -0
  141. package/src/client/sync/stream/JsonValue.ts +3 -0
  142. package/src/client/sync/sync-streams.ts +23 -10
  143. package/src/client/triggers/TriggerManager.ts +29 -59
  144. package/src/client/triggers/sanitizeSQL.ts +5 -0
  145. package/src/client/watched/GetAllQuery.ts +8 -4
  146. package/src/client/watched/WatchedQuery.ts +20 -11
  147. package/src/client/watched/processors/DifferentialQueryProcessor.ts +19 -219
  148. package/src/client/watched/processors/OnChangeQueryProcessor.ts +4 -109
  149. package/src/client/watched/processors/comparators.ts +8 -0
  150. package/src/db/DBAdapter.ts +160 -175
  151. package/src/db/QueryResult.ts +195 -0
  152. package/src/db/crud/SyncProgress.ts +8 -43
  153. package/src/db/crud/SyncStatus.ts +66 -216
  154. package/src/db/crud/UploadQueueStatus.ts +3 -0
  155. package/src/db/schema/Column.ts +22 -5
  156. package/src/db/schema/Index.ts +9 -3
  157. package/src/db/schema/IndexedColumn.ts +9 -3
  158. package/src/db/schema/RawTable.ts +7 -1
  159. package/src/db/schema/Schema.ts +12 -7
  160. package/src/db/schema/Table.ts +130 -180
  161. package/src/index.ts +6 -22
  162. package/src/types/types.ts +6 -0
  163. package/src/utils/BaseObserver.ts +12 -0
  164. package/src/utils/Logger.ts +86 -31
  165. package/src/utils/MetaBaseObserver.ts +14 -60
  166. package/src/utils/mutex.ts +4 -189
  167. package/dist/bundle.cjs +0 -14136
  168. package/dist/bundle.cjs.map +0 -1
  169. package/dist/bundle.mjs +0 -14056
  170. package/dist/bundle.mjs.map +0 -1
  171. package/dist/bundle.node.cjs +0 -11613
  172. package/dist/bundle.node.cjs.map +0 -1
  173. package/dist/bundle.node.mjs +0 -11533
  174. package/dist/bundle.node.mjs.map +0 -1
  175. package/dist/index.d.cts +0 -4052
  176. package/legacy/sync_protocol.d.ts +0 -103
  177. package/lib/client/AbstractPowerSyncDatabase.js +0 -987
  178. package/lib/client/AbstractPowerSyncDatabase.js.map +0 -1
  179. package/lib/client/AbstractPowerSyncOpenFactory.d.ts +0 -22
  180. package/lib/client/AbstractPowerSyncOpenFactory.js +0 -25
  181. package/lib/client/AbstractPowerSyncOpenFactory.js.map +0 -1
  182. package/lib/client/ConnectionManager.d.ts +0 -112
  183. package/lib/client/ConnectionManager.js +0 -294
  184. package/lib/client/ConnectionManager.js.map +0 -1
  185. package/lib/client/CustomQuery.d.ts +0 -22
  186. package/lib/client/CustomQuery.js +0 -43
  187. package/lib/client/CustomQuery.js.map +0 -1
  188. package/lib/client/constants.d.ts +0 -1
  189. package/lib/client/constants.js +0 -2
  190. package/lib/client/constants.js.map +0 -1
  191. package/lib/client/sync/bucket/BucketStorageAdapter.d.ts +0 -40
  192. package/lib/client/sync/bucket/BucketStorageAdapter.js +0 -19
  193. package/lib/client/sync/bucket/BucketStorageAdapter.js.map +0 -1
  194. package/lib/client/sync/bucket/SqliteBucketStorage.d.ts +0 -34
  195. package/lib/client/sync/bucket/SqliteBucketStorage.js +0 -162
  196. package/lib/client/sync/bucket/SqliteBucketStorage.js.map +0 -1
  197. package/lib/client/sync/stream/AbstractRemote.d.ts +0 -151
  198. package/lib/client/sync/stream/AbstractRemote.js +0 -473
  199. package/lib/client/sync/stream/AbstractRemote.js.map +0 -1
  200. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +0 -218
  201. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +0 -663
  202. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +0 -1
  203. package/lib/client/sync/stream/WebsocketClientTransport.d.ts +0 -15
  204. package/lib/client/sync/stream/WebsocketClientTransport.js +0 -61
  205. package/lib/client/sync/stream/WebsocketClientTransport.js.map +0 -1
  206. package/lib/client/sync/stream/core-instruction.d.ts +0 -71
  207. package/lib/client/sync/stream/core-instruction.js +0 -27
  208. package/lib/client/sync/stream/core-instruction.js.map +0 -1
  209. package/lib/client/triggers/MemoryTriggerClaimManager.d.ts +0 -6
  210. package/lib/client/triggers/MemoryTriggerClaimManager.js +0 -21
  211. package/lib/client/triggers/MemoryTriggerClaimManager.js.map +0 -1
  212. package/lib/client/triggers/TriggerManagerImpl.d.ts +0 -39
  213. package/lib/client/triggers/TriggerManagerImpl.js +0 -405
  214. package/lib/client/triggers/TriggerManagerImpl.js.map +0 -1
  215. package/lib/client/watched/processors/AbstractQueryProcessor.d.ts +0 -68
  216. package/lib/client/watched/processors/AbstractQueryProcessor.js +0 -151
  217. package/lib/client/watched/processors/AbstractQueryProcessor.js.map +0 -1
  218. package/lib/db/ConnectionClosedError.d.ts +0 -10
  219. package/lib/db/ConnectionClosedError.js +0 -21
  220. package/lib/db/ConnectionClosedError.js.map +0 -1
  221. package/lib/db/schema/TableV2.d.ts +0 -9
  222. package/lib/db/schema/TableV2.js +0 -9
  223. package/lib/db/schema/TableV2.js.map +0 -1
  224. package/lib/utils/AbortOperation.d.ts +0 -9
  225. package/lib/utils/AbortOperation.js +0 -19
  226. package/lib/utils/AbortOperation.js.map +0 -1
  227. package/lib/utils/ControlledExecutor.d.ts +0 -25
  228. package/lib/utils/ControlledExecutor.js +0 -51
  229. package/lib/utils/ControlledExecutor.js.map +0 -1
  230. package/lib/utils/async.d.ts +0 -14
  231. package/lib/utils/async.js +0 -46
  232. package/lib/utils/async.js.map +0 -1
  233. package/lib/utils/parseQuery.d.ts +0 -6
  234. package/lib/utils/parseQuery.js +0 -17
  235. package/lib/utils/parseQuery.js.map +0 -1
  236. package/lib/utils/queue.d.ts +0 -16
  237. package/lib/utils/queue.js +0 -42
  238. package/lib/utils/queue.js.map +0 -1
  239. package/lib/utils/stream_transform.d.ts +0 -39
  240. package/lib/utils/stream_transform.js +0 -206
  241. package/lib/utils/stream_transform.js.map +0 -1
  242. package/src/client/AbstractPowerSyncDatabase.ts +0 -1362
  243. package/src/client/AbstractPowerSyncOpenFactory.ts +0 -39
  244. package/src/client/ConnectionManager.ts +0 -402
  245. package/src/client/CustomQuery.ts +0 -56
  246. package/src/client/constants.ts +0 -1
  247. package/src/client/sync/bucket/BucketStorageAdapter.ts +0 -49
  248. package/src/client/sync/bucket/SqliteBucketStorage.ts +0 -199
  249. package/src/client/sync/stream/AbstractRemote.ts +0 -602
  250. package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +0 -905
  251. package/src/client/sync/stream/WebsocketClientTransport.ts +0 -80
  252. package/src/client/sync/stream/core-instruction.ts +0 -98
  253. package/src/client/triggers/MemoryTriggerClaimManager.ts +0 -25
  254. package/src/client/triggers/TriggerManagerImpl.ts +0 -496
  255. package/src/client/watched/processors/AbstractQueryProcessor.ts +0 -226
  256. package/src/db/ConnectionClosedError.ts +0 -23
  257. package/src/db/schema/TableV2.ts +0 -9
  258. package/src/utils/AbortOperation.ts +0 -17
  259. package/src/utils/ControlledExecutor.ts +0 -72
  260. package/src/utils/async.ts +0 -50
  261. package/src/utils/parseQuery.ts +0 -25
  262. package/src/utils/queue.ts +0 -48
  263. package/src/utils/stream_transform.ts +0 -252
@@ -1,1362 +0,0 @@
1
- import { EventIterator } from 'event-iterator';
2
- import Logger, { ILogger } from 'js-logger';
3
- import {
4
- BatchedUpdateNotification,
5
- DBAdapter,
6
- QueryResult,
7
- Transaction,
8
- UpdateNotification,
9
- isBatchedUpdateNotification
10
- } from '../db/DBAdapter.js';
11
- import { SyncStatus } from '../db/crud/SyncStatus.js';
12
- import { UploadQueueStats } from '../db/crud/UploadQueueStatus.js';
13
- import { Schema } from '../db/schema/Schema.js';
14
- import { BaseObserver } from '../utils/BaseObserver.js';
15
- import { ControlledExecutor } from '../utils/ControlledExecutor.js';
16
- import { throttleTrailing } from '../utils/async.js';
17
- import {
18
- ConnectionManager,
19
- CreateSyncImplementationOptions,
20
- InternalSubscriptionAdapter
21
- } from './ConnectionManager.js';
22
- import { CustomQuery } from './CustomQuery.js';
23
- import { ArrayQueryDefinition, Query } from './Query.js';
24
- import { SQLOpenFactory, SQLOpenOptions, isDBAdapter, isSQLOpenFactory, isSQLOpenOptions } from './SQLOpenFactory.js';
25
- import { PowerSyncBackendConnector } from './connection/PowerSyncBackendConnector.js';
26
- import { BucketStorageAdapter, PSInternalTable } from './sync/bucket/BucketStorageAdapter.js';
27
- import { CrudBatch } from './sync/bucket/CrudBatch.js';
28
- import { CrudEntry, CrudEntryJSON } from './sync/bucket/CrudEntry.js';
29
- import { CrudTransaction } from './sync/bucket/CrudTransaction.js';
30
- import {
31
- DEFAULT_CRUD_UPLOAD_THROTTLE_MS,
32
- DEFAULT_RETRY_DELAY_MS,
33
- InternalConnectionOptions,
34
- StreamingSyncImplementation,
35
- StreamingSyncImplementationListener,
36
- type AdditionalConnectionOptions,
37
- type PowerSyncConnectionOptions,
38
- type RequiredAdditionalConnectionOptions
39
- } from './sync/stream/AbstractStreamingSyncImplementation.js';
40
- import { CoreSyncStatus, coreStatusToJs } from './sync/stream/core-instruction.js';
41
- import { SyncStream } from './sync/sync-streams.js';
42
- import { MEMORY_TRIGGER_CLAIM_MANAGER } from './triggers/MemoryTriggerClaimManager.js';
43
- import { TriggerManager, TriggerManagerConfig } from './triggers/TriggerManager.js';
44
- import { TriggerManagerImpl } from './triggers/TriggerManagerImpl.js';
45
- import { DEFAULT_WATCH_THROTTLE_MS, WatchCompatibleQuery } from './watched/WatchedQuery.js';
46
- import { OnChangeQueryProcessor } from './watched/processors/OnChangeQueryProcessor.js';
47
- import { WatchedQueryComparator } from './watched/processors/comparators.js';
48
- import { Mutex } from '../utils/mutex.js';
49
-
50
- export interface DisconnectAndClearOptions {
51
- /** When set to false, data in local-only tables is preserved. */
52
- clearLocal?: boolean;
53
- }
54
-
55
- export interface BasePowerSyncDatabaseOptions extends AdditionalConnectionOptions {
56
- /** Schema used for the local database. */
57
- schema: Schema;
58
- /**
59
- * @deprecated Use {@link retryDelayMs} instead as this will be removed in future releases.
60
- */
61
- retryDelay?: number;
62
- logger?: ILogger;
63
- }
64
-
65
- export interface PowerSyncDatabaseOptions extends BasePowerSyncDatabaseOptions {
66
- /**
67
- * Source for a SQLite database connection.
68
- * This can be either:
69
- * - A {@link DBAdapter} if providing an instantiated SQLite connection
70
- * - A {@link SQLOpenFactory} which will be used to open a SQLite connection
71
- * - {@link SQLOpenOptions} for opening a SQLite connection with a default {@link SQLOpenFactory}
72
- */
73
- database: DBAdapter | SQLOpenFactory | SQLOpenOptions;
74
- }
75
-
76
- export interface PowerSyncDatabaseOptionsWithDBAdapter extends BasePowerSyncDatabaseOptions {
77
- database: DBAdapter;
78
- }
79
- export interface PowerSyncDatabaseOptionsWithOpenFactory extends BasePowerSyncDatabaseOptions {
80
- database: SQLOpenFactory;
81
- }
82
- export interface PowerSyncDatabaseOptionsWithSettings extends BasePowerSyncDatabaseOptions {
83
- database: SQLOpenOptions;
84
- }
85
-
86
- export interface SQLOnChangeOptions {
87
- signal?: AbortSignal;
88
- tables?: string[];
89
- /** The minimum interval between queries. */
90
- throttleMs?: number;
91
- /**
92
- * @deprecated All tables specified in {@link tables} will be watched, including PowerSync tables with prefixes.
93
- *
94
- * Allows for watching any SQL table
95
- * by not removing PowerSync table name prefixes
96
- */
97
- rawTableNames?: boolean;
98
- /**
99
- * Emits an empty result set immediately
100
- */
101
- triggerImmediate?: boolean;
102
- }
103
-
104
- export interface SQLWatchOptions extends SQLOnChangeOptions {
105
- /**
106
- * Optional comparator which will be used to compare the results of the query.
107
- * The watched query will only yield results if the comparator returns false.
108
- */
109
- comparator?: WatchedQueryComparator<QueryResult>;
110
- }
111
-
112
- export interface WatchOnChangeEvent {
113
- changedTables: string[];
114
- }
115
-
116
- export interface WatchHandler {
117
- onResult: (results: QueryResult) => void;
118
- onError?: (error: Error) => void;
119
- }
120
-
121
- export interface WatchOnChangeHandler {
122
- onChange: (event: WatchOnChangeEvent) => Promise<void> | void;
123
- onError?: (error: Error) => void;
124
- }
125
-
126
- export interface PowerSyncDBListener extends StreamingSyncImplementationListener {
127
- initialized: () => void;
128
- schemaChanged: (schema: Schema) => void;
129
- closing: () => Promise<void> | void;
130
- closed: () => Promise<void> | void;
131
- }
132
-
133
- export interface PowerSyncCloseOptions {
134
- /**
135
- * Disconnect the sync stream client if connected.
136
- * This is usually true, but can be false for Web when using
137
- * multiple tabs and a shared sync provider.
138
- */
139
- disconnect?: boolean;
140
- }
141
-
142
- const POWERSYNC_TABLE_MATCH = /(^ps_data__|^ps_data_local__)/;
143
-
144
- const DEFAULT_DISCONNECT_CLEAR_OPTIONS: DisconnectAndClearOptions = {
145
- clearLocal: true
146
- };
147
-
148
- export const DEFAULT_POWERSYNC_CLOSE_OPTIONS: PowerSyncCloseOptions = {
149
- disconnect: true
150
- };
151
-
152
- export const DEFAULT_POWERSYNC_DB_OPTIONS = {
153
- retryDelayMs: 5000,
154
- crudUploadThrottleMs: DEFAULT_CRUD_UPLOAD_THROTTLE_MS
155
- };
156
-
157
- export const DEFAULT_CRUD_BATCH_LIMIT = 100;
158
-
159
- /**
160
- * Requesting nested or recursive locks can block the application in some circumstances.
161
- * This default lock timeout will act as a failsafe to throw an error if a lock cannot
162
- * be obtained.
163
- */
164
- export const DEFAULT_LOCK_TIMEOUT_MS = 120_000; // 2 mins
165
-
166
- /**
167
- * Tests if the input is a {@link PowerSyncDatabaseOptionsWithSettings}
168
- * @internal
169
- */
170
- export const isPowerSyncDatabaseOptionsWithSettings = (test: any): test is PowerSyncDatabaseOptionsWithSettings => {
171
- return typeof test == 'object' && isSQLOpenOptions(test.database);
172
- };
173
-
174
- export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDBListener> {
175
- /**
176
- * Returns true if the connection is closed.
177
- */
178
- closed: boolean;
179
- ready: boolean;
180
-
181
- /**
182
- * Current connection status.
183
- */
184
- currentStatus: SyncStatus;
185
-
186
- sdkVersion: string;
187
-
188
- protected bucketStorageAdapter: BucketStorageAdapter;
189
- protected _isReadyPromise: Promise<void>;
190
- protected connectionManager: ConnectionManager;
191
- private subscriptions: InternalSubscriptionAdapter;
192
-
193
- get syncStreamImplementation() {
194
- return this.connectionManager.syncStreamImplementation;
195
- }
196
-
197
- /**
198
- * The connector used to connect to the PowerSync service.
199
- *
200
- * @returns The connector used to connect to the PowerSync service or null if `connect()` has not been called.
201
- */
202
- get connector() {
203
- return this.connectionManager.connector;
204
- }
205
-
206
- /**
207
- * The resolved connection options used to connect to the PowerSync service.
208
- *
209
- * @returns The resolved connection options used to connect to the PowerSync service or null if `connect()` has not been called.
210
- */
211
- get connectionOptions() {
212
- return this.connectionManager.connectionOptions;
213
- }
214
-
215
- protected _schema: Schema;
216
-
217
- private _database: DBAdapter;
218
-
219
- protected runExclusiveMutex: Mutex;
220
-
221
- /**
222
- * @experimental
223
- * Allows creating SQLite triggers which can be used to track various operations on SQLite tables.
224
- */
225
- readonly triggers: TriggerManager;
226
- protected triggersImpl: TriggerManagerImpl;
227
-
228
- logger: ILogger;
229
-
230
- constructor(options: PowerSyncDatabaseOptionsWithDBAdapter);
231
- constructor(options: PowerSyncDatabaseOptionsWithOpenFactory);
232
- constructor(options: PowerSyncDatabaseOptionsWithSettings);
233
- constructor(options: PowerSyncDatabaseOptions); // Note this is important for extending this class and maintaining API compatibility
234
- constructor(protected options: PowerSyncDatabaseOptions) {
235
- super();
236
-
237
- const { database, schema } = options;
238
-
239
- if (typeof schema?.toJSON != 'function') {
240
- throw new Error('The `schema` option should be provided and should be an instance of `Schema`.');
241
- }
242
-
243
- if (isDBAdapter(database)) {
244
- this._database = database;
245
- } else if (isSQLOpenFactory(database)) {
246
- this._database = database.openDB();
247
- } else if (isPowerSyncDatabaseOptionsWithSettings(options)) {
248
- this._database = this.openDBAdapter(options);
249
- } else {
250
- throw new Error('The provided `database` option is invalid.');
251
- }
252
-
253
- this.logger = options.logger ?? Logger.get(`PowerSyncDatabase[${this._database.name}]`);
254
-
255
- this.bucketStorageAdapter = this.generateBucketStorageAdapter();
256
- this.closed = false;
257
- this.currentStatus = new SyncStatus({});
258
- this.options = { ...DEFAULT_POWERSYNC_DB_OPTIONS, ...options };
259
- this._schema = schema;
260
- this.ready = false;
261
- this.sdkVersion = '';
262
- this.runExclusiveMutex = new Mutex();
263
-
264
- // Start async init
265
- this.subscriptions = {
266
- firstStatusMatching: (predicate, abort) => this.waitForStatus(predicate, abort),
267
- resolveOfflineSyncStatus: () => this.resolveOfflineSyncStatus(),
268
- rustSubscriptionsCommand: async (payload) => {
269
- await this.writeTransaction((tx) => {
270
- return tx.execute('select powersync_control(?,?)', ['subscriptions', JSON.stringify(payload)]);
271
- });
272
- }
273
- };
274
- this.connectionManager = new ConnectionManager({
275
- createSyncImplementation: async (connector, options) => {
276
- await this.waitForReady();
277
- return this.runExclusive(async () => {
278
- const sync = this.generateSyncStreamImplementation(connector, this.resolvedConnectionOptions(options));
279
- const onDispose = sync.registerListener({
280
- statusChanged: (status) => {
281
- this.currentStatus = new SyncStatus({
282
- ...status.toJSON(),
283
- hasSynced: this.currentStatus?.hasSynced || !!status.lastSyncedAt
284
- });
285
- this.iterateListeners((cb) => cb.statusChanged?.(this.currentStatus));
286
- }
287
- });
288
- await sync.waitForReady();
289
-
290
- return {
291
- sync,
292
- onDispose
293
- };
294
- });
295
- },
296
- logger: this.logger
297
- });
298
-
299
- this._isReadyPromise = this.initialize();
300
-
301
- this.triggers = this.triggersImpl = new TriggerManagerImpl({
302
- db: this,
303
- schema: this.schema,
304
- ...this.generateTriggerManagerConfig()
305
- });
306
- }
307
-
308
- /**
309
- * Schema used for the local database.
310
- */
311
- get schema() {
312
- return this._schema;
313
- }
314
-
315
- /**
316
- * The underlying database.
317
- *
318
- * For the most part, behavior is the same whether querying on the underlying database, or on {@link AbstractPowerSyncDatabase}.
319
- */
320
- get database() {
321
- return this._database;
322
- }
323
-
324
- /**
325
- * Whether a connection to the PowerSync service is currently open.
326
- */
327
- get connected() {
328
- return this.currentStatus?.connected || false;
329
- }
330
-
331
- get connecting() {
332
- return this.currentStatus?.connecting || false;
333
- }
334
-
335
- /**
336
- * Opens the DBAdapter given open options using a default open factory
337
- */
338
- protected abstract openDBAdapter(options: PowerSyncDatabaseOptionsWithSettings): DBAdapter;
339
-
340
- /**
341
- * Generates a base configuration for {@link TriggerManagerImpl}.
342
- * Implementations should override this if necessary.
343
- */
344
- protected generateTriggerManagerConfig(): TriggerManagerConfig {
345
- return {
346
- claimManager: MEMORY_TRIGGER_CLAIM_MANAGER
347
- };
348
- }
349
-
350
- protected abstract generateSyncStreamImplementation(
351
- connector: PowerSyncBackendConnector,
352
- options: CreateSyncImplementationOptions & RequiredAdditionalConnectionOptions
353
- ): StreamingSyncImplementation;
354
-
355
- protected abstract generateBucketStorageAdapter(): BucketStorageAdapter;
356
-
357
- /**
358
- * @returns A promise which will resolve once initialization is completed.
359
- */
360
- async waitForReady(): Promise<void> {
361
- if (this.ready) {
362
- return;
363
- }
364
-
365
- await this._isReadyPromise;
366
- }
367
-
368
- /**
369
- * Wait for the first sync operation to complete.
370
- *
371
- * @param request Either an abort signal (after which the promise will complete regardless of
372
- * whether a full sync was completed) or an object providing an abort signal and a priority target.
373
- * When a priority target is set, the promise may complete when all buckets with the given (or higher)
374
- * priorities have been synchronized. This can be earlier than a complete sync.
375
- * @returns A promise which will resolve once the first full sync has completed.
376
- */
377
- async waitForFirstSync(request?: AbortSignal | { signal?: AbortSignal; priority?: number }): Promise<void> {
378
- const signal = request instanceof AbortSignal ? request : request?.signal;
379
- const priority = request && 'priority' in request ? request.priority : undefined;
380
-
381
- const statusMatches =
382
- priority === undefined
383
- ? (status: SyncStatus) => status.hasSynced
384
- : (status: SyncStatus) => status.statusForPriority(priority).hasSynced;
385
-
386
- return this.waitForStatus(statusMatches, signal);
387
- }
388
-
389
- /**
390
- * Waits for the first sync status for which the `status` callback returns a truthy value.
391
- */
392
- async waitForStatus(predicate: (status: SyncStatus) => any, signal?: AbortSignal): Promise<void> {
393
- if (predicate(this.currentStatus)) {
394
- return;
395
- }
396
-
397
- return new Promise((resolve) => {
398
- const dispose = this.registerListener({
399
- statusChanged: (status) => {
400
- if (predicate(status)) {
401
- abort();
402
- }
403
- }
404
- });
405
-
406
- function abort() {
407
- dispose();
408
- resolve();
409
- }
410
-
411
- if (signal?.aborted) {
412
- abort();
413
- } else {
414
- signal?.addEventListener('abort', abort);
415
- }
416
- });
417
- }
418
-
419
- /**
420
- * Allows for extended implementations to execute custom initialization
421
- * logic as part of the total init process
422
- */
423
- abstract _initialize(): Promise<void>;
424
-
425
- /**
426
- * Entry point for executing initialization logic.
427
- * This is to be automatically executed in the constructor.
428
- */
429
- protected async initialize() {
430
- await this._initialize();
431
- await this.bucketStorageAdapter.init();
432
- await this.loadVersion();
433
- await this.updateSchema(this.options.schema);
434
- await this.resolveOfflineSyncStatus();
435
- await this.database.execute('PRAGMA RECURSIVE_TRIGGERS=TRUE');
436
- await this.triggersImpl.cleanupResources();
437
- this.ready = true;
438
- this.iterateListeners((cb) => cb.initialized?.());
439
- }
440
-
441
- protected async loadVersion() {
442
- try {
443
- const { version } = await this.database.get<{ version: string }>('SELECT powersync_rs_version() as version');
444
- this.sdkVersion = version;
445
- } catch (e) {
446
- throw new Error(`The powersync extension is not loaded correctly. Details: ${e.message}`);
447
- }
448
- let versionInts: number[];
449
- try {
450
- versionInts = this.sdkVersion!.split(/[.\/]/)
451
- .slice(0, 3)
452
- .map((n) => parseInt(n));
453
- } catch (e) {
454
- throw new Error(
455
- `Unsupported powersync extension version. Need >=0.4.10 <1.0.0, got: ${this.sdkVersion}. Details: ${e.message}`
456
- );
457
- }
458
-
459
- // Validate >=0.4.10 <1.0.0
460
- if (versionInts[0] != 0 || versionInts[1] < 4 || (versionInts[1] == 4 && versionInts[2] < 10)) {
461
- throw new Error(`Unsupported powersync extension version. Need >=0.4.10 <1.0.0, got: ${this.sdkVersion}`);
462
- }
463
- }
464
-
465
- protected async resolveOfflineSyncStatus() {
466
- const result = await this.database.get<{ r: string }>('SELECT powersync_offline_sync_status() as r');
467
- const parsed = JSON.parse(result.r) as CoreSyncStatus;
468
-
469
- const updatedStatus = new SyncStatus({
470
- ...this.currentStatus.toJSON(),
471
- ...coreStatusToJs(parsed)
472
- });
473
-
474
- if (!updatedStatus.isEqual(this.currentStatus)) {
475
- this.currentStatus = updatedStatus;
476
- this.iterateListeners((l) => l.statusChanged?.(this.currentStatus));
477
- }
478
- }
479
-
480
- /**
481
- * Replace the schema with a new version. This is for advanced use cases - typically the schema should just be specified once in the constructor.
482
- *
483
- * Cannot be used while connected - this should only be called before {@link AbstractPowerSyncDatabase.connect}.
484
- */
485
- async updateSchema(schema: Schema) {
486
- if (this.syncStreamImplementation) {
487
- throw new Error('Cannot update schema while connected');
488
- }
489
-
490
- /**
491
- * TODO
492
- * Validations only show a warning for now.
493
- * The next major release should throw an exception.
494
- */
495
- try {
496
- schema.validate();
497
- } catch (ex) {
498
- this.logger.warn('Schema validation failed. Unexpected behaviour could occur', ex);
499
- }
500
- this._schema = schema;
501
-
502
- await this.database.execute('SELECT powersync_replace_schema(?)', [JSON.stringify(this.schema.toJSON())]);
503
- await this.database.refreshSchema();
504
- this.iterateListeners(async (cb) => cb.schemaChanged?.(schema));
505
- }
506
-
507
- /**
508
- * Wait for initialization to complete.
509
- * While initializing is automatic, this helps to catch and report initialization errors.
510
- */
511
- async init() {
512
- return this.waitForReady();
513
- }
514
-
515
- // Use the options passed in during connect, or fallback to the options set during database creation or fallback to the default options
516
- protected resolvedConnectionOptions(
517
- options: CreateSyncImplementationOptions
518
- ): CreateSyncImplementationOptions & RequiredAdditionalConnectionOptions {
519
- return {
520
- ...options,
521
- retryDelayMs:
522
- options?.retryDelayMs ?? this.options.retryDelayMs ?? this.options.retryDelay ?? DEFAULT_RETRY_DELAY_MS,
523
- crudUploadThrottleMs:
524
- options?.crudUploadThrottleMs ?? this.options.crudUploadThrottleMs ?? DEFAULT_CRUD_UPLOAD_THROTTLE_MS
525
- };
526
- }
527
-
528
- /**
529
- * @deprecated Use {@link AbstractPowerSyncDatabase#close} instead.
530
- * Clears all listeners registered by {@link AbstractPowerSyncDatabase#registerListener}.
531
- */
532
- dispose(): void {
533
- return super.dispose();
534
- }
535
-
536
- /**
537
- * Locking mechanism for exclusively running critical portions of connect/disconnect operations.
538
- * Locking here is mostly only important on web for multiple tab scenarios.
539
- */
540
- protected runExclusive<T>(callback: () => Promise<T>): Promise<T> {
541
- return this.runExclusiveMutex.runExclusive(callback);
542
- }
543
-
544
- /**
545
- * Connects to stream of events from the PowerSync instance.
546
- */
547
- async connect(connector: PowerSyncBackendConnector, options?: PowerSyncConnectionOptions) {
548
- const resolvedOptions: InternalConnectionOptions = options ?? {};
549
- resolvedOptions.serializedSchema = this.schema.toJSON();
550
-
551
- return this.connectionManager.connect(connector, resolvedOptions);
552
- }
553
-
554
- /**
555
- * Close the sync connection.
556
- *
557
- * Use {@link connect} to connect again.
558
- */
559
- async disconnect() {
560
- return this.connectionManager.disconnect();
561
- }
562
-
563
- /**
564
- * Disconnect and clear the database.
565
- * Use this when logging out.
566
- * The database can still be queried after this is called, but the tables
567
- * would be empty.
568
- *
569
- * To preserve data in local-only tables, set clearLocal to false.
570
- */
571
- async disconnectAndClear(options = DEFAULT_DISCONNECT_CLEAR_OPTIONS) {
572
- await this.disconnect();
573
- await this.waitForReady();
574
-
575
- const { clearLocal } = options;
576
-
577
- await this.database.writeTransaction(async (tx) => {
578
- await tx.execute('SELECT powersync_clear(?)', [clearLocal ? 1 : 0]);
579
- });
580
-
581
- // The data has been deleted - reset the sync status
582
- this.currentStatus = new SyncStatus({});
583
- this.iterateListeners((l) => l.statusChanged?.(this.currentStatus));
584
- }
585
-
586
- /**
587
- * Create a sync stream to query its status or to subscribe to it.
588
- *
589
- * @param name The name of the stream to subscribe to.
590
- * @param params Optional parameters for the stream subscription.
591
- * @returns A {@link SyncStream} instance that can be subscribed to.
592
- * @experimental Sync streams are currently in alpha.
593
- */
594
- syncStream(name: string, params?: Record<string, any>): SyncStream {
595
- return this.connectionManager.stream(this.subscriptions, name, params ?? null);
596
- }
597
-
598
- /**
599
- * Close the database, releasing resources.
600
- *
601
- * Also disconnects any active connection.
602
- *
603
- * Once close is called, this connection cannot be used again - a new one
604
- * must be constructed.
605
- */
606
- async close(options: PowerSyncCloseOptions = DEFAULT_POWERSYNC_CLOSE_OPTIONS) {
607
- await this.waitForReady();
608
-
609
- if (this.closed) {
610
- return;
611
- }
612
-
613
- this.triggersImpl.dispose();
614
-
615
- await this.iterateAsyncListeners(async (cb) => cb.closing?.());
616
-
617
- const { disconnect } = options;
618
- if (disconnect) {
619
- await this.disconnect();
620
- }
621
-
622
- await this.connectionManager.close();
623
- await this.database.close();
624
- this.closed = true;
625
- await this.iterateAsyncListeners(async (cb) => cb.closed?.());
626
- }
627
-
628
- /**
629
- * Get upload queue size estimate and count.
630
- */
631
- async getUploadQueueStats(includeSize?: boolean): Promise<UploadQueueStats> {
632
- return this.readTransaction(async (tx) => {
633
- if (includeSize) {
634
- const result = await tx.execute(
635
- `SELECT SUM(cast(data as blob) + 20) as size, count(*) as count FROM ${PSInternalTable.CRUD}`
636
- );
637
-
638
- const row = result.rows!.item(0);
639
- return new UploadQueueStats(row?.count ?? 0, row?.size ?? 0);
640
- } else {
641
- const result = await tx.execute(`SELECT count(*) as count FROM ${PSInternalTable.CRUD}`);
642
- const row = result.rows!.item(0);
643
- return new UploadQueueStats(row?.count ?? 0);
644
- }
645
- });
646
- }
647
-
648
- /**
649
- * Get a batch of CRUD data to upload.
650
- *
651
- * Returns null if there is no data to upload.
652
- *
653
- * Use this from the {@link PowerSyncBackendConnector.uploadData} callback.
654
- *
655
- * Once the data have been successfully uploaded, call {@link CrudBatch.complete} before
656
- * requesting the next batch.
657
- *
658
- * Use {@link limit} to specify the maximum number of updates to return in a single
659
- * batch.
660
- *
661
- * This method does include transaction ids in the result, but does not group
662
- * data by transaction. One batch may contain data from multiple transactions,
663
- * and a single transaction may be split over multiple batches.
664
- *
665
- * @param limit Maximum number of CRUD entries to include in the batch
666
- * @returns A batch of CRUD operations to upload, or null if there are none
667
- */
668
- async getCrudBatch(limit: number = DEFAULT_CRUD_BATCH_LIMIT): Promise<CrudBatch | null> {
669
- const result = await this.getAll<CrudEntryJSON>(
670
- `SELECT id, tx_id, data FROM ${PSInternalTable.CRUD} ORDER BY id ASC LIMIT ?`,
671
- [limit + 1]
672
- );
673
-
674
- const all: CrudEntry[] = result.map((row) => CrudEntry.fromRow(row)) ?? [];
675
-
676
- let haveMore = false;
677
- if (all.length > limit) {
678
- all.pop();
679
- haveMore = true;
680
- }
681
- if (all.length == 0) {
682
- return null;
683
- }
684
-
685
- const last = all[all.length - 1];
686
- return new CrudBatch(all, haveMore, async (writeCheckpoint?: string) =>
687
- this.handleCrudCheckpoint(last.clientId, writeCheckpoint)
688
- );
689
- }
690
-
691
- /**
692
- * Get the next recorded transaction to upload.
693
- *
694
- * Returns null if there is no data to upload.
695
- *
696
- * Use this from the {@link PowerSyncBackendConnector.uploadData} callback.
697
- *
698
- * Once the data have been successfully uploaded, call {@link CrudTransaction.complete} before
699
- * requesting the next transaction.
700
- *
701
- * Unlike {@link getCrudBatch}, this only returns data from a single transaction at a time.
702
- * All data for the transaction is loaded into memory.
703
- *
704
- * @returns A transaction of CRUD operations to upload, or null if there are none
705
- */
706
- async getNextCrudTransaction(): Promise<CrudTransaction | null> {
707
- const iterator = this.getCrudTransactions()[Symbol.asyncIterator]();
708
- return (await iterator.next()).value;
709
- }
710
-
711
- /**
712
- * Returns an async iterator of completed transactions with local writes against the database.
713
- *
714
- * This is typically used from the {@link PowerSyncBackendConnector.uploadData} callback. Each entry emitted by the
715
- * returned iterator is a full transaction containing all local writes made while that transaction was active.
716
- *
717
- * Unlike {@link getNextCrudTransaction}, which always returns the oldest transaction that hasn't been
718
- * {@link CrudTransaction.complete}d yet, this iterator can be used to receive multiple transactions. Calling
719
- * {@link CrudTransaction.complete} will mark that and all prior transactions emitted by the iterator as completed.
720
- *
721
- * This can be used to upload multiple transactions in a single batch, e.g with:
722
- *
723
- * ```JavaScript
724
- * let lastTransaction = null;
725
- * let batch = [];
726
- *
727
- * for await (const transaction of database.getCrudTransactions()) {
728
- * batch.push(...transaction.crud);
729
- * lastTransaction = transaction;
730
- *
731
- * if (batch.length > 10) {
732
- * break;
733
- * }
734
- * }
735
- * ```
736
- *
737
- * If there is no local data to upload, the async iterator complete without emitting any items.
738
- *
739
- * Note that iterating over async iterables requires a [polyfill](https://github.com/powersync-ja/powersync-js/tree/main/packages/react-native#babel-plugins-watched-queries)
740
- * for React Native.
741
- */
742
- getCrudTransactions(): AsyncIterable<CrudTransaction, null> {
743
- return {
744
- [Symbol.asyncIterator]: () => {
745
- let lastCrudItemId = -1;
746
- const sql = `
747
- WITH RECURSIVE crud_entries AS (
748
- SELECT id, tx_id, data FROM ps_crud WHERE id = (SELECT min(id) FROM ps_crud WHERE id > ?)
749
- UNION ALL
750
- SELECT ps_crud.id, ps_crud.tx_id, ps_crud.data FROM ps_crud
751
- INNER JOIN crud_entries ON crud_entries.id + 1 = rowid
752
- WHERE crud_entries.tx_id = ps_crud.tx_id
753
- )
754
- SELECT * FROM crud_entries;
755
- `;
756
-
757
- return {
758
- next: async () => {
759
- const nextTransaction = await this.database.getAll<CrudEntryJSON>(sql, [lastCrudItemId]);
760
- if (nextTransaction.length == 0) {
761
- return { done: true, value: null };
762
- }
763
-
764
- const items = nextTransaction.map((row) => CrudEntry.fromRow(row));
765
- const last = items[items.length - 1];
766
- const txId = last.transactionId;
767
- lastCrudItemId = last.clientId;
768
-
769
- return {
770
- done: false,
771
- value: new CrudTransaction(
772
- items,
773
- async (writeCheckpoint?: string) => this.handleCrudCheckpoint(last.clientId, writeCheckpoint),
774
- txId
775
- )
776
- };
777
- }
778
- };
779
- }
780
- };
781
- }
782
-
783
- /**
784
- * Get an unique client id for this database.
785
- *
786
- * The id is not reset when the database is cleared, only when the database is deleted.
787
- *
788
- * @returns A unique identifier for the database instance
789
- */
790
- async getClientId(): Promise<string> {
791
- return this.bucketStorageAdapter.getClientId();
792
- }
793
-
794
- private async handleCrudCheckpoint(lastClientId: number, writeCheckpoint?: string) {
795
- return this.writeTransaction(async (tx) => {
796
- await tx.execute(`DELETE FROM ${PSInternalTable.CRUD} WHERE id <= ?`, [lastClientId]);
797
- if (writeCheckpoint) {
798
- const check = await tx.execute(`SELECT 1 FROM ${PSInternalTable.CRUD} LIMIT 1`);
799
- if (!check.rows?.length) {
800
- await tx.execute(`UPDATE ${PSInternalTable.BUCKETS} SET target_op = CAST(? as INTEGER) WHERE name='$local'`, [
801
- writeCheckpoint
802
- ]);
803
- }
804
- } else {
805
- await tx.execute(`UPDATE ${PSInternalTable.BUCKETS} SET target_op = CAST(? as INTEGER) WHERE name='$local'`, [
806
- this.bucketStorageAdapter.getMaxOpId()
807
- ]);
808
- }
809
- });
810
- }
811
-
812
- /**
813
- * Execute a SQL write (INSERT/UPDATE/DELETE) query
814
- * and optionally return results.
815
- *
816
- * When using the default client-side [JSON-based view system](https://docs.powersync.com/architecture/client-architecture#client-side-schema-and-sqlite-database-structure),
817
- * the returned result's `rowsAffected` may be `0` for successful `UPDATE` and `DELETE` statements.
818
- * Use a `RETURNING` clause and inspect `result.rows` when you need to confirm which rows changed.
819
- *
820
- * @param sql The SQL query to execute
821
- * @param parameters Optional array of parameters to bind to the query
822
- * @returns The query result as an object with structured key-value pairs
823
- */
824
- async execute(sql: string, parameters?: any[]) {
825
- return this.writeLock((tx) => tx.execute(sql, parameters));
826
- }
827
-
828
- /**
829
- * Execute a SQL write (INSERT/UPDATE/DELETE) query directly on the database without any PowerSync processing.
830
- * This bypasses certain PowerSync abstractions and is useful for accessing the raw database results.
831
- *
832
- * @param sql The SQL query to execute
833
- * @param parameters Optional array of parameters to bind to the query
834
- * @returns The raw query result from the underlying database as a nested array of raw values, where each row is
835
- * represented as an array of column values without field names.
836
- */
837
- async executeRaw(sql: string, parameters?: any[]) {
838
- await this.waitForReady();
839
- return this.database.executeRaw(sql, parameters);
840
- }
841
-
842
- /**
843
- * Execute a write query (INSERT/UPDATE/DELETE) multiple times with each parameter set
844
- * and optionally return results.
845
- * This is faster than executing separately with each parameter set.
846
- *
847
- * @param sql The SQL query to execute
848
- * @param parameters Optional 2D array of parameter sets, where each inner array is a set of parameters for one execution
849
- * @returns The query result
850
- */
851
- async executeBatch(sql: string, parameters?: any[][]) {
852
- await this.waitForReady();
853
- return this.database.executeBatch(sql, parameters);
854
- }
855
-
856
- /**
857
- * Execute a read-only query and return results.
858
- *
859
- * @param sql The SQL query to execute
860
- * @param parameters Optional array of parameters to bind to the query
861
- * @returns An array of results
862
- */
863
- async getAll<T>(sql: string, parameters?: any[]): Promise<T[]> {
864
- await this.waitForReady();
865
- return this.database.getAll(sql, parameters);
866
- }
867
-
868
- /**
869
- * Execute a read-only query and return the first result, or null if the ResultSet is empty.
870
- *
871
- * @param sql The SQL query to execute
872
- * @param parameters Optional array of parameters to bind to the query
873
- * @returns The first result if found, or null if no results are returned
874
- */
875
- async getOptional<T>(sql: string, parameters?: any[]): Promise<T | null> {
876
- await this.waitForReady();
877
- return this.database.getOptional(sql, parameters);
878
- }
879
-
880
- /**
881
- * Execute a read-only query and return the first result, error if the ResultSet is empty.
882
- *
883
- * @param sql The SQL query to execute
884
- * @param parameters Optional array of parameters to bind to the query
885
- * @returns The first result matching the query
886
- * @throws Error if no rows are returned
887
- */
888
- async get<T>(sql: string, parameters?: any[]): Promise<T> {
889
- await this.waitForReady();
890
- return this.database.get(sql, parameters);
891
- }
892
-
893
- /**
894
- * Takes a read lock, without starting a transaction.
895
- * In most cases, {@link readTransaction} should be used instead.
896
- */
897
- async readLock<T>(callback: (db: DBAdapter) => Promise<T>) {
898
- await this.waitForReady();
899
- return this.database.readLock(callback);
900
- }
901
-
902
- /**
903
- * Takes a global lock, without starting a transaction.
904
- * In most cases, {@link writeTransaction} should be used instead.
905
- */
906
- async writeLock<T>(callback: (db: DBAdapter) => Promise<T>) {
907
- await this.waitForReady();
908
- return this.database.writeLock(callback);
909
- }
910
-
911
- /**
912
- * Open a read-only transaction.
913
- * Read transactions can run concurrently to a write transaction.
914
- * Changes from any write transaction are not visible to read transactions started before it.
915
- *
916
- * @param callback Function to execute within the transaction
917
- * @param lockTimeout Time in milliseconds to wait for a lock before throwing an error
918
- * @returns The result of the callback
919
- * @throws Error if the lock cannot be obtained within the timeout period
920
- */
921
- async readTransaction<T>(
922
- callback: (tx: Transaction) => Promise<T>,
923
- lockTimeout: number = DEFAULT_LOCK_TIMEOUT_MS
924
- ): Promise<T> {
925
- await this.waitForReady();
926
- return this.database.readTransaction(
927
- async (tx) => {
928
- const res = await callback(tx);
929
- await tx.rollback();
930
- return res;
931
- },
932
- { timeoutMs: lockTimeout }
933
- );
934
- }
935
-
936
- /**
937
- * Open a read-write transaction.
938
- * This takes a global lock - only one write transaction can execute against the database at a time.
939
- * Statements within the transaction must be done on the provided {@link Transaction} interface.
940
- *
941
- * @param callback Function to execute within the transaction
942
- * @param lockTimeout Time in milliseconds to wait for a lock before throwing an error
943
- * @returns The result of the callback
944
- * @throws Error if the lock cannot be obtained within the timeout period
945
- */
946
- async writeTransaction<T>(
947
- callback: (tx: Transaction) => Promise<T>,
948
- lockTimeout: number = DEFAULT_LOCK_TIMEOUT_MS
949
- ): Promise<T> {
950
- await this.waitForReady();
951
- return this.database.writeTransaction(
952
- async (tx) => {
953
- const res = await callback(tx);
954
- await tx.commit();
955
- return res;
956
- },
957
- { timeoutMs: lockTimeout }
958
- );
959
- }
960
-
961
- /**
962
- * This version of `watch` uses {@link AsyncGenerator}, for documentation see {@link watchWithAsyncGenerator}.
963
- * Can be overloaded to use a callback handler instead, for documentation see {@link watchWithCallback}.
964
- *
965
- * @example
966
- * ```javascript
967
- * async *attachmentIds() {
968
- * for await (const result of this.powersync.watch(
969
- * `SELECT photo_id as id FROM todos WHERE photo_id IS NOT NULL`,
970
- * []
971
- * )) {
972
- * yield result.rows?._array.map((r) => r.id) ?? [];
973
- * }
974
- * }
975
- * ```
976
- */
977
- watch(sql: string, parameters?: any[], options?: SQLWatchOptions): AsyncIterable<QueryResult>;
978
- /**
979
- * See {@link watchWithCallback}.
980
- *
981
- * @example
982
- * ```javascript
983
- * onAttachmentIdsChange(onResult) {
984
- * this.powersync.watch(
985
- * `SELECT photo_id as id FROM todos WHERE photo_id IS NOT NULL`,
986
- * [],
987
- * {
988
- * onResult: (result) => onResult(result.rows?._array.map((r) => r.id) ?? [])
989
- * }
990
- * );
991
- * }
992
- * ```
993
- */
994
- watch(sql: string, parameters?: any[], handler?: WatchHandler, options?: SQLWatchOptions): void;
995
-
996
- watch(
997
- sql: string,
998
- parameters?: any[],
999
- handlerOrOptions?: WatchHandler | SQLWatchOptions,
1000
- maybeOptions?: SQLWatchOptions
1001
- ): void | AsyncIterable<QueryResult> {
1002
- if (handlerOrOptions && typeof handlerOrOptions === 'object' && 'onResult' in handlerOrOptions) {
1003
- const handler = handlerOrOptions as WatchHandler;
1004
- const options = maybeOptions;
1005
-
1006
- return this.watchWithCallback(sql, parameters, handler, options);
1007
- }
1008
-
1009
- const options = handlerOrOptions as SQLWatchOptions | undefined;
1010
- return this.watchWithAsyncGenerator(sql, parameters, options);
1011
- }
1012
-
1013
- /**
1014
- * Allows defining a query which can be used to build a {@link WatchedQuery}.
1015
- * The defined query will be executed with {@link AbstractPowerSyncDatabase#getAll}.
1016
- * An optional mapper function can be provided to transform the results.
1017
- *
1018
- * @example
1019
- * ```javascript
1020
- * const watchedTodos = powersync.query({
1021
- * sql: `SELECT photo_id as id FROM todos WHERE photo_id IS NOT NULL`,
1022
- * parameters: [],
1023
- * mapper: (row) => ({
1024
- * ...row,
1025
- * created_at: new Date(row.created_at as string)
1026
- * })
1027
- * })
1028
- * .watch()
1029
- * // OR use .differentialWatch() for fine-grained watches.
1030
- * ```
1031
- */
1032
- query<RowType>(query: ArrayQueryDefinition<RowType>): Query<RowType> {
1033
- const { sql, parameters = [], mapper } = query;
1034
- const compatibleQuery: WatchCompatibleQuery<RowType[]> = {
1035
- compile: () => ({
1036
- sql,
1037
- parameters
1038
- }),
1039
- execute: async ({ sql, parameters }) => {
1040
- const result = await this.getAll(sql, parameters);
1041
- return mapper ? result.map(mapper) : (result as RowType[]);
1042
- }
1043
- };
1044
- return this.customQuery(compatibleQuery);
1045
- }
1046
-
1047
- /**
1048
- * Allows building a {@link WatchedQuery} using an existing {@link WatchCompatibleQuery}.
1049
- * The watched query will use the provided {@link WatchCompatibleQuery.execute} method to query results.
1050
- *
1051
- * @example
1052
- * ```javascript
1053
- *
1054
- * // Potentially a query from an ORM like Drizzle
1055
- * const query = db.select().from(lists);
1056
- *
1057
- * const watchedTodos = powersync.customQuery(query)
1058
- * .watch()
1059
- * // OR use .differentialWatch() for fine-grained watches.
1060
- * ```
1061
- */
1062
- customQuery<RowType>(query: WatchCompatibleQuery<RowType[]>): Query<RowType> {
1063
- return new CustomQuery({
1064
- db: this,
1065
- query
1066
- });
1067
- }
1068
-
1069
- /**
1070
- * Execute a read query every time the source tables are modified.
1071
- * Use {@link SQLWatchOptions.throttleMs} to specify the minimum interval between queries.
1072
- * Source tables are automatically detected using `EXPLAIN QUERY PLAN`.
1073
- *
1074
- * Note that the `onChange` callback member of the handler is required.
1075
- *
1076
- * @param sql The SQL query to execute
1077
- * @param parameters Optional array of parameters to bind to the query
1078
- * @param handler Callbacks for handling results and errors
1079
- * @param options Options for configuring watch behavior
1080
- */
1081
- watchWithCallback(sql: string, parameters?: any[], handler?: WatchHandler, options?: SQLWatchOptions): void {
1082
- const { onResult, onError = (e: Error) => this.logger.error(e) } = handler ?? {};
1083
- if (!onResult) {
1084
- throw new Error('onResult is required');
1085
- }
1086
- const { comparator } = options ?? {};
1087
-
1088
- // This API yields a QueryResult type.
1089
- // This is not a standard Array result, which makes it incompatible with the .query API.
1090
- const watchedQuery = new OnChangeQueryProcessor({
1091
- db: this,
1092
- comparator,
1093
- placeholderData: null,
1094
- watchOptions: {
1095
- query: {
1096
- compile: () => ({
1097
- sql: sql,
1098
- parameters: parameters ?? []
1099
- }),
1100
- execute: () => this.executeReadOnly(sql, parameters)
1101
- },
1102
- reportFetching: false,
1103
- throttleMs: options?.throttleMs ?? DEFAULT_WATCH_THROTTLE_MS,
1104
- triggerOnTables: options?.tables
1105
- }
1106
- });
1107
-
1108
- const dispose = watchedQuery.registerListener({
1109
- onData: (data) => {
1110
- if (!data) {
1111
- // This should not happen. We only use null for the initial data.
1112
- return;
1113
- }
1114
- onResult(data);
1115
- },
1116
- onError: (error) => {
1117
- onError(error);
1118
- }
1119
- });
1120
-
1121
- options?.signal?.addEventListener('abort', () => {
1122
- dispose();
1123
- watchedQuery.close();
1124
- });
1125
- }
1126
-
1127
- /**
1128
- * Execute a read query every time the source tables are modified.
1129
- * Use {@link SQLWatchOptions.throttleMs} to specify the minimum interval between queries.
1130
- * Source tables are automatically detected using `EXPLAIN QUERY PLAN`.
1131
- *
1132
- * @param sql The SQL query to execute
1133
- * @param parameters Optional array of parameters to bind to the query
1134
- * @param options Options for configuring watch behavior
1135
- * @returns An AsyncIterable that yields QueryResults whenever the data changes
1136
- */
1137
- watchWithAsyncGenerator(sql: string, parameters?: any[], options?: SQLWatchOptions): AsyncIterable<QueryResult> {
1138
- return new EventIterator<QueryResult>((eventOptions) => {
1139
- const handler: WatchHandler = {
1140
- onResult: (result) => {
1141
- eventOptions.push(result);
1142
- },
1143
- onError: (error) => {
1144
- eventOptions.fail(error);
1145
- }
1146
- };
1147
-
1148
- this.watchWithCallback(sql, parameters, handler, options);
1149
-
1150
- options?.signal?.addEventListener('abort', () => {
1151
- eventOptions.stop();
1152
- });
1153
- });
1154
- }
1155
-
1156
- /**
1157
- * Resolves the list of tables that are used in a SQL query.
1158
- * If tables are specified in the options, those are used directly.
1159
- * Otherwise, analyzes the query using EXPLAIN to determine which tables are accessed.
1160
- *
1161
- * @param sql The SQL query to analyze
1162
- * @param parameters Optional parameters for the SQL query
1163
- * @param options Optional watch options that may contain explicit table list
1164
- * @returns Array of table names that the query depends on
1165
- */
1166
- async resolveTables(sql: string, parameters?: any[], options?: SQLWatchOptions): Promise<string[]> {
1167
- const resolvedTables = options?.tables ? [...options.tables] : [];
1168
- if (!options?.tables) {
1169
- const explained = await this.getAll<{ opcode: string; p3: number; p2: number }>(`EXPLAIN ${sql}`, parameters);
1170
- const rootPages = explained
1171
- .filter((row) => row.opcode == 'OpenRead' && row.p3 == 0 && typeof row.p2 == 'number')
1172
- .map((row) => row.p2);
1173
- const tables = await this.getAll<{ tbl_name: string }>(
1174
- `SELECT DISTINCT tbl_name FROM sqlite_master WHERE rootpage IN (SELECT json_each.value FROM json_each(?))`,
1175
- [JSON.stringify(rootPages)]
1176
- );
1177
- for (const table of tables) {
1178
- resolvedTables.push(table.tbl_name.replace(POWERSYNC_TABLE_MATCH, ''));
1179
- }
1180
- }
1181
-
1182
- return resolvedTables;
1183
- }
1184
-
1185
- /**
1186
- * This version of `onChange` uses {@link AsyncGenerator}, for documentation see {@link onChangeWithAsyncGenerator}.
1187
- * Can be overloaded to use a callback handler instead, for documentation see {@link onChangeWithCallback}.
1188
- *
1189
- * @example
1190
- * ```javascript
1191
- * async monitorChanges() {
1192
- * for await (const event of this.powersync.onChange({tables: ['todos']})) {
1193
- * console.log('Detected change event:', event);
1194
- * }
1195
- * }
1196
- * ```
1197
- */
1198
- onChange(options?: SQLOnChangeOptions): AsyncIterable<WatchOnChangeEvent>;
1199
- /**
1200
- * See {@link onChangeWithCallback}.
1201
- *
1202
- * @example
1203
- * ```javascript
1204
- * monitorChanges() {
1205
- * this.powersync.onChange({
1206
- * onChange: (event) => {
1207
- * console.log('Change detected:', event);
1208
- * }
1209
- * }, { tables: ['todos'] });
1210
- * }
1211
- * ```
1212
- */
1213
- onChange(handler?: WatchOnChangeHandler, options?: SQLOnChangeOptions): () => void;
1214
-
1215
- onChange(
1216
- handlerOrOptions?: WatchOnChangeHandler | SQLOnChangeOptions,
1217
- maybeOptions?: SQLOnChangeOptions
1218
- ): (() => void) | AsyncIterable<WatchOnChangeEvent> {
1219
- if (handlerOrOptions && typeof handlerOrOptions === 'object' && 'onChange' in handlerOrOptions) {
1220
- const handler = handlerOrOptions as WatchOnChangeHandler;
1221
- const options = maybeOptions;
1222
-
1223
- return this.onChangeWithCallback(handler, options);
1224
- }
1225
-
1226
- const options = handlerOrOptions as SQLWatchOptions | undefined;
1227
- return this.onChangeWithAsyncGenerator(options);
1228
- }
1229
-
1230
- /**
1231
- * Invoke the provided callback on any changes to any of the specified tables.
1232
- *
1233
- * This is preferred over {@link watchWithCallback} when multiple queries need to be performed
1234
- * together when data is changed.
1235
- *
1236
- * Note that the `onChange` callback member of the handler is required.
1237
- *
1238
- * @param handler Callbacks for handling change events and errors
1239
- * @param options Options for configuring watch behavior
1240
- * @returns A dispose function to stop watching for changes
1241
- */
1242
- onChangeWithCallback(handler?: WatchOnChangeHandler, options?: SQLOnChangeOptions): () => void {
1243
- const { onChange, onError = (e: Error) => this.logger.error(e) } = handler ?? {};
1244
- if (!onChange) {
1245
- throw new Error('onChange is required');
1246
- }
1247
-
1248
- const resolvedOptions = options ?? {};
1249
- const watchedTables = new Set<string>(
1250
- (resolvedOptions?.tables ?? []).flatMap((table) => [table, `ps_data__${table}`, `ps_data_local__${table}`])
1251
- );
1252
-
1253
- const changedTables = new Set<string>();
1254
- const throttleMs = resolvedOptions.throttleMs ?? DEFAULT_WATCH_THROTTLE_MS;
1255
-
1256
- const executor = new ControlledExecutor(async (e: WatchOnChangeEvent) => {
1257
- await onChange(e);
1258
- });
1259
-
1260
- const flushTableUpdates = throttleTrailing(
1261
- () =>
1262
- this.handleTableChanges(changedTables, watchedTables, (intersection) => {
1263
- if (resolvedOptions?.signal?.aborted) return;
1264
- executor.schedule({ changedTables: intersection });
1265
- }),
1266
- throttleMs
1267
- );
1268
-
1269
- if (options?.triggerImmediate) {
1270
- executor.schedule({ changedTables: [] });
1271
- }
1272
-
1273
- const dispose = this.database.registerListener({
1274
- tablesUpdated: async (update) => {
1275
- try {
1276
- this.processTableUpdates(update, changedTables);
1277
- flushTableUpdates();
1278
- } catch (error) {
1279
- onError?.(error);
1280
- }
1281
- }
1282
- });
1283
-
1284
- resolvedOptions.signal?.addEventListener('abort', () => {
1285
- executor.dispose();
1286
- dispose();
1287
- });
1288
-
1289
- return () => dispose();
1290
- }
1291
-
1292
- /**
1293
- * Create a Stream of changes to any of the specified tables.
1294
- *
1295
- * This is preferred over {@link watchWithAsyncGenerator} when multiple queries need to be performed
1296
- * together when data is changed.
1297
- *
1298
- * Note: do not declare this as `async *onChange` as it will not work in React Native.
1299
- *
1300
- * @param options Options for configuring watch behavior
1301
- * @returns An AsyncIterable that yields change events whenever the specified tables change
1302
- */
1303
- onChangeWithAsyncGenerator(options?: SQLWatchOptions): AsyncIterable<WatchOnChangeEvent> {
1304
- const resolvedOptions = options ?? {};
1305
-
1306
- return new EventIterator<WatchOnChangeEvent>((eventOptions) => {
1307
- const dispose = this.onChangeWithCallback(
1308
- {
1309
- onChange: (event): void => {
1310
- eventOptions.push(event);
1311
- },
1312
- onError: (error) => {
1313
- eventOptions.fail(error);
1314
- }
1315
- },
1316
- options
1317
- );
1318
-
1319
- resolvedOptions.signal?.addEventListener('abort', () => {
1320
- eventOptions.stop();
1321
- // Maybe fail?
1322
- });
1323
-
1324
- return () => dispose();
1325
- });
1326
- }
1327
-
1328
- private handleTableChanges(
1329
- changedTables: Set<string>,
1330
- watchedTables: Set<string>,
1331
- onDetectedChanges: (changedTables: string[]) => void
1332
- ): void {
1333
- if (changedTables.size > 0) {
1334
- const intersection = Array.from(changedTables.values()).filter((change) => watchedTables.has(change));
1335
- if (intersection.length) {
1336
- onDetectedChanges(intersection);
1337
- }
1338
- }
1339
- changedTables.clear();
1340
- }
1341
-
1342
- private processTableUpdates(
1343
- updateNotification: BatchedUpdateNotification | UpdateNotification,
1344
- changedTables: Set<string>
1345
- ): void {
1346
- const tables = isBatchedUpdateNotification(updateNotification)
1347
- ? updateNotification.tables
1348
- : [updateNotification.table];
1349
-
1350
- for (const table of tables) {
1351
- changedTables.add(table);
1352
- }
1353
- }
1354
-
1355
- /**
1356
- * @ignore
1357
- */
1358
- private async executeReadOnly(sql: string, params?: any[]) {
1359
- await this.waitForReady();
1360
- return this.database.readLock((tx) => tx.execute(sql, params));
1361
- }
1362
- }