@powersync/common 1.57.3 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (229) 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 +85 -47
  5. package/lib/attachments/AttachmentQueue.js +55 -18
  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/AttachmentTransportAdapter.d.ts +47 -0
  11. package/lib/attachments/AttachmentTransportAdapter.js +2 -0
  12. package/lib/attachments/AttachmentTransportAdapter.js.map +1 -0
  13. package/lib/attachments/BufferedAttachmentTransport.d.ts +24 -0
  14. package/lib/attachments/BufferedAttachmentTransport.js +32 -0
  15. package/lib/attachments/BufferedAttachmentTransport.js.map +1 -0
  16. package/lib/attachments/LocalStorageAdapter.d.ts +19 -0
  17. package/lib/attachments/Schema.d.ts +2 -2
  18. package/lib/attachments/SyncingService.d.ts +8 -5
  19. package/lib/attachments/SyncingService.js +31 -15
  20. package/lib/attachments/SyncingService.js.map +1 -1
  21. package/lib/client/{AbstractPowerSyncDatabase.d.ts → CommonPowerSyncDatabase.d.ts} +56 -232
  22. package/lib/client/CommonPowerSyncDatabase.js +2 -0
  23. package/lib/client/CommonPowerSyncDatabase.js.map +1 -0
  24. package/lib/client/SQLOpenFactory.d.ts +35 -13
  25. package/lib/client/SQLOpenFactory.js +1 -25
  26. package/lib/client/SQLOpenFactory.js.map +1 -1
  27. package/lib/client/compilableQueryWatch.d.ts +2 -2
  28. package/lib/client/compilableQueryWatch.js +1 -2
  29. package/lib/client/compilableQueryWatch.js.map +1 -1
  30. package/lib/client/connection/PowerSyncBackendConnector.d.ts +3 -3
  31. package/lib/client/runOnSchemaChange.d.ts +2 -2
  32. package/lib/client/runOnSchemaChange.js.map +1 -1
  33. package/lib/client/sync/bucket/CrudEntry.d.ts +3 -34
  34. package/lib/client/sync/bucket/CrudEntry.js +0 -99
  35. package/lib/client/sync/bucket/CrudEntry.js.map +1 -1
  36. package/lib/client/sync/options.d.ts +65 -0
  37. package/lib/client/sync/options.js +25 -0
  38. package/lib/client/sync/options.js.map +1 -0
  39. package/lib/client/sync/sync-streams.d.ts +1 -1
  40. package/lib/client/triggers/TriggerManager.d.ts +9 -38
  41. package/lib/client/watched/GetAllQuery.d.ts +3 -3
  42. package/lib/client/watched/GetAllQuery.js +1 -1
  43. package/lib/client/watched/GetAllQuery.js.map +1 -1
  44. package/lib/client/watched/WatchedQuery.d.ts +2 -10
  45. package/lib/client/watched/WatchedQuery.js +0 -11
  46. package/lib/client/watched/WatchedQuery.js.map +1 -1
  47. package/lib/client/watched/processors/DifferentialQueryProcessor.d.ts +0 -50
  48. package/lib/client/watched/processors/DifferentialQueryProcessor.js +1 -176
  49. package/lib/client/watched/processors/DifferentialQueryProcessor.js.map +1 -1
  50. package/lib/client/watched/processors/OnChangeQueryProcessor.d.ts +0 -22
  51. package/lib/client/watched/processors/OnChangeQueryProcessor.js +1 -82
  52. package/lib/client/watched/processors/OnChangeQueryProcessor.js.map +1 -1
  53. package/lib/db/DBAdapter.d.ts +72 -154
  54. package/lib/db/DBAdapter.js +70 -108
  55. package/lib/db/DBAdapter.js.map +1 -1
  56. package/lib/db/QueryResult.d.ts +104 -0
  57. package/lib/db/QueryResult.js +96 -0
  58. package/lib/db/QueryResult.js.map +1 -0
  59. package/lib/db/crud/SyncProgress.d.ts +1 -13
  60. package/lib/db/crud/SyncProgress.js +1 -62
  61. package/lib/db/crud/SyncProgress.js.map +1 -1
  62. package/lib/db/crud/SyncStatus.d.ts +32 -66
  63. package/lib/db/crud/SyncStatus.js +1 -253
  64. package/lib/db/crud/SyncStatus.js.map +1 -1
  65. package/lib/db/schema/Column.d.ts +0 -7
  66. package/lib/db/schema/Column.js +0 -7
  67. package/lib/db/schema/Column.js.map +1 -1
  68. package/lib/db/schema/Index.d.ts +2 -6
  69. package/lib/db/schema/Index.js +1 -4
  70. package/lib/db/schema/Index.js.map +1 -1
  71. package/lib/db/schema/IndexedColumn.d.ts +2 -6
  72. package/lib/db/schema/IndexedColumn.js +1 -4
  73. package/lib/db/schema/IndexedColumn.js.map +1 -1
  74. package/lib/db/schema/Schema.d.ts +4 -28
  75. package/lib/db/schema/Schema.js.map +1 -1
  76. package/lib/db/schema/Table.d.ts +50 -96
  77. package/lib/db/schema/Table.js +83 -91
  78. package/lib/db/schema/Table.js.map +1 -1
  79. package/lib/index.d.ts +6 -21
  80. package/lib/index.js +6 -22
  81. package/lib/index.js.map +1 -1
  82. package/lib/utils/BaseObserver.d.ts +1 -1
  83. package/lib/utils/BaseObserver.js +1 -1
  84. package/lib/utils/Logger.d.ts +52 -23
  85. package/lib/utils/Logger.js +35 -33
  86. package/lib/utils/Logger.js.map +1 -1
  87. package/lib/utils/MetaBaseObserver.d.ts +11 -11
  88. package/lib/utils/MetaBaseObserver.js +1 -50
  89. package/lib/utils/MetaBaseObserver.js.map +1 -1
  90. package/lib/utils/mutex.d.ts +3 -53
  91. package/lib/utils/mutex.js +1 -156
  92. package/lib/utils/mutex.js.map +1 -1
  93. package/package.json +8 -45
  94. package/src/attachments/AttachmentContext.ts +11 -10
  95. package/src/attachments/AttachmentQueue.ts +129 -70
  96. package/src/attachments/AttachmentService.ts +7 -6
  97. package/src/attachments/AttachmentTransportAdapter.ts +49 -0
  98. package/src/attachments/BufferedAttachmentTransport.ts +37 -0
  99. package/src/attachments/LocalStorageAdapter.ts +20 -0
  100. package/src/attachments/Schema.ts +2 -2
  101. package/src/attachments/SyncingService.ts +34 -20
  102. package/src/client/CommonPowerSyncDatabase.ts +540 -0
  103. package/src/client/SQLOpenFactory.ts +38 -22
  104. package/src/client/compilableQueryWatch.ts +3 -4
  105. package/src/client/connection/PowerSyncBackendConnector.ts +3 -3
  106. package/src/client/runOnSchemaChange.ts +2 -2
  107. package/src/client/sync/bucket/CrudEntry.ts +4 -104
  108. package/src/client/sync/options.ts +77 -0
  109. package/src/client/sync/sync-streams.ts +1 -1
  110. package/src/client/triggers/TriggerManager.ts +10 -41
  111. package/src/client/watched/GetAllQuery.ts +3 -3
  112. package/src/client/watched/WatchedQuery.ts +2 -15
  113. package/src/client/watched/processors/DifferentialQueryProcessor.ts +0 -223
  114. package/src/client/watched/processors/OnChangeQueryProcessor.ts +0 -111
  115. package/src/db/DBAdapter.ts +141 -226
  116. package/src/db/QueryResult.ts +195 -0
  117. package/src/db/crud/SyncProgress.ts +2 -42
  118. package/src/db/crud/SyncStatus.ts +45 -220
  119. package/src/db/schema/Column.ts +0 -8
  120. package/src/db/schema/Index.ts +3 -6
  121. package/src/db/schema/IndexedColumn.ts +3 -6
  122. package/src/db/schema/Schema.ts +4 -4
  123. package/src/db/schema/Table.ts +111 -186
  124. package/src/index.ts +6 -21
  125. package/src/utils/BaseObserver.ts +1 -1
  126. package/src/utils/Logger.ts +80 -42
  127. package/src/utils/MetaBaseObserver.ts +14 -60
  128. package/src/utils/mutex.ts +4 -201
  129. package/dist/bundle.cjs +0 -14398
  130. package/dist/bundle.cjs.map +0 -1
  131. package/dist/bundle.mjs +0 -14317
  132. package/dist/bundle.mjs.map +0 -1
  133. package/dist/bundle.node.cjs +0 -12029
  134. package/dist/bundle.node.cjs.map +0 -1
  135. package/dist/bundle.node.mjs +0 -11948
  136. package/dist/bundle.node.mjs.map +0 -1
  137. package/dist/index.d.cts +0 -4651
  138. package/legacy/sync_protocol.d.ts +0 -103
  139. package/lib/client/AbstractPowerSyncDatabase.js +0 -990
  140. package/lib/client/AbstractPowerSyncDatabase.js.map +0 -1
  141. package/lib/client/AbstractPowerSyncOpenFactory.d.ts +0 -28
  142. package/lib/client/AbstractPowerSyncOpenFactory.js +0 -28
  143. package/lib/client/AbstractPowerSyncOpenFactory.js.map +0 -1
  144. package/lib/client/ConnectionManager.d.ts +0 -115
  145. package/lib/client/ConnectionManager.js +0 -294
  146. package/lib/client/ConnectionManager.js.map +0 -1
  147. package/lib/client/CustomQuery.d.ts +0 -22
  148. package/lib/client/CustomQuery.js +0 -43
  149. package/lib/client/CustomQuery.js.map +0 -1
  150. package/lib/client/constants.d.ts +0 -4
  151. package/lib/client/constants.js +0 -5
  152. package/lib/client/constants.js.map +0 -1
  153. package/lib/client/sync/bucket/BucketStorageAdapter.d.ts +0 -56
  154. package/lib/client/sync/bucket/BucketStorageAdapter.js +0 -29
  155. package/lib/client/sync/bucket/BucketStorageAdapter.js.map +0 -1
  156. package/lib/client/sync/bucket/SqliteBucketStorage.d.ts +0 -37
  157. package/lib/client/sync/bucket/SqliteBucketStorage.js +0 -165
  158. package/lib/client/sync/bucket/SqliteBucketStorage.js.map +0 -1
  159. package/lib/client/sync/stream/AbstractRemote.d.ts +0 -180
  160. package/lib/client/sync/stream/AbstractRemote.js +0 -508
  161. package/lib/client/sync/stream/AbstractRemote.js.map +0 -1
  162. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +0 -265
  163. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +0 -706
  164. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +0 -1
  165. package/lib/client/sync/stream/WebsocketClientTransport.d.ts +0 -15
  166. package/lib/client/sync/stream/WebsocketClientTransport.js +0 -62
  167. package/lib/client/sync/stream/WebsocketClientTransport.js.map +0 -1
  168. package/lib/client/sync/stream/core-instruction.d.ts +0 -77
  169. package/lib/client/sync/stream/core-instruction.js +0 -30
  170. package/lib/client/sync/stream/core-instruction.js.map +0 -1
  171. package/lib/client/triggers/MemoryTriggerClaimManager.d.ts +0 -6
  172. package/lib/client/triggers/MemoryTriggerClaimManager.js +0 -21
  173. package/lib/client/triggers/MemoryTriggerClaimManager.js.map +0 -1
  174. package/lib/client/triggers/TriggerManagerImpl.d.ts +0 -39
  175. package/lib/client/triggers/TriggerManagerImpl.js +0 -410
  176. package/lib/client/triggers/TriggerManagerImpl.js.map +0 -1
  177. package/lib/client/watched/processors/AbstractQueryProcessor.d.ts +0 -68
  178. package/lib/client/watched/processors/AbstractQueryProcessor.js +0 -151
  179. package/lib/client/watched/processors/AbstractQueryProcessor.js.map +0 -1
  180. package/lib/db/ConnectionClosedError.d.ts +0 -12
  181. package/lib/db/ConnectionClosedError.js +0 -23
  182. package/lib/db/ConnectionClosedError.js.map +0 -1
  183. package/lib/db/schema/TableV2.d.ts +0 -11
  184. package/lib/db/schema/TableV2.js +0 -11
  185. package/lib/db/schema/TableV2.js.map +0 -1
  186. package/lib/utils/AbortOperation.d.ts +0 -11
  187. package/lib/utils/AbortOperation.js +0 -21
  188. package/lib/utils/AbortOperation.js.map +0 -1
  189. package/lib/utils/ControlledExecutor.d.ts +0 -31
  190. package/lib/utils/ControlledExecutor.js +0 -58
  191. package/lib/utils/ControlledExecutor.js.map +0 -1
  192. package/lib/utils/async.d.ts +0 -46
  193. package/lib/utils/async.js +0 -147
  194. package/lib/utils/async.js.map +0 -1
  195. package/lib/utils/compatibility.d.ts +0 -8
  196. package/lib/utils/compatibility.js +0 -9
  197. package/lib/utils/compatibility.js.map +0 -1
  198. package/lib/utils/parseQuery.d.ts +0 -12
  199. package/lib/utils/parseQuery.js +0 -20
  200. package/lib/utils/parseQuery.js.map +0 -1
  201. package/lib/utils/queue.d.ts +0 -16
  202. package/lib/utils/queue.js +0 -42
  203. package/lib/utils/queue.js.map +0 -1
  204. package/lib/utils/stream_transform.d.ts +0 -41
  205. package/lib/utils/stream_transform.js +0 -211
  206. package/lib/utils/stream_transform.js.map +0 -1
  207. package/src/client/AbstractPowerSyncDatabase.ts +0 -1408
  208. package/src/client/AbstractPowerSyncOpenFactory.ts +0 -45
  209. package/src/client/ConnectionManager.ts +0 -405
  210. package/src/client/CustomQuery.ts +0 -56
  211. package/src/client/constants.ts +0 -4
  212. package/src/client/sync/bucket/BucketStorageAdapter.ts +0 -65
  213. package/src/client/sync/bucket/SqliteBucketStorage.ts +0 -202
  214. package/src/client/sync/stream/AbstractRemote.ts +0 -648
  215. package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +0 -971
  216. package/src/client/sync/stream/WebsocketClientTransport.ts +0 -82
  217. package/src/client/sync/stream/core-instruction.ts +0 -109
  218. package/src/client/triggers/MemoryTriggerClaimManager.ts +0 -25
  219. package/src/client/triggers/TriggerManagerImpl.ts +0 -501
  220. package/src/client/watched/processors/AbstractQueryProcessor.ts +0 -226
  221. package/src/db/ConnectionClosedError.ts +0 -25
  222. package/src/db/schema/TableV2.ts +0 -11
  223. package/src/utils/AbortOperation.ts +0 -19
  224. package/src/utils/ControlledExecutor.ts +0 -81
  225. package/src/utils/async.ts +0 -185
  226. package/src/utils/compatibility.ts +0 -9
  227. package/src/utils/parseQuery.ts +0 -31
  228. package/src/utils/queue.ts +0 -48
  229. 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,48 @@
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';
4
+ export * from './attachments/AttachmentTransportAdapter.js';
5
5
  export * from './attachments/LocalStorageAdapter.js';
