@@ -191,40 +191,6 @@ public protocol PowerSyncDatabaseProtocol: Queries, Sendable {
191191 options: ConnectOptions ?
192192 ) async throws
193193
194- /// Get a batch of crud data to upload.
195- ///
196- /// Returns nil if there is no data to upload.
197- ///
198- /// Use this from the `PowerSyncBackendConnector.uploadData` callback.
199- ///
200- /// Once the data have been successfully uploaded, call `CrudBatch.complete` before
201- /// requesting the next batch.
202- ///
203- /// - Parameter limit: Maximum number of updates to return in a single batch. Default is 100.
204- ///
205- /// This method does include transaction ids in the result, but does not group
206- /// data by transaction. One batch may contain data from multiple transactions,
207- /// and a single transaction may be split over multiple batches.
208- func getCrudBatch( limit: Int32 ) async throws -> CrudBatch ?
209-
210- /// Obtains an async iterator of completed transactions with local writes against the database.
211- ///
212- /// This is typically used from the ``PowerSyncBackendConnectorProtocol/uploadData(database:)`` callback.
213- /// Each entry emitted by teh returned flow is a full transaction containing all local writes made while that transaction was
214- /// active.
215- ///
216- /// Unlike ``getNextCrudTransaction()``, which always returns the oldest transaction that hasn't been
217- /// ``CrudTransaction/complete()``d yet, this iterator can be used to upload multiple transactions.
218- /// Calling ``CrudTransaction/complete()`` will mark that and all prior transactions returned by this iterator as
219- /// completed.
220- ///
221- /// This can be used to upload multiple transactions in a single batch, e.g. with
222- ///
223- /// ```Swift
224- ///
225- /// ```
226- func getCrudTransactions( ) -> any CrudTransactions
227-
228194 /// Convenience method to get the current version of PowerSync.
229195 func getPowerSyncVersion( ) async throws -> String
230196
@@ -343,9 +309,60 @@ public extension PowerSyncDatabaseProtocol {
343309 try await disconnectAndClear ( clearLocal: true , soft: soft)
344310 }
345311
312+ /// Get a batch of crud data to upload.
313+ ///
314+ /// Returns nil if there is no data to upload.
315+ ///
316+ /// Use this from the `PowerSyncBackendConnector.uploadData` callback.
317+ ///
318+ /// Once the data have been successfully uploaded, call `CrudBatch.complete` before
319+ /// requesting the next batch.
320+ ///
321+ /// - Parameter limit: Maximum number of updates to return in a single batch. Default is 100.
322+ ///
323+ /// This method does include transaction ids in the result, but does not group
324+ /// data by transaction. One batch may contain data from multiple transactions,
325+ /// and a single transaction may be split over multiple batches.
346326 func getCrudBatch( limit: Int32 = 100 ) async throws -> CrudBatch ? {
347- try await getCrudBatch (
348- limit: limit
327+ var entries = try await getAll (
328+ sql: " SELECT id, tx_id, data FROM ps_crud ORDER BY id ASC LIMIT ? " ,
329+ parameters: [ Int64 ( limit + 1 ) ] ,
330+ mapper: CrudEntry . fromCursor
349331 )
332+
333+ if entries. isEmpty {
334+ return nil
335+ }
336+
337+ let hasMore = entries. count > limit
338+ if hasMore {
339+ entries. removeLast ( )
340+ }
341+
342+ return CrudBatch (
343+ hasMore: hasMore,
344+ crud: entries,
345+ db: self
346+ )
347+ }
348+
349+ /// Obtains an async iterator of completed transactions with local writes against the database.
350+ ///
351+ /// This is typically used from the ``PowerSyncBackendConnectorProtocol/uploadData(database:)`` callback.
352+ /// Each entry emitted by teh returned flow is a full transaction containing all local writes made while that transaction was
353+ /// active.
354+ ///
355+ /// Unlike ``getNextCrudTransaction()``, which always returns the oldest transaction that hasn't been
356+ /// ``CrudTransaction/complete()``d yet, this iterator can be used to upload multiple transactions.
357+ /// Calling ``CrudTransaction/complete()`` will mark that and all prior transactions returned by this iterator as
358+ /// completed.
359+ ///
360+ /// This can be used to upload multiple transactions in a single batch, e.g. with
361+ ///
362+ /// ```Swift
363+ ///
364+ /// ```
365+ func getCrudTransactions( ) -> CrudTransactions {
366+ CrudTransactions ( db: self )
350367 }
351368}
0 commit comments