meadow-integration 1.0.5 → 1.0.7
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/.dockerignore +11 -0
- package/Docker-Build.sh +2 -0
- package/Docker-Compose.sh +2 -0
- package/Docker-Push.sh +2 -0
- package/Docker-Tag.sh +2 -0
- package/Dockerfile +28 -0
- package/Dockerfile_LUXURYCode +23 -0
- package/README.md +139 -25
- package/docker-compose.yml +16 -0
- package/docs/README.md +65 -18
- package/docs/_cover.md +3 -2
- package/docs/_sidebar.md +52 -7
- package/docs/_topbar.md +2 -0
- package/docs/api/clone-rest-client.md +278 -0
- package/docs/api/connection-manager.md +179 -0
- package/docs/api/guid-map.md +234 -0
- package/docs/api/integration-adapter.md +283 -0
- package/docs/api/operation.md +241 -0
- package/docs/api/sync-entity-initial.md +227 -0
- package/docs/api/sync-entity-ongoing.md +244 -0
- package/docs/api/sync.md +213 -0
- package/docs/api/tabular-check.md +213 -0
- package/docs/api/tabular-transform.md +316 -0
- package/docs/architecture.md +423 -0
- package/docs/cli/comprehensionarray.md +111 -0
- package/docs/cli/comprehensionintersect.md +132 -0
- package/docs/cli/csvcheck.md +111 -0
- package/docs/cli/csvtransform.md +170 -0
- package/docs/cli/data-clone.md +277 -0
- package/docs/cli/jsonarraytransform.md +166 -0
- package/docs/cli/load-comprehension.md +129 -0
- package/docs/cli/objectarraytocsv.md +159 -0
- package/docs/cli/overview.md +96 -0
- package/docs/cli/serve.md +102 -0
- package/docs/cli/tsvtransform.md +144 -0
- package/docs/data-clone/configuration.md +357 -0
- package/docs/data-clone/connection-manager.md +206 -0
- package/docs/data-clone/docker.md +290 -0
- package/docs/data-clone/overview.md +173 -0
- package/docs/data-clone/sync-modes.md +186 -0
- package/docs/implementation-reference.md +311 -0
- package/docs/overview.md +156 -0
- package/docs/quickstart.md +233 -0
- package/docs/rest/comprehension-push.md +209 -0
- package/docs/rest/comprehension.md +506 -0
- package/docs/rest/csv.md +255 -0
- package/docs/rest/entity-generation.md +158 -0
- package/docs/rest/json-array.md +243 -0
- package/docs/rest/overview.md +120 -0
- package/docs/rest/status.md +63 -0
- package/docs/rest/tsv.md +241 -0
- package/docs/retold-catalog.json +93 -3
- package/docs/retold-keyword-index.json +23683 -1901
- package/package.json +6 -3
- package/scripts/run.sh +18 -0
- package/source/Meadow-Integration.js +15 -1
- package/source/cli/Default-Meadow-Integration-Configuration.json +37 -2
- package/source/cli/Meadow-Integration-CLI-Program.js +4 -1
- package/source/cli/commands/Meadow-Integration-Command-DataClone.js +284 -0
- package/source/services/clone/Meadow-Service-ConnectionManager.js +251 -0
- package/source/services/clone/Meadow-Service-Operation.js +196 -0
- package/source/services/clone/Meadow-Service-RestClient.js +364 -0
- package/source/services/clone/Meadow-Service-Sync-Entity-Initial.js +502 -0
- package/source/services/clone/Meadow-Service-Sync-Entity-Ongoing.js +592 -0
- package/source/services/clone/Meadow-Service-Sync.js +154 -0
- /package/docs/examples/bookstore/{mapping_books_Author.json → mapping_books_author.json} +0 -0
- /package/docs/examples/bookstore/{mapping_books_Book.json → mapping_books_book.json} +0 -0
|
@@ -0,0 +1,592 @@
|
|
|
1
|
+
const libFableServiceProviderBase = require('fable-serviceproviderbase');
|
|
2
|
+
const libMeadowOperation = require('./Meadow-Service-Operation.js');
|
|
3
|
+
|
|
4
|
+
class MeadowSyncEntityOngoing extends libFableServiceProviderBase
|
|
5
|
+
{
|
|
6
|
+
constructor(pFable, pOptions, pServiceHash)
|
|
7
|
+
{
|
|
8
|
+
super(pFable, pOptions, pServiceHash);
|
|
9
|
+
|
|
10
|
+
this.serviceType = 'MeadowSyncEntityOngoing';
|
|
11
|
+
|
|
12
|
+
if (!this.options.hasOwnProperty('MeadowEntitySchema'))
|
|
13
|
+
{
|
|
14
|
+
throw new Error('MeadowSyncEntityOngoing requires a valid MeadowEntitySchema option.');
|
|
15
|
+
}
|
|
16
|
+
if (typeof(this.options.MeadowEntitySchema) != 'object')
|
|
17
|
+
{
|
|
18
|
+
throw new Error(`MeadowSyncEntityOngoing requires MeadowEntitySchema to be an object; got ${typeof(this.options.MeadowEntitySchema)}.`);
|
|
19
|
+
}
|
|
20
|
+
if (!this.options.MeadowEntitySchema.hasOwnProperty('TableName') ||
|
|
21
|
+
typeof(this.options.MeadowEntitySchema.TableName) != 'string' ||
|
|
22
|
+
this.options.MeadowEntitySchema.TableName.length < 1)
|
|
23
|
+
{
|
|
24
|
+
throw new Error('MeadowSyncEntityOngoing requires a valid MeadowEntitySchema.TableName.');
|
|
25
|
+
}
|
|
26
|
+
if (!this.options.MeadowEntitySchema.hasOwnProperty('Columns') ||
|
|
27
|
+
!Array.isArray(this.options.MeadowEntitySchema.Columns) ||
|
|
28
|
+
this.options.MeadowEntitySchema.Columns.length < 1)
|
|
29
|
+
{
|
|
30
|
+
throw new Error('MeadowSyncEntityOngoing requires a valid MeadowEntitySchema.Columns array.');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
this.EntitySchema = JSON.parse(JSON.stringify(this.options.MeadowEntitySchema));
|
|
34
|
+
|
|
35
|
+
if (!this.EntitySchema.hasOwnProperty('MeadowSchema'))
|
|
36
|
+
{
|
|
37
|
+
throw new Error('MeadowSyncEntityOngoing requires MeadowEntitySchema.MeadowSchema; please update stricture and recompile the extended JSON.');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
this.DefaultIdentifier = this.EntitySchema.MeadowSchema.DefaultIdentifier;
|
|
41
|
+
this.PageSize = this.options.PageSize || 100;
|
|
42
|
+
this.SyncDeletedRecords = this.options.SyncDeletedRecords || false;
|
|
43
|
+
|
|
44
|
+
this.Meadow = false;
|
|
45
|
+
|
|
46
|
+
this.operation = new libMeadowOperation(this.fable);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
initialize(fCallback)
|
|
50
|
+
{
|
|
51
|
+
if (this.fable.hasOwnProperty('Meadow'))
|
|
52
|
+
{
|
|
53
|
+
this.Meadow = this.fable.Meadow.loadFromPackageObject(this.EntitySchema.MeadowSchema);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
this.log.info(`Sync for ${this.EntitySchema.TableName} creating table if it doesn't exist...`);
|
|
57
|
+
|
|
58
|
+
if (this.Meadow && this.Meadow.provider)
|
|
59
|
+
{
|
|
60
|
+
return this.Meadow.provider.getProvider().createTable(this.EntitySchema, (pCreateError) =>
|
|
61
|
+
{
|
|
62
|
+
const tmpGUIDColumn = this.EntitySchema.Columns.find((c) => c.DataType == 'GUID');
|
|
63
|
+
const tmpDeletedColumn = this.EntitySchema.Columns.find((c) => c.Column == 'Deleted');
|
|
64
|
+
|
|
65
|
+
if (!tmpGUIDColumn && !tmpDeletedColumn)
|
|
66
|
+
{
|
|
67
|
+
this.log.info(`No GUID or Deleted columns for ${this.EntitySchema.TableName}; skipping index creation`);
|
|
68
|
+
return fCallback(pCreateError);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (!this.fable.MeadowConnectionManager || !this.fable.MeadowConnectionManager.ConnectionPool)
|
|
72
|
+
{
|
|
73
|
+
this.log.info(`No connection manager available; skipping index creation for ${this.EntitySchema.TableName}`);
|
|
74
|
+
return fCallback(pCreateError);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
let tmpAnticipate = this.fable.newAnticipate();
|
|
78
|
+
if (tmpGUIDColumn)
|
|
79
|
+
{
|
|
80
|
+
tmpAnticipate.anticipate(
|
|
81
|
+
(fNext) =>
|
|
82
|
+
{
|
|
83
|
+
return this.fable.MeadowConnectionManager.createIndex(this.EntitySchema, tmpGUIDColumn, true, fNext);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
if (tmpDeletedColumn)
|
|
87
|
+
{
|
|
88
|
+
tmpAnticipate.anticipate(
|
|
89
|
+
(fNext) =>
|
|
90
|
+
{
|
|
91
|
+
return this.fable.MeadowConnectionManager.createIndex(this.EntitySchema, tmpDeletedColumn, false, fNext);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
tmpAnticipate.wait(fCallback);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
return fCallback();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
marshalRecord(pSourceRecord)
|
|
101
|
+
{
|
|
102
|
+
const tmpRecordToCommit = {};
|
|
103
|
+
|
|
104
|
+
for (const tmpColumn of this.EntitySchema.Columns)
|
|
105
|
+
{
|
|
106
|
+
if (pSourceRecord.hasOwnProperty(tmpColumn.Column))
|
|
107
|
+
{
|
|
108
|
+
switch (typeof(pSourceRecord[tmpColumn.Column]))
|
|
109
|
+
{
|
|
110
|
+
case 'null':
|
|
111
|
+
case 'undefined':
|
|
112
|
+
break;
|
|
113
|
+
case 'object':
|
|
114
|
+
if (pSourceRecord[tmpColumn.Column])
|
|
115
|
+
{
|
|
116
|
+
tmpRecordToCommit[tmpColumn.Column] = JSON.stringify(pSourceRecord[tmpColumn.Column]);
|
|
117
|
+
}
|
|
118
|
+
break;
|
|
119
|
+
default:
|
|
120
|
+
if (pSourceRecord[tmpColumn.Column] !== '')
|
|
121
|
+
{
|
|
122
|
+
tmpRecordToCommit[tmpColumn.Column] = pSourceRecord[tmpColumn.Column];
|
|
123
|
+
}
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
else if (tmpColumn.Column.endsWith('JSON') && typeof pSourceRecord[tmpColumn.Column.substring(0, tmpColumn.Column.length - 4)] === 'object')
|
|
128
|
+
{
|
|
129
|
+
tmpRecordToCommit[tmpColumn.Column] = JSON.stringify(pSourceRecord[tmpColumn.Column.substring(0, tmpColumn.Column.length - 4)]);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return tmpRecordToCommit;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
syncDeletedRecords(fCallback)
|
|
137
|
+
{
|
|
138
|
+
const tmpDeletedColumn = this.EntitySchema.Columns.find((c) => c.Column == 'Deleted');
|
|
139
|
+
if (!tmpDeletedColumn)
|
|
140
|
+
{
|
|
141
|
+
this.fable.log.info(`No Deleted column for ${this.EntitySchema.TableName}; skipping delete sync.`);
|
|
142
|
+
return fCallback();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
this.fable.log.info(`Checking for deleted records on server for ${this.EntitySchema.TableName}...`);
|
|
146
|
+
|
|
147
|
+
// Get the count of deleted records from the server.
|
|
148
|
+
// The explicit FBV~Deleted~EQ~1 filter overrides foxhound's automatic Deleted=0 filter.
|
|
149
|
+
this.fable.MeadowCloneRestClient.getJSON(`${this.EntitySchema.TableName}s/Count/FilteredTo/FBV~Deleted~EQ~1`,
|
|
150
|
+
(pError, pResponse, pBody) =>
|
|
151
|
+
{
|
|
152
|
+
if (pError || !pBody || !pBody.hasOwnProperty('Count'))
|
|
153
|
+
{
|
|
154
|
+
this.fable.log.warn(`Could not get deleted record count for ${this.EntitySchema.TableName}; skipping delete sync.`);
|
|
155
|
+
return fCallback();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const tmpDeletedCount = pBody.Count;
|
|
159
|
+
if (tmpDeletedCount < 1)
|
|
160
|
+
{
|
|
161
|
+
this.fable.log.info(`No deleted records on server for ${this.EntitySchema.TableName}.`);
|
|
162
|
+
return fCallback();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
this.fable.log.info(`Found ${tmpDeletedCount} deleted records on server for ${this.EntitySchema.TableName}; syncing deletions...`);
|
|
166
|
+
|
|
167
|
+
// Generate paginated URLs for deleted records
|
|
168
|
+
const tmpDeleteURLPartials = [];
|
|
169
|
+
for (let i = 0; i < tmpDeletedCount; i += this.PageSize)
|
|
170
|
+
{
|
|
171
|
+
tmpDeleteURLPartials.push(`${this.EntitySchema.TableName}s/FilteredTo/FBV~Deleted~EQ~1~FSF~${this.DefaultIdentifier}~ASC~ASC/${i}/${this.PageSize}`);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
this.fable.Utility.eachLimit(tmpDeleteURLPartials, 1,
|
|
175
|
+
(pURLPartial, fPageComplete) =>
|
|
176
|
+
{
|
|
177
|
+
this.fable.MeadowCloneRestClient.getJSON(pURLPartial,
|
|
178
|
+
(pDownloadError, pResponse, pBody) =>
|
|
179
|
+
{
|
|
180
|
+
if (pDownloadError || !pBody || !Array.isArray(pBody) || pBody.length < 1)
|
|
181
|
+
{
|
|
182
|
+
return fPageComplete();
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
this.fable.Utility.eachLimit(pBody, 5,
|
|
186
|
+
(pEntityRecord, fRecordComplete) =>
|
|
187
|
+
{
|
|
188
|
+
const tmpRecordID = pEntityRecord[this.DefaultIdentifier];
|
|
189
|
+
if (!tmpRecordID || tmpRecordID < 1)
|
|
190
|
+
{
|
|
191
|
+
return fRecordComplete();
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Read local record with delete tracking disabled so we can see all records
|
|
195
|
+
const tmpQuery = this.Meadow.query;
|
|
196
|
+
tmpQuery.addFilter(this.DefaultIdentifier, tmpRecordID);
|
|
197
|
+
tmpQuery.setDisableDeleteTracking(true);
|
|
198
|
+
|
|
199
|
+
this.Meadow.doRead(tmpQuery,
|
|
200
|
+
(pReadError, pQuery, pRecord) =>
|
|
201
|
+
{
|
|
202
|
+
if (pReadError || !pRecord)
|
|
203
|
+
{
|
|
204
|
+
// Record doesn't exist locally -- create it as deleted
|
|
205
|
+
const tmpRecordToCommit = this.marshalRecord(pEntityRecord);
|
|
206
|
+
|
|
207
|
+
const tmpCreateQuery = this.Meadow.query.addRecord(tmpRecordToCommit);
|
|
208
|
+
tmpCreateQuery.setDisableAutoIdentity(true);
|
|
209
|
+
tmpCreateQuery.setDisableAutoDateStamp(true);
|
|
210
|
+
tmpCreateQuery.setDisableAutoUserStamp(true);
|
|
211
|
+
tmpCreateQuery.setDisableDeleteTracking(true);
|
|
212
|
+
tmpCreateQuery.AllowIdentityInsert = true;
|
|
213
|
+
|
|
214
|
+
this.Meadow.doCreate(tmpCreateQuery,
|
|
215
|
+
(pCreateError) =>
|
|
216
|
+
{
|
|
217
|
+
if (pCreateError)
|
|
218
|
+
{
|
|
219
|
+
this.log.error(`Error creating deleted record ${this.EntitySchema.TableName} ID ${tmpRecordID}: ${pCreateError}`);
|
|
220
|
+
}
|
|
221
|
+
return fRecordComplete();
|
|
222
|
+
});
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (pRecord.Deleted == 1)
|
|
227
|
+
{
|
|
228
|
+
// Already marked deleted locally
|
|
229
|
+
return fRecordComplete();
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Record exists locally but is not deleted -- update it
|
|
233
|
+
const tmpRecordToCommit = this.marshalRecord(pEntityRecord);
|
|
234
|
+
|
|
235
|
+
const tmpUpdateQuery = this.Meadow.query.addRecord(tmpRecordToCommit);
|
|
236
|
+
tmpUpdateQuery.setDisableAutoIdentity(true);
|
|
237
|
+
tmpUpdateQuery.setDisableAutoDateStamp(true);
|
|
238
|
+
tmpUpdateQuery.setDisableAutoUserStamp(true);
|
|
239
|
+
tmpUpdateQuery.setDisableDeleteTracking(true);
|
|
240
|
+
|
|
241
|
+
this.Meadow.doUpdate(tmpUpdateQuery,
|
|
242
|
+
(pUpdateError) =>
|
|
243
|
+
{
|
|
244
|
+
if (pUpdateError)
|
|
245
|
+
{
|
|
246
|
+
this.log.error(`Error marking record as deleted ${this.EntitySchema.TableName} ID ${tmpRecordID}: ${pUpdateError}`);
|
|
247
|
+
}
|
|
248
|
+
return fRecordComplete();
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
},
|
|
252
|
+
(pRecordSyncError) =>
|
|
253
|
+
{
|
|
254
|
+
return fPageComplete();
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
},
|
|
258
|
+
(pDeleteSyncError) =>
|
|
259
|
+
{
|
|
260
|
+
this.fable.log.info(`Delete sync complete for ${this.EntitySchema.TableName} (${tmpDeletedCount} deleted records processed).`);
|
|
261
|
+
return fCallback();
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
addSyncAnticipateEntry(tmpSyncState, tmpAnticipate)
|
|
267
|
+
{
|
|
268
|
+
tmpAnticipate.anticipate(
|
|
269
|
+
(fNext) =>
|
|
270
|
+
{
|
|
271
|
+
const tmpURLPartial = `${this.EntitySchema.TableName}s/FilteredTo/FBV~${this.DefaultIdentifier}~GT~${tmpSyncState.LastRequestedID}~FSF~${this.DefaultIdentifier}~ASC~ASC/0/${this.PageSize}`;
|
|
272
|
+
this.fable.MeadowCloneRestClient.getJSON(tmpURLPartial,
|
|
273
|
+
(pDownloadError, pResponse, pBody) =>
|
|
274
|
+
{
|
|
275
|
+
if (pDownloadError)
|
|
276
|
+
{
|
|
277
|
+
this.fable.log.error(`Error getting URL Partial [${tmpURLPartial}]: ${pDownloadError}`, { Error: pDownloadError });
|
|
278
|
+
return fNext();
|
|
279
|
+
}
|
|
280
|
+
if (pBody && pBody.length > 0)
|
|
281
|
+
{
|
|
282
|
+
for (let i = 0; i < pBody.length; i++)
|
|
283
|
+
{
|
|
284
|
+
const tmpRecord = pBody[i];
|
|
285
|
+
|
|
286
|
+
tmpAnticipate.anticipate(
|
|
287
|
+
(fNextEntityRecordSync) =>
|
|
288
|
+
{
|
|
289
|
+
const tmpQuery = this.Meadow.query;
|
|
290
|
+
|
|
291
|
+
if (tmpRecord[this.DefaultIdentifier] > tmpSyncState.LastRequestedID)
|
|
292
|
+
{
|
|
293
|
+
tmpSyncState.LastRequestedID = tmpRecord[this.DefaultIdentifier];
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if ((typeof(tmpRecord[this.DefaultIdentifier]) !== 'undefined') && (tmpRecord[this.DefaultIdentifier] > 0))
|
|
297
|
+
{
|
|
298
|
+
tmpQuery.addFilter(this.DefaultIdentifier, tmpRecord[this.DefaultIdentifier]);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
this.Meadow.doRead(tmpQuery,
|
|
302
|
+
(pReadError, pQuery, pRecord) =>
|
|
303
|
+
{
|
|
304
|
+
if (pReadError)
|
|
305
|
+
{
|
|
306
|
+
this.fable.log.error(`Error reading record ${this.EntitySchema.TableName}: ${pReadError}`, { Error: pReadError, PassedRecord: tmpRecord });
|
|
307
|
+
return fNextEntityRecordSync();
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if (pRecord)
|
|
311
|
+
{
|
|
312
|
+
const tmpAgeDifference = this.fable.Dates.dayJS(tmpRecord.UpdateDate).diff(this.fable.Dates.dayJS(pRecord.UpdateDate));
|
|
313
|
+
|
|
314
|
+
if (Math.abs(tmpAgeDifference) < 5)
|
|
315
|
+
{
|
|
316
|
+
return fNextEntityRecordSync();
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
this.fable.log.info(`Syncing ${this.EntitySchema.TableName} record ${tmpRecord[this.DefaultIdentifier]} with age difference of ${tmpAgeDifference} ms.`);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const tmpRecordToCommit = this.marshalRecord(tmpRecord);
|
|
323
|
+
|
|
324
|
+
const tmpSyncQuery = this.Meadow.query.addRecord(tmpRecordToCommit);
|
|
325
|
+
|
|
326
|
+
tmpSyncQuery.setDisableAutoIdentity(true);
|
|
327
|
+
tmpSyncQuery.setDisableAutoDateStamp(true);
|
|
328
|
+
tmpSyncQuery.setDisableAutoUserStamp(true);
|
|
329
|
+
tmpSyncQuery.setDisableDeleteTracking(true);
|
|
330
|
+
|
|
331
|
+
if (!pRecord)
|
|
332
|
+
{
|
|
333
|
+
// Record not found -- create
|
|
334
|
+
tmpSyncQuery.AllowIdentityInsert = true;
|
|
335
|
+
|
|
336
|
+
this.Meadow.doCreate(tmpSyncQuery,
|
|
337
|
+
(pCreateError) =>
|
|
338
|
+
{
|
|
339
|
+
if (pCreateError)
|
|
340
|
+
{
|
|
341
|
+
this.log.error(`Error creating record ${this.EntitySchema.TableName}: ${pCreateError}`, pCreateError);
|
|
342
|
+
return fNextEntityRecordSync();
|
|
343
|
+
}
|
|
344
|
+
this.operation.incrementProgressTrackerStatus(`UpdateSync-${this.EntitySchema.TableName}`, 1);
|
|
345
|
+
return fNextEntityRecordSync();
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
else
|
|
349
|
+
{
|
|
350
|
+
// Record found -- update
|
|
351
|
+
this.Meadow.doUpdate(tmpSyncQuery,
|
|
352
|
+
(pUpdateError) =>
|
|
353
|
+
{
|
|
354
|
+
if (pUpdateError)
|
|
355
|
+
{
|
|
356
|
+
this.log.error(`Error updating record ${this.EntitySchema.TableName}: ${pUpdateError}`, pUpdateError);
|
|
357
|
+
return fNextEntityRecordSync();
|
|
358
|
+
}
|
|
359
|
+
this.operation.incrementProgressTrackerStatus(`UpdateSync-${this.EntitySchema.TableName}`, 1);
|
|
360
|
+
return fNextEntityRecordSync();
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
});
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
tmpSyncState.RequestsPerformed++;
|
|
367
|
+
if (tmpSyncState.RequestsPerformed < tmpSyncState.EstimatedRequestCount)
|
|
368
|
+
{
|
|
369
|
+
this.fable.log.info(`Syncing ${this.EntitySchema.TableName} request ${tmpSyncState.RequestsPerformed} of ${tmpSyncState.EstimatedRequestCount}...`);
|
|
370
|
+
this.addSyncAnticipateEntry(tmpSyncState, tmpAnticipate);
|
|
371
|
+
}
|
|
372
|
+
return fNext();
|
|
373
|
+
}
|
|
374
|
+
else
|
|
375
|
+
{
|
|
376
|
+
return fNext();
|
|
377
|
+
}
|
|
378
|
+
});
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
sync(fCallback)
|
|
383
|
+
{
|
|
384
|
+
this.operation.createTimeStamp('EntityOngoingSync');
|
|
385
|
+
|
|
386
|
+
let tmpAnticipate = this.fable.newAnticipate();
|
|
387
|
+
|
|
388
|
+
const tmpSyncState = (
|
|
389
|
+
{
|
|
390
|
+
Local: { MaxIDEntity: -1, RecordCount: 0, HasUpdateDate: false, LatestUpdateDate: false },
|
|
391
|
+
Server: { MaxIDEntity: -1, RecordCount: 0, HasUpdateDate: false, LatestUpdateDate: false },
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
this.fable.Utility.waterfall(
|
|
395
|
+
[
|
|
396
|
+
(fStageComplete) =>
|
|
397
|
+
{
|
|
398
|
+
if (!this.EntitySchema || !this.EntitySchema.MeadowSchema || !Array.isArray(this.EntitySchema.MeadowSchema.Schema))
|
|
399
|
+
{
|
|
400
|
+
return fStageComplete('MeadowSyncEntityOngoing requires a valid MeadowEntitySchema.MeadowSchema.Schema.');
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
for (let i = 0; i < this.EntitySchema.MeadowSchema.Schema.length; i++)
|
|
404
|
+
{
|
|
405
|
+
const tmpColumn = this.EntitySchema.MeadowSchema.Schema[i];
|
|
406
|
+
if (tmpColumn.Column == 'UpdateDate')
|
|
407
|
+
{
|
|
408
|
+
tmpSyncState.Local.HasUpdateDate = true;
|
|
409
|
+
tmpSyncState.Server.HasUpdateDate = true;
|
|
410
|
+
this.log.info(`Entity ${this.EntitySchema.TableName} has UpdateDate column.`);
|
|
411
|
+
break;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
this.log.info(`Syncing with UPDATE STRATEGY entity ${this.EntitySchema.TableName}...`);
|
|
416
|
+
return fStageComplete();
|
|
417
|
+
},
|
|
418
|
+
(fStageComplete) =>
|
|
419
|
+
{
|
|
420
|
+
// Get the Max ID from local database
|
|
421
|
+
const tmpQuery = this.Meadow.query;
|
|
422
|
+
tmpQuery.setSort({ Column: this.DefaultIdentifier, Direction: 'Descending' });
|
|
423
|
+
tmpQuery.setCap(1);
|
|
424
|
+
this.Meadow.doRead(tmpQuery,
|
|
425
|
+
(pReadError, pQuery, pRecord) =>
|
|
426
|
+
{
|
|
427
|
+
if (pReadError)
|
|
428
|
+
{
|
|
429
|
+
this.fable.log.error(`Error reading local max entity ID ${this.EntitySchema.TableName}: ${pReadError}`, { Error: pReadError });
|
|
430
|
+
return fStageComplete(`Error reading local max entity ID ${this.EntitySchema.TableName}: ${pReadError}`);
|
|
431
|
+
}
|
|
432
|
+
if (!pRecord)
|
|
433
|
+
{
|
|
434
|
+
this.fable.log.warn(`No records found in local ${this.EntitySchema.TableName}.`);
|
|
435
|
+
return fStageComplete();
|
|
436
|
+
}
|
|
437
|
+
this.fable.log.info(`Found local max entity ID ${this.EntitySchema.TableName}: ${pRecord[this.DefaultIdentifier]}`);
|
|
438
|
+
tmpSyncState.Local.MaxIDEntity = pRecord[this.DefaultIdentifier];
|
|
439
|
+
return fStageComplete();
|
|
440
|
+
});
|
|
441
|
+
},
|
|
442
|
+
(fStageComplete) =>
|
|
443
|
+
{
|
|
444
|
+
// Get the Max UpdateDate from local database
|
|
445
|
+
const tmpQuery = this.Meadow.query;
|
|
446
|
+
tmpQuery.setSort({ Column: 'UpdateDate', Direction: 'Descending' });
|
|
447
|
+
tmpQuery.setCap(1);
|
|
448
|
+
this.Meadow.doRead(tmpQuery,
|
|
449
|
+
(pReadError, pQuery, pRecord) =>
|
|
450
|
+
{
|
|
451
|
+
if (pReadError)
|
|
452
|
+
{
|
|
453
|
+
this.fable.log.error(`Error reading local max UpdateDate ${this.EntitySchema.TableName}: ${pReadError}`, { Error: pReadError });
|
|
454
|
+
return fStageComplete(`Error reading local max UpdateDate ${this.EntitySchema.TableName}: ${pReadError}`);
|
|
455
|
+
}
|
|
456
|
+
if (!pRecord)
|
|
457
|
+
{
|
|
458
|
+
this.fable.log.warn(`No records found in local checking UpdateDate ${this.EntitySchema.TableName}.`);
|
|
459
|
+
return fStageComplete();
|
|
460
|
+
}
|
|
461
|
+
this.fable.log.info(`Found local max UpdateDate ${this.EntitySchema.TableName}: ${pRecord.UpdateDate}`);
|
|
462
|
+
tmpSyncState.Local.MaxUpdateDate = pRecord.UpdateDate;
|
|
463
|
+
return fStageComplete();
|
|
464
|
+
});
|
|
465
|
+
},
|
|
466
|
+
(fStageComplete) =>
|
|
467
|
+
{
|
|
468
|
+
// Get the count from local database
|
|
469
|
+
const tmpQuery = this.Meadow.query;
|
|
470
|
+
this.Meadow.doCount(tmpQuery,
|
|
471
|
+
(pCountError, pQuery, pCount) =>
|
|
472
|
+
{
|
|
473
|
+
if (pCountError)
|
|
474
|
+
{
|
|
475
|
+
this.fable.log.error(`Error getting local count of ${this.EntitySchema.TableName}: ${pCountError}`, { Error: pCountError });
|
|
476
|
+
return fStageComplete(`Error getting local count of ${this.EntitySchema.TableName}: ${pCountError}`);
|
|
477
|
+
}
|
|
478
|
+
tmpSyncState.Local.RecordCount = pCount;
|
|
479
|
+
return fStageComplete();
|
|
480
|
+
});
|
|
481
|
+
},
|
|
482
|
+
(fStageComplete) =>
|
|
483
|
+
{
|
|
484
|
+
// Get the Max ID from server
|
|
485
|
+
this.fable.MeadowCloneRestClient.getJSON(`${this.EntitySchema.TableName}/Max/${this.DefaultIdentifier}`,
|
|
486
|
+
(pError, pResponse, pBody) =>
|
|
487
|
+
{
|
|
488
|
+
if (pError)
|
|
489
|
+
{
|
|
490
|
+
this.fable.log.error(`Error getting server max entity ID ${this.EntitySchema.TableName}: ${pError}`, { Error: pError });
|
|
491
|
+
return fStageComplete(`Error getting server max entity ID ${this.EntitySchema.TableName}: ${pError}`);
|
|
492
|
+
}
|
|
493
|
+
if (pBody && pBody.hasOwnProperty(this.DefaultIdentifier))
|
|
494
|
+
{
|
|
495
|
+
this.fable.log.info(`Found server max entity ID ${this.EntitySchema.TableName}: ${pBody[this.DefaultIdentifier]}`);
|
|
496
|
+
tmpSyncState.Server.MaxIDEntity = pBody[this.DefaultIdentifier];
|
|
497
|
+
}
|
|
498
|
+
else
|
|
499
|
+
{
|
|
500
|
+
this.fable.log.warn(`No records found in server for max entity ID of ${this.EntitySchema.TableName}.`);
|
|
501
|
+
}
|
|
502
|
+
return fStageComplete();
|
|
503
|
+
});
|
|
504
|
+
},
|
|
505
|
+
(fStageComplete) =>
|
|
506
|
+
{
|
|
507
|
+
// Get the Max UpdateDate from server
|
|
508
|
+
this.fable.MeadowCloneRestClient.getJSON(`${this.EntitySchema.TableName}/Max/UpdateDate`,
|
|
509
|
+
(pError, pResponse, pBody) =>
|
|
510
|
+
{
|
|
511
|
+
if (pError)
|
|
512
|
+
{
|
|
513
|
+
this.fable.log.error(`Error getting server max UpdateDate ${this.EntitySchema.TableName}: ${pError}`, { Error: pError });
|
|
514
|
+
return fStageComplete(`Error getting server max UpdateDate ${this.EntitySchema.TableName}: ${pError}`);
|
|
515
|
+
}
|
|
516
|
+
if (pBody && pBody.hasOwnProperty(this.DefaultIdentifier))
|
|
517
|
+
{
|
|
518
|
+
this.fable.log.info(`Found server max UpdateDate ${this.EntitySchema.TableName}: ${pBody['UpdateDate']}`);
|
|
519
|
+
tmpSyncState.Server.MaxUpdateDate = pBody.UpdateDate;
|
|
520
|
+
}
|
|
521
|
+
else
|
|
522
|
+
{
|
|
523
|
+
this.fable.log.warn(`No records found in server for max UpdateDate of ${this.EntitySchema.TableName}.`);
|
|
524
|
+
}
|
|
525
|
+
return fStageComplete();
|
|
526
|
+
});
|
|
527
|
+
},
|
|
528
|
+
(fStageComplete) =>
|
|
529
|
+
{
|
|
530
|
+
// Get the count from server
|
|
531
|
+
this.fable.MeadowCloneRestClient.getJSON(`${this.EntitySchema.TableName}s/Count`,
|
|
532
|
+
(pError, pResponse, pBody) =>
|
|
533
|
+
{
|
|
534
|
+
if (pError)
|
|
535
|
+
{
|
|
536
|
+
this.fable.log.error(`Error getting server count for ${this.EntitySchema.TableName}: ${pError}`, { Error: pError });
|
|
537
|
+
return fStageComplete(`Error getting server count for ${this.EntitySchema.TableName}: ${pError}`);
|
|
538
|
+
}
|
|
539
|
+
if (pBody && pBody.hasOwnProperty('Count'))
|
|
540
|
+
{
|
|
541
|
+
this.fable.log.info(`Found server count for ${this.EntitySchema.TableName}: ${pBody.Count}`);
|
|
542
|
+
tmpSyncState.Server.RecordCount = pBody.Count;
|
|
543
|
+
}
|
|
544
|
+
else
|
|
545
|
+
{
|
|
546
|
+
this.fable.log.warn(`No records found in server based on count for ${this.EntitySchema.TableName}.`);
|
|
547
|
+
}
|
|
548
|
+
return fStageComplete();
|
|
549
|
+
});
|
|
550
|
+
},
|
|
551
|
+
(fStageComplete) =>
|
|
552
|
+
{
|
|
553
|
+
tmpSyncState.EstimatedRequestCount = Math.ceil(tmpSyncState.Server.RecordCount / this.PageSize);
|
|
554
|
+
tmpSyncState.RequestsPerformed = 0;
|
|
555
|
+
tmpSyncState.LastRequestedID = 0;
|
|
556
|
+
|
|
557
|
+
this.operation.createProgressTracker(tmpSyncState.EstimatedRequestCount, `UpdateSync-${this.EntitySchema.TableName}`);
|
|
558
|
+
this.operation.printProgressTrackerStatus(`UpdateSync-${this.EntitySchema.TableName}`);
|
|
559
|
+
|
|
560
|
+
return fStageComplete();
|
|
561
|
+
},
|
|
562
|
+
(fStageComplete) =>
|
|
563
|
+
{
|
|
564
|
+
if (tmpSyncState.EstimatedRequestCount < 1)
|
|
565
|
+
{
|
|
566
|
+
this.fable.log.info(`No records to update sync for ${this.EntitySchema.TableName}.`);
|
|
567
|
+
return fStageComplete();
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
this.addSyncAnticipateEntry(tmpSyncState, tmpAnticipate);
|
|
571
|
+
|
|
572
|
+
tmpAnticipate.wait(fStageComplete);
|
|
573
|
+
},
|
|
574
|
+
],
|
|
575
|
+
(pError) =>
|
|
576
|
+
{
|
|
577
|
+
if (pError)
|
|
578
|
+
{
|
|
579
|
+
this.fable.log.error(`Error performing Update sync ${this.EntitySchema.TableName}: ${pError}`, { Error: pError });
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
if (this.SyncDeletedRecords)
|
|
583
|
+
{
|
|
584
|
+
return this.syncDeletedRecords(() => { return fCallback(); });
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
return fCallback();
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
module.exports = MeadowSyncEntityOngoing;
|