6
6
  export * from './attachments/RemoteStorageAdapter.js';
7
7
  export * from './attachments/Schema.js';
8
- export * from './attachments/SyncingService.js';
9
8
  export * from './attachments/WatchedAttachmentItem.js';
10
9
 
11
- export * from './client/AbstractPowerSyncDatabase.js';
12
- export * from './client/AbstractPowerSyncOpenFactory.js';
10
+ export * from './client/CommonPowerSyncDatabase.js';
13
11
  export { compilableQueryWatch, CompilableQueryWatchHandler } from './client/compilableQueryWatch.js';
14
12
  export * from './client/connection/PowerSyncBackendConnector.js';
15
13
  export * from './client/connection/PowerSyncCredentials.js';
16
- export { MAX_OP_ID } from './client/constants.js';
17
- export { runOnSchemaChange } from './client/runOnSchemaChange.js';
18
14
  export * from './client/SQLOpenFactory.js';
19
- export * from './client/sync/bucket/BucketStorageAdapter.js';
20
15
  export * from './client/sync/bucket/CrudBatch.js';
21
16
  export { CrudEntry, OpId, UpdateType } from './client/sync/bucket/CrudEntry.js';
22
17
  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
18
  export * from './client/sync/stream/JsonValue.js';
27
19
  export * from './client/sync/sync-streams.js';
