@powersync/common 1.37.0 → 1.38.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.
package/dist/index.d.cts CHANGED
@@ -440,6 +440,73 @@ interface BucketStorageAdapter extends BaseObserverInterface<BucketStorageListen
440
440
  control(op: PowerSyncControlCommand, payload: string | Uint8Array | null): Promise<string>;
441
441
  }
442
442
 
443
+ type DataStreamOptions<ParsedData, SourceData> = {
444
+ mapLine?: (line: SourceData) => ParsedData;
445
+ /**
446
+ * Close the stream if any consumer throws an error
447
+ */
448
+ closeOnError?: boolean;
449
+ pressure?: {
450
+ highWaterMark?: number;
451
+ lowWaterMark?: number;
452
+ };
453
+ logger?: ILogger;
454
+ };
455
+ type DataStreamCallback<Data extends any = any> = (data: Data) => Promise<void>;
456
+ interface DataStreamListener<Data extends any = any> extends BaseListener {
457
+ data: (data: Data) => Promise<void>;
458
+ closed: () => void;
459
+ error: (error: Error) => void;
460
+ highWater: () => Promise<void>;
461
+ lowWater: () => Promise<void>;
462
+ }
463
+ declare const DEFAULT_PRESSURE_LIMITS: {
464
+ highWater: number;
465
+ lowWater: number;
466
+ };
467
+ /**
468
+ * A very basic implementation of a data stream with backpressure support which does not use
469
+ * native JS streams or async iterators.
470
+ * This is handy for environments such as React Native which need polyfills for the above.
471
+ */
472
+ declare class DataStream<ParsedData, SourceData = any> extends BaseObserver<DataStreamListener<ParsedData>> {
473
+ protected options?: DataStreamOptions<ParsedData, SourceData> | undefined;
474
+ dataQueue: SourceData[];
475
+ protected isClosed: boolean;
476
+ protected processingPromise: Promise<void> | null;
477
+ protected notifyDataAdded: (() => void) | null;
478
+ protected logger: ILogger;
479
+ protected mapLine: (line: SourceData) => ParsedData;
480
+ constructor(options?: DataStreamOptions<ParsedData, SourceData> | undefined);
481
+ get highWatermark(): number;
482
+ get lowWatermark(): number;
483
+ get closed(): boolean;
484
+ close(): Promise<void>;
485
+ /**
486
+ * Enqueues data for the consumers to read
487
+ */
488
+ enqueueData(data: SourceData): void;
489
+ /**
490
+ * Reads data once from the data stream
491
+ * @returns a Data payload or Null if the stream closed.
492
+ */
493
+ read(): Promise<ParsedData | null>;
494
+ /**
495
+ * Executes a callback for each data item in the stream
496
+ */
497
+ forEach(callback: DataStreamCallback<ParsedData>): () => void;
498
+ protected processQueue(): Promise<void> | undefined;
499
+ protected hasDataReader(): boolean;
500
+ protected _processQueue(): Promise<void>;
501
+ protected iterateAsyncErrored(cb: (l: Partial<DataStreamListener<ParsedData>>) => Promise<void>): Promise<void>;
502
+ }
503
+
504
+ interface PowerSyncCredentials {
505
+ endpoint: string;
506
+ token: string;
507
+ expiresAt?: Date;
508
+ }
509
+
443
510
  /**
444
511
  * For sync2.json
445
512
  */
@@ -576,1027 +643,968 @@ interface CrudResponse {
576
643
  checkpoint?: OpId;
577
644
  }
578
645
 
