Skip to content

Latest commit

 

History

History
570 lines (331 loc) · 14.9 KB

SynchronizableCollection.md

File metadata and controls

570 lines (331 loc) · 14.9 KB

collection-sync / Exports / SynchronizableCollection

Class: SynchronizableCollection

Implements

Table of contents

Constructors

Properties

Accessors

Methods

Constructors

constructor

new SynchronizableCollection(syncMetadata)

Parameters

Name Type
syncMetadata CollectionSyncMetadata

Defined in

SynchronizableCollection.ts:24

Properties

_parent

Private Optional _parent: Collection

Defined in

SynchronizableCollection.ts:20


defaultSyncOptions

Private Readonly defaultSyncOptions: SyncOptions

Defined in

SynchronizableCollection.ts:13


syncMetadata

syncMetadata: CollectionSyncMetadata

Defined in

SynchronizableCollection.ts:22


synchronizers

Private synchronizers: Synchronizer[] = []

Store history of sync operations.

Defined in

SynchronizableCollection.ts:18

Accessors

lastSynchronizer

get lastSynchronizer(): undefined | Synchronizer

Returns

undefined | Synchronizer

Defined in

SynchronizableCollection.ts:77


parent

get parent(): undefined | Collection

Returns

undefined | Collection

Defined in

SynchronizableCollection.ts:73

set parent(p): void

Parameters

Name Type
p undefined | Collection

Returns

void

Defined in

SynchronizableCollection.ts:69

Methods

cleanUp

cleanUp(_synchronizer): Promise<void>

Executed at the end of each sync operation (whether it succeeded or not). It's recommended to implement cleaning logic if necessary.

Parameters

Name Type
_synchronizer Synchronizer

Returns

Promise<void>

Defined in

SynchronizableCollection.ts:63


commitSync

commitSync(_itemsToSync, _ignoredItems, _conflictItems): Promise<boolean>

Commits the sync operation. Database engines that don't support this should implement a method that returns true (because the data was already added without the need for a commit statement). Make sure to commit the data from one specific sync process in order to avoid committing data pushed by multiple users synchronizing at the same time.

Parameters

Name Type
_itemsToSync SyncItem[]
_ignoredItems SyncItem[]
_conflictItems SyncItem[]

Returns

Promise<boolean>

Implementation of

Collection.commitSync

Defined in

SynchronizableCollection.ts:52


countAll

Abstract countAll(): number | Promise<number>

Gets the number of items in the collection.

Returns

number | Promise<number>

Implementation of

Collection.countAll

Defined in

SynchronizableCollection.ts:28


findByIds

Abstract findByIds(ids): SyncItem[] | Promise<SyncItem[]>

Returns a list of records using an ID list as search query.

Parameters

Name Type
ids DocId[]

Returns

SyncItem[] | Promise<SyncItem[]>

Implementation of

Collection.findByIds

Defined in

SynchronizableCollection.ts:29


initialize

Abstract initialize(): Promise<void>

Executes async logic to initialize collection or datastore (open file, create database connection, etc).

Returns

Promise<void>

Implementation of

Collection.initialize

Defined in

SynchronizableCollection.ts:33


itemsNewerThan

Abstract itemsNewerThan(date, limit): SyncItem[] | Promise<SyncItem[]>

Returns a list of items that have updatedAt greater than argument provided. The list MUST be ordered by updatedAt ASC, otherwise an exception will be thrown (no syncing will be executed).

Parameters

Name Type
date undefined | Date
limit number

Returns

SyncItem[] | Promise<SyncItem[]>

Implementation of

Collection.itemsNewerThan

Defined in

SynchronizableCollection.ts:31


itemsToFetch

Private itemsToFetch(lastSyncAt, limit): Promise<SyncItem[]>

Parameters

Name Type
lastSyncAt undefined | Date
limit number

Returns

Promise<SyncItem[]>

Defined in

SynchronizableCollection.ts:115


itemsToPost

Private itemsToPost(lastSyncAt, limit): Promise<SyncItem[]>

Parameters