20
+ export { SyncOptions, SyncStreamConnectionMethod, FetchStrategy } from './client/sync/options.js';
28
21
 
29
- export * from './client/ConnectionManager.js';
30
- export * from './db/ConnectionClosedError.js';
31
22
  export { ProgressWithOperations, SyncProgress } from './db/crud/SyncProgress.js';
32
23
  export * from './db/crud/SyncStatus.js';
33
24
  export * from './db/crud/UploadQueueStatus.js';
34
25
  export * from './db/DBAdapter.js';
26
+ export * from './db/QueryResult.js';
35
27
  export * from './db/schema/Column.js';
36
28
  export * from './db/schema/Index.js';
37
29
  export * from './db/schema/IndexedColumn.js';
38
30
  export { PendingStatement, PendingStatementParameter, RawTableType } from './db/schema/RawTable.js';
39
31
  export * from './db/schema/Schema.js';
40
32
  export * from './db/schema/Table.js';
41
- export * from './db/schema/TableV2.js';
42
33
 
43
34
  export * from './client/Query.js';
44
- export { MEMORY_TRIGGER_CLAIM_MANAGER } from './client/triggers/MemoryTriggerClaimManager.js';
45
35
  export * from './client/triggers/sanitizeSQL.js';
46
36
  export * from './client/triggers/TriggerManager.js';
