@powersync/common 0.0.0-dev-20240506092851
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.
- package/LICENSE +201 -0
- package/README.md +6 -0
- package/lib/client/AbstractPowerSyncDatabase.d.ts +367 -0
- package/lib/client/AbstractPowerSyncDatabase.js +644 -0
- package/lib/client/AbstractPowerSyncOpenFactory.d.ts +29 -0
- package/lib/client/AbstractPowerSyncOpenFactory.js +24 -0
- package/lib/client/connection/PowerSyncBackendConnector.d.ts +23 -0
- package/lib/client/connection/PowerSyncBackendConnector.js +1 -0
- package/lib/client/connection/PowerSyncCredentials.d.ts +5 -0
- package/lib/client/connection/PowerSyncCredentials.js +1 -0
- package/lib/client/sync/bucket/BucketStorageAdapter.d.ts +71 -0
- package/lib/client/sync/bucket/BucketStorageAdapter.js +7 -0
- package/lib/client/sync/bucket/CrudBatch.d.ts +31 -0
- package/lib/client/sync/bucket/CrudBatch.js +25 -0
- package/lib/client/sync/bucket/CrudEntry.d.ts +86 -0
- package/lib/client/sync/bucket/CrudEntry.js +84 -0
- package/lib/client/sync/bucket/CrudTransaction.d.ts +29 -0
- package/lib/client/sync/bucket/CrudTransaction.js +24 -0
- package/lib/client/sync/bucket/OpType.d.ts +16 -0
- package/lib/client/sync/bucket/OpType.js +22 -0
- package/lib/client/sync/bucket/OplogEntry.d.ts +23 -0
- package/lib/client/sync/bucket/OplogEntry.js +33 -0
- package/lib/client/sync/bucket/SqliteBucketStorage.d.ts +66 -0
- package/lib/client/sync/bucket/SqliteBucketStorage.js +298 -0
- package/lib/client/sync/bucket/SyncDataBatch.d.ts +6 -0
- package/lib/client/sync/bucket/SyncDataBatch.js +11 -0
- package/lib/client/sync/bucket/SyncDataBucket.d.ts +40 -0
- package/lib/client/sync/bucket/SyncDataBucket.js +40 -0
- package/lib/client/sync/stream/AbstractRemote.d.ts +25 -0
- package/lib/client/sync/stream/AbstractRemote.js +42 -0
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +101 -0
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +477 -0
- package/lib/client/sync/stream/streaming-sync-types.d.ts +116 -0
- package/lib/client/sync/stream/streaming-sync-types.js +22 -0
- package/lib/db/Column.d.ts +19 -0
- package/lib/db/Column.js +25 -0
- package/lib/db/DBAdapter.d.ts +95 -0
- package/lib/db/DBAdapter.js +19 -0
- package/lib/db/crud/SyncStatus.d.ts +38 -0
- package/lib/db/crud/SyncStatus.js +57 -0
- package/lib/db/crud/UploadQueueStatus.d.ts +20 -0
- package/lib/db/crud/UploadQueueStatus.js +24 -0
- package/lib/db/schema/Index.d.ts +22 -0
- package/lib/db/schema/Index.js +29 -0
- package/lib/db/schema/IndexedColumn.d.ts +19 -0
- package/lib/db/schema/IndexedColumn.js +29 -0
- package/lib/db/schema/Schema.d.ts +38 -0
- package/lib/db/schema/Schema.js +37 -0
- package/lib/db/schema/Table.d.ts +51 -0
- package/lib/db/schema/Table.js +113 -0
- package/lib/db/schema/TableV2.d.ts +30 -0
- package/lib/db/schema/TableV2.js +43 -0
- package/lib/index.d.ts +32 -0
- package/lib/index.js +32 -0
- package/lib/types/types.d.ts +8 -0
- package/lib/types/types.js +1 -0
- package/lib/utils/AbortOperation.d.ts +9 -0
- package/lib/utils/AbortOperation.js +18 -0
- package/lib/utils/BaseObserver.d.ts +20 -0
- package/lib/utils/BaseObserver.js +22 -0
- package/lib/utils/mutex.d.ts +7 -0
- package/lib/utils/mutex.js +28 -0
- package/lib/utils/parseQuery.d.ts +6 -0
- package/lib/utils/parseQuery.js +16 -0
- package/lib/utils/strings.d.ts +3 -0
- package/lib/utils/strings.js +9 -0
- package/package.json +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
# PowerSync SDK common JS
|
|
2
|
+
|
|
3
|
+
This package contains a TypeScript implementation of a PowerSync database connector and streaming sync bucket implementation, which is used in the following SDKs:
|
|
4
|
+
|
|
5
|
+
- [packages/react-native](./packages/react-native/README.md)
|
|
6
|
+
- [packages/web](./packages/web/README.md)
|
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Mutex } from 'async-mutex';
|
|
3
|
+
import { ILogger } from 'js-logger';
|
|
4
|
+
import { DBAdapter, QueryResult, Transaction } from '../db/DBAdapter';
|
|
5
|
+
import { SyncStatus } from '../db/crud/SyncStatus';
|
|
6
|
+
import { UploadQueueStats } from '../db/crud/UploadQueueStatus';
|
|
7
|
+
import { Schema } from '../db/schema/Schema';
|
|
8
|
+
import { BaseObserver } from '../utils/BaseObserver';
|
|
9
|
+
import { PowerSyncBackendConnector } from './connection/PowerSyncBackendConnector';
|
|
10
|
+
import { BucketStorageAdapter } from './sync/bucket/BucketStorageAdapter';
|
|
11
|
+
import { CrudBatch } from './sync/bucket/CrudBatch';
|
|
12
|
+
import { CrudTransaction } from './sync/bucket/CrudTransaction';
|
|
13
|
+
import { AbstractStreamingSyncImplementation, StreamingSyncImplementationListener, StreamingSyncImplementation } from './sync/stream/AbstractStreamingSyncImplementation';
|
|
14
|
+
export interface DisconnectAndClearOptions {
|
|
15
|
+
/** When set to false, data in local-only tables is preserved. */
|
|
16
|
+
clearLocal?: boolean;
|
|
17
|
+
}
|
|
18
|
+
export interface PowerSyncDatabaseOptions {
|
|
19
|
+
/** Schema used for the local database. */
|
|
20
|
+
schema: Schema;
|
|
21
|
+
database: DBAdapter;
|
|
22
|
+
/**
|
|
23
|
+
* Delay for retrying sync streaming operations
|
|
24
|
+
* from the PowerSync backend after an error occurs.
|
|
25
|
+
*/
|
|
26
|
+
retryDelay?: number;
|
|
27
|
+
/**
|
|
28
|
+
* Backend Connector CRUD operations are throttled
|
|
29
|
+
* to occur at most every `crudUploadThrottleMs`
|
|
30
|
+
* milliseconds.
|
|
31
|
+
*/
|
|
32
|
+
crudUploadThrottleMs?: number;
|
|
33
|
+
logger?: ILogger;
|
|
34
|
+
}
|
|
35
|
+
export interface SQLWatchOptions {
|
|
36
|
+
signal?: AbortSignal;
|
|
37
|
+
tables?: string[];
|
|
38
|
+
/** The minimum interval between queries. */
|
|
39
|
+
throttleMs?: number;
|
|
40
|
+
/**
|
|
41
|
+
* Allows for watching any SQL table
|
|
42
|
+
* by not removing PowerSync table name prefixes
|
|
43
|
+
*/
|
|
44
|
+
rawTableNames?: boolean;
|
|
45
|
+
}
|
|
46
|
+
export interface WatchOnChangeEvent {
|
|
47
|
+
changedTables: string[];
|
|
48
|
+
}
|
|
49
|
+
export interface WatchHandler {
|
|
50
|
+
onResult: (results: QueryResult) => void;
|
|
51
|
+
onError?: (error: Error) => void;
|
|
52
|
+
}
|
|
53
|
+
export interface WatchOnChangeHandler {
|
|
54
|
+
onChange: (event: WatchOnChangeEvent) => void;
|
|
55
|
+
onError?: (error: Error) => void;
|
|
56
|
+
}
|
|
57
|
+
export interface PowerSyncDBListener extends StreamingSyncImplementationListener {
|
|
58
|
+
initialized: () => void;
|
|
59
|
+
}
|
|
60
|
+
export interface PowerSyncCloseOptions {
|
|
61
|
+
/**
|
|
62
|
+
* Disconnect the sync stream client if connected.
|
|
63
|
+
* This is usually true, but can be false for Web when using
|
|
64
|
+
* multiple tabs and a shared sync provider.
|
|
65
|
+
*/
|
|
66
|
+
disconnect?: boolean;
|
|
67
|
+
}
|
|
68
|
+
export declare const DEFAULT_POWERSYNC_CLOSE_OPTIONS: PowerSyncCloseOptions;
|
|
69
|
+
export declare const DEFAULT_WATCH_THROTTLE_MS = 30;
|
|
70
|
+
export declare const DEFAULT_POWERSYNC_DB_OPTIONS: {
|
|
71
|
+
retryDelay: number;
|
|
72
|
+
logger: ILogger;
|
|
73
|
+
crudUploadThrottleMs: number;
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* Requesting nested or recursive locks can block the application in some circumstances.
|
|
77
|
+
* This default lock timeout will act as a failsafe to throw an error if a lock cannot
|
|
78
|
+
* be obtained.
|
|
79
|
+
*/
|
|
80
|
+
export declare const DEFAULT_LOCK_TIMEOUT_MS = 120000;
|
|
81
|
+
export declare abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDBListener> {
|
|
82
|
+
protected options: PowerSyncDatabaseOptions;
|
|
83
|
+
/**
|
|
84
|
+
* Transactions should be queued in the DBAdapter, but we also want to prevent
|
|
85
|
+
* calls to `.execute` while an async transaction is running.
|
|
86
|
+
*/
|
|
87
|
+
protected static transactionMutex: Mutex;
|
|
88
|
+
/**
|
|
89
|
+
* Returns true if the connection is closed.
|
|
90
|
+
*/
|
|
91
|
+
closed: boolean;
|
|
92
|
+
ready: boolean;
|
|
93
|
+
/**
|
|
94
|
+
* Current connection status.
|
|
95
|
+
*/
|
|
96
|
+
currentStatus: SyncStatus;
|
|
97
|
+
syncStreamImplementation?: StreamingSyncImplementation;
|
|
98
|
+
sdkVersion: string;
|
|
99
|
+
protected bucketStorageAdapter: BucketStorageAdapter;
|
|
100
|
+
private syncStatusListenerDisposer?;
|
|
101
|
+
protected _isReadyPromise: Promise<void>;
|
|
102
|
+
private hasSyncedWatchDisposer?;
|
|
103
|
+
protected _schema: Schema;
|
|
104
|
+
constructor(options: PowerSyncDatabaseOptions);
|
|
105
|
+
/**
|
|
106
|
+
* Schema used for the local database.
|
|
107
|
+
*/
|
|
108
|
+
get schema(): Schema<{
|
|
109
|
+
[x: string]: import("..").TableV2<any>;
|
|
110
|
+
}>;
|
|
111
|
+
/**
|
|
112
|
+
* The underlying database.
|
|
113
|
+
*
|
|
114
|
+
* For the most part, behavior is the same whether querying on the underlying database, or on {@link AbstractPowerSyncDatabase}.
|
|
115
|
+
*/
|
|
116
|
+
get database(): DBAdapter;
|
|
117
|
+
/**
|
|
118
|
+
* Whether a connection to the PowerSync service is currently open.
|
|
119
|
+
*/
|
|
120
|
+
get connected(): boolean;
|
|
121
|
+
protected abstract generateSyncStreamImplementation(connector: PowerSyncBackendConnector): AbstractStreamingSyncImplementation;
|
|
122
|
+
protected abstract generateBucketStorageAdapter(): BucketStorageAdapter;
|
|
123
|
+
/**
|
|
124
|
+
* @returns A promise which will resolve once initialization is completed.
|
|
125
|
+
*/
|
|
126
|
+
waitForReady(): Promise<void>;
|
|
127
|
+
/**
|
|
128
|
+
* @returns A promise which will resolve once the first full sync has completed.
|
|
129
|
+
*/
|
|
130
|
+
waitForFirstSync(signal?: AbortSignal): Promise<void>;
|
|
131
|
+
/**
|
|
132
|
+
* Allows for extended implementations to execute custom initialization
|
|
133
|
+
* logic as part of the total init process
|
|
134
|
+
*/
|
|
135
|
+
abstract _initialize(): Promise<void>;
|
|
136
|
+
/**
|
|
137
|
+
* Entry point for executing initialization logic.
|
|
138
|
+
* This is to be automatically executed in the constructor.
|
|
139
|
+
*/
|
|
140
|
+
protected initialize(): Promise<void>;
|
|
141
|
+
protected updateHasSynced(): Promise<void>;
|
|
142
|
+
/**
|
|
143
|
+
* Replace the schema with a new version. This is for advanced use cases - typically the schema should just be specified once in the constructor.
|
|
144
|
+
*
|
|
145
|
+
* Cannot be used while connected - this should only be called before {@link AbstractPowerSyncDatabase.connect}.
|
|
146
|
+
*/
|
|
147
|
+
updateSchema(schema: Schema): Promise<void>;
|
|
148
|
+
/**
|
|
149
|
+
* Wait for initialization to complete.
|
|
150
|
+
* While initializing is automatic, this helps to catch and report initialization errors.
|
|
151
|
+
*/
|
|
152
|
+
init(): Promise<void>;
|
|
153
|
+
/**
|
|
154
|
+
* Connects to stream of events from the PowerSync instance.
|
|
155
|
+
*/
|
|
156
|
+
connect(connector: PowerSyncBackendConnector): Promise<void>;
|
|
157
|
+
/**
|
|
158
|
+
* Close the sync connection.
|
|
159
|
+
*
|
|
160
|
+
* Use {@link connect} to connect again.
|
|
161
|
+
*/
|
|
162
|
+
disconnect(): Promise<void>;
|
|
163
|
+
/**
|
|
164
|
+
* Disconnect and clear the database.
|
|
165
|
+
* Use this when logging out.
|
|
166
|
+
* The database can still be queried after this is called, but the tables
|
|
167
|
+
* would be empty.
|
|
168
|
+
*
|
|
169
|
+
* To preserve data in local-only tables, set clearLocal to false.
|
|
170
|
+
*/
|
|
171
|
+
disconnectAndClear(options?: DisconnectAndClearOptions): Promise<void>;
|
|
172
|
+
/**
|
|
173
|
+
* Close the database, releasing resources.
|
|
174
|
+
*
|
|
175
|
+
* Also disconnects any active connection.
|
|
176
|
+
*
|
|
177
|
+
* Once close is called, this connection cannot be used again - a new one
|
|
178
|
+
* must be constructed.
|
|
179
|
+
*/
|
|
180
|
+
close(options?: PowerSyncCloseOptions): Promise<void>;
|
|
181
|
+
/**
|
|
182
|
+
* Get upload queue size estimate and count.
|
|
183
|
+
*/
|
|
184
|
+
getUploadQueueStats(includeSize?: boolean): Promise<UploadQueueStats>;
|
|
185
|
+
/**
|
|
186
|
+
* Get a batch of crud data to upload.
|
|
187
|
+
*
|
|
188
|
+
* Returns null if there is no data to upload.
|
|
189
|
+
*
|
|
190
|
+
* Use this from the {@link PowerSyncBackendConnector.uploadData} callback.
|
|
191
|
+
*
|
|
192
|
+
* Once the data have been successfully uploaded, call {@link CrudBatch.complete} before
|
|
193
|
+
* requesting the next batch.
|
|
194
|
+
*
|
|
195
|
+
* Use {@link limit} to specify the maximum number of updates to return in a single
|
|
196
|
+
* batch.
|
|
197
|
+
*
|
|
198
|
+
* This method does include transaction ids in the result, but does not group
|
|
199
|
+
* data by transaction. One batch may contain data from multiple transactions,
|
|
200
|
+
* and a single transaction may be split over multiple batches.
|
|
201
|
+
*/
|
|
202
|
+
getCrudBatch(limit: number): Promise<CrudBatch | null>;
|
|
203
|
+
/**
|
|
204
|
+
* Get the next recorded transaction to upload.
|
|
205
|
+
*
|
|
206
|
+
* Returns null if there is no data to upload.
|
|
207
|
+
*
|
|
208
|
+
* Use this from the {@link PowerSyncBackendConnector.uploadData} callback.
|
|
209
|
+
*
|
|
210
|
+
* Once the data have been successfully uploaded, call {@link CrudTransaction.complete} before
|
|
211
|
+
* requesting the next transaction.
|
|
212
|
+
*
|
|
213
|
+
* Unlike {@link getCrudBatch}, this only returns data from a single transaction at a time.
|
|
214
|
+
* All data for the transaction is loaded into memory.
|
|
215
|
+
*/
|
|
216
|
+
getNextCrudTransaction(): Promise<CrudTransaction | null>;
|
|
217
|
+
private handleCrudCheckpoint;
|
|
218
|
+
/**
|
|
219
|
+
* Execute a write (INSERT/UPDATE/DELETE) query
|
|
220
|
+
* and optionally return results.
|
|
221
|
+
*/
|
|
222
|
+
execute(sql: string, parameters?: any[]): Promise<QueryResult>;
|
|
223
|
+
/**
|
|
224
|
+
* Execute a write query (INSERT/UPDATE/DELETE) multiple times with each parameter set
|
|
225
|
+
* and optionally return results.
|
|
226
|
+
* This is faster than executing separately with each parameter set.
|
|
227
|
+
*/
|
|
228
|
+
executeBatch(sql: string, parameters?: any[][]): Promise<QueryResult>;
|
|
229
|
+
/**
|
|
230
|
+
* Execute a read-only query and return results.
|
|
231
|
+
*/
|
|
232
|
+
getAll<T>(sql: string, parameters?: any[]): Promise<T[]>;
|
|
233
|
+
/**
|
|
234
|
+
* Execute a read-only query and return the first result, or null if the ResultSet is empty.
|
|
235
|
+
*/
|
|
236
|
+
getOptional<T>(sql: string, parameters?: any[]): Promise<T | null>;
|
|
237
|
+
/**
|
|
238
|
+
* Execute a read-only query and return the first result, error if the ResultSet is empty.
|
|
239
|
+
*/
|
|
240
|
+
get<T>(sql: string, parameters?: any[]): Promise<T>;
|
|
241
|
+
/**
|
|
242
|
+
* Takes a read lock, without starting a transaction.
|
|
243
|
+
* In most cases, {@link readTransaction} should be used instead.
|
|
244
|
+
*/
|
|
245
|
+
readLock<T>(callback: (db: DBAdapter) => Promise<T>): Promise<T>;
|
|
246
|
+
/**
|
|
247
|
+
* Takes a global lock, without starting a transaction.
|
|
248
|
+
* In most cases, {@link writeTransaction} should be used instead.
|
|
249
|
+
*/
|
|
250
|
+
writeLock<T>(callback: (db: DBAdapter) => Promise<T>): Promise<T>;
|
|
251
|
+
/**
|
|
252
|
+
* Open a read-only transaction.
|
|
253
|
+
* Read transactions can run concurrently to a write transaction.
|
|
254
|
+
* Changes from any write transaction are not visible to read transactions started before it.
|
|
255
|
+
*/
|
|
256
|
+
readTransaction<T>(callback: (tx: Transaction) => Promise<T>, lockTimeout?: number): Promise<T>;
|
|
257
|
+
/**
|
|
258
|
+
* Open a read-write transaction.
|
|
259
|
+
* This takes a global lock - only one write transaction can execute against the database at a time.
|
|
260
|
+
* Statements within the transaction must be done on the provided {@link Transaction} interface.
|
|
261
|
+
*/
|
|
262
|
+
writeTransaction<T>(callback: (tx: Transaction) => Promise<T>, lockTimeout?: number): Promise<T>;
|
|
263
|
+
/**
|
|
264
|
+
* This version of `watch` uses {@link AsyncGenerator}, for documentation see {@link watchWithAsyncGenerator}.
|
|
265
|
+
* Can be overloaded to use a callback handler instead, for documentation see {@link watchWithCallback}.
|
|
266
|
+
*
|
|
267
|
+
* @example
|
|
268
|
+
* ```javascript
|
|
269
|
+
* async *attachmentIds() {
|
|
270
|
+
* for await (const result of this.powersync.watch(
|
|
271
|
+
* `SELECT photo_id as id FROM todos WHERE photo_id IS NOT NULL`,
|
|
272
|
+
* []
|
|
273
|
+
* )) {
|
|
274
|
+
* yield result.rows?._array.map((r) => r.id) ?? [];
|
|
275
|
+
* }
|
|
276
|
+
* }
|
|
277
|
+
* ```
|
|
278
|
+
*/
|
|
279
|
+
watch(sql: string, parameters?: any[], options?: SQLWatchOptions): AsyncIterable<QueryResult>;
|
|
280
|
+
/**
|
|
281
|
+
* See {@link watchWithCallback}.
|
|
282
|
+
*
|
|
283
|
+
* @example
|
|
284
|
+
* ```javascript
|
|
285
|
+
* onAttachmentIdsChange(onResult) {
|
|
286
|
+
* this.powersync.watch(
|
|
287
|
+
* `SELECT photo_id as id FROM todos WHERE photo_id IS NOT NULL`,
|
|
288
|
+
* [],
|
|
289
|
+
* {
|
|
290
|
+
* onResult: (result) => onResult(result.rows?._array.map((r) => r.id) ?? [])
|
|
291
|
+
* }
|
|
292
|
+
* );
|
|
293
|
+
* }
|
|
294
|
+
* ```
|
|
295
|
+
*/
|
|
296
|
+
watch(sql: string, parameters?: any[], handler?: WatchHandler, options?: SQLWatchOptions): void;
|
|
297
|
+
/**
|
|
298
|
+
* Execute a read query every time the source tables are modified.
|
|
299
|
+
* Use {@link SQLWatchOptions.throttleMs} to specify the minimum interval between queries.
|
|
300
|
+
* Source tables are automatically detected using `EXPLAIN QUERY PLAN`.
|
|
301
|
+
*
|
|
302
|
+
* Note that the `onChange` callback member of the handler is required.
|
|
303
|
+
*/
|
|
304
|
+
watchWithCallback(sql: string, parameters?: any[], handler?: WatchHandler, options?: SQLWatchOptions): void;
|
|
305
|
+
/**
|
|
306
|
+
* Execute a read query every time the source tables are modified.
|
|
307
|
+
* Use {@link SQLWatchOptions.throttleMs} to specify the minimum interval between queries.
|
|
308
|
+
* Source tables are automatically detected using `EXPLAIN QUERY PLAN`.
|
|
309
|
+
*/
|
|
310
|
+
watchWithAsyncGenerator(sql: string, parameters?: any[], options?: SQLWatchOptions): AsyncIterable<QueryResult>;
|
|
311
|
+
resolveTables(sql: string, parameters?: any[], options?: SQLWatchOptions): Promise<string[]>;
|
|
312
|
+
/**
|
|
313
|
+
* This version of `onChange` uses {@link AsyncGenerator}, for documentation see {@link onChangeWithAsyncGenerator}.
|
|
314
|
+
* Can be overloaded to use a callback handler instead, for documentation see {@link onChangeWithCallback}.
|
|
315
|
+
*
|
|
316
|
+
* @example
|
|
317
|
+
* ```javascript
|
|
318
|
+
* async monitorChanges() {
|
|
319
|
+
* for await (const event of this.powersync.onChange({tables: ['todos']})) {
|
|
320
|
+
* console.log('Detected change event:', event);
|
|
321
|
+
* }
|
|
322
|
+
* }
|
|
323
|
+
* ```
|
|
324
|
+
*/
|
|
325
|
+
onChange(options?: SQLWatchOptions): AsyncIterable<WatchOnChangeEvent>;
|
|
326
|
+
/**
|
|
327
|
+
* See {@link onChangeWithCallback}.
|
|
328
|
+
*
|
|
329
|
+
* @example
|
|
330
|
+
* ```javascript
|
|
331
|
+
* monitorChanges() {
|
|
332
|
+
* this.powersync.onChange({
|
|
333
|
+
* onChange: (event) => {
|
|
334
|
+
* console.log('Change detected:', event);
|
|
335
|
+
* }
|
|
336
|
+
* }, { tables: ['todos'] });
|
|
337
|
+
* }
|
|
338
|
+
* ```
|
|
339
|
+
*/
|
|
340
|
+
onChange(handler?: WatchOnChangeHandler, options?: SQLWatchOptions): () => void;
|
|
341
|
+
/**
|
|
342
|
+
* Invoke the provided callback on any changes to any of the specified tables.
|
|
343
|
+
*
|
|
344
|
+
* This is preferred over {@link watchWithCallback} when multiple queries need to be performed
|
|
345
|
+
* together when data is changed.
|
|
346
|
+
*
|
|
347
|
+
* Note that the `onChange` callback member of the handler is required.
|
|
348
|
+
*
|
|
349
|
+
* Returns dispose function to stop watching.
|
|
350
|
+
*/
|
|
351
|
+
onChangeWithCallback(handler?: WatchOnChangeHandler, options?: SQLWatchOptions): () => void;
|
|
352
|
+
/**
|
|
353
|
+
* Create a Stream of changes to any of the specified tables.
|
|
354
|
+
*
|
|
355
|
+
* This is preferred over {@link watchWithAsyncGenerator} when multiple queries need to be performed
|
|
356
|
+
* together when data is changed.
|
|
357
|
+
*
|
|
358
|
+
* Note, do not declare this as `async *onChange` as it will not work in React Native
|
|
359
|
+
*/
|
|
360
|
+
onChangeWithAsyncGenerator(options?: SQLWatchOptions): AsyncIterable<WatchOnChangeEvent>;
|
|
361
|
+
private handleTableChanges;
|
|
362
|
+
private processTableUpdates;
|
|
363
|
+
/**
|
|
364
|
+
* @ignore
|
|
365
|
+
*/
|
|
366
|
+
private executeReadOnly;
|
|
367
|
+
}
|