579
- interface BucketProgress {
580
- priority: number;
581
- at_last: number;
582
- since_last: number;
583
- target_count: number;
584
- }
585
-
586
- /** @internal */
587
- type InternalProgressInformation = Record<string, BucketProgress>;
588
- /**
589
- * Information about a progressing download made by the PowerSync SDK.
590
- *
591
- * To obtain these values, use {@link SyncProgress}, available through
592
- * {@link SyncStatus#downloadProgress}.
593
- */
594
- interface ProgressWithOperations {
595
- /**
596
- * The total amount of operations to download for the current sync iteration
597
- * to complete.
598
- */
599
- totalOperations: number;
646
+ type BSONImplementation = typeof BSON;
647
+ type RemoteConnector = {
648
+ fetchCredentials: () => Promise<PowerSyncCredentials | null>;
649
+ invalidateCredentials?: () => void;
650
+ };
651
+ declare const DEFAULT_REMOTE_LOGGER: Logger.ILogger;
652
+ type SyncStreamOptions = {
653
+ path: string;
654
+ data: StreamingSyncRequest;
655
+ headers?: Record<string, string>;
656
+ abortSignal?: AbortSignal;
657
+ fetchOptions?: Request;
658
+ };
659
+ declare enum FetchStrategy {
600
660
  /**
601
- * The amount of operations that have already been downloaded.
661
+ * Queues multiple sync events before processing, reducing round-trips.
662
+ * This comes at the cost of more processing overhead, which may cause ACK timeouts on older/weaker devices for big enough datasets.
602
663
  */
603
- downloadedOperations: number;
664
+ Buffered = "buffered",
604
665
  /**
605
- * Relative progress, as {@link downloadedOperations} of {@link totalOperations}.
606
- *
607
- * This will be a number between `0.0` and `1.0` (inclusive).
608
- *
609
- * When this number reaches `1.0`, all changes have been received from the sync service.
610
- * Actually applying these changes happens before the `downloadProgress` field is cleared from
611
- * {@link SyncStatus}, so progress can stay at `1.0` for a short while before completing.
666
+ * Processes each sync event immediately before requesting the next.
667
+ * This reduces processing overhead and improves real-time responsiveness.
612
668
  */
613
- downloadedFraction: number;
669
+ Sequential = "sequential"
614
670
  }
671
+ type SocketSyncStreamOptions = SyncStreamOptions & {
672
+ fetchStrategy: FetchStrategy;
673
+ };
674
+ type FetchImplementation = typeof fetch;
615
675
  /**
616
- * Provides realtime progress on how PowerSync is downloading rows.
617
- *
618
- * The progress until the next complete sync is available through the fields on {@link ProgressWithOperations},
619
- * which this class implements.
620
- * Additionally, the {@link SyncProgress.untilPriority} method can be used to otbain progress towards
621
- * a specific priority (instead of the progress for the entire download).
622
- *
623
- * The reported progress always reflects the status towards the end of a sync iteration (after
624
- * which a consistent snapshot of all buckets is available locally).
625
- *
626
- * In rare cases (in particular, when a [compacting](https://docs.powersync.com/usage/lifecycle-maintenance/compacting-buckets)
627
- * operation takes place between syncs), it's possible for the returned numbers to be slightly
628
- * inaccurate. For this reason, {@link SyncProgress} should be seen as an approximation of progress.
629
- * The information returned is good enough to build progress bars, but not exact enough to track
630
- * individual download counts.
631
- *
632
- * Also note that data is downloaded in bulk, which means that individual counters are unlikely
633
- * to be updated one-by-one.
676
+ * Class wrapper for providing a fetch implementation.
677
+ * The class wrapper is used to distinguish the fetchImplementation
678
+ * option in [AbstractRemoteOptions] from the general fetch method
679
+ * which is typeof "function"
634
680
  */
635
- declare class SyncProgress implements ProgressWithOperations {
636
- protected internal: InternalProgressInformation;
637
- totalOperations: number;
638
- downloadedOperations: number;
639
- downloadedFraction: number;
640
- constructor(internal: InternalProgressInformation);
641
- /**
642
- * Returns download progress towards all data up until the specified priority being received.
643
- *
644
- * The returned {@link ProgressWithOperations} tracks the target amount of operations that need
645
- * to be downloaded in total and how many of them have already been received.
646
- */
647
- untilPriority(priority: number): ProgressWithOperations;
681
+ declare class FetchImplementationProvider {
682
+ getFetch(): FetchImplementation;
648
683
  }
649
-
650
- type SyncDataFlowStatus = Partial<{
651
- downloading: boolean;
652
- uploading: boolean;
684
+ type AbstractRemoteOptions = {
653
685
  /**
654
- * Error during downloading (including connecting).
655
- *
656
- * Cleared on the next successful data download.
686
+ * Transforms the PowerSync base URL which might contain
687
+ * `http(s)://` to the corresponding WebSocket variant
688
+ * e.g. `ws(s)://`
657
689
  */
658
- downloadError?: Error;
690
+ socketUrlTransformer: (url: string) => string;
659
691
  /**
660
- * Error during uploading.
661
- * Cleared on the next successful upload.
692
+ * Optionally provide the fetch implementation to use.
693
+ * Note that this usually needs to be bound to the global scope.
694
+ * Binding should be done before passing here.
662
695
  */
663
- uploadError?: Error;
696
+ fetchImplementation: FetchImplementation | FetchImplementationProvider;
664
697
  /**
665
- * Internal information about how far we are downloading operations in buckets.
698
+ * Optional options to pass directly to all `fetch` calls.
666
699
  *
667
- * Please use the {@link SyncStatus#downloadProgress} property to track sync progress.
700
+ * This can include fields such as `dispatcher` (e.g. for proxy support),
701
+ * `cache`, or any other fetch-compatible options.
668
702
  */
669
- downloadProgress: InternalProgressInformation | null;
670
- }>;
671
- interface SyncPriorityStatus {
672
- priority: number;
673
- lastSyncedAt?: Date;
674
- hasSynced?: boolean;
675
- }
676
- type SyncStatusOptions = {
677
- connected?: boolean;
678
- connecting?: boolean;
679
- dataFlow?: SyncDataFlowStatus;
680
- lastSyncedAt?: Date;
681
- hasSynced?: boolean;
682
- priorityStatusEntries?: SyncPriorityStatus[];
703
+ fetchOptions?: {};
683
704
  };
684
- declare class SyncStatus {
685
- protected options: SyncStatusOptions;
686
- constructor(options: SyncStatusOptions);
705
+ declare const DEFAULT_REMOTE_OPTIONS: AbstractRemoteOptions;
706
+ declare abstract class AbstractRemote {
707
+ protected connector: RemoteConnector;
708
+ protected logger: ILogger;
709
+ protected credentials: PowerSyncCredentials | null;
710
+ protected options: AbstractRemoteOptions;
711
+ constructor(connector: RemoteConnector, logger?: ILogger, options?: Partial<AbstractRemoteOptions>);
687
712
  /**
688
- * Indicates if the client is currently connected to the PowerSync service.
689
- *
690
- * @returns {boolean} True if connected, false otherwise. Defaults to false if not specified.
713
+ * @returns a fetch implementation (function)
714
+ * which can be called to perform fetch requests
691
715
  */
692
- get connected(): boolean;
716
+ get fetch(): FetchImplementation;
693
717
  /**
694
- * Indicates if the client is in the process of establishing a connection to the PowerSync service.
718
+ * Get credentials currently cached, or fetch new credentials if none are
719
+ * available.
695
720
  *
696
- * @returns {boolean} True if connecting, false otherwise. Defaults to false if not specified.
721
+ * These credentials may have expired already.
697
722
  */
698
- get connecting(): boolean;
723
+ getCredentials(): Promise<PowerSyncCredentials | null>;
699
724
  /**
700
- * Time that a last sync has fully completed, if any.
701
- * This timestamp is reset to null after a restart of the PowerSync service.
725
+ * Fetch a new set of credentials and cache it.
702
726
  *
703
- * @returns {Date | undefined} The timestamp of the last successful sync, or undefined if no sync has completed.
727
+ * Until this call succeeds, `getCredentials` will still return the
728
+ * old credentials.
729
+ *
730
+ * This may be called before the current credentials have expired.
704
731
  */
705
- get lastSyncedAt(): Date | undefined;
732
+ prefetchCredentials(): Promise<PowerSyncCredentials | null>;
706
733
  /**
707
- * Indicates whether there has been at least one full sync completed since initialization.
734
+ * Get credentials for PowerSync.
708
735
  *
709
- * @returns {boolean | undefined} True if at least one sync has completed, false if no sync has completed,
710
- * or undefined when the state is still being loaded from the database.
736
+ * This should always fetch a fresh set of credentials - don't use cached
737
+ * values.
711
738
  */
712
- get hasSynced(): boolean | undefined;
713
- /**
714
- * Provides the current data flow status regarding uploads and downloads.
739
+ fetchCredentials(): Promise<PowerSyncCredentials | null>;
740
+ /***
741
+ * Immediately invalidate credentials.
715
742
  *
716
- * @returns {SyncDataFlowStatus} An object containing:
717
- * - downloading: True if actively downloading changes (only when connected is also true)
718
- * - uploading: True if actively uploading changes
719
- * Defaults to {downloading: false, uploading: false} if not specified.
743
+ * This may be called when the current credentials have expired.
720
744
  */
721
- get dataFlowStatus(): Partial<{
722
- downloading: boolean;
723
- uploading: boolean;
724
- /**
725
- * Error during downloading (including connecting).
726
- *
727
- * Cleared on the next successful data download.
728
- */
729
- downloadError?: Error;
730
- /**
731
- * Error during uploading.
732
- * Cleared on the next successful upload.
733
- */
734
- uploadError?: Error;
735
- /**
736
- * Internal information about how far we are downloading operations in buckets.
737
- *
738
- * Please use the {@link SyncStatus#downloadProgress} property to track sync progress.
739
- */
740
- downloadProgress: InternalProgressInformation | null;
745
+ invalidateCredentials(): void;
746
+ getUserAgent(): string;
747
+ protected buildRequest(path: string): Promise<{
748
+ url: string;
749
+ headers: {
750
+ 'content-type': string;
751
+ Authorization: string;
752
+ 'x-user-agent': string;
753
+ };
741
754
  }>;
755
+ post(path: string, data: any, headers?: Record<string, string>): Promise<any>;
756
+ get(path: string, headers?: Record<string, string>): Promise<any>;
742
757
  /**
743
- * Provides sync status information for all bucket priorities, sorted by priority (highest first).
758
+ * Provides a BSON implementation. The import nature of this varies depending on the platform
759
+ */
760
+ abstract getBSON(): Promise<BSONImplementation>;
761
+ protected createSocket(url: string): WebSocket;
762
+ /**
763
+ * Returns a data stream of sync line data.
744
764
  *
745
- * @returns {SyncPriorityStatus[]} An array of status entries for different sync priority levels,
746
- * sorted with highest priorities (lower numbers) first.
765
+ * @param map Maps received payload frames to the typed event value.
766
+ * @param bson A BSON encoder and decoder. When set, the data stream will be requested with a BSON payload
767
+ * (required for compatibility with older sync services).
747
768
  */
748
- get priorityStatusEntries(): SyncPriorityStatus[];
769
+ socketStreamRaw<T>(options: SocketSyncStreamOptions, map: (buffer: Uint8Array) => T, bson?: typeof BSON): Promise<DataStream<T>>;
749
770
  /**
750
- * A realtime progress report on how many operations have been downloaded and
751
- * how many are necessary in total to complete the next sync iteration.
771
+ * Connects to the sync/stream http endpoint, mapping and emitting each received string line.
772
+ */
773
+ postStreamRaw<T>(options: SyncStreamOptions, mapLine: (line: string) => T): Promise<DataStream<T>>;
774
+ }
775
+
776
+ declare enum LockType {
777
+ CRUD = "crud",
778
+ SYNC = "sync"
779
+ }
780
+ declare enum SyncStreamConnectionMethod {
781
+ HTTP = "http",
782
+ WEB_SOCKET = "web-socket"
783
+ }
784
+ declare enum SyncClientImplementation {
785
+ /**
786
+ * Decodes and handles sync lines received from the sync service in JavaScript.
752
787
  *
753
- * This field is only set when {@link SyncDataFlowStatus#downloading} is also true.
788
+ * This is the default option.
789
+ *
790
+ * @deprecated Don't use {@link SyncClientImplementation.JAVASCRIPT} directly. Instead, use
791
+ * {@link DEFAULT_SYNC_CLIENT_IMPLEMENTATION} or omit the option. The explicit choice to use
792
+ * the JavaScript-based sync implementation will be removed from a future version of the SDK.
754
793
  */
755
- get downloadProgress(): SyncProgress | null;
794
+ JAVASCRIPT = "js",
756
795
  /**
757
- * Reports the sync status (a pair of {@link SyncStatus#hasSynced} and {@link SyncStatus#lastSyncedAt} fields)
758
- * for a specific bucket priority level.
796
+ * This implementation offloads the sync line decoding and handling into the PowerSync
797
+ * core extension.
759
798
  *
760
- * When buckets with different priorities are declared, PowerSync may choose to synchronize higher-priority
761
- * buckets first. When a consistent view over all buckets for all priorities up until the given priority is
762
- * reached, PowerSync makes data from those buckets available before lower-priority buckets have finished
763
- * syncing.
799
+ * @experimental
800
+ * While this implementation is more performant than {@link SyncClientImplementation.JAVASCRIPT},
801
+ * it has seen less real-world testing and is marked as __experimental__ at the moment.
764
802
  *
765
- * This method returns the status for the requested priority or the next higher priority level that has
766
- * status information available. This is because when PowerSync makes data for a given priority available,
767
- * all buckets in higher-priorities are guaranteed to be consistent with that checkpoint.
803
+ * ## Compatibility warning
768
804
  *
769
- * For example, if PowerSync just finished synchronizing buckets in priority level 3, calling this method
770
- * with a priority of 1 may return information for priority level 3.
805
+ * The Rust sync client stores sync data in a format that is slightly different than the one used
806
+ * by the old {@link JAVASCRIPT} implementation. When adopting the {@link RUST} client on existing
807
+ * databases, the PowerSync SDK will migrate the format automatically.
808
+ * Further, the {@link JAVASCRIPT} client in recent versions of the PowerSync JS SDK (starting from
809
+ * the version introducing {@link RUST} as an option) also supports the new format, so you can switch
810
+ * back to {@link JAVASCRIPT} later.
771
811
  *
772
- * @param {number} priority The bucket priority for which the status should be reported
773
- * @returns {SyncPriorityStatus} Status information for the requested priority level or the next higher level with available status
812
+ * __However__: Upgrading the SDK version, then adopting {@link RUST} as a sync client and later
813
+ * downgrading the SDK to an older version (necessarily using the JavaScript-based implementation then)
814
+ * can lead to sync issues.
774
815
  */
775
- statusForPriority(priority: number): SyncPriorityStatus;
816
+ RUST = "rust"
817
+ }
818
+ /**
819
+ * The default {@link SyncClientImplementation} to use.
820
+ *
821
+ * Please use this field instead of {@link SyncClientImplementation.JAVASCRIPT} directly. A future version
822
+ * of the PowerSync SDK will enable {@link SyncClientImplementation.RUST} by default and remove the JavaScript
823
+ * option.
824
+ */
825
+ declare const DEFAULT_SYNC_CLIENT_IMPLEMENTATION = SyncClientImplementation.JAVASCRIPT;
826
+ /**
827
+ * Abstract Lock to be implemented by various JS environments
828
+ */
829
+ interface LockOptions<T> {
830
+ callback: () => Promise<T>;
831
+ type: LockType;
832
+ signal?: AbortSignal;
833
+ }
834
+ interface AbstractStreamingSyncImplementationOptions extends AdditionalConnectionOptions {
835
+ adapter: BucketStorageAdapter;
836
+ uploadCrud: () => Promise<void>;
776
837
  /**
777
- * Compares this SyncStatus instance with another to determine if they are equal.
778
- * Equality is determined by comparing the serialized JSON representation of both instances.
779
- *
780
- * @param {SyncStatus} status The SyncStatus instance to compare against
781
- * @returns {boolean} True if the instances are considered equal, false otherwise
838
+ * An identifier for which PowerSync DB this sync implementation is
839
+ * linked to. Most commonly DB name, but not restricted to DB name.
782
840
  */
783
- isEqual(status: SyncStatus): boolean;
841
+ identifier?: string;
842
+ logger?: ILogger;
843
+ remote: AbstractRemote;
844
+ }
845
+ interface StreamingSyncImplementationListener extends BaseListener {
784
846
  /**
785
- * Creates a human-readable string representation of the current sync status.
786
- * Includes information about connection state, sync completion, and data flow.
787
- *
788
- * @returns {string} A string representation of the sync status
847
+ * Triggered whenever a status update has been attempted to be made or
848
+ * refreshed.
789
849
  */
790
- getMessage(): string;
850
+ statusUpdated?: ((statusUpdate: SyncStatusOptions) => void) | undefined;
791
851
  /**
792
- * Serializes the SyncStatus instance to a plain object.
793
- *
794
- * @returns {SyncStatusOptions} A plain object representation of the sync status
852
+ * Triggers whenever the status' members have changed in value
795
853
  */
796
- toJSON(): SyncStatusOptions;
797
- private static comparePriorities;
854
+ statusChanged?: ((status: SyncStatus) => void) | undefined;
798
855
  }
799
-
800
- declare class UploadQueueStats {
856
+ /**
857
+ * Configurable options to be used when connecting to the PowerSync
858
+ * backend instance.
859
+ */
860
+ type PowerSyncConnectionOptions = Omit<InternalConnectionOptions, 'serializedSchema'>;
861
+ interface InternalConnectionOptions extends BaseConnectionOptions, AdditionalConnectionOptions {
862
+ }
863
+ /** @internal */
864
+ interface BaseConnectionOptions {
801
865
  /**
802
- * Number of records in the upload queue.
866
+ * Whether to use a JavaScript implementation to handle received sync lines from the sync
867
+ * service, or whether this work should be offloaded to the PowerSync core extension.
868
+ *
869
+ * This defaults to the JavaScript implementation ({@link SyncClientImplementation.JAVASCRIPT})
870
+ * since the ({@link SyncClientImplementation.RUST}) implementation is experimental at the moment.
803
871
  */
804
- count: number;
872
+ clientImplementation?: SyncClientImplementation;
805
873
  /**
806
- * Size of the upload queue in bytes.
874
+ * The connection method to use when streaming updates from
875
+ * the PowerSync backend instance.
876
+ * Defaults to a HTTP streaming connection.
807
877
  */
808
- size: number | null;
809
- constructor(
878
+ connectionMethod?: SyncStreamConnectionMethod;
810
879
  /**
811
- * Number of records in the upload queue.
880
+ * The fetch strategy to use when streaming updates from the PowerSync backend instance.
812
881
  */
813
- count: number,
882
+ fetchStrategy?: FetchStrategy;
814
883
  /**
815
- * Size of the upload queue in bytes.
884
+ * These parameters are passed to the sync rules, and will be available under the`user_parameters` object.
816
885
  */
817
- size?: number | null);
818
- toString(): string;
819
- }
820
-
821
- declare enum ColumnType {
822
- TEXT = "TEXT",
823
- INTEGER = "INTEGER",
824
- REAL = "REAL"
825
- }
826
- interface ColumnOptions {
827
- name: string;
828
- type?: ColumnType;
886
+ params?: Record<string, StreamingSyncRequestParameterType>;
887
+ /**
888
+ * The serialized schema - mainly used to forward information about raw tables to the sync client.
889
+ */
890
+ serializedSchema?: any;
829
891
  }
830
- type BaseColumnType<T extends number | string | null> = {
831
- type: ColumnType;
832
- };
833
- type ColumnsType = Record<string, BaseColumnType<any>>;
834
- type ExtractColumnValueType<T extends BaseColumnType<any>> = T extends BaseColumnType<infer R> ? R : unknown;
835
- declare const MAX_AMOUNT_OF_COLUMNS = 1999;
836
- declare const column: {
837
- text: BaseColumnType<string | null>;
838
- integer: BaseColumnType<number | null>;
839
- real: BaseColumnType<number | null>;
840
- };
841
- declare class Column {
842
- protected options: ColumnOptions;
843
- constructor(options: ColumnOptions);
844
- get name(): string;
845
- get type(): ColumnType | undefined;
846
- toJSON(): {
847
- name: string;
848
- type: ColumnType | undefined;
849
- };
892
+ /** @internal */
893
+ interface AdditionalConnectionOptions {
894
+ /**
895
+ * Delay for retrying sync streaming operations
896
+ * from the PowerSync backend after an error occurs.
897
+ */
898
+ retryDelayMs?: number;
899
+ /**
900
+ * Backend Connector CRUD operations are throttled
901
+ * to occur at most every `crudUploadThrottleMs`
902
+ * milliseconds.
903
+ */
904
+ crudUploadThrottleMs?: number;
850
905
  }
851
-
852
- /**
853
- * A pending variant of a {@link RawTable} that doesn't have a name (because it would be inferred when creating the
854
- * schema).
855
- */
856
- type RawTableType = {
906
+ /** @internal */
907
+ type RequiredAdditionalConnectionOptions = Required<AdditionalConnectionOptions>;
908
+ interface StreamingSyncImplementation extends BaseObserverInterface<StreamingSyncImplementationListener>, Disposable {
857
909
  /**
858
- * The statement to run when PowerSync detects that a row needs to be inserted or updated.
910
+ * Connects to the sync service
859
911
  */
860
- put: PendingStatement;
912
+ connect(options?: InternalConnectionOptions): Promise<void>;
861
913
  /**
862
- * The statement to run when PowerSync detects that a row needs to be deleted.
914
+ * Disconnects from the sync services.
915
+ * @throws if not connected or if abort is not controlled internally
863
916
  */
864
- delete: PendingStatement;
865
- };
866
- /**
867
- * A parameter to use as part of {@link PendingStatement}.
868
- *
869
- * For delete statements, only the `"Id"` value is supported - the sync client will replace it with the id of the row to
870
- * be synced.
871
- *
872
- * For insert and replace operations, the values of columns in the table are available as parameters through
873
- * `{Column: 'name'}`.
874
- */
875
- type PendingStatementParameter = 'Id' | {
876
- Column: string;
877
- };
878
- /**
879
- * A statement that the PowerSync client should use to insert or delete data into a table managed by the user.
880
- */
881
- type PendingStatement = {
882
- sql: string;
883
- params: PendingStatementParameter[];
917
+ disconnect(): Promise<void>;
918
+ getWriteCheckpoint: () => Promise<string>;
919
+ hasCompletedSync: () => Promise<boolean>;
920
+ isConnected: boolean;
921
+ lastSyncedAt?: Date;
922
+ syncStatus: SyncStatus;
923
+ triggerCrudUpload: () => void;
924
+ waitForReady(): Promise<void>;
925
+ waitForStatus(status: SyncStatusOptions): Promise<void>;
926
+ waitUntilStatusMatches(predicate: (status: SyncStatus) => boolean): Promise<void>;
927
+ }
928
+ declare const DEFAULT_CRUD_UPLOAD_THROTTLE_MS = 1000;
929
+ declare const DEFAULT_RETRY_DELAY_MS = 5000;
930
+ declare const DEFAULT_STREAMING_SYNC_OPTIONS: {
931
+ retryDelayMs: number;
932
+ crudUploadThrottleMs: number;
884
933
  };
885
- /**
886
- * Instructs PowerSync to sync data into a "raw" table.
887
- *
888
- * Since raw tables are not backed by JSON, running complex queries on them may be more efficient. Further, they allow
889
- * using client-side table and column constraints.
890
- *
891
- * To collect local writes to raw tables with PowerSync, custom triggers are required. See
892
- * {@link https://docs.powersync.com/usage/use-case-examples/raw-tables the documentation} for details and an example on
893
- * using raw tables.
894
- *
895
- * Note that raw tables are only supported when using the new `SyncClientImplementation.rust` sync client.
896
- *
897
- * @experimental Please note that this feature is experimental at the moment, and not covered by PowerSync semver or
898
- * stability guarantees.
899
- */
900
- declare class RawTable implements RawTableType {
934
+ type RequiredPowerSyncConnectionOptions = Required<BaseConnectionOptions>;
935
+ declare const DEFAULT_STREAM_CONNECTION_OPTIONS: RequiredPowerSyncConnectionOptions;
936
+ declare abstract class AbstractStreamingSyncImplementation extends BaseObserver<StreamingSyncImplementationListener> implements StreamingSyncImplementation {
937
+ protected _lastSyncedAt: Date | null;
938
+ protected options: AbstractStreamingSyncImplementationOptions;
939
+ protected abortController: AbortController | null;
940
+ protected uploadAbortController: AbortController | null;
941
+ protected crudUpdateListener?: () => void;
942
+ protected streamingSyncPromise?: Promise<void>;
943
+ protected logger: ILogger;
944
+ private isUploadingCrud;
945
+ private notifyCompletedUploads?;
946
+ syncStatus: SyncStatus;
947
+ triggerCrudUpload: () => void;
948
+ constructor(options: AbstractStreamingSyncImplementationOptions);
949
+ waitForReady(): Promise<void>;
950
+ waitForStatus(status: SyncStatusOptions): Promise<void>;
951
+ waitUntilStatusMatches(predicate: (status: SyncStatus) => boolean): Promise<void>;
952
+ get lastSyncedAt(): Date | undefined;
953
+ get isConnected(): boolean;
954
+ dispose(): Promise<void>;
955
+ abstract obtainLock<T>(lockOptions: LockOptions<T>): Promise<T>;
956
+ hasCompletedSync(): Promise<boolean>;
957
+ getWriteCheckpoint(): Promise<string>;
958
+ protected _uploadAllCrud(): Promise<void>;
959
+ connect(options?: PowerSyncConnectionOptions): Promise<void>;
960
+ disconnect(): Promise<void>;
901
961
  /**
902
- * The name of the table.
962
+ * @deprecated use [connect instead]
963
+ */
964
+ streamingSync(signal?: AbortSignal, options?: PowerSyncConnectionOptions): Promise<void>;
965
+ private collectLocalBucketState;
966
+ /**
967
+ * Older versions of the JS SDK used to encode subkeys as JSON in {@link OplogEntry.toJSON}.
968
+ * Because subkeys are always strings, this leads to quotes being added around them in `ps_oplog`.
969
+ * While this is not a problem as long as it's done consistently, it causes issues when a database
970
+ * created by the JS SDK is used with other SDKs, or (more likely) when the new Rust sync client
971
+ * is enabled.
903
972
  *
904
- * This does not have to match the actual table name in the schema - {@link put} and {@link delete} are free to use
905
- * another table. Instead, this name is used by the sync client to recognize that operations on this table (as it
906
- * appears in the source / backend database) are to be handled specially.
973
+ * So, we add a migration from the old key format (with quotes) to the new one (no quotes). The
974
+ * migration is only triggered when necessary (for now). The function returns whether the new format
975
+ * should be used, so that the JS SDK is able to write to updated databases.
976
+ *
977
+ * @param requireFixedKeyFormat Whether we require the new format or also support the old one.
978
+ * The Rust client requires the new subkey format.
979
+ * @returns Whether the database is now using the new, fixed subkey format.
907
980
  */
908
- name: string;
909
- put: PendingStatement;
910
- delete: PendingStatement;
911
- constructor(name: string, type: RawTableType);
912
- }
913
-
914
- interface IndexColumnOptions {
915
- name: string;
916
- ascending?: boolean;
917
- }
918
- declare const DEFAULT_INDEX_COLUMN_OPTIONS: Partial<IndexColumnOptions>;
919
- declare class IndexedColumn {
920
- protected options: IndexColumnOptions;
921
- static createAscending(column: string): IndexedColumn;
922
- constructor(options: IndexColumnOptions);
923
- get name(): string;
924
- get ascending(): boolean | undefined;
925
- toJSON(table: Table): {
926
- name: string;
927
- ascending: boolean | undefined;
928
- type: ColumnType;
929
- };
981
+ private requireKeyFormat;
982
+ protected streamingSyncIteration(signal: AbortSignal, options?: PowerSyncConnectionOptions): Promise<void>;
983
+ private legacyStreamingSyncIteration;
984
+ private rustSyncIteration;
985
+ private updateSyncStatusForStartingCheckpoint;
986
+ private applyCheckpoint;
987
+ protected updateSyncStatus(options: SyncStatusOptions): void;
988
+ private delayRetry;
930
989
  }
931
990
 
932
- interface IndexOptions {
933
- name: string;
934
- columns?: IndexedColumn[];
935
- }
936
- declare const DEFAULT_INDEX_OPTIONS: Partial<IndexOptions>;
937
- declare class Index {
938
- protected options: IndexOptions;
939
- static createAscending(options: IndexOptions, columnNames: string[]): Index;
940
- constructor(options: IndexOptions);
941
- get name(): string;
942
- get columns(): IndexedColumn[];
943
- toJSON(table: Table): {
944
- name: string;
945
- columns: {
946
- name: string;
947
- ascending: boolean | undefined;
948
- type: ColumnType;
949
- }[];
950
- };
991
+ interface BucketProgress {
992
+ priority: number;
993
+ at_last: number;
994
+ since_last: number;
995
+ target_count: number;
951
996
  }
952
997
 
998
+ /** @internal */
999
+ type InternalProgressInformation = Record<string, BucketProgress>;
953
1000
  /**
954
- Generate a new table from the columns and indexes
955
- @deprecated You should use {@link Table} instead as it now allows TableV2 syntax.
956
- This will be removed in the next major release.
957
- */
958
- declare class TableV2<Columns extends ColumnsType = ColumnsType> extends Table<Columns> {
959
- }
960
-
961
- interface SharedTableOptions {
962
- localOnly?: boolean;
963
- insertOnly?: boolean;
964
- viewName?: string;
965
- trackPrevious?: boolean | TrackPreviousOptions;
966
- trackMetadata?: boolean;
967
- ignoreEmptyUpdates?: boolean;
968
- }
969
- /** Whether to include previous column values when PowerSync tracks local changes.
1001
+ * Information about a progressing download made by the PowerSync SDK.
970
1002
  *
971
- * Including old values may be helpful for some backend connector implementations, which is
972
- * why it can be enabled on per-table or per-columm basis.
1003
+ * To obtain these values, use {@link SyncProgress}, available through
1004
+ * {@link SyncStatus#downloadProgress}.
973
1005
  */
974
- interface TrackPreviousOptions {
975
- /** When defined, a list of column names for which old values should be tracked. */
976
- columns?: string[];
977
- /** When enabled, only include values that have actually been changed by an update. */
978
- onlyWhenChanged?: boolean;
979
- }
980
- interface TableOptions extends SharedTableOptions {
1006
+ interface ProgressWithOperations {
981
1007
  /**
982
- * The synced table name, matching sync rules
1008
+ * The total amount of operations to download for the current sync iteration
1009
+ * to complete.
983
1010
  */
984
- name: string;
985
- columns: Column[];
986
- indexes?: Index[];
987
- }
988
- type RowType<T extends TableV2<any>> = {
989
- [K in keyof T['columnMap']]: ExtractColumnValueType<T['columnMap'][K]>;
990
- } & {
991
- id: string;
992
- };
993
- type IndexShorthand = Record<string, string[]>;
994
- interface TableV2Options extends SharedTableOptions {
995
- indexes?: IndexShorthand;
996
- }
997
- declare const DEFAULT_TABLE_OPTIONS: {
998
- indexes: never[];
999
- insertOnly: boolean;
1000
- localOnly: boolean;
1001
- trackPrevious: boolean;
1002
- trackMetadata: boolean;
1003
- ignoreEmptyUpdates: boolean;
1004
- };
1005
- declare const InvalidSQLCharacters: RegExp;
1006
- declare class Table<Columns extends ColumnsType = ColumnsType> {
1007
- protected options: TableOptions;
1008
- protected _mappedColumns: Columns;
1009
- static createLocalOnly(options: TableOptions): Table<ColumnsType>;
1010
- static createInsertOnly(options: TableOptions): Table<ColumnsType>;
1011
+ totalOperations: number;
1011
1012
  /**
1012
- * Create a table.
1013
- * @deprecated This was only only included for TableV2 and is no longer necessary.
1014
- * Prefer to use new Table() directly.
1015
- *
1016
- * TODO remove in the next major release.
1013
+ * The amount of operations that have already been downloaded.
1017
1014
  */
1018
- static createTable(name: string, table: Table): Table<ColumnsType>;
1015
+ downloadedOperations: number;
1019
1016
  /**
1020
- * Creates a new Table instance.
1021
- *
1022
- * This constructor supports two different versions:
1023
- * 1. New constructor: Using a Columns object and an optional TableV2Options object
1024
- * 2. Deprecated constructor: Using a TableOptions object (will be removed in the next major release)
1025
- *
1026
- * @constructor
1027
- * @param {Columns | TableOptions} optionsOrColumns - Either a Columns object (for V2 syntax) or a TableOptions object (for V1 syntax)
1028
- * @param {TableV2Options} [v2Options] - Optional configuration options for V2 syntax
1029
- *
1030
- * @example
1031
- * ```javascript
1032
- * // New Constructor
1033
- * const table = new Table(
1034
- * {
1035
- * name: column.text,
1036
- * age: column.integer
1037
- * },
1038
- * { indexes: { nameIndex: ['name'] } }
1039
- * );
1040
- *```
1017
+ * Relative progress, as {@link downloadedOperations} of {@link totalOperations}.
1041
1018
  *
1019
+ * This will be a number between `0.0` and `1.0` (inclusive).
1042
1020
  *
1043
- * @example
1044
- * ```javascript
1045
- * // Deprecated Constructor
1046
- * const table = new Table({
1047
- * name: 'users',
1048
- * columns: [
1049
- * new Column({ name: 'name', type: ColumnType.TEXT }),
1050
- * new Column({ name: 'age', type: ColumnType.INTEGER })
1051
- * ]
1052
- * });
1053
- *```
1021
+ * When this number reaches `1.0`, all changes have been received from the sync service.
1022
+ * Actually applying these changes happens before the `downloadProgress` field is cleared from
1023
+ * {@link SyncStatus}, so progress can stay at `1.0` for a short while before completing.
1054
1024
  */
1055
- constructor(columns: Columns, options?: TableV2Options);
1056
- /**
1057
- * @deprecated This constructor will be removed in the next major release.
1058
- * Use the new constructor shown below instead as this does not show types.
1059
- * @example
1060
- * <caption>Use this instead</caption>
1061
- * ```javascript
1062
- * const table = new Table(
1063
- * {
1064
- * name: column.text,
1065
- * age: column.integer
1066
- * },
1067
- * { indexes: { nameIndex: ['name'] } }
1068
- * );
1069
- *```
1070
- */
1071
- constructor(options: TableOptions);
1072
- copyWithName(name: string): Table;
1073
- private isTableV1;
1074
- private initTableV1;
1075
- private initTableV2;
1076
- private applyDefaultOptions;
1077
- get name(): string;
1078
- get viewNameOverride(): string | undefined;
1079
- get viewName(): string;
1080
- get columns(): Column[];
1081
- get columnMap(): Columns;
1082
- get indexes(): Index[];
1083
- get localOnly(): boolean;
1084
- get insertOnly(): boolean;
1085
- get trackPrevious(): boolean | TrackPreviousOptions;
1086
- get trackMetadata(): boolean;
1087
- get ignoreEmptyUpdates(): boolean;
1088
- get internalName(): string;
1089
- get validName(): boolean;
1090
- validate(): void;
1091
- toJSON(): {
1092
- name: string;
1093
- view_name: string;
1094
- local_only: boolean;
1095
- insert_only: boolean;
1096
- include_old: any;
1097
- include_old_only_when_changed: boolean;
1098
- include_metadata: boolean;
1099
- ignore_empty_update: boolean;
1100
- columns: {
1101
- name: string;
1102
- type: ColumnType | undefined;
1103
- }[];
1104
- indexes: {
1105
- name: string;
1106
- columns: {
1107
- name: string;
1108
- ascending: boolean | undefined;
1109
- type: ColumnType;
1110
- }[];
1111
- }[];
1112
- };
1025
+ downloadedFraction: number;
1113
1026
  }
1114
-
1115
- type SchemaType = Record<string, Table<any>>;
1116
- type SchemaTableType<S extends SchemaType> = {
1117
- [K in keyof S]: RowType<S[K]>;
1118
- };
1119
1027
  /**
1120
- * A schema is a collection of tables. It is used to define the structure of a database.
1028
+ * Provides realtime progress on how PowerSync is downloading rows.
1029
+ *
1030
+ * The progress until the next complete sync is available through the fields on {@link ProgressWithOperations},
1031
+ * which this class implements.
1032
+ * Additionally, the {@link SyncProgress.untilPriority} method can be used to otbain progress towards
1033
+ * a specific priority (instead of the progress for the entire download).
1034
+ *
1035
+ * The reported progress always reflects the status towards the end of a sync iteration (after
1036
+ * which a consistent snapshot of all buckets is available locally).
1037
+ *
1038
+ * In rare cases (in particular, when a [compacting](https://docs.powersync.com/usage/lifecycle-maintenance/compacting-buckets)
1039
+ * operation takes place between syncs), it's possible for the returned numbers to be slightly
1040
+ * inaccurate. For this reason, {@link SyncProgress} should be seen as an approximation of progress.
1041
+ * The information returned is good enough to build progress bars, but not exact enough to track
1042
+ * individual download counts.
1043
+ *
1044
+ * Also note that data is downloaded in bulk, which means that individual counters are unlikely
1045
+ * to be updated one-by-one.
1121
1046
  */
1122
- declare class Schema<S extends SchemaType = SchemaType> {
1123
- readonly types: SchemaTableType<S>;
1124
- readonly props: S;
1125
- readonly tables: Table[];
1126
- readonly rawTables: RawTable[];
1127
- constructor(tables: Table[] | S);
1047
+ declare class SyncProgress implements ProgressWithOperations {
1048
+ protected internal: InternalProgressInformation;
1049
+ totalOperations: number;
1050
+ downloadedOperations: number;
1051
+ downloadedFraction: number;
1052
+ constructor(internal: InternalProgressInformation);
1128
1053
  /**
1129
- * Adds raw tables to this schema. Raw tables are identified by their name, but entirely managed by the application
1130
- * developer instead of automatically by PowerSync.
1131
- * Since raw tables are not backed by JSON, running complex queries on them may be more efficient. Further, they allow
1132
- * using client-side table and column constraints.
1133
- * Note that raw tables are only supported when using the new `SyncClientImplementation.rust` sync client.
1054
+ * Returns download progress towards all data up until the specified priority being received.
1134
1055
  *
1135
- * @param tables An object of (table name, raw table definition) entries.
1136
- * @experimental Note that the raw tables API is still experimental and may change in the future.
1056
+ * The returned {@link ProgressWithOperations} tracks the target amount of operations that need
1057
+ * to be downloaded in total and how many of them have already been received.
1137
1058
  */
1138
- withRawTables(tables: Record<string, RawTableType>): void;
1139
- validate(): void;
1140
- toJSON(): {
1141
- tables: {
1142
- name: string;
1143
- view_name: string;
1144
- local_only: boolean;
1145
- insert_only: boolean;
1146
- include_old: any;
1147
- include_old_only_when_changed: boolean;
1148
- include_metadata: boolean;
1149
- ignore_empty_update: boolean;
1150
- columns: {
1151
- name: string;
1152
- type: ColumnType | undefined;
1153
- }[];
1154
- indexes: {
1155
- name: string;
1156
- columns: {
1157
- name: string;
1158
- ascending: boolean | undefined;
1159
- type: ColumnType;
1160
- }[];
1161
- }[];
1162
- }[];
1163
- raw_tables: RawTable[];
1164
- };
1165
- private convertToClassicTables;
1166
- }
1167
-
1168
- interface PowerSyncCredentials {
1169
- endpoint: string;
1170
- token: string;
1171
- expiresAt?: Date;
1059
+ untilPriority(priority: number): ProgressWithOperations;
1172
1060
  }
1173
1061
 
1174
- interface PowerSyncBackendConnector {
1175
- /** Allows the PowerSync client to retrieve an authentication token from your backend
1176
- * which is used to authenticate against the PowerSync service.
1177
- *
1178
- * This should always fetch a fresh set of credentials - don't use cached
1179
- * values.
1180
- *
1181
- * Return null if the user is not signed in. Throw an error if credentials
1182
- * cannot be fetched due to a network error or other temporary error.
1062
+ type SyncDataFlowStatus = Partial<{
1063
+ downloading: boolean;
1064
+ uploading: boolean;
1065
+ /**
1066
+ * Error during downloading (including connecting).
1183
1067
  *
1184
- * This token is kept for the duration of a sync connection.
1068
+ * Cleared on the next successful data download.
1185
1069
  */
1186
- fetchCredentials: () => Promise<PowerSyncCredentials | null>;
1187
- /** Upload local changes to the app backend.
1188
- *
1189
- * Use {@link AbstractPowerSyncDatabase.getCrudBatch} to get a batch of changes to upload.
1190
- *
1191
- * Any thrown errors will result in a retry after the configured wait period (default: 5 seconds).
1070
+ downloadError?: Error;
1071
+ /**
1072
+ * Error during uploading.
1073
+ * Cleared on the next successful upload.
1192
1074
  */
1193
- uploadData: (database: AbstractPowerSyncDatabase) => Promise<void>;
1194
- }
1195
-
1196
- type DataStreamOptions<ParsedData, SourceData> = {
1197
- mapLine?: (line: SourceData) => ParsedData;
1075
+ uploadError?: Error;
1198
1076
  /**
1199
- * Close the stream if any consumer throws an error
1077
+ * Internal information about how far we are downloading operations in buckets.
1078
+ *
1079
+ * Please use the {@link SyncStatus#downloadProgress} property to track sync progress.
1200
1080
  */
1201
- closeOnError?: boolean;
1202
- pressure?: {
1203
- highWaterMark?: number;
1204
- lowWaterMark?: number;
1205
- };
1206
- logger?: ILogger;
1207
- };
1208
- type DataStreamCallback<Data extends any = any> = (data: Data) => Promise<void>;
1209
- interface DataStreamListener<Data extends any = any> extends BaseListener {
1210
- data: (data: Data) => Promise<void>;
1211
- closed: () => void;
1212
- error: (error: Error) => void;
1213
- highWater: () => Promise<void>;
1214
- lowWater: () => Promise<void>;
1081
+ downloadProgress: InternalProgressInformation | null;
1082
+ }>;
1083
+ interface SyncPriorityStatus {
1084
+ priority: number;
1085
+ lastSyncedAt?: Date;
1086
+ hasSynced?: boolean;
1215
1087
  }
1216
- declare const DEFAULT_PRESSURE_LIMITS: {
1217
- highWater: number;
1218
- lowWater: number;
1088
+ type SyncStatusOptions = {
1089
+ connected?: boolean;
1090
+ connecting?: boolean;
1091
+ dataFlow?: SyncDataFlowStatus;
1092
+ lastSyncedAt?: Date;
1093
+ hasSynced?: boolean;
1094
+ priorityStatusEntries?: SyncPriorityStatus[];
1095
+ clientImplementation?: SyncClientImplementation;
1219
1096
  };
1220
- /**
1221
- * A very basic implementation of a data stream with backpressure support which does not use
1222
- * native JS streams or async iterators.
1223
- * This is handy for environments such as React Native which need polyfills for the above.
1224
- */
1225
- declare class DataStream<ParsedData, SourceData = any> extends BaseObserver<DataStreamListener<ParsedData>> {
1226
- protected options?: DataStreamOptions<ParsedData, SourceData> | undefined;
1227
- dataQueue: SourceData[];
1228
- protected isClosed: boolean;
1229
- protected processingPromise: Promise<void> | null;
1230
- protected notifyDataAdded: (() => void) | null;
1231
- protected logger: ILogger;
1232
- protected mapLine: (line: SourceData) => ParsedData;
1233
- constructor(options?: DataStreamOptions<ParsedData, SourceData> | undefined);
1234
- get highWatermark(): number;
1235
- get lowWatermark(): number;
1236
- get closed(): boolean;
1237
- close(): Promise<void>;
1097
+ declare class SyncStatus {
1098
+ protected options: SyncStatusOptions;
1099
+ constructor(options: SyncStatusOptions);
1238
1100
  /**
1239
- * Enqueues data for the consumers to read
1101
+ * Returns the used sync client implementation (either the one implemented in JavaScript or the newer Rust-based
1102
+ * implementation).
1103
+ *
1104
+ * This information is only available after a connection has been requested.
1240
1105
  */
1241
- enqueueData(data: SourceData): void;
1106
+ get clientImplementation(): SyncClientImplementation | undefined;
1242
1107
  /**
1243
- * Reads data once from the data stream
1244
- * @returns a Data payload or Null if the stream closed.
1108
+ * Indicates if the client is currently connected to the PowerSync service.
1109
+ *
1110
+ * @returns {boolean} True if connected, false otherwise. Defaults to false if not specified.
1245
1111
  */
1246
- read(): Promise<ParsedData | null>;
1112
+ get connected(): boolean;
1247
1113
  /**
1248
- * Executes a callback for each data item in the stream
1114
+ * Indicates if the client is in the process of establishing a connection to the PowerSync service.
1115
+ *
1116
+ * @returns {boolean} True if connecting, false otherwise. Defaults to false if not specified.
1249
1117
  */
1250
- forEach(callback: DataStreamCallback<ParsedData>): () => void;
1251
- protected processQueue(): Promise<void> | undefined;
1252
- protected hasDataReader(): boolean;
1253
- protected _processQueue(): Promise<void>;
1254
- protected iterateAsyncErrored(cb: (l: Partial<DataStreamListener<ParsedData>>) => Promise<void>): Promise<void>;
1255
- }
1256
-
1257
- type BSONImplementation = typeof BSON;
1258
- type RemoteConnector = {
1259
- fetchCredentials: () => Promise<PowerSyncCredentials | null>;
1260
- invalidateCredentials?: () => void;
1261
- };
1262
- declare const DEFAULT_REMOTE_LOGGER: Logger.ILogger;
1263
- type SyncStreamOptions = {
1264
- path: string;
1265
- data: StreamingSyncRequest;
1266
- headers?: Record<string, string>;
1267
- abortSignal?: AbortSignal;
1268
- fetchOptions?: Request;
1269
- };
1270
- declare enum FetchStrategy {
1118
+ get connecting(): boolean;
1271
1119
  /**
1272
- * Queues multiple sync events before processing, reducing round-trips.
1273
- * This comes at the cost of more processing overhead, which may cause ACK timeouts on older/weaker devices for big enough datasets.
1120
+ * Time that a last sync has fully completed, if any.
1121
+ * This timestamp is reset to null after a restart of the PowerSync service.
1122
+ *
1123
+ * @returns {Date | undefined} The timestamp of the last successful sync, or undefined if no sync has completed.
1274
1124
  */
1275
- Buffered = "buffered",
1125
+ get lastSyncedAt(): Date | undefined;
1276
1126
  /**
1277
- * Processes each sync event immediately before requesting the next.
1278
- * This reduces processing overhead and improves real-time responsiveness.
1127
+ * Indicates whether there has been at least one full sync completed since initialization.
1128
+ *
1129
+ * @returns {boolean | undefined} True if at least one sync has completed, false if no sync has completed,
1130
+ * or undefined when the state is still being loaded from the database.
1279
1131
  */
1280
- Sequential = "sequential"
1281
- }
1282
- type SocketSyncStreamOptions = SyncStreamOptions & {
1283
- fetchStrategy: FetchStrategy;
1284
- };
1285
- type FetchImplementation = typeof fetch;
1286
- /**
1287
- * Class wrapper for providing a fetch implementation.
1288
- * The class wrapper is used to distinguish the fetchImplementation
1289
- * option in [AbstractRemoteOptions] from the general fetch method
1290
- * which is typeof "function"
1291
- */
1292
- declare class FetchImplementationProvider {
1293
- getFetch(): FetchImplementation;
1294
- }
1295
- type AbstractRemoteOptions = {
1132
+ get hasSynced(): boolean | undefined;
1296
1133
  /**
1297
- * Transforms the PowerSync base URL which might contain
1298
- * `http(s)://` to the corresponding WebSocket variant
1299
- * e.g. `ws(s)://`
1134
+ * Provides the current data flow status regarding uploads and downloads.
1135
+ *
1136
+ * @returns {SyncDataFlowStatus} An object containing:
1137
+ * - downloading: True if actively downloading changes (only when connected is also true)
1138
+ * - uploading: True if actively uploading changes
1139
+ * Defaults to {downloading: false, uploading: false} if not specified.
1300
1140
  */
1301
- socketUrlTransformer: (url: string) => string;
1141
+ get dataFlowStatus(): Partial<{
1142
+ downloading: boolean;
1143
+ uploading: boolean;
1144
+ /**
1145
+ * Error during downloading (including connecting).
1146
+ *
1147
+ * Cleared on the next successful data download.
1148
+ */
1149
+ downloadError?: Error;
1150
+ /**
1151
+ * Error during uploading.
1152
+ * Cleared on the next successful upload.
1153
+ */
1154
+ uploadError?: Error;
1155
+ /**
1156
+ * Internal information about how far we are downloading operations in buckets.
1157
+ *
1158
+ * Please use the {@link SyncStatus#downloadProgress} property to track sync progress.
1159
+ */
1160
+ downloadProgress: InternalProgressInformation | null;
1161
+ }>;
1302
1162
  /**
1303
- * Optionally provide the fetch implementation to use.
1304
- * Note that this usually needs to be bound to the global scope.
1305
- * Binding should be done before passing here.
1163
+ * Provides sync status information for all bucket priorities, sorted by priority (highest first).
1164
+ *
1165
+ * @returns {SyncPriorityStatus[]} An array of status entries for different sync priority levels,
1166
+ * sorted with highest priorities (lower numbers) first.
1306
1167
  */
1307
- fetchImplementation: FetchImplementation | FetchImplementationProvider;
1168
+ get priorityStatusEntries(): SyncPriorityStatus[];
1308
1169
  /**
1309
- * Optional options to pass directly to all `fetch` calls.
1170
+ * A realtime progress report on how many operations have been downloaded and
1171
+ * how many are necessary in total to complete the next sync iteration.
1310
1172
  *
1311
- * This can include fields such as `dispatcher` (e.g. for proxy support),
1312
- * `cache`, or any other fetch-compatible options.
1173
+ * This field is only set when {@link SyncDataFlowStatus#downloading} is also true.
1313
1174
  */
1314
- fetchOptions?: {};
1315
- };
1316
- declare const DEFAULT_REMOTE_OPTIONS: AbstractRemoteOptions;
1317
- declare abstract class AbstractRemote {
1318
- protected connector: RemoteConnector;
1319
- protected logger: ILogger;
1320
- protected credentials: PowerSyncCredentials | null;
1321
- protected options: AbstractRemoteOptions;
1322
- constructor(connector: RemoteConnector, logger?: ILogger, options?: Partial<AbstractRemoteOptions>);
1175
+ get downloadProgress(): SyncProgress | null;
1323
1176
  /**
1324
- * @returns a fetch implementation (function)
1325
- * which can be called to perform fetch requests
1177
+ * Reports the sync status (a pair of {@link SyncStatus#hasSynced} and {@link SyncStatus#lastSyncedAt} fields)
1178
+ * for a specific bucket priority level.
1179
+ *
1180
+ * When buckets with different priorities are declared, PowerSync may choose to synchronize higher-priority
1181
+ * buckets first. When a consistent view over all buckets for all priorities up until the given priority is
1182
+ * reached, PowerSync makes data from those buckets available before lower-priority buckets have finished
1183
+ * syncing.
1184
+ *
1185
+ * This method returns the status for the requested priority or the next higher priority level that has
1186
+ * status information available. This is because when PowerSync makes data for a given priority available,
1187
+ * all buckets in higher-priorities are guaranteed to be consistent with that checkpoint.
1188
+ *
1189
+ * For example, if PowerSync just finished synchronizing buckets in priority level 3, calling this method
1190
+ * with a priority of 1 may return information for priority level 3.
1191
+ *
1192
+ * @param {number} priority The bucket priority for which the status should be reported
1193
+ * @returns {SyncPriorityStatus} Status information for the requested priority level or the next higher level with available status
1326
1194
  */
1327
- get fetch(): FetchImplementation;
1195
+ statusForPriority(priority: number): SyncPriorityStatus;
1328
1196
  /**
1329
- * Get credentials currently cached, or fetch new credentials if none are
1330
- * available.
1197
+ * Compares this SyncStatus instance with another to determine if they are equal.
1198
+ * Equality is determined by comparing the serialized JSON representation of both instances.
1331
1199
  *
1332
- * These credentials may have expired already.
1200
+ * @param {SyncStatus} status The SyncStatus instance to compare against
1201
+ * @returns {boolean} True if the instances are considered equal, false otherwise
1333
1202
  */
1334
- getCredentials(): Promise<PowerSyncCredentials | null>;
1203
+ isEqual(status: SyncStatus): boolean;
1335
1204
  /**
1336
- * Fetch a new set of credentials and cache it.
1337
- *
1338
- * Until this call succeeds, `getCredentials` will still return the
1339
- * old credentials.
1205
+ * Creates a human-readable string representation of the current sync status.
1206
+ * Includes information about connection state, sync completion, and data flow.
1340
1207
  *
1341
- * This may be called before the current credentials have expired.
1208
+ * @returns {string} A string representation of the sync status
1342
1209
  */
1343
- prefetchCredentials(): Promise<PowerSyncCredentials | null>;
1210
+ getMessage(): string;
1344
1211
  /**
1345
- * Get credentials for PowerSync.
1212
+ * Serializes the SyncStatus instance to a plain object.
1346
1213
  *
1347
- * This should always fetch a fresh set of credentials - don't use cached
1348
- * values.
1214
+ * @returns {SyncStatusOptions} A plain object representation of the sync status
1349
1215
  */
1350
- fetchCredentials(): Promise<PowerSyncCredentials | null>;
1351
- /***
1352
- * Immediately invalidate credentials.
1353
- *
1354
- * This may be called when the current credentials have expired.
1216
+ toJSON(): SyncStatusOptions;
1217
+ private static comparePriorities;
1218
+ }
1219
+
1220
+ declare class UploadQueueStats {
1221
+ /**
1222
+ * Number of records in the upload queue.
1355
1223
  */
1356
- invalidateCredentials(): void;
1357
- getUserAgent(): string;
1358
- protected buildRequest(path: string): Promise<{
1359
- url: string;
1360
- headers: {
1361
- 'content-type': string;
1362
- Authorization: string;
1363
- 'x-user-agent': string;
1364
- };
1365
- }>;
1366
- post(path: string, data: any, headers?: Record<string, string>): Promise<any>;
1367
- get(path: string, headers?: Record<string, string>): Promise<any>;
1224
+ count: number;
1368
1225
  /**
1369
- * Provides a BSON implementation. The import nature of this varies depending on the platform
1226
+ * Size of the upload queue in bytes.
1370
1227
  */
1371
- abstract getBSON(): Promise<BSONImplementation>;
1372
- protected createSocket(url: string): WebSocket;
1228
+ size: number | null;
1229
+ constructor(
1373
1230
  /**
1374
- * Returns a data stream of sync line data.
1375
- *
1376
- * @param map Maps received payload frames to the typed event value.
1377
- * @param bson A BSON encoder and decoder. When set, the data stream will be requested with a BSON payload
1378
- * (required for compatibility with older sync services).
1231
+ * Number of records in the upload queue.
1379
1232
  */
1380
- socketStreamRaw<T>(options: SocketSyncStreamOptions, map: (buffer: Uint8Array) => T, bson?: typeof BSON): Promise<DataStream<T>>;
1233
+ count: number,
1381
1234
  /**
1382
- * Connects to the sync/stream http endpoint, mapping and emitting each received string line.
1235
+ * Size of the upload queue in bytes.
1383
1236
  */
1384
- postStreamRaw<T>(options: SyncStreamOptions, mapLine: (line: string) => T): Promise<DataStream<T>>;
1237
+ size?: number | null);
1238
+ toString(): string;
1385
1239
  }
1386
1240
 
1387
- declare enum LockType {
1388
- CRUD = "crud",
1389
- SYNC = "sync"
1241
+ declare enum ColumnType {
1242
+ TEXT = "TEXT",
1243
+ INTEGER = "INTEGER",
1244
+ REAL = "REAL"
1390
1245
  }
1391
- declare enum SyncStreamConnectionMethod {
1392
- HTTP = "http",
1393
- WEB_SOCKET = "web-socket"
1246
+ interface ColumnOptions {
1247
+ name: string;
1248
+ type?: ColumnType;
1394
1249
  }
1395
- declare enum SyncClientImplementation {
1250
+ type BaseColumnType<T extends number | string | null> = {
1251
+ type: ColumnType;
1252
+ };
1253
+ type ColumnsType = Record<string, BaseColumnType<any>>;
1254
+ type ExtractColumnValueType<T extends BaseColumnType<any>> = T extends BaseColumnType<infer R> ? R : unknown;
1255
+ declare const MAX_AMOUNT_OF_COLUMNS = 1999;
1256
+ declare const column: {
1257
+ text: BaseColumnType<string | null>;
1258
+ integer: BaseColumnType<number | null>;
1259
+ real: BaseColumnType<number | null>;
1260
+ };
1261
+ declare class Column {
1262
+ protected options: ColumnOptions;
1263
+ constructor(options: ColumnOptions);
1264
+ get name(): string;
1265
+ get type(): ColumnType | undefined;
1266
+ toJSON(): {
1267
+ name: string;
1268
+ type: ColumnType | undefined;
1269
+ };
1270
+ }
1271
+
1272
+ /**
1273
+ * A pending variant of a {@link RawTable} that doesn't have a name (because it would be inferred when creating the
1274
+ * schema).
1275
+ */
1276
+ type RawTableType = {
1396
1277
  /**
1397
- * Decodes and handles sync lines received from the sync service in JavaScript.
1398
- *
1399
- * This is the default option.
1400
- *
1401
- * @deprecated Don't use {@link SyncClientImplementation.JAVASCRIPT} directly. Instead, use
1402
- * {@link DEFAULT_SYNC_CLIENT_IMPLEMENTATION} or omit the option. The explicit choice to use
1403
- * the JavaScript-based sync implementation will be removed from a future version of the SDK.
1278
+ * The statement to run when PowerSync detects that a row needs to be inserted or updated.
1404
1279
  */
1405
- JAVASCRIPT = "js",
1280
+ put: PendingStatement;
1406
1281
  /**
1407
- * This implementation offloads the sync line decoding and handling into the PowerSync
1408
- * core extension.
1409
- *
1410
- * @experimental
1411
- * While this implementation is more performant than {@link SyncClientImplementation.JAVASCRIPT},
1412
- * it has seen less real-world testing and is marked as __experimental__ at the moment.
1413
- *
1414
- * ## Compatibility warning
1415
- *
1416
- * The Rust sync client stores sync data in a format that is slightly different than the one used
1417
- * by the old {@link JAVASCRIPT} implementation. When adopting the {@link RUST} client on existing
1418
- * databases, the PowerSync SDK will migrate the format automatically.
1419
- * Further, the {@link JAVASCRIPT} client in recent versions of the PowerSync JS SDK (starting from
1420
- * the version introducing {@link RUST} as an option) also supports the new format, so you can switch
1421
- * back to {@link JAVASCRIPT} later.
1422
- *
1423
- * __However__: Upgrading the SDK version, then adopting {@link RUST} as a sync client and later
1424
- * downgrading the SDK to an older version (necessarily using the JavaScript-based implementation then)
1425
- * can lead to sync issues.
1282
+ * The statement to run when PowerSync detects that a row needs to be deleted.
1426
1283
  */
1427
- RUST = "rust"
1428
- }
1284
+ delete: PendingStatement;
1285
+ };
1429
1286
  /**
1430
- * The default {@link SyncClientImplementation} to use.
1287
+ * A parameter to use as part of {@link PendingStatement}.
1431
1288
  *
1432
- * Please use this field instead of {@link SyncClientImplementation.JAVASCRIPT} directly. A future version
1433
- * of the PowerSync SDK will enable {@link SyncClientImplementation.RUST} by default and remove the JavaScript
1434
- * option.
1435
- */
1436
- declare const DEFAULT_SYNC_CLIENT_IMPLEMENTATION = SyncClientImplementation.JAVASCRIPT;
1289
+ * For delete statements, only the `"Id"` value is supported - the sync client will replace it with the id of the row to
1290
+ * be synced.
1291
+ *
1292
+ * For insert and replace operations, the values of columns in the table are available as parameters through
1293
+ * `{Column: 'name'}`.
1294
+ */
1295
+ type PendingStatementParameter = 'Id' | {
1296
+ Column: string;
1297
+ };
1437
1298
  /**
1438
- * Abstract Lock to be implemented by various JS environments
1299
+ * A statement that the PowerSync client should use to insert or delete data into a table managed by the user.
1439
1300
  */
1440
- interface LockOptions<T> {
1441
- callback: () => Promise<T>;
1442
- type: LockType;
1443
- signal?: AbortSignal;
1444
- }
1445
- interface AbstractStreamingSyncImplementationOptions extends AdditionalConnectionOptions {
1446
- adapter: BucketStorageAdapter;
1447
- uploadCrud: () => Promise<void>;
1301
+ type PendingStatement = {
1302
+ sql: string;
1303
+ params: PendingStatementParameter[];
1304
+ };
1305
+ /**
1306
+ * Instructs PowerSync to sync data into a "raw" table.
1307
+ *
1308
+ * Since raw tables are not backed by JSON, running complex queries on them may be more efficient. Further, they allow
1309
+ * using client-side table and column constraints.
1310
+ *
1311
+ * To collect local writes to raw tables with PowerSync, custom triggers are required. See
1312
+ * {@link https://docs.powersync.com/usage/use-case-examples/raw-tables the documentation} for details and an example on
1313
+ * using raw tables.
1314
+ *
1315
+ * Note that raw tables are only supported when using the new `SyncClientImplementation.rust` sync client.
1316
+ *
1317
+ * @experimental Please note that this feature is experimental at the moment, and not covered by PowerSync semver or
1318
+ * stability guarantees.
1319
+ */
1320
+ declare class RawTable implements RawTableType {
1448
1321
  /**
1449
- * An identifier for which PowerSync DB this sync implementation is
1450
- * linked to. Most commonly DB name, but not restricted to DB name.
1322
+ * The name of the table.
1323
+ *
1324
+ * This does not have to match the actual table name in the schema - {@link put} and {@link delete} are free to use
1325
+ * another table. Instead, this name is used by the sync client to recognize that operations on this table (as it
1326
+ * appears in the source / backend database) are to be handled specially.
1451
1327
  */
1452
- identifier?: string;
1453
- logger?: ILogger;
1454
- remote: AbstractRemote;
1328
+ name: string;
1329
+ put: PendingStatement;
1330
+ delete: PendingStatement;
1331
+ constructor(name: string, type: RawTableType);
1455
1332
  }
1456
- interface StreamingSyncImplementationListener extends BaseListener {
1457
- /**
1458
- * Triggered whenever a status update has been attempted to be made or
1459
- * refreshed.
1460
- */
1461
- statusUpdated?: ((statusUpdate: SyncStatusOptions) => void) | undefined;
1462
- /**
1463
- * Triggers whenever the status' members have changed in value
1464
- */
1465
- statusChanged?: ((status: SyncStatus) => void) | undefined;
1333
+
1334
+ interface IndexColumnOptions {
1335
+ name: string;
1336
+ ascending?: boolean;
1337
+ }
1338
+ declare const DEFAULT_INDEX_COLUMN_OPTIONS: Partial<IndexColumnOptions>;
1339
+ declare class IndexedColumn {
1340
+ protected options: IndexColumnOptions;
1341
+ static createAscending(column: string): IndexedColumn;
1342
+ constructor(options: IndexColumnOptions);
1343
+ get name(): string;
1344
+ get ascending(): boolean | undefined;
1345
+ toJSON(table: Table): {
1346
+ name: string;
1347
+ ascending: boolean | undefined;
1348
+ type: ColumnType;
1349
+ };
1350
+ }
1351
+
1352
+ interface IndexOptions {
1353
+ name: string;
1354
+ columns?: IndexedColumn[];
1466
1355
  }
1356
+ declare const DEFAULT_INDEX_OPTIONS: Partial<IndexOptions>;
1357
+ declare class Index {
1358
+ protected options: IndexOptions;
1359
+ static createAscending(options: IndexOptions, columnNames: string[]): Index;
1360
+ constructor(options: IndexOptions);
1361
+ get name(): string;
1362
+ get columns(): IndexedColumn[];
1363
+ toJSON(table: Table): {
1364
+ name: string;
1365
+ columns: {
1366
+ name: string;
1367
+ ascending: boolean | undefined;
1368
+ type: ColumnType;
1369
+ }[];
1370
+ };
1371
+ }
1372
+
1467
1373
  /**
1468
- * Configurable options to be used when connecting to the PowerSync
1469
- * backend instance.
1374
+ Generate a new table from the columns and indexes
1375
+ @deprecated You should use {@link Table} instead as it now allows TableV2 syntax.
1376
+ This will be removed in the next major release.
1377
+ */
1378
+ declare class TableV2<Columns extends ColumnsType = ColumnsType> extends Table<Columns> {
1379
+ }
1380
+
1381
+ interface SharedTableOptions {
1382
+ localOnly?: boolean;
1383
+ insertOnly?: boolean;
1384
+ viewName?: string;
1385
+ trackPrevious?: boolean | TrackPreviousOptions;
1386
+ trackMetadata?: boolean;
1387
+ ignoreEmptyUpdates?: boolean;
1388
+ }
1389
+ /** Whether to include previous column values when PowerSync tracks local changes.
1390
+ *
1391
+ * Including old values may be helpful for some backend connector implementations, which is
1392
+ * why it can be enabled on per-table or per-columm basis.
1470
1393
  */
1471
- type PowerSyncConnectionOptions = Omit<InternalConnectionOptions, 'serializedSchema'>;
1472
- interface InternalConnectionOptions extends BaseConnectionOptions, AdditionalConnectionOptions {
1394
+ interface TrackPreviousOptions {
1395
+ /** When defined, a list of column names for which old values should be tracked. */
1396
+ columns?: string[];
1397
+ /** When enabled, only include values that have actually been changed by an update. */
1398
+ onlyWhenChanged?: boolean;
1473
1399
  }
1474
- /** @internal */
1475
- interface BaseConnectionOptions {
1476
- /**
1477
- * Whether to use a JavaScript implementation to handle received sync lines from the sync
1478
- * service, or whether this work should be offloaded to the PowerSync core extension.
1479
- *
1480
- * This defaults to the JavaScript implementation ({@link SyncClientImplementation.JAVASCRIPT})
1481
- * since the ({@link SyncClientImplementation.RUST}) implementation is experimental at the moment.
1482
- */
1483
- clientImplementation?: SyncClientImplementation;
1484
- /**
1485
- * The connection method to use when streaming updates from
1486
- * the PowerSync backend instance.
1487
- * Defaults to a HTTP streaming connection.
1488
- */
1489
- connectionMethod?: SyncStreamConnectionMethod;
1490
- /**
1491
- * The fetch strategy to use when streaming updates from the PowerSync backend instance.
1492
- */
1493
- fetchStrategy?: FetchStrategy;
1494
- /**
1495
- * These parameters are passed to the sync rules, and will be available under the`user_parameters` object.
1496
- */
1497
- params?: Record<string, StreamingSyncRequestParameterType>;
1400
+ interface TableOptions extends SharedTableOptions {
1498
1401
  /**
1499
- * The serialized schema - mainly used to forward information about raw tables to the sync client.
1402
+ * The synced table name, matching sync rules
1500
1403
  */
1501
- serializedSchema?: any;
1404
+ name: string;
1405
+ columns: Column[];
1406
+ indexes?: Index[];
1502
1407
  }
1503
- /** @internal */
1504
- interface AdditionalConnectionOptions {
1505
- /**
1506
- * Delay for retrying sync streaming operations
1507
- * from the PowerSync backend after an error occurs.
1508
- */
1509
- retryDelayMs?: number;
1408
+ type RowType<T extends TableV2<any>> = {
1409
+ [K in keyof T['columnMap']]: ExtractColumnValueType<T['columnMap'][K]>;
1410
+ } & {
1411
+ id: string;
1412
+ };
1413
+ type IndexShorthand = Record<string, string[]>;
1414
+ interface TableV2Options extends SharedTableOptions {
1415
+ indexes?: IndexShorthand;
1416
+ }
1417
+ declare const DEFAULT_TABLE_OPTIONS: {
1418
+ indexes: never[];
1419
+ insertOnly: boolean;
1420
+ localOnly: boolean;
1421
+ trackPrevious: boolean;
1422
+ trackMetadata: boolean;
1423
+ ignoreEmptyUpdates: boolean;
1424
+ };
1425
+ declare const InvalidSQLCharacters: RegExp;
1426
+ declare class Table<Columns extends ColumnsType = ColumnsType> {
1427
+ protected options: TableOptions;
1428
+ protected _mappedColumns: Columns;
1429
+ static createLocalOnly(options: TableOptions): Table<ColumnsType>;
1430
+ static createInsertOnly(options: TableOptions): Table<ColumnsType>;
1510
1431
  /**
1511
- * Backend Connector CRUD operations are throttled
1512
- * to occur at most every `crudUploadThrottleMs`
1513
- * milliseconds.
1432
+ * Create a table.
1433
+ * @deprecated This was only only included for TableV2 and is no longer necessary.
1434
+ * Prefer to use new Table() directly.
1435
+ *
1436
+ * TODO remove in the next major release.
1514
1437
  */
1515
- crudUploadThrottleMs?: number;
1516
- }
1517
- /** @internal */
1518
- type RequiredAdditionalConnectionOptions = Required<AdditionalConnectionOptions>;
1519
- interface StreamingSyncImplementation extends BaseObserverInterface<StreamingSyncImplementationListener>, Disposable {
1438
+ static createTable(name: string, table: Table): Table<ColumnsType>;
1520
1439
  /**
1521
- * Connects to the sync service
1440
+ * Creates a new Table instance.
1441
+ *
1442
+ * This constructor supports two different versions:
1443
+ * 1. New constructor: Using a Columns object and an optional TableV2Options object
1444
+ * 2. Deprecated constructor: Using a TableOptions object (will be removed in the next major release)
1445
+ *
1446
+ * @constructor
1447
+ * @param {Columns | TableOptions} optionsOrColumns - Either a Columns object (for V2 syntax) or a TableOptions object (for V1 syntax)
1448
+ * @param {TableV2Options} [v2Options] - Optional configuration options for V2 syntax
1449
+ *
1450
+ * @example
1451
+ * ```javascript
1452
+ * // New Constructor
1453
+ * const table = new Table(
1454
+ * {
1455
+ * name: column.text,
1456
+ * age: column.integer
1457
+ * },
1458
+ * { indexes: { nameIndex: ['name'] } }
1459
+ * );
1460
+ *```
1461
+ *
1462
+ *
1463
+ * @example
1464
+ * ```javascript
1465
+ * // Deprecated Constructor
1466
+ * const table = new Table({
1467
+ * name: 'users',
1468
+ * columns: [
1469
+ * new Column({ name: 'name', type: ColumnType.TEXT }),
1470
+ * new Column({ name: 'age', type: ColumnType.INTEGER })
1471
+ * ]
1472
+ * });
1473
+ *```
1522
1474
  */
1523
- connect(options?: InternalConnectionOptions): Promise<void>;
1475
+ constructor(columns: Columns, options?: TableV2Options);
1524
1476
  /**
1525
- * Disconnects from the sync services.
1526
- * @throws if not connected or if abort is not controlled internally
1477
+ * @deprecated This constructor will be removed in the next major release.
1478
+ * Use the new constructor shown below instead as this does not show types.
1479
+ * @example
1480
+ * <caption>Use this instead</caption>
1481
+ * ```javascript
1482
+ * const table = new Table(
1483
+ * {
1484
+ * name: column.text,
1485
+ * age: column.integer
1486
+ * },
1487
+ * { indexes: { nameIndex: ['name'] } }
1488
+ * );
1489
+ *```
1527
1490
  */
1528
- disconnect(): Promise<void>;
1529
- getWriteCheckpoint: () => Promise<string>;
1530
- hasCompletedSync: () => Promise<boolean>;
1531
- isConnected: boolean;
1532
- lastSyncedAt?: Date;
1533
- syncStatus: SyncStatus;
1534
- triggerCrudUpload: () => void;
1535
- waitForReady(): Promise<void>;
1536
- waitForStatus(status: SyncStatusOptions): Promise<void>;
1537
- waitUntilStatusMatches(predicate: (status: SyncStatus) => boolean): Promise<void>;
1491
+ constructor(options: TableOptions);
1492
+ copyWithName(name: string): Table;
1493
+ private isTableV1;
1494
+ private initTableV1;
1495
+ private initTableV2;
1496
+ private applyDefaultOptions;
1497
+ get name(): string;
1498
+ get viewNameOverride(): string | undefined;
1499
+ get viewName(): string;
1500
+ get columns(): Column[];
1501
+ get columnMap(): Columns;
1502
+ get indexes(): Index[];
1503
+ get localOnly(): boolean;
1504
+ get insertOnly(): boolean;
1505
+ get trackPrevious(): boolean | TrackPreviousOptions;
1506
+ get trackMetadata(): boolean;
1507
+ get ignoreEmptyUpdates(): boolean;
1508
+ get internalName(): string;
1509
+ get validName(): boolean;
1510
+ validate(): void;
1511
+ toJSON(): {
1512
+ name: string;
1513
+ view_name: string;
1514
+ local_only: boolean;
1515
+ insert_only: boolean;
1516
+ include_old: any;
1517
+ include_old_only_when_changed: boolean;
1518
+ include_metadata: boolean;
1519
+ ignore_empty_update: boolean;
1520
+ columns: {
1521
+ name: string;
1522
+ type: ColumnType | undefined;
1523
+ }[];
1524
+ indexes: {
1525
+ name: string;
1526
+ columns: {
1527
+ name: string;
1528
+ ascending: boolean | undefined;
1529
+ type: ColumnType;
1530
+ }[];
1531
+ }[];
1532
+ };
1538
1533
  }
1539
- declare const DEFAULT_CRUD_UPLOAD_THROTTLE_MS = 1000;
1540
- declare const DEFAULT_RETRY_DELAY_MS = 5000;
1541
- declare const DEFAULT_STREAMING_SYNC_OPTIONS: {
1542
- retryDelayMs: number;
1543
- crudUploadThrottleMs: number;
1534
+
1535
+ type SchemaType = Record<string, Table<any>>;
1536
+ type SchemaTableType<S extends SchemaType> = {
1537
+ [K in keyof S]: RowType<S[K]>;
1544
1538
  };
1545
- type RequiredPowerSyncConnectionOptions = Required<BaseConnectionOptions>;
1546
- declare const DEFAULT_STREAM_CONNECTION_OPTIONS: RequiredPowerSyncConnectionOptions;
1547
- declare abstract class AbstractStreamingSyncImplementation extends BaseObserver<StreamingSyncImplementationListener> implements StreamingSyncImplementation {
1548
- protected _lastSyncedAt: Date | null;
1549
- protected options: AbstractStreamingSyncImplementationOptions;
1550
- protected abortController: AbortController | null;
1551
- protected uploadAbortController: AbortController | null;
1552
- protected crudUpdateListener?: () => void;
1553
- protected streamingSyncPromise?: Promise<void>;
1554
- protected logger: ILogger;
1555
- private isUploadingCrud;
1556
- private notifyCompletedUploads?;
1557
- syncStatus: SyncStatus;
1558
- triggerCrudUpload: () => void;
1559
- constructor(options: AbstractStreamingSyncImplementationOptions);
1560
- waitForReady(): Promise<void>;
1561
- waitForStatus(status: SyncStatusOptions): Promise<void>;
1562
- waitUntilStatusMatches(predicate: (status: SyncStatus) => boolean): Promise<void>;
1563
- get lastSyncedAt(): Date | undefined;
1564
- get isConnected(): boolean;
1565
- dispose(): Promise<void>;
1566
- abstract obtainLock<T>(lockOptions: LockOptions<T>): Promise<T>;
1567
- hasCompletedSync(): Promise<boolean>;
1568
- getWriteCheckpoint(): Promise<string>;
1569
- protected _uploadAllCrud(): Promise<void>;
1570
- connect(options?: PowerSyncConnectionOptions): Promise<void>;
1571
- disconnect(): Promise<void>;
1539
+ /**
1540
+ * A schema is a collection of tables. It is used to define the structure of a database.
1541
+ */
1542
+ declare class Schema<S extends SchemaType = SchemaType> {
1543
+ readonly types: SchemaTableType<S>;
1544
+ readonly props: S;
1545
+ readonly tables: Table[];
1546
+ readonly rawTables: RawTable[];
1547
+ constructor(tables: Table[] | S);
1572
1548
  /**
1573
- * @deprecated use [connect instead]
1549
+ * Adds raw tables to this schema. Raw tables are identified by their name, but entirely managed by the application
1550
+ * developer instead of automatically by PowerSync.
1551
+ * Since raw tables are not backed by JSON, running complex queries on them may be more efficient. Further, they allow
1552
+ * using client-side table and column constraints.
1553
+ * Note that raw tables are only supported when using the new `SyncClientImplementation.rust` sync client.
1554
+ *
1555
+ * @param tables An object of (table name, raw table definition) entries.
1556
+ * @experimental Note that the raw tables API is still experimental and may change in the future.
1574
1557
  */
1575
- streamingSync(signal?: AbortSignal, options?: PowerSyncConnectionOptions): Promise<void>;
1576
- private collectLocalBucketState;
1577
- /**
1578
- * Older versions of the JS SDK used to encode subkeys as JSON in {@link OplogEntry.toJSON}.
1579
- * Because subkeys are always strings, this leads to quotes being added around them in `ps_oplog`.
1580
- * While this is not a problem as long as it's done consistently, it causes issues when a database
1581
- * created by the JS SDK is used with other SDKs, or (more likely) when the new Rust sync client
1582
- * is enabled.
1558
+ withRawTables(tables: Record<string, RawTableType>): void;
1559
+ validate(): void;
1560
+ toJSON(): {
1561
+ tables: {
1562
+ name: string;
1563
+ view_name: string;
1564
+ local_only: boolean;
1565
+ insert_only: boolean;
1566
+ include_old: any;
1567
+ include_old_only_when_changed: boolean;
1568
+ include_metadata: boolean;
1569
+ ignore_empty_update: boolean;
1570
+ columns: {
1571
+ name: string;
1572
+ type: ColumnType | undefined;
1573
+ }[];
1574
+ indexes: {
1575
+ name: string;
1576
+ columns: {
1577
+ name: string;
1578
+ ascending: boolean | undefined;
1579
+ type: ColumnType;
1580
+ }[];
1581
+ }[];
1582
+ }[];
1583
+ raw_tables: RawTable[];
1584
+ };
1585
+ private convertToClassicTables;
1586
+ }
1587
+
1588
+ interface PowerSyncBackendConnector {
1589
+ /** Allows the PowerSync client to retrieve an authentication token from your backend
1590
+ * which is used to authenticate against the PowerSync service.
1583
1591
  *
1584
- * So, we add a migration from the old key format (with quotes) to the new one (no quotes). The
1585
- * migration is only triggered when necessary (for now). The function returns whether the new format
1586
- * should be used, so that the JS SDK is able to write to updated databases.
1592
+ * This should always fetch a fresh set of credentials - don't use cached
1593
+ * values.
1587
1594
  *
1588
- * @param requireFixedKeyFormat Whether we require the new format or also support the old one.
1589
- * The Rust client requires the new subkey format.
1590
- * @returns Whether the database is now using the new, fixed subkey format.
1595
+ * Return null if the user is not signed in. Throw an error if credentials
1596
+ * cannot be fetched due to a network error or other temporary error.
1597
+ *
1598
+ * This token is kept for the duration of a sync connection.
1591
1599
  */
1592
- private requireKeyFormat;
1593
- protected streamingSyncIteration(signal: AbortSignal, options?: PowerSyncConnectionOptions): Promise<void>;
1594
- private legacyStreamingSyncIteration;
1595
- private rustSyncIteration;
1596
- private updateSyncStatusForStartingCheckpoint;
1597
- private applyCheckpoint;
1598
- protected updateSyncStatus(options: SyncStatusOptions): void;
1599
- private delayRetry;
1600
+ fetchCredentials: () => Promise<PowerSyncCredentials | null>;
1601
+ /** Upload local changes to the app backend.
1602
+ *
1603
+ * Use {@link AbstractPowerSyncDatabase.getCrudBatch} to get a batch of changes to upload.
1604
+ *
1605
+ * Any thrown errors will result in a retry after the configured wait period (default: 5 seconds).
1606
+ */
1607
+ uploadData: (database: AbstractPowerSyncDatabase) => Promise<void>;
1600
1608
  }
1601
1609
 
1602
1610
  /**