47
- export { TriggerManagerImpl } from './client/triggers/TriggerManagerImpl.js';
48
37
  export * from './client/watched/GetAllQuery.js';
49
- export * from './client/watched/processors/AbstractQueryProcessor.js';
50
38
  export * from './client/watched/processors/comparators.js';
51
39
  export * from './client/watched/processors/DifferentialQueryProcessor.js';
52
40
  export * from './client/watched/processors/OnChangeQueryProcessor.js';
53
41
  export * from './client/watched/WatchedQuery.js';
54
42
 
55
- export * from './utils/AbortOperation.js';
43
+ export { type Mutex } from './utils/mutex.js';
56
44
  export * from './utils/BaseObserver.js';
57
- export * from './utils/ControlledExecutor.js';
45
+ export * from './utils/MetaBaseObserver.js';
58
46
  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
47
 
63
48
  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,64 +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: ILogLevel;
12
- DEBUG: ILogLevel;
13
- INFO: ILogLevel;
14
- TIME: ILogLevel;
15
- WARN: ILogLevel;
16
- ERROR: ILogLevel;
17
- OFF: ILogLevel;
18
- } = {
19
- TRACE: TypedLogger.TRACE,
20
- DEBUG: TypedLogger.DEBUG,
21
- INFO: TypedLogger.INFO,
22
- TIME: TypedLogger.TIME,
23
- WARN: TypedLogger.WARN,
24
- ERROR: TypedLogger.ERROR,
25
- OFF: TypedLogger.OFF
26
- };
4
+ export const LogLevels = {
5
+ trace: 10,
6
+ debug: 20,
7
+ info: 30,
8
+ warn: 40,
9
+ error: 50
10
+ } as const;
27
11
 
