@powersync/common 1.57.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (219) hide show
  1. package/lib/attachments/AttachmentContext.d.ts +5 -5
  2. package/lib/attachments/AttachmentContext.js +6 -2
  3. package/lib/attachments/AttachmentContext.js.map +1 -1
  4. package/lib/attachments/AttachmentQueue.d.ts +6 -6
  5. package/lib/attachments/AttachmentQueue.js +9 -8
  6. package/lib/attachments/AttachmentQueue.js.map +1 -1
  7. package/lib/attachments/AttachmentService.d.ts +3 -3
  8. package/lib/attachments/AttachmentService.js +4 -3
  9. package/lib/attachments/AttachmentService.js.map +1 -1
  10. package/lib/attachments/Schema.d.ts +2 -2
  11. package/lib/attachments/SyncingService.d.ts +2 -2
  12. package/lib/attachments/SyncingService.js +21 -6
  13. package/lib/attachments/SyncingService.js.map +1 -1
  14. package/lib/client/{AbstractPowerSyncDatabase.d.ts → CommonPowerSyncDatabase.d.ts} +56 -232
  15. package/lib/client/CommonPowerSyncDatabase.js +2 -0
  16. package/lib/client/CommonPowerSyncDatabase.js.map +1 -0
  17. package/lib/client/SQLOpenFactory.d.ts +35 -13
  18. package/lib/client/SQLOpenFactory.js +1 -25
  19. package/lib/client/SQLOpenFactory.js.map +1 -1
  20. package/lib/client/compilableQueryWatch.d.ts +2 -2
  21. package/lib/client/compilableQueryWatch.js +1 -2
  22. package/lib/client/compilableQueryWatch.js.map +1 -1
  23. package/lib/client/connection/PowerSyncBackendConnector.d.ts +3 -3
  24. package/lib/client/runOnSchemaChange.d.ts +2 -2
  25. package/lib/client/runOnSchemaChange.js.map +1 -1
  26. package/lib/client/sync/bucket/CrudEntry.d.ts +3 -34
  27. package/lib/client/sync/bucket/CrudEntry.js +0 -99
  28. package/lib/client/sync/bucket/CrudEntry.js.map +1 -1
  29. package/lib/client/sync/options.d.ts +65 -0
  30. package/lib/client/sync/options.js +25 -0
  31. package/lib/client/sync/options.js.map +1 -0
  32. package/lib/client/sync/sync-streams.d.ts +1 -1
  33. package/lib/client/triggers/TriggerManager.d.ts +9 -38
  34. package/lib/client/watched/GetAllQuery.d.ts +3 -3
  35. package/lib/client/watched/GetAllQuery.js +1 -1
  36. package/lib/client/watched/GetAllQuery.js.map +1 -1
  37. package/lib/client/watched/WatchedQuery.d.ts +2 -10
  38. package/lib/client/watched/WatchedQuery.js +0 -11
  39. package/lib/client/watched/WatchedQuery.js.map +1 -1
  40. package/lib/client/watched/processors/DifferentialQueryProcessor.d.ts +0 -50
  41. package/lib/client/watched/processors/DifferentialQueryProcessor.js +1 -176
  42. package/lib/client/watched/processors/DifferentialQueryProcessor.js.map +1 -1
  43. package/lib/client/watched/processors/OnChangeQueryProcessor.d.ts +0 -22
  44. package/lib/client/watched/processors/OnChangeQueryProcessor.js +1 -82
  45. package/lib/client/watched/processors/OnChangeQueryProcessor.js.map +1 -1
  46. package/lib/db/DBAdapter.d.ts +72 -154
  47. package/lib/db/DBAdapter.js +70 -108
  48. package/lib/db/DBAdapter.js.map +1 -1
  49. package/lib/db/QueryResult.d.ts +104 -0
  50. package/lib/db/QueryResult.js +96 -0
  51. package/lib/db/QueryResult.js.map +1 -0
  52. package/lib/db/crud/SyncProgress.d.ts +1 -13
  53. package/lib/db/crud/SyncProgress.js +1 -62
  54. package/lib/db/crud/SyncProgress.js.map +1 -1
  55. package/lib/db/crud/SyncStatus.d.ts +32 -66
  56. package/lib/db/crud/SyncStatus.js +1 -253
  57. package/lib/db/crud/SyncStatus.js.map +1 -1
  58. package/lib/db/schema/Column.d.ts +0 -7
  59. package/lib/db/schema/Column.js +0 -7
  60. package/lib/db/schema/Column.js.map +1 -1
  61. package/lib/db/schema/Index.d.ts +2 -6
  62. package/lib/db/schema/Index.js +1 -4
  63. package/lib/db/schema/Index.js.map +1 -1
  64. package/lib/db/schema/IndexedColumn.d.ts +2 -6
  65. package/lib/db/schema/IndexedColumn.js +1 -4
  66. package/lib/db/schema/IndexedColumn.js.map +1 -1
  67. package/lib/db/schema/Schema.d.ts +4 -28
  68. package/lib/db/schema/Schema.js.map +1 -1
  69. package/lib/db/schema/Table.d.ts +50 -96
  70. package/lib/db/schema/Table.js +83 -91
  71. package/lib/db/schema/Table.js.map +1 -1
  72. package/lib/index.d.ts +5 -21
  73. package/lib/index.js +5 -22
  74. package/lib/index.js.map +1 -1
  75. package/lib/utils/BaseObserver.d.ts +1 -1
  76. package/lib/utils/BaseObserver.js +1 -1
  77. package/lib/utils/Logger.d.ts +52 -23
  78. package/lib/utils/Logger.js +35 -33
  79. package/lib/utils/Logger.js.map +1 -1
  80. package/lib/utils/MetaBaseObserver.d.ts +11 -11
  81. package/lib/utils/MetaBaseObserver.js +1 -50
  82. package/lib/utils/MetaBaseObserver.js.map +1 -1
  83. package/lib/utils/mutex.d.ts +3 -53
  84. package/lib/utils/mutex.js +1 -156
  85. package/lib/utils/mutex.js.map +1 -1
  86. package/package.json +8 -45
  87. package/src/attachments/AttachmentContext.ts +11 -10
  88. package/src/attachments/AttachmentQueue.ts +15 -14
  89. package/src/attachments/AttachmentService.ts +7 -6
  90. package/src/attachments/Schema.ts +2 -2
  91. package/src/attachments/SyncingService.ts +23 -9
  92. package/src/client/CommonPowerSyncDatabase.ts +540 -0
  93. package/src/client/SQLOpenFactory.ts +38 -22
  94. package/src/client/compilableQueryWatch.ts +3 -4
  95. package/src/client/connection/PowerSyncBackendConnector.ts +3 -3
  96. package/src/client/runOnSchemaChange.ts +2 -2
  97. package/src/client/sync/bucket/CrudEntry.ts +4 -104
  98. package/src/client/sync/options.ts +77 -0
  99. package/src/client/sync/sync-streams.ts +1 -1
  100. package/src/client/triggers/TriggerManager.ts +10 -41
  101. package/src/client/watched/GetAllQuery.ts +3 -3
  102. package/src/client/watched/WatchedQuery.ts +2 -15
  103. package/src/client/watched/processors/DifferentialQueryProcessor.ts +0 -223
  104. package/src/client/watched/processors/OnChangeQueryProcessor.ts +0 -111
  105. package/src/db/DBAdapter.ts +141 -226
  106. package/src/db/QueryResult.ts +195 -0
  107. package/src/db/crud/SyncProgress.ts +2 -42
  108. package/src/db/crud/SyncStatus.ts +45 -220
  109. package/src/db/schema/Column.ts +0 -8
  110. package/src/db/schema/Index.ts +3 -6
  111. package/src/db/schema/IndexedColumn.ts +3 -6
  112. package/src/db/schema/Schema.ts +4 -4
  113. package/src/db/schema/Table.ts +111 -186
  114. package/src/index.ts +5 -21
  115. package/src/utils/BaseObserver.ts +1 -1
  116. package/src/utils/Logger.ts +80 -34
  117. package/src/utils/MetaBaseObserver.ts +14 -60
  118. package/src/utils/mutex.ts +4 -201
  119. package/dist/bundle.cjs +0 -14390
  120. package/dist/bundle.cjs.map +0 -1
  121. package/dist/bundle.mjs +0 -14309
  122. package/dist/bundle.mjs.map +0 -1
  123. package/dist/bundle.node.cjs +0 -12021
  124. package/dist/bundle.node.cjs.map +0 -1
  125. package/dist/bundle.node.mjs +0 -11940
  126. package/dist/bundle.node.mjs.map +0 -1
  127. package/dist/index.d.cts +0 -4651
  128. package/legacy/sync_protocol.d.ts +0 -103
  129. package/lib/client/AbstractPowerSyncDatabase.js +0 -990
  130. package/lib/client/AbstractPowerSyncDatabase.js.map +0 -1
  131. package/lib/client/AbstractPowerSyncOpenFactory.d.ts +0 -28
  132. package/lib/client/AbstractPowerSyncOpenFactory.js +0 -28
  133. package/lib/client/AbstractPowerSyncOpenFactory.js.map +0 -1
  134. package/lib/client/ConnectionManager.d.ts +0 -115
  135. package/lib/client/ConnectionManager.js +0 -294
  136. package/lib/client/ConnectionManager.js.map +0 -1
  137. package/lib/client/CustomQuery.d.ts +0 -22
  138. package/lib/client/CustomQuery.js +0 -43
  139. package/lib/client/CustomQuery.js.map +0 -1
  140. package/lib/client/constants.d.ts +0 -4
  141. package/lib/client/constants.js +0 -5
  142. package/lib/client/constants.js.map +0 -1
  143. package/lib/client/sync/bucket/BucketStorageAdapter.d.ts +0 -56
  144. package/lib/client/sync/bucket/BucketStorageAdapter.js +0 -29
  145. package/lib/client/sync/bucket/BucketStorageAdapter.js.map +0 -1
  146. package/lib/client/sync/bucket/SqliteBucketStorage.d.ts +0 -37
  147. package/lib/client/sync/bucket/SqliteBucketStorage.js +0 -165
  148. package/lib/client/sync/bucket/SqliteBucketStorage.js.map +0 -1
  149. package/lib/client/sync/stream/AbstractRemote.d.ts +0 -180
  150. package/lib/client/sync/stream/AbstractRemote.js +0 -505
  151. package/lib/client/sync/stream/AbstractRemote.js.map +0 -1
  152. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +0 -265
  153. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +0 -705
  154. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +0 -1
  155. package/lib/client/sync/stream/WebsocketClientTransport.d.ts +0 -15
  156. package/lib/client/sync/stream/WebsocketClientTransport.js +0 -62
  157. package/lib/client/sync/stream/WebsocketClientTransport.js.map +0 -1
  158. package/lib/client/sync/stream/core-instruction.d.ts +0 -77
  159. package/lib/client/sync/stream/core-instruction.js +0 -30
  160. package/lib/client/sync/stream/core-instruction.js.map +0 -1
  161. package/lib/client/triggers/MemoryTriggerClaimManager.d.ts +0 -6
  162. package/lib/client/triggers/MemoryTriggerClaimManager.js +0 -21
  163. package/lib/client/triggers/MemoryTriggerClaimManager.js.map +0 -1
  164. package/lib/client/triggers/TriggerManagerImpl.d.ts +0 -39
  165. package/lib/client/triggers/TriggerManagerImpl.js +0 -410
  166. package/lib/client/triggers/TriggerManagerImpl.js.map +0 -1
  167. package/lib/client/watched/processors/AbstractQueryProcessor.d.ts +0 -68
  168. package/lib/client/watched/processors/AbstractQueryProcessor.js +0 -151
  169. package/lib/client/watched/processors/AbstractQueryProcessor.js.map +0 -1
  170. package/lib/db/ConnectionClosedError.d.ts +0 -12
  171. package/lib/db/ConnectionClosedError.js +0 -23
  172. package/lib/db/ConnectionClosedError.js.map +0 -1
  173. package/lib/db/schema/TableV2.d.ts +0 -11
  174. package/lib/db/schema/TableV2.js +0 -11
  175. package/lib/db/schema/TableV2.js.map +0 -1
  176. package/lib/utils/AbortOperation.d.ts +0 -11
  177. package/lib/utils/AbortOperation.js +0 -21
  178. package/lib/utils/AbortOperation.js.map +0 -1
  179. package/lib/utils/ControlledExecutor.d.ts +0 -31
  180. package/lib/utils/ControlledExecutor.js +0 -54
  181. package/lib/utils/ControlledExecutor.js.map +0 -1
  182. package/lib/utils/async.d.ts +0 -46
  183. package/lib/utils/async.js +0 -147
  184. package/lib/utils/async.js.map +0 -1
  185. package/lib/utils/compatibility.d.ts +0 -8
  186. package/lib/utils/compatibility.js +0 -9
  187. package/lib/utils/compatibility.js.map +0 -1
  188. package/lib/utils/parseQuery.d.ts +0 -12
  189. package/lib/utils/parseQuery.js +0 -20
  190. package/lib/utils/parseQuery.js.map +0 -1
  191. package/lib/utils/queue.d.ts +0 -16
  192. package/lib/utils/queue.js +0 -42
  193. package/lib/utils/queue.js.map +0 -1
  194. package/lib/utils/stream_transform.d.ts +0 -41
  195. package/lib/utils/stream_transform.js +0 -211
  196. package/lib/utils/stream_transform.js.map +0 -1
  197. package/src/client/AbstractPowerSyncDatabase.ts +0 -1408
  198. package/src/client/AbstractPowerSyncOpenFactory.ts +0 -45
  199. package/src/client/ConnectionManager.ts +0 -405
  200. package/src/client/CustomQuery.ts +0 -56
  201. package/src/client/constants.ts +0 -4
  202. package/src/client/sync/bucket/BucketStorageAdapter.ts +0 -65
  203. package/src/client/sync/bucket/SqliteBucketStorage.ts +0 -202
  204. package/src/client/sync/stream/AbstractRemote.ts +0 -644
  205. package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +0 -970
  206. package/src/client/sync/stream/WebsocketClientTransport.ts +0 -82
  207. package/src/client/sync/stream/core-instruction.ts +0 -109
  208. package/src/client/triggers/MemoryTriggerClaimManager.ts +0 -25
  209. package/src/client/triggers/TriggerManagerImpl.ts +0 -501
  210. package/src/client/watched/processors/AbstractQueryProcessor.ts +0 -226
  211. package/src/db/ConnectionClosedError.ts +0 -25
  212. package/src/db/schema/TableV2.ts +0 -11
  213. package/src/utils/AbortOperation.ts +0 -19
  214. package/src/utils/ControlledExecutor.ts +0 -78
  215. package/src/utils/async.ts +0 -185
  216. package/src/utils/compatibility.ts +0 -9
  217. package/src/utils/parseQuery.ts +0 -31
  218. package/src/utils/queue.ts +0 -48
  219. package/src/utils/stream_transform.ts +0 -260