Name Type
lastSyncAt undefined | Date
limit number

Returns

Promise<SyncItem[]>

Defined in

SynchronizableCollection.ts:119


itemsToSync

itemsToSync(syncOperation, limit): Promise<SyncItem[]>

Gets list of items that can be synced (to either fetch or post).

Parameters

Name Type
syncOperation SyncOperation
limit number

Returns

Promise<SyncItem[]>

Defined in

SynchronizableCollection.ts:124


latestUpdatedItem

Abstract latestUpdatedItem(): undefined | SyncItem | Promise<undefined | SyncItem>

Gets the highest updateAt date in the collection.

Returns

undefined | SyncItem | Promise<undefined | SyncItem>

Implementation of

Collection.latestUpdatedItem

Defined in

SynchronizableCollection.ts:32


needsSync

needsSync(syncOperation): Promise<boolean>

Determines if synchronization is needed. This only performs a simple date check to see if the source collection has at least one item that's newer than the last sync date. This doesn't determine if items are actually different from those in the destination collection. This means that even if this method returns true, there may be occasions where no item is actually synced because they were identical. Checking which items have actually changed is more expensive, and usually it's recommended to be done while executing the actual sync, and in small batches (the last sync date is updated after every batch, meaning that after enough sync operations, this method will begin to return false).

One example of this can be seen when the local collection posts an item to the parent collection, updating the last post date, but not the fetch date. Then, when checking if fetch is necessary, it will return true because it just posted a new object and only based on dates, it will determine that the item has not been fetched yet. However when executing the fetch operation, all items will be ignored because that new item from the server collection is the same as the one in the local collection (since it was the local collection which posted it in the first place). This happens because last fetch and post dates are updated individually.

Parameters

Name Type
syncOperation SyncOperation

Returns

Promise<boolean>

Defined in

SynchronizableCollection.ts:100


preCommitSync

preCommitSync(_synchronizer): Promise<boolean>

Executes before committing the data. If this method returns false, then committing will be aborted. It will only commit the data if the return value is true.

Parameters

Name Type
_synchronizer Synchronizer

Returns

Promise<boolean>

Defined in

SynchronizableCollection.ts:48


preExecuteSync

preExecuteSync(_synchronizer): Promise<boolean>

Executes before starting to send the data to the destination collection. If this method returns false, syncing will be aborted, and will continue only if the return value is true.

Parameters

Name Type
_synchronizer Synchronizer

Returns

Promise<boolean>

Defined in

SynchronizableCollection.ts:40


rollbackSync

rollbackSync(_itemsToSync, _ignoredItems, _conflictItems): Promise<void>

Rollbacks the current data that's being synchronized. Make sure to rollback the data from one specific sync process in order to avoid discarding data pushed by multiple users synchronizing at the same time.

Parameters

Name Type
_itemsToSync SyncItem[]
_ignoredItems SyncItem[]
_conflictItems SyncItem[]

Returns

Promise<void>

Implementation of

Collection.rollbackSync

Defined in

SynchronizableCollection.ts:56


sync

sync(syncOperation, limit, options?, date?): Promise<Synchronizer>

Wraps sync operation so that cleanUp and rollback are conveniently placed at the end and always executed.

Parameters

Name Type
syncOperation SyncOperation
limit number
options SyncOptions
date? Date

Returns

Promise<Synchronizer>

Defined in

SynchronizableCollection.ts:147


syncAux

Private syncAux(synchronizer, syncOperation): Promise<void>

Parameters

Name Type
synchronizer Synchronizer
syncOperation SyncOperation

Returns

Promise<void>

Defined in

SynchronizableCollection.ts:173


syncBatch

Abstract syncBatch(items): SyncItem[] | Promise<SyncItem[]>

Syncs (upsert/delete) a batch (list) of items into this collection. Order of document processing doesn't need to be in any particular order.

Parameters

Name Type
items SyncItem[]

Returns

SyncItem[] | Promise<SyncItem[]>

Implementation of

Collection.syncBatch

Defined in

SynchronizableCollection.ts:30