Skip to content

Commit 86c1056

Browse files
committed
0.9.1 - improve 'syncUpAndUpdateCollection()' type signature
1 parent 6ab12c0 commit 86c1056

File tree

6 files changed

+74
-58
lines changed

6 files changed

+74
-58
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ This project does its best to adhere to [Semantic Versioning](http://semver.org/
44

55

66
--------
7-
### [0.9.0](N/A) - 2020-09-05
7+
### [0.9.1](N/A) - 2020-12-11
8+
#### Changed
9+
* Improve `syncUpAndUpdateCollection()` type signature to support either null `primaryKey` or null `primaryKeys` but not both
10+
11+
12+
--------
13+
### [0.9.0](https://github.com/TeamworkGuy2/lokijs-collections-syncing/commit/6ab12c05a18606eaa5c6ba1ce4fd1f89dcd73b63) - 2020-09-05
814
#### Changed
915
* Update to TypeScript 4.0
1016

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lokijs-collections-syncing",
3-
"version": "0.9.0",
3+
"version": "0.9.1",
44
"description": "lokijs-collections syncing to and from a remote data source",
55
"author": "TeamworkGuy2",
66
"homepage": "https://github.com/TeamworkGuy2/lokijs-collections-syncing",

sync/SyncDataCollection.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,6 @@ var SyncDataCollection = /** @class */ (function () {
177177
}
178178
return this.syncUpAndUpdateCollection(localColl, primaryKey, primaryKeys, convertAndSendItemsToServer);
179179
};
180-
/** A wrapper around a 'syncAction' function that syncs data to a destination.
181-
* This function loads the items from storage for 'syncAction' and is called back if the data transfer is successful or not.
182-
* If the transfer is successful, the items in storage are updated to reflect that a sync has occurred.
183-
* @template E the local collection data model. This type should contain deleted, synched, and last modified properties corresponding to the prop names passed to the constructor
184-
* @template F the local collection data model with optional properties. This type should contain deleted, synched, and last modified properties corresponding to the prop names passed to the constructor
185-
* @template R the 'syncAction' response type
186-
* @template S the remote data model. This type should contain deleted, synched, and last modified properties corresponding to the prop names passed to the constructor
187-
* @param dfd deferred object to reject or resolve once 'syncAction' has completed or failed
188-
* @param table the data source where data can be updated or retrieved
189-
* @param primaryKey the table data model's primary key, this or 'primaryKeys' must not be null, 'primaryKey' takes recedence
190-
* @param primaryKeys the table data model's primary keys, this or 'primaryKey' must not be null
191-
* @param syncAction the action which performs the data sync
192-
*/
193180
SyncDataCollection.prototype.syncUpAndUpdateCollection = function (table, primaryKey, primaryKeys, syncAction) {
194181
var self = this;
195182
var dfd = Defer.newDefer();

sync/SyncDataCollection.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ class SyncDataCollection {
4646
* @param [notifyActionEnd] optional event listener type function which is called whenever a sync action finishes, see {@link SyncAction}. Note: this method is only called if the action is successful, see 'notifyActionFailure'
4747
* @param [notifyActionFailure] optional event listener type function which is called whenever a sync action fails, see {@link SyncAction}
4848
*/
49-
constructor(getLastSyncDownTimestamp: (table: DataCollection<any, any>) => number, updateLastSyncDownTimestamp: (table: DataCollection<any, any>) => void,
50-
isDeletedPropName: string, isSynchedPropName: string, lastModifiedPropName: string,
49+
constructor(
50+
getLastSyncDownTimestamp: (table: DataCollection<any, any>) => number,
51+
updateLastSyncDownTimestamp: (table: DataCollection<any, any>) => void,
52+
isDeletedPropName: string,
53+
isSynchedPropName: string,
54+
lastModifiedPropName: string,
5155
notifyActionStart?: (action: SyncDataCollection.SyncAction, table: DataCollection<any, any>) => any,
5256
notifyActionEnd?: (action: SyncDataCollection.SyncAction, table: DataCollection<any, any>, startTimerKey: any) => void,
5357
notifyActionFailure?: (action: SyncDataCollection.SyncAction, table: DataCollection<any, any>, startTimerKey: any, err: any) => void
@@ -95,8 +99,10 @@ class SyncDataCollection {
9599
* @template R1 the sync down function error type
96100
* @template R2 the process results callback error type
97101
*/
98-
public syncDownCollection<E extends F, F, P, S, R1, R2>(params: P, table: DataCollection<E, F>, syncDownFunc: (params: P) => PsPromise<S, R1>,
99-
processResultsCallback: (results: S) => void | PsPromise<void, R2>): PsPromise<void, SyncError> {
102+
public syncDownCollection<E extends F, F, P, S, R1, R2>(params: P, table: DataCollection<E, F>,
103+
syncDownFunc: (params: P) => PsPromise<S, R1>,
104+
processResultsCallback: (results: S) => void | PsPromise<void, R2>
105+
): PsPromise<void, SyncError> {
100106
var self = this;
101107
var dfd = Defer.newDefer<void, SyncError>();
102108

@@ -235,6 +241,8 @@ class SyncDataCollection {
235241
* @param primaryKeys the table data model's primary keys, this or 'primaryKey' must not be null
236242
* @param syncAction the action which performs the data sync
237243
*/
244+
public syncUpAndUpdateCollection<E extends F, F, R, S>(table: DataCollection<E, F>, primaryKey: (keyof E) | null, primaryKeys: (keyof E)[], syncAction: (items: E[]) => PsPromise<R, S>): PsPromise<R | null, S>;
245+
public syncUpAndUpdateCollection<E extends F, F, R, S>(table: DataCollection<E, F>, primaryKey: (keyof E), primaryKeys: (keyof E)[] | null, syncAction: (items: E[]) => PsPromise<R, S>): PsPromise<R | null, S>;
238246
public syncUpAndUpdateCollection<E extends F, F, R, S>(table: DataCollection<E, F>, primaryKey: (keyof E) | null, primaryKeys: (keyof E)[], syncAction: (items: E[]) => PsPromise<R, S>): PsPromise<R | null, S> {
239247
var self = this;
240248
var dfd = Defer.newDefer<R | null, S>();
@@ -467,7 +475,6 @@ module SyncDataCollection {
467475

468476

469477

470-
471478
/** A utility function for picking a 'SyncDownOp' based on a set of flags describing the desired syncing behavior
472479
* @param clearData true if all existing local data should be deleted before syncing down new data, false to keep local data (with some cavets)
473480
* @param removeDeletedData true to remove local data marked deleted (based 'isDeletedPropName' values of true) before syncing down new data, false to keep deleted data

sync/SyncSettingsBuilder.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,21 @@ var SyncSettingsBuilder = /** @class */ (function () {
9696
inst.copyObjectFunc = settings.copyObjectFunc;
9797
return inst;
9898
};
99-
SyncSettingsBuilder.fromDataCollectionAndSyncFuncs = function (table, syncDownFunc, syncUpFunc) {
100-
var tableModel = table.getDataModel();
101-
var tableFuncs = table.getDataModelFuncs();
99+
SyncSettingsBuilder.fromDataCollectionAndSyncFuncs = function (localCollection, syncDownFunc, syncUpFunc) {
100+
var collModel = localCollection.getDataModel();
101+
var collFuncs = localCollection.getDataModelFuncs();
102102
var inst = new SyncSettingsBuilder();
103103
// sync settings
104-
inst.localCollection = table;
105-
inst.primaryKeys = tableModel.primaryKeys;
106-
inst.hasPrimaryKeyCheckers = tableModel.primaryKeys.map(function (k) { return function (itm) { return !!itm[k]; }; });
107-
inst.copyObjectFunc = tableFuncs.copyFunc;
104+
inst.localCollection = localCollection;
105+
inst.primaryKeys = collModel.primaryKeys;
106+
inst.hasPrimaryKeyCheckers = collModel.primaryKeys.map(function (k) { return function (itm) { return !!itm[k]; }; });
107+
inst.copyObjectFunc = collFuncs.copyFunc;
108108
// sync down
109109
inst.syncDownFunc = syncDownFunc;
110-
inst.toLocalObject = tableFuncs.toLocalObject;
110+
inst.toLocalObject = collFuncs.toLocalObject;
111111
// sync up
112112
inst.syncUpFunc = syncUpFunc;
113-
inst.toSvcObject = tableFuncs.toSvcObject;
113+
inst.toSvcObject = collFuncs.toSvcObject;
114114
return {
115115
addFilterFuncs: function (findFilterFunc) {
116116
inst.findFilterFunc = findFilterFunc;

sync/SyncSettingsBuilder.ts

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,13 @@ class SyncSettingsBuilder<E extends F, F, P, S, U, R> implements SettingsBuilder
4343
}
4444

4545

46-
public addSettings(localCollection: DataCollection<E, F>, primaryKeys: (keyof E & string)[], hasPrimaryKeyCheckers: ((obj: E) => boolean) | ((obj: E) => boolean)[],
47-
findFilterFunc: (item: any) => any,
48-
copyObjectFunc: (item: E) => E) {
49-
46+
public addSettings(
47+
localCollection: DataCollection<E, F>,
48+
primaryKeys: (keyof E & string)[],
49+
hasPrimaryKeyCheckers: ((obj: E) => boolean) | ((obj: E) => boolean)[],
50+
findFilterFunc: (item: any) => any,
51+
copyObjectFunc: (item: E) => E
52+
) {
5053
this.localCollection = localCollection;
5154
this.primaryKeys = primaryKeys;
5255
this.hasPrimaryKeyCheckers = Arrays.asArray(hasPrimaryKeyCheckers);
@@ -101,11 +104,15 @@ class SyncSettingsBuilder<E extends F, F, P, S, U, R> implements SettingsBuilder
101104
}
102105

103106

104-
public static fromSettingsConvert<E extends F, F, R>(localCollection: DataCollection<E, F>, primaryKeys: (keyof E & string)[], hasPrimaryKeyCheckers: ((obj: E) => boolean) | ((obj: E) => boolean)[],
105-
findFilterFunc: (item: any) => any,
106-
copyObjectFunc: (item: E) => E,
107-
convertUrlToSyncDownFunc: (url: string) => (params: any) => PsPromise<any[], R>,
108-
convertUrlToSyncUpFunc: (url: string) => (params: any, items: any[]) => PsPromise<any, R>): SyncDownBuilderWithUrl<E, F> & SyncUpBuilderWithUrl<E, F> {
107+
public static fromSettingsConvert<E extends F, F, R>(
108+
localCollection: DataCollection<E, F>,
109+
primaryKeys: (keyof E & string)[],
110+
hasPrimaryKeyCheckers: ((obj: E) => boolean) | ((obj: E) => boolean)[],
111+
findFilterFunc: (item: any) => any,
112+
copyObjectFunc: (item: E) => E,
113+
convertUrlToSyncDownFunc: (url: string) => (params: any) => PsPromise<any[], R>,
114+
convertUrlToSyncUpFunc: (url: string) => (params: any, items: any[]) => PsPromise<any, R>
115+
): SyncDownBuilderWithUrl<E, F> & SyncUpBuilderWithUrl<E, F> {
109116

110117
var inst = new SyncSettingsBuilder<E, F, any, any, any, R>();
111118
inst.localCollection = localCollection;
@@ -119,9 +126,13 @@ class SyncSettingsBuilder<E extends F, F, P, S, U, R> implements SettingsBuilder
119126
}
120127

121128

122-
public static fromSettings<E extends F, F, R>(localCollection: DataCollection<E, F>, primaryKeys: (keyof E & string)[], hasPrimaryKeyCheckers: ((obj: E) => boolean) | ((obj: E) => boolean)[],
123-
findFilterFunc: (item: any) => any,
124-
copyObjectFunc: (item: E) => E): SyncDownBuilder<E, F> & SyncUpBuilder<E, F> {
129+
public static fromSettings<E extends F, F, R>(
130+
localCollection: DataCollection<E, F>,
131+
primaryKeys: (keyof E & string)[],
132+
hasPrimaryKeyCheckers: ((obj: E) => boolean) | ((obj: E) => boolean)[],
133+
findFilterFunc: (item: any) => any,
134+
copyObjectFunc: (item: E) => E
135+
): SyncDownBuilder<E, F> & SyncUpBuilder<E, F> {
125136

126137
var inst = new SyncSettingsBuilder<E, F, any, any, any, R>();
127138
inst.localCollection = localCollection;
@@ -144,24 +155,26 @@ class SyncSettingsBuilder<E extends F, F, P, S, U, R> implements SettingsBuilder
144155
}
145156

146157

147-
public static fromDataCollectionAndSyncFuncs<E extends F, F, P, S, U, R>(table: DataCollection<E, F>,
148-
syncDownFunc: (params: P) => PsPromise<S[], R>,
149-
syncUpFunc: (params: P, items: S[]) => PsPromise<U, R>): { addFilterFuncs: (findFilterFunc: (item: S) => F) => BuilderEnd<E, F, P, S, U, R> } {
158+
public static fromDataCollectionAndSyncFuncs<E extends F, F, P, S, U, R>(
159+
localCollection: DataCollection<E, F>,
160+
syncDownFunc: (params: P) => PsPromise<S[], R>,
161+
syncUpFunc: (params: P, items: S[]) => PsPromise<U, R>
162+
): { addFilterFuncs: (findFilterFunc: (item: S) => F) => BuilderEnd<E, F, P, S, U, R> } {
150163

151-
var tableModel = table.getDataModel();
152-
var tableFuncs = <DtoAllFuncs<E, S>>table.getDataModelFuncs();
164+
var collModel = localCollection.getDataModel();
165+
var collFuncs = <DtoAllFuncs<E, S>>localCollection.getDataModelFuncs();
153166
var inst = new SyncSettingsBuilder<E, F, P, S, U, R>();
154167
// sync settings
155-
inst.localCollection = table;
156-
inst.primaryKeys = tableModel.primaryKeys;
157-
inst.hasPrimaryKeyCheckers = tableModel.primaryKeys.map(k => (itm: E) => !!itm[k]);
158-
inst.copyObjectFunc = tableFuncs.copyFunc;
168+
inst.localCollection = localCollection;
169+
inst.primaryKeys = collModel.primaryKeys;
170+
inst.hasPrimaryKeyCheckers = collModel.primaryKeys.map(k => (itm: E) => !!itm[k]);
171+
inst.copyObjectFunc = collFuncs.copyFunc;
159172
// sync down
160173
inst.syncDownFunc = syncDownFunc;
161-
inst.toLocalObject = tableFuncs.toLocalObject;
174+
inst.toLocalObject = collFuncs.toLocalObject;
162175
// sync up
163176
inst.syncUpFunc = syncUpFunc;
164-
inst.toSvcObject = tableFuncs.toSvcObject;
177+
inst.toSvcObject = collFuncs.toSvcObject;
165178

166179
return {
167180
addFilterFuncs: function (findFilterFunc: (item: S) => F) {
@@ -188,12 +201,15 @@ module SyncSettingsBuilder {
188201
convertUrlToSyncUpFunc: ((url: string) => (params: any, items: any[]) => PsPromise<any, R>) | undefined;
189202

190203

191-
constructor(localCollection: DataCollection<E, F>, primaryKeys: (keyof E & string)[], hasPrimaryKeyCheckers: ((obj: E) => boolean) | ((obj: E) => boolean)[],
192-
findFilterFunc: (item: S) => F,
193-
copyObj: (item: E) => E,
194-
convertUrlToSyncDownFunc?: (url: string) => (params: any) => PsPromise<any[], R>,
195-
convertUrlToSyncUpFunc?: (url: string) => (params: any, items: any[]) => PsPromise<any, R>) {
196-
204+
constructor(
205+
localCollection: DataCollection<E, F>,
206+
primaryKeys: (keyof E & string)[],
207+
hasPrimaryKeyCheckers: ((obj: E) => boolean) | ((obj: E) => boolean)[],
208+
findFilterFunc: (item: S) => F,
209+
copyObj: (item: E) => E,
210+
convertUrlToSyncDownFunc?: (url: string) => (params: any) => PsPromise<any[], R>,
211+
convertUrlToSyncUpFunc?: (url: string) => (params: any, items: any[]) => PsPromise<any, R>
212+
) {
197213
this.localCollection = localCollection;
198214
this.primaryKeys = primaryKeys;
199215
this.hasPrimaryKeyCheckers = Arrays.asArray(hasPrimaryKeyCheckers);

0 commit comments

Comments
 (0)