@@ -1,6 +1,6 @@
1
1
  import { encodeTableOptions } from './internal.js';
2
2
  import { RawTable, RawTableType } from './RawTable.js';
3
- import { RowType, Table } from './Table.js';
3
+ import { ResolvedTable, RowType, Table } from './Table.js';
4
4
 
5
5
  type SchemaType = Record<string, Table<any>>;
6
6
 
@@ -22,10 +22,10 @@ export class Schema<S extends SchemaType = SchemaType> {
22
22
  */
23
23
  readonly types!: SchemaTableType<S>;
24
24
  readonly props!: S;
25
- readonly tables: Table[];
25
+ readonly tables: ResolvedTable[];
26
26
  readonly rawTables: RawTable[];
27
27
 
28
- constructor(tables: Table[] | S) {
28
+ constructor(tables: ResolvedTable[] | S) {
29
29
  if (Array.isArray(tables)) {
30
30
  /*
31
31
  We need to validate that the tables have a name here because a user could pass in an array
@@ -71,7 +71,7 @@ export class Schema<S extends SchemaType = SchemaType> {
71
71
  }
72
72
  }
73
73
 
74
- toJSON() {
74
+ toJSON(): unknown {
75
75
  return {
76
76
  tables: this.tables.map((t) => t.toJSON()),
77
77
  raw_tables: this.rawTables.map(Schema.rawTableToJson)
@@ -1,15 +1,13 @@
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.
@@ -45,7 +43,7 @@ export interface TrackPreviousOptions {
45
43
  /**
46
44
  * @public
47
45
  */
48
- export interface TableOptions extends SharedTableOptions {
46
+ export interface ResolvedTableOptions extends SharedTableOptions {
49
47
  /**
50
48
  * The synced table name, matching sync rules
51
49
  */
@@ -57,29 +55,24 @@ export interface TableOptions extends SharedTableOptions {
57
55
  /**
58
56
  * @public
59
57
  */
60
- export type RowType<T extends TableV2<any>> = {
61
- [K in keyof T['columnMap']]: ExtractColumnValueType<T['columnMap'][K]>;
62
- } & {
63
- id: string;
64
- };
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;
65
62
 
66
63
  /**
67
64
  * @public
68
65
  */
69
- export type IndexShorthand = Record<string, string[]>;
66
+ export type IndexShorthand = Record<string, (string | IndexedColumn)[]>;
70
67
 
71
68
  /**
72
69
  * @public
73
70
  */
74
-
75
- export interface TableV2Options extends SharedTableOptions {
71
+ export interface TableOptions extends SharedTableOptions {
76
72
  indexes?: IndexShorthand;
77
73
  }
78
74
 
79
- /**
80
- * @internal
81
- */
82
- export const DEFAULT_TABLE_OPTIONS = {
75
+ const DEFAULT_TABLE_OPTIONS = {
83
76
  indexes: [],
84
77
  insertOnly: false,
85
78
  localOnly: false,
@@ -88,165 +81,18 @@ export const DEFAULT_TABLE_OPTIONS = {
88
81
  ignoreEmptyUpdates: false
89
82
  };
90
83
 
91
- /**
92
- * @internal
93
- */
94
- export const InvalidSQLCharacters = /["'%,.#\s[\]]/;
84
+ const InvalidSQLCharacters = /["'%,.#\s[\]]/;
95
85
 
96
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
+ *
97
91
  * @public
98
92
  */
99
- export class Table<Columns extends ColumnsType = ColumnsType> {
100
- protected options!: TableOptions;
101
-
102
- protected _mappedColumns!: Columns;
103
-
104
- static createLocalOnly(options: TableOptions) {
105
- return new Table({ ...options, localOnly: true, insertOnly: false });
106
- }
107
-
108
- static createInsertOnly(options: TableOptions) {
109
- return new Table({ ...options, localOnly: false, insertOnly: true });
110
- }
111
-
112
- /**
113
- * Create a table.
114
- * @deprecated This was only only included for TableV2 and is no longer necessary.
115
- * Prefer to use new Table() directly.
116
- *
117
- * TODO remove in the next major release.
118
- */
119
- static createTable(name: string, table: Table) {
120
- return new Table({
121
- name,
122
- columns: table.columns,
123
- indexes: table.indexes,
124
- localOnly: table.options.localOnly,
125
- insertOnly: table.options.insertOnly,
126
- viewName: table.options.viewName
127
- });
128
- }
129
-
130
- /**
131
- * Creates a new Table instance.
132
- *
133
- * This constructor supports two different versions:
134
- * 1. New constructor: Using a Columns object and an optional TableV2Options object
135
- * 2. Deprecated constructor: Using a TableOptions object (will be removed in the next major release)
136
- *
137
- * @param columns - Either a Columns object (for V2 syntax) or a TableOptions object (for V1 syntax)
138
- * @param options - Optional configuration options for V2 syntax
139
- *
140
- * @example
141
- * ```javascript
142
- * // New Constructor
143
- * const table = new Table(
144
- * {
145
- * name: column.text,
146
- * age: column.integer
147
- * },
148
- * { indexes: { nameIndex: ['name'] } }
149
- * );
150
- *```
151
- *
152
- *
153
- * @example
154
- * ```javascript
155
- * // Deprecated Constructor
156
- * const table = new Table({
157
- * name: 'users',
158
- * columns: [
159
- * new Column({ name: 'name', type: ColumnType.TEXT }),
160
- * new Column({ name: 'age', type: ColumnType.INTEGER })
161
- * ]
162
- * });
163
- *```
164
- */
165
- constructor(columns: Columns, options?: TableV2Options);
166
- /**
167
- * @deprecated This constructor will be removed in the next major release.
168
- * Use the new constructor shown below instead as this does not show types.
169
- * @example
170
- * <caption>Use this instead</caption>
171
- * ```javascript
172
- * const table = new Table(
173
- * {
174
- * name: column.text,
175
- * age: column.integer
176
- * },
177
- * { indexes: { nameIndex: ['name'] } }
178
- * );
179
- *```
180
- */
181
- constructor(options: TableOptions);
182
- constructor(optionsOrColumns: Columns | TableOptions, v2Options?: TableV2Options) {
183
- if (this.isTableV1(optionsOrColumns)) {
184
- this.initTableV1(optionsOrColumns);
185
- } else {
186
- this.initTableV2(optionsOrColumns, v2Options);
187
- }
188
- }
189
-
190
- copyWithName(name: string): Table {
191
- return new Table({
192
- ...this.options,
193
- name
194
- });
195
- }
196
-
197
- private isTableV1(arg: TableOptions | Columns): arg is TableOptions {
198
- return 'columns' in arg && Array.isArray(arg.columns);
199
- }
200
-
201
- private initTableV1(options: TableOptions) {
202
- this.options = {
203
- ...options,
204
- indexes: options.indexes || []
205
- };
206
- this.applyDefaultOptions();
207
- }
208
-
209
- private initTableV2(columns: Columns, options?: TableV2Options) {
210
- const convertedColumns = Object.entries(columns).map(
211
- ([name, columnInfo]) => new Column({ name, type: columnInfo.type })
212
- );
213
-
214
- const convertedIndexes = Object.entries(options?.indexes ?? {}).map(
215
- ([name, columnNames]) =>
216
- new Index({
217
- name,
218
- columns: columnNames.map(
219
- (name) =>
220
- new IndexedColumn({
221
- name: name.replace(/^-/, ''),
222
- ascending: !name.startsWith('-')
223
- })
224
- )
225
- })
226
- );
227
-
228
- this.options = {
229
- name: '',
230
- columns: convertedColumns,
231
- indexes: convertedIndexes,
232
- viewName: options?.viewName,
233
- insertOnly: options?.insertOnly,
234
- localOnly: options?.localOnly,
235
- trackPrevious: options?.trackPrevious,
236
- trackMetadata: options?.trackMetadata,
237
- ignoreEmptyUpdates: options?.ignoreEmptyUpdates
238
- };
93
+ export class ResolvedTable {
94
+ constructor(readonly options: ResolvedTableOptions) {
239
95
  this.applyDefaultOptions();
240
-
241
- this._mappedColumns = columns;
242
- }
243
-
244
- private applyDefaultOptions() {
245
- this.options.insertOnly ??= DEFAULT_TABLE_OPTIONS.insertOnly;
246
- this.options.localOnly ??= DEFAULT_TABLE_OPTIONS.localOnly;
247
- this.options.trackPrevious ??= DEFAULT_TABLE_OPTIONS.trackPrevious;
248
- this.options.trackMetadata ??= DEFAULT_TABLE_OPTIONS.trackMetadata;
249
- this.options.ignoreEmptyUpdates ??= DEFAULT_TABLE_OPTIONS.ignoreEmptyUpdates;
250
96
  }
251
97
 
252
98
  get name() {
@@ -265,16 +111,6 @@ export class Table<Columns extends ColumnsType = ColumnsType> {
265
111
  return this.options.columns;
266
112
  }
267
113
 
268
- get columnMap(): Columns {
269
- return (
270
- this._mappedColumns ??
271
- this.columns.reduce((hash: Record<string, BaseColumnType<any>>, column) => {
272
- hash[column.name] = { type: column.type ?? ColumnType.TEXT };
273
- return hash;
274
- }, {} as Columns)
275
- );
276
- }
277
-
278
114
  get indexes() {
279
115
  return this.options.indexes ?? [];
280
116
  }
@@ -381,4 +217,93 @@ export class Table<Columns extends ColumnsType = ColumnsType> {
381
217
  ...encodeTableOptions(this)
382
218
  };
383
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
+ }
384
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
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';
@@ -18,7 +18,7 @@ export interface BaseObserverInterface<T extends BaseListener> {
18
18
  }
19
19
 
20
20
  /**
21
- * @internal
21
+ * @public
22
22
  */
23
23
  export class BaseObserver<T extends BaseListener = BaseListener> implements BaseObserverInterface<T> {
24
24
  protected listeners = new Set<Partial<T>>();
@@ -1,56 +1,102 @@
1
- import Logger, { type ILogger, type ILogLevel } from 'js-logger';
2
-
3
- export { GlobalLogger, ILogger, ILoggerOpts, ILogHandler, ILogLevel } from 'js-logger';
4
-
5
- const TypedLogger: ILogger = Logger as any;
6
-
7
1
  /**
8
2
  * @public
9
3
  */
10
- export const LogLevel = {
11
- TRACE: TypedLogger.TRACE,
12
- DEBUG: TypedLogger.DEBUG,
13
- INFO: TypedLogger.INFO,
14
- TIME: TypedLogger.TIME,
15
- WARN: TypedLogger.WARN,
16
- ERROR: TypedLogger.ERROR,
17
- OFF: TypedLogger.OFF
18
- };
4
+ export const LogLevels = {
5
+ trace: 10,
6
+ debug: 20,
7
+ info: 30,
8
+ warn: 40,
9
+ error: 50
10
+ } as const;
19
11
 
20
12
  /**
13
+ * A log record passed to a {@link PowerSyncLogger}.
14
+ *
21
15
  * @public
22
16
  */
23
- export interface CreateLoggerOptions {
24
- logLevel?: ILogLevel;
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;
23
+
24
+ /**
25
+ * The main message to log.
26
+ */
27
+ message: string;
28
+
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;
25
36
  }
26
37
 
27
38
  /**
28
- * Retrieves the base (default) logger instance.
39
+ * A logger used by the PowerSync SDK.
29
40
  *
30
- * This base logger controls the default logging configuration and is shared
31
- * across all loggers created with `createLogger`. Adjusting settings on this
32
- * base logger affects all loggers derived from it unless explicitly overridden.
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.
44
+ *
45
+ * By default, the SDK uses a {@link createConsoleLogger} instance forwarding messages to `console.log`.
33
46
  *
34
47
  * @public
35
48
  */
36
- export function createBaseLogger() {
37
- return Logger;
49
+ export interface PowerSyncLogger {
50
+ log(record: LogRecord): void;
38
51
  }
39
52
 
40
53
  /**
41
- * Creates and configures a new named logger based on the base logger.
42
- *
43
- * Named loggers allow specific modules or areas of your application to have
44
- * their own logging levels and behaviors. These loggers inherit configuration
45
- * from the base logger by default but can override settings independently.
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`.
46
72
  *
73
+ * @param options - Options to configure a minimum severity of the logger or a prefix to make messages more recognizable.
47
74
  * @public
48
75
  */
49
- export function createLogger(name: string, options: CreateLoggerOptions = {}): ILogger {
50
- const logger = Logger.get(name);
51
- if (options.logLevel) {
52
- logger.setLevel(options.logLevel);
53
- }
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
+ }
54
93
 
55
- return logger;
94
+ const messageWithPrefix = `[${prefix}]: ${message}`;
95
+ if (error) {
96
+ emitter(messageWithPrefix, error);
97
+ } else {
98
+ emitter(messageWithPrefix);
99
+ }
100
+ }
101
+ };
56
102
  }
@@ -1,7 +1,9 @@
1
- import { BaseListener, BaseObserver, BaseObserverInterface } from './BaseObserver.js';
1
+ import { BaseListener, BaseObserverInterface } from './BaseObserver.js';
2
2
 
3
3
  /**
4
4
  * Represents the counts of listeners for each event type in a BaseListener.
5
+ *
6
+ * @public
5
7
  */
6
8
  export type ListenerCounts<Listener extends BaseListener> = Partial<Record<keyof Listener, number>> & {
7
9
  total: number;
@@ -9,73 +11,25 @@ export type ListenerCounts<Listener extends BaseListener> = Partial<Record<keyof
9
11
 
10
12
  /**
11
13
  * Meta listener which reports the counts of listeners for each event type.
14
+ *
15
+ * @public
12
16
  */
13
17
  export interface MetaListener<ParentListener extends BaseListener> extends BaseListener {
14
18
  listenersChanged?: (counts: ListenerCounts<ParentListener>) => void;
15
19
  }
16
20
 
17
- export interface ListenerMetaManager<Listener extends BaseListener>
18
- extends BaseObserverInterface<MetaListener<Listener>> {
21
+ /**
22
+ * @public
23
+ */
24
+ export interface ListenerMetaManager<Listener extends BaseListener> extends BaseObserverInterface<
25
+ MetaListener<Listener>
26
+ > {
19
27
  counts: ListenerCounts<Listener>;
20
28
  }
21
29
 
22
- export interface MetaBaseObserverInterface<Listener extends BaseListener> extends BaseObserverInterface<Listener> {
23
- listenerMeta: ListenerMetaManager<Listener>;
24
- }
25
-
26
30
  /**
27
- * A BaseObserver that tracks the counts of listeners for each event type.
31
+ * @public
28
32
  */
29
- export class MetaBaseObserver<Listener extends BaseListener>
30
- extends BaseObserver<Listener>
31
- implements MetaBaseObserverInterface<Listener>
32
- {
33
- protected get listenerCounts(): ListenerCounts<Listener> {
34
- const counts = {} as Partial<Record<keyof Listener, number>>;
35
- let total = 0;
36
- for (const listener of this.listeners) {
37
- for (const key in listener) {
38
- if (listener[key]) {
39
- counts[key] = (counts[key] ?? 0) + 1;
40
- total++;
41
- }
42
- }
43
- }
44
- return {
45
- ...counts,
46
- total
47
- };
48
- }
49
-
50
- get listenerMeta(): ListenerMetaManager<Listener> {
51
- return {
52
- counts: this.listenerCounts,
53
- // Allows registering a meta listener that will be notified of changes in listener counts
54
- registerListener: (listener: Partial<MetaListener<Listener>>) => {
55
- return this.metaListener.registerListener(listener);
56
- }
57
- };
58
- }
59
-
60
- protected metaListener: BaseObserver<MetaListener<Listener>>;
61
-
62
- constructor() {
63
- super();
64
- this.metaListener = new BaseObserver<MetaListener<Listener>>();
65
- }
66
-
67
- registerListener(listener: Partial<Listener>): () => void {
68
- const dispose = super.registerListener(listener);
69
- const updatedCount = this.listenerCounts;
70
- this.metaListener.iterateListeners((l) => {
71
- l.listenersChanged?.(updatedCount);
72
- });
73
- return () => {
74
- dispose();
75
- const updatedCount = this.listenerCounts;
76
- this.metaListener.iterateListeners((l) => {
77
- l.listenersChanged?.(updatedCount);
78
- });
79
- };
80
- }
33
+ export interface MetaBaseObserverInterface<Listener extends BaseListener> extends BaseObserverInterface<Listener> {
34
+ listenerMeta: ListenerMetaManager<Listener>;
81
35
  }