28
12
  /**
13
+ * A log record passed to a {@link PowerSyncLogger}.
14
+ *
29
15
  * @public
30
16
  */
31
- export interface CreateLoggerOptions {
32
- 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;
33
36
  }
34
37
 
35
38
  /**
36
- * Retrieves the base (default) logger instance.
39
+ * A logger used by the PowerSync SDK.
37
40
  *
38
- * This base logger controls the default logging configuration and is shared
39
- * across all loggers created with `createLogger`. Adjusting settings on this
40
- * 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`.
41
46
  *
42
47
  * @public
43
48
  */
44
- export function createBaseLogger() {
45
- return Logger;
49
+ export interface PowerSyncLogger {
50
+ log(record: LogRecord): void;
46
51
  }
47
52
 
48
53
  /**
49
- * Creates and configures a new named logger based on the base logger.
50
- *
51
- * Named loggers allow specific modules or areas of your application to have
52
- * their own logging levels and behaviors. These loggers inherit configuration
53
- * 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`.
54
72
  *
73
+ * @param options - Options to configure a minimum severity of the logger or a prefix to make messages more recognizable.
55
74
  * @public
56
75
  */
57
- export function createLogger(name: string, options: CreateLoggerOptions = {}): ILogger {
58
- const logger = Logger.get(name);
59
- if (options.logLevel) {
60
- logger.setLevel(options.logLevel);
61
- }
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
+ }
62
93
 
63
- return logger;
94
+ const messageWithPrefix = `[${prefix}]: ${message}`;
95
+ if (error) {
96
+ emitter(messageWithPrefix, error);
97
+ } else {
98
+ emitter(messageWithPrefix);
99
+ }
100
+ }
101
+ };
64
102
  }