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

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,18 +1,18 @@
1
- import {
2
- BaseColumnType,
3
- Column,
4
- ColumnsType,
5
- ColumnType,
6
- ExtractColumnValueType,
7
- MAX_AMOUNT_OF_COLUMNS
8
- } from './Column.js';
1
+ import { Column, ColumnsType, ExtractColumnValueType } from './Column.js';
9
2
  import { Index } from './Index.js';
10
3
  import { IndexedColumn } from './IndexedColumn.js';
11
4
  import { encodeTableOptions } from './internal.js';
12
- import { TableV2 } from './TableV2.js';
5
+
6
+ /**
7
+ * powersync-sqlite-core limits the number of column per table to 1999, due to internal SQLite limits.
8
+ * In earlier versions this was limited to 63.
9
+ */
10
+ const MAX_AMOUNT_OF_COLUMNS = 1999;
13
11
 
14
12
  /**
15
13
  * Options that apply both to JSON-based tables and raw tables.
14
+ *
15
+ * @public
16
16
  */
17
17
  export interface TableOrRawTableOptions {
18
18
  localOnly?: boolean;
@@ -30,6 +30,8 @@ interface SharedTableOptions extends TableOrRawTableOptions {
30
30
  *
31
31
  * Including old values may be helpful for some backend connector implementations, which is
32
32
  * why it can be enabled on per-table or per-columm basis.
33
+ *
34
+ * @public
33
35
  */
34
36
  export interface TrackPreviousOptions {
35
37
  /** When defined, a list of column names for which old values should be tracked. */
@@ -38,7 +40,10 @@ export interface TrackPreviousOptions {
38
40
  onlyWhenChanged?: boolean;
39
41
  }
40
42
 
41
- export interface TableOptions extends SharedTableOptions {
43
+ /**
44
+ * @public
45
+ */
46
+ export interface ResolvedTableOptions extends SharedTableOptions {
42
47
  /**
43
48
  * The synced table name, matching sync rules
44
49
  */
@@ -47,19 +52,27 @@ export interface TableOptions extends SharedTableOptions {
47
52
  indexes?: Index[];
48
53
  }
49
54
 
50
- export type RowType<T extends TableV2<any>> = {
51
- [K in keyof T['columnMap']]: ExtractColumnValueType<T['columnMap'][K]>;
52
- } & {
53
- id: string;
54
- };
55
+ /**
56
+ * @public
57
+ */
58
+ export type RowType<T extends Table<any>> =
59
+ T extends Table<infer Columns>
60
+ ? { [K in keyof Columns]: ExtractColumnValueType<Columns[K]> } & { id: string }
61
+ : never;
55
62
 
56
- export type IndexShorthand = Record<string, string[]>;
63
+ /**
64
+ * @public
65
+ */
66
+ export type IndexShorthand = Record<string, (string | IndexedColumn)[]>;
57
67
 
58
- export interface TableV2Options extends SharedTableOptions {
68
+ /**
69
+ * @public
70
+ */
71
+ export interface TableOptions extends SharedTableOptions {
59
72
  indexes?: IndexShorthand;
60
73
  }
61
74
 
62
- export const DEFAULT_TABLE_OPTIONS = {
75
+ const DEFAULT_TABLE_OPTIONS = {
63
76
  indexes: [],
64
77
  insertOnly: false,
65
78
  localOnly: false,
@@ -68,160 +81,18 @@ export const DEFAULT_TABLE_OPTIONS = {
68
81
  ignoreEmptyUpdates: false
69
82
  };
70
83
 
71
- export const InvalidSQLCharacters = /["'%,.#\s[\]]/;
72
-
73
- export class Table<Columns extends ColumnsType = ColumnsType> {
74
- protected options: TableOptions;
75
-
76
- protected _mappedColumns: Columns;
77
-
78
- static createLocalOnly(options: TableOptions) {
79
- return new Table({ ...options, localOnly: true, insertOnly: false });
80
- }
81
-
82
- static createInsertOnly(options: TableOptions) {
83
- return new Table({ ...options, localOnly: false, insertOnly: true });
84
- }
85
-
86
- /**
87
- * Create a table.
88
- * @deprecated This was only only included for TableV2 and is no longer necessary.
89
- * Prefer to use new Table() directly.
90
- *
91
- * TODO remove in the next major release.
92
- */
93
- static createTable(name: string, table: Table) {
94
- return new Table({
95
- name,
96
- columns: table.columns,
97
- indexes: table.indexes,
98
- localOnly: table.options.localOnly,
99
- insertOnly: table.options.insertOnly,
100
- viewName: table.options.viewName
101
- });
102
- }
84
+ const InvalidSQLCharacters = /["'%,.#\s[\]]/;
103
85
 
104
- /**
105
- * Creates a new Table instance.
106
- *
107
- * This constructor supports two different versions:
108
- * 1. New constructor: Using a Columns object and an optional TableV2Options object
109
- * 2. Deprecated constructor: Using a TableOptions object (will be removed in the next major release)
110
- *
111
- * @constructor
112
- * @param {Columns | TableOptions} optionsOrColumns - Either a Columns object (for V2 syntax) or a TableOptions object (for V1 syntax)
113
- * @param {TableV2Options} [v2Options] - Optional configuration options for V2 syntax
114
- *
115
- * @example
116
- * ```javascript
117
- * // New Constructor
118
- * const table = new Table(
119
- * {
120
- * name: column.text,
121
- * age: column.integer
122
- * },
123
- * { indexes: { nameIndex: ['name'] } }
124
- * );
125
- *```
126
- *
127
- *
128
- * @example
129
- * ```javascript
130
- * // Deprecated Constructor
131
- * const table = new Table({
132
- * name: 'users',
133
- * columns: [
134
- * new Column({ name: 'name', type: ColumnType.TEXT }),
135
- * new Column({ name: 'age', type: ColumnType.INTEGER })
136
- * ]
137
- * });
138
- *```
139
- */
140
- constructor(columns: Columns, options?: TableV2Options);
141
- /**
142
- * @deprecated This constructor will be removed in the next major release.
143
- * Use the new constructor shown below instead as this does not show types.
144
- * @example
145
- * <caption>Use this instead</caption>
146
- * ```javascript
147
- * const table = new Table(
148
- * {
149
- * name: column.text,
150
- * age: column.integer
151
- * },
152
- * { indexes: { nameIndex: ['name'] } }
153
- * );
154
- *```
155
- */
156
- constructor(options: TableOptions);
157
- constructor(optionsOrColumns: Columns | TableOptions, v2Options?: TableV2Options) {
158
- if (this.isTableV1(optionsOrColumns)) {
159
- this.initTableV1(optionsOrColumns);
160
- } else {
161
- this.initTableV2(optionsOrColumns, v2Options);
162
- }
163
- }
164
-
165
- copyWithName(name: string): Table {
166
- return new Table({
167
- ...this.options,
168
- name
169
- });
170
- }
171
-
172
- private isTableV1(arg: TableOptions | Columns): arg is TableOptions {
173
- return 'columns' in arg && Array.isArray(arg.columns);
174
- }
175
-
176
- private initTableV1(options: TableOptions) {
177
- this.options = {
178
- ...options,
179
- indexes: options.indexes || []
180
- };
181
- this.applyDefaultOptions();
182
- }
183
-
184
- private initTableV2(columns: Columns, options?: TableV2Options) {
185
- const convertedColumns = Object.entries(columns).map(
186
- ([name, columnInfo]) => new Column({ name, type: columnInfo.type })
187
- );
188
-
189
- const convertedIndexes = Object.entries(options?.indexes ?? {}).map(
190
- ([name, columnNames]) =>
191
- new Index({
192
- name,
193
- columns: columnNames.map(
194
- (name) =>
195
- new IndexedColumn({
196
- name: name.replace(/^-/, ''),
197
- ascending: !name.startsWith('-')
198
- })
199
- )
200
- })
201
- );
202
-
203
- this.options = {
204
- name: '',
205
- columns: convertedColumns,
206
- indexes: convertedIndexes,
207
- viewName: options?.viewName,
208
- insertOnly: options?.insertOnly,
209
- localOnly: options?.localOnly,
210
- trackPrevious: options?.trackPrevious,
211
- trackMetadata: options?.trackMetadata,
212
- ignoreEmptyUpdates: options?.ignoreEmptyUpdates
213
- };
86
+ /**
87
+ * A resolved table in the PowerSync schema, with all columns, index definitions and options.
88
+ *
89
+ * When constructing tables for your schema, consider using {@link Table} instead.
90
+ *
91
+ * @public
92
+ */
93
+ export class ResolvedTable {
94
+ constructor(readonly options: ResolvedTableOptions) {
214
95
  this.applyDefaultOptions();
215
-
216
- this._mappedColumns = columns;
217
- }
218
-
219
- private applyDefaultOptions() {
220
- this.options.insertOnly ??= DEFAULT_TABLE_OPTIONS.insertOnly;
221
- this.options.localOnly ??= DEFAULT_TABLE_OPTIONS.localOnly;
222
- this.options.trackPrevious ??= DEFAULT_TABLE_OPTIONS.trackPrevious;
223
- this.options.trackMetadata ??= DEFAULT_TABLE_OPTIONS.trackMetadata;
224
- this.options.ignoreEmptyUpdates ??= DEFAULT_TABLE_OPTIONS.ignoreEmptyUpdates;
225
96
  }
226
97
 
227
98
  get name() {
@@ -240,16 +111,6 @@ export class Table<Columns extends ColumnsType = ColumnsType> {
240
111
  return this.options.columns;
241
112
  }
242
113
 
243
- get columnMap(): Columns {
244
- return (
245
- this._mappedColumns ??
246
- this.columns.reduce((hash: Record<string, BaseColumnType<any>>, column) => {
247
- hash[column.name] = { type: column.type ?? ColumnType.TEXT };
248
- return hash;
249
- }, {} as Columns)
250
- );
251
- }
252
-
253
114
  get indexes() {
254
115
  return this.options.indexes ?? [];
255
116
  }
@@ -356,4 +217,93 @@ export class Table<Columns extends ColumnsType = ColumnsType> {
356
217
  ...encodeTableOptions(this)
357
218
  };
358
219
  }
220
+
221
+ private applyDefaultOptions() {
222
+ this.options.insertOnly ??= DEFAULT_TABLE_OPTIONS.insertOnly;
223
+ this.options.localOnly ??= DEFAULT_TABLE_OPTIONS.localOnly;
224
+ this.options.trackPrevious ??= DEFAULT_TABLE_OPTIONS.trackPrevious;
225
+ this.options.trackMetadata ??= DEFAULT_TABLE_OPTIONS.trackMetadata;
226
+ this.options.ignoreEmptyUpdates ??= DEFAULT_TABLE_OPTIONS.ignoreEmptyUpdates;
227
+ }
228
+ }
229
+
230
+ /**
231
+ * A table with a statically-typed `Columns` record structure.
232
+ *
233
+ * This is the recommended way to declare tables in PowerSync schemas.
234
+ *
235
+ * @public
236
+ */
237
+ export class Table<Columns extends ColumnsType = ColumnsType> extends ResolvedTable {
238
+ protected _mappedColumns!: Columns;
239
+
240
+ static createLocalOnly<Columns extends ColumnsType = ColumnsType>(columns: Columns, options?: TableOptions) {
241
+ return new Table(columns, { localOnly: true, insertOnly: false, ...options });
242
+ }
243
+
244
+ static createInsertOnly<Columns extends ColumnsType = ColumnsType>(columns: Columns, options?: TableOptions) {
245
+ return new Table(columns, { localOnly: false, insertOnly: true, ...options });
246
+ }
247
+
248
+ /**
249
+ * Creates a new Table instance.
250
+ *
251
+ * @param columns - A Columns object (a record of column names to column types).
252
+ * @param options - Optional configuration options for the table.
253
+ *
254
+ * @example
255
+ * ```javascript
256
+ * const table = new Table(
257
+ * {
258
+ * name: column.text,
259
+ * age: column.integer
260
+ * },
261
+ * { indexes: { nameIndex: ['name'] } }
262
+ * );
263
+ *```
264
+ */
265
+ constructor(columns: Columns, options?: TableOptions) {
266
+ super(Table.optionsFromColumns<Columns>(columns, options));
267
+ this._mappedColumns = columns;
268
+ }
269
+
270
+ copyWithName(name: string): ResolvedTable {
271
+ return new ResolvedTable({ ...this.options, name });
272
+ }
273
+
274
+ private static optionsFromColumns<Columns extends ColumnsType>(
275
+ columns: Columns,
276
+ options?: TableOptions
277
+ ): ResolvedTableOptions {
278
+ const convertedColumns = Object.entries(columns).map(
279
+ ([name, columnInfo]) => new Column({ name, type: columnInfo.type })
280
+ );
281
+
282
+ const convertedIndexes = Object.entries(options?.indexes ?? {}).map(
283
+ ([name, columnNames]) =>
284
+ new Index({
285
+ name,
286
+ columns: columnNames.map((nameOrIndexedColumn) =>
287
+ typeof nameOrIndexedColumn === 'string'
288
+ ? new IndexedColumn({
289
+ name: nameOrIndexedColumn.replace(/^-/, ''),
290
+ ascending: !nameOrIndexedColumn.startsWith('-')
291
+ })
292
+ : nameOrIndexedColumn
293
+ )
294
+ })
295
+ );
296
+
297
+ return {
298
+ name: '',
299
+ columns: convertedColumns,
300
+ indexes: convertedIndexes,
301
+ viewName: options?.viewName,
302
+ insertOnly: options?.insertOnly,
303
+ localOnly: options?.localOnly,
304
+ trackPrevious: options?.trackPrevious,
305
+ trackMetadata: options?.trackMetadata,
306
+ ignoreEmptyUpdates: options?.ignoreEmptyUpdates
307
+ };
308
+ }
359
309
  }
package/src/index.ts CHANGED
@@ -1,63 +1,47 @@
1
1
  export * from './attachments/AttachmentContext.js';
2
2
  export * from './attachments/AttachmentErrorHandler.js';
3
3
  export * from './attachments/AttachmentQueue.js';
4
- export * from './attachments/AttachmentService.js';
5
4
  export * from './attachments/LocalStorageAdapter.js';
6
5
  export * from './attachments/RemoteStorageAdapter.js';
7
6
  export * from './attachments/Schema.js';
8
- export * from './attachments/SyncingService.js';
9
7
  export * from './attachments/WatchedAttachmentItem.js';
10
8
 
11
- export * from './client/AbstractPowerSyncDatabase.js';
12
- export * from './client/AbstractPowerSyncOpenFactory.js';
9
+ export * from './client/CommonPowerSyncDatabase.js';
13
10
  export { compilableQueryWatch, CompilableQueryWatchHandler } from './client/compilableQueryWatch.js';
14
11
  export * from './client/connection/PowerSyncBackendConnector.js';
15
12
  export * from './client/connection/PowerSyncCredentials.js';
16
- export { MAX_OP_ID } from './client/constants.js';
17
- export { runOnSchemaChange } from './client/runOnSchemaChange.js';
18
13
  export * from './client/SQLOpenFactory.js';
19
- export * from './client/sync/bucket/BucketStorageAdapter.js';
20
14
  export * from './client/sync/bucket/CrudBatch.js';
21
15
  export { CrudEntry, OpId, UpdateType } from './client/sync/bucket/CrudEntry.js';
22
16
  export * from './client/sync/bucket/CrudTransaction.js';
23
- export * from './client/sync/bucket/SqliteBucketStorage.js';
24
- export * from './client/sync/stream/AbstractRemote.js';
25
- export * from './client/sync/stream/AbstractStreamingSyncImplementation.js';
26
17
  export * from './client/sync/stream/JsonValue.js';
27
18
  export * from './client/sync/sync-streams.js';
19
+ export { SyncOptions, SyncStreamConnectionMethod, FetchStrategy } from './client/sync/options.js';
28
20
 
29
- export * from './client/ConnectionManager.js';
30
- export * from './db/ConnectionClosedError.js';
31
21
  export { ProgressWithOperations, SyncProgress } from './db/crud/SyncProgress.js';
32
22
  export * from './db/crud/SyncStatus.js';
33
23
  export * from './db/crud/UploadQueueStatus.js';
34
24
  export * from './db/DBAdapter.js';
25
+ export * from './db/QueryResult.js';
35
26
  export * from './db/schema/Column.js';
36
27
  export * from './db/schema/Index.js';
37
28
  export * from './db/schema/IndexedColumn.js';
38
- export { RawTableType, PendingStatementParameter, PendingStatement } from './db/schema/RawTable.js';
29
+ export { PendingStatement, PendingStatementParameter, RawTableType } from './db/schema/RawTable.js';
39
30
  export * from './db/schema/Schema.js';
40
31
  export * from './db/schema/Table.js';
41
- export * from './db/schema/TableV2.js';
42
32
 
43
33
  export * from './client/Query.js';
44
- export { MEMORY_TRIGGER_CLAIM_MANAGER } from './client/triggers/MemoryTriggerClaimManager.js';
45
34
  export * from './client/triggers/sanitizeSQL.js';
46
35
  export * from './client/triggers/TriggerManager.js';
47
- export { TriggerManagerImpl } from './client/triggers/TriggerManagerImpl.js';
48
36
  export * from './client/watched/GetAllQuery.js';
49
- export * from './client/watched/processors/AbstractQueryProcessor.js';
50
37
  export * from './client/watched/processors/comparators.js';
51
38
  export * from './client/watched/processors/DifferentialQueryProcessor.js';
52
39
  export * from './client/watched/processors/OnChangeQueryProcessor.js';
53
40
  export * from './client/watched/WatchedQuery.js';
54
41
 
55
- export * from './utils/AbortOperation.js';
42
+ export { type Mutex } from './utils/mutex.js';
56
43
  export * from './utils/BaseObserver.js';
57
- export * from './utils/ControlledExecutor.js';
44
+ export * from './utils/MetaBaseObserver.js';
58
45
  export * from './utils/Logger.js';
59
- export * from './utils/mutex.js';
60
- export * from './utils/parseQuery.js';
61
- export type { SimpleAsyncIterator } from './utils/stream_transform.js';
62
46
 
63
47
  export * from './types/types.js';
@@ -1,8 +1,14 @@
1
+ /**
2
+ * @public
3
+ */
1
4
  export interface CompilableQuery<T> {
2
5
  execute(): Promise<T[]>;
3
6
  compile(): CompiledQuery;
4
7
  }
5
8
 
9
+ /**
10
+ * @public
11
+ */
6
12
  export interface CompiledQuery {
7
13
  readonly sql: string;
8
14
  readonly parameters: ReadonlyArray<unknown>;
@@ -1,13 +1,25 @@
1
+ /**
2
+ * @public
3
+ */
1
4
  export interface Disposable {
2
5
  dispose: () => Promise<void> | void;
3
6
  }
4
7
 
8
+ /**
9
+ * @public
10
+ */
5
11
  export type BaseListener = Record<string, ((...event: any) => any) | undefined>;
6
12
 
13
+ /**
14
+ * @public
15
+ */
7
16
  export interface BaseObserverInterface<T extends BaseListener> {
8
17
  registerListener(listener: Partial<T>): () => void;
9
18
  }
10
19
 
20
+ /**
21
+ * @public
22
+ */
11
23
  export class BaseObserver<T extends BaseListener = BaseListener> implements BaseObserverInterface<T> {
12
24
  protected listeners = new Set<Partial<T>>();
13
25
 
@@ -1,47 +1,102 @@
1
- import Logger, { type ILogger, type ILogLevel } from 'js-logger';
2
-
3
- export { GlobalLogger, ILogger, ILoggerOpts, ILogHandler, ILogLevel } from 'js-logger';
1
+ /**
2
+ * @public
3
+ */
4
+ export const LogLevels = {
5
+ trace: 10,
6
+ debug: 20,
7
+ info: 30,
8
+ warn: 40,
9
+ error: 50
10
+ } as const;
4
11
 
5
- const TypedLogger: ILogger = Logger as any;
12
+ /**
13
+ * A log record passed to a {@link PowerSyncLogger}.
14
+ *
15
+ * @public
16
+ */
17
+ export interface LogRecord {
18
+ /**
19
+ * The log level (see {@link LogLevels} for preconfigured values) for the message. Depending on how a receiving logger
20
+ * has been configured, messages below a configured minimum level may be ignored.
21
+ */
22
+ level: number;
6
23
 
7
- export const LogLevel = {
8
- TRACE: TypedLogger.TRACE,
9
- DEBUG: TypedLogger.DEBUG,
10
- INFO: TypedLogger.INFO,
11
- TIME: TypedLogger.TIME,
12
- WARN: TypedLogger.WARN,
13
- ERROR: TypedLogger.ERROR,
14
- OFF: TypedLogger.OFF
15
- };
24
+ /**
25
+ * The main message to log.
26
+ */
27
+ message: string;
16
28
 
17
- export interface CreateLoggerOptions {
18
- logLevel?: ILogLevel;
29
+ /**
30
+ * When the log message contains an error, the error causing the log.
31
+ *
32
+ * This is not guaranteed to be an `Error` instance. On the web, we might have to serialize objects across message
33
+ * channels and represent them as a string.
34
+ */
35
+ error?: unknown;
19
36
  }
20
37
 
21
38
  /**
22
- * Retrieves the base (default) logger instance.
39
+ * A logger used by the PowerSync SDK.
40
+ *
41
+ * This is deliberately a very simple interface, and it's not a designed to be a general-purpose logger you would use in
42
+ * your application. Instead, you can provide an implementation of this to PowerSync to make it use your preferred
43
+ * logging libraries.
23
44
  *
24
- * This base logger controls the default logging configuration and is shared
25
- * across all loggers created with `createLogger`. Adjusting settings on this
26
- * base logger affects all loggers derived from it unless explicitly overridden.
45
+ * By default, the SDK uses a {@link createConsoleLogger} instance forwarding messages to `console.log`.
27
46
  *
47
+ * @public
28
48
  */
29
- export function createBaseLogger() {
30
- return Logger;
49
+ export interface PowerSyncLogger {
50
+ log(record: LogRecord): void;
31
51
  }
32
52
 
33
53
  /**
34
- * Creates and configures a new named logger based on the base logger.
54
+ * @public
55
+ */
56
+ export interface CreateLoggerOptions {
57
+ /**
58
+ * A prefix for messages emitted by {@link createConsoleLogger} to make them more recognizable.
59
+ *
60
+ * Defaults to `'PowerSync'`.
61
+ */
62
+ prefix: string;
63
+
64
+ /**
65
+ * The minimum log level to consider for messages. Defaults to {@link LogLevels.info}.
66
+ */
67
+ minLevel: number;
68
+ }
69
+
70
+ /**
71
+ * A very simple {@link PowerSyncLogger} implementation forwarding messages to `console.log`.
35
72
  *
36
- * Named loggers allow specific modules or areas of your application to have
37
- * their own logging levels and behaviors. These loggers inherit configuration
38
- * from the base logger by default but can override settings independently.
73
+ * @param options - Options to configure a minimum severity of the logger or a prefix to make messages more recognizable.
74
+ * @public
39
75
  */
40
- export function createLogger(name: string, options: CreateLoggerOptions = {}): ILogger {
41
- const logger = Logger.get(name);
42
- if (options.logLevel) {
43
- logger.setLevel(options.logLevel);
44
- }
76
+ export function createConsoleLogger(options?: Partial<CreateLoggerOptions>): PowerSyncLogger & CreateLoggerOptions {
77
+ const { prefix = 'PowerSync', minLevel = LogLevels.info } = options ?? {};
78
+
79
+ return {
80
+ prefix,
81
+ minLevel,
82
+ log({ level, message, error }) {
83
+ if (level < this.minLevel) return;
84
+
85
+ let emitter = console.log;
86
+ if (level >= LogLevels.error) {
87
+ emitter = console.error;
88
+ } else if (level >= LogLevels.warn) {
89
+ emitter = console.warn;
90
+ } else if (level >= LogLevels.info) {
91
+ emitter = console.info;
92
+ }
45
93
 
46
- return logger;
94
+ const messageWithPrefix = `[${prefix}]: ${message}`;
95
+ if (error) {
96
+ emitter(messageWithPrefix, error);
97
+ } else {
98
+ emitter(messageWithPrefix);
99
+ }
100
+ }
101
+ };
47
102
  }