meadow-integration 1.0.4 → 1.0.6

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.
Files changed (67) hide show
  1. package/.dockerignore +11 -0
  2. package/Docker-Build.sh +2 -0
  3. package/Docker-Compose.sh +2 -0
  4. package/Docker-Push.sh +2 -0
  5. package/Docker-Tag.sh +2 -0
  6. package/Dockerfile +28 -0
  7. package/Dockerfile_LUXURYCode +23 -0
  8. package/README.md +139 -25
  9. package/docker-compose.yml +16 -0
  10. package/docs/README.md +65 -18
  11. package/docs/{cover.md → _cover.md} +3 -2
  12. package/docs/_sidebar.md +52 -7
  13. package/docs/_topbar.md +2 -0
  14. package/docs/api/clone-rest-client.md +278 -0
  15. package/docs/api/connection-manager.md +179 -0
  16. package/docs/api/guid-map.md +234 -0
  17. package/docs/api/integration-adapter.md +283 -0
  18. package/docs/api/operation.md +241 -0
  19. package/docs/api/sync-entity-initial.md +227 -0
  20. package/docs/api/sync-entity-ongoing.md +244 -0
  21. package/docs/api/sync.md +213 -0
  22. package/docs/api/tabular-check.md +213 -0
  23. package/docs/api/tabular-transform.md +316 -0
  24. package/docs/architecture.md +423 -0
  25. package/docs/cli/comprehensionarray.md +111 -0
  26. package/docs/cli/comprehensionintersect.md +132 -0
  27. package/docs/cli/csvcheck.md +111 -0
  28. package/docs/cli/csvtransform.md +170 -0
  29. package/docs/cli/data-clone.md +277 -0
  30. package/docs/cli/jsonarraytransform.md +166 -0
  31. package/docs/cli/load-comprehension.md +129 -0
  32. package/docs/cli/objectarraytocsv.md +159 -0
  33. package/docs/cli/overview.md +96 -0
  34. package/docs/cli/serve.md +102 -0
  35. package/docs/cli/tsvtransform.md +144 -0
  36. package/docs/data-clone/configuration.md +357 -0
  37. package/docs/data-clone/connection-manager.md +206 -0
  38. package/docs/data-clone/docker.md +290 -0
  39. package/docs/data-clone/overview.md +173 -0
  40. package/docs/data-clone/sync-modes.md +186 -0
  41. package/docs/implementation-reference.md +311 -0
  42. package/docs/overview.md +156 -0
  43. package/docs/quickstart.md +233 -0
  44. package/docs/rest/comprehension-push.md +209 -0
  45. package/docs/rest/comprehension.md +506 -0
  46. package/docs/rest/csv.md +255 -0
  47. package/docs/rest/entity-generation.md +158 -0
  48. package/docs/rest/json-array.md +243 -0
  49. package/docs/rest/overview.md +120 -0
  50. package/docs/rest/status.md +63 -0
  51. package/docs/rest/tsv.md +241 -0
  52. package/docs/retold-catalog.json +93 -3
  53. package/docs/retold-keyword-index.json +23683 -1901
  54. package/package.json +13 -10
  55. package/scripts/run.sh +18 -0
  56. package/source/Meadow-Integration.js +15 -1
  57. package/source/cli/Default-Meadow-Integration-Configuration.json +37 -2
  58. package/source/cli/Meadow-Integration-CLI-Program.js +4 -1
  59. package/source/cli/commands/Meadow-Integration-Command-DataClone.js +284 -0
  60. package/source/services/clone/Meadow-Service-ConnectionManager.js +251 -0
  61. package/source/services/clone/Meadow-Service-Operation.js +196 -0
  62. package/source/services/clone/Meadow-Service-RestClient.js +364 -0
  63. package/source/services/clone/Meadow-Service-Sync-Entity-Initial.js +367 -0
  64. package/source/services/clone/Meadow-Service-Sync-Entity-Ongoing.js +457 -0
  65. package/source/services/clone/Meadow-Service-Sync.js +142 -0
  66. /package/docs/examples/bookstore/{mapping_books_Author.json → mapping_books_author.json} +0 -0
  67. /package/docs/examples/bookstore/{mapping_books_Book.json → mapping_books_book.json} +0 -0
@@ -0,0 +1,367 @@
1
+ const libFableServiceProviderBase = require('fable-serviceproviderbase');
2
+ const libMeadowOperation = require('./Meadow-Service-Operation.js');
3
+
4
+ class MeadowSyncEntityInitial extends libFableServiceProviderBase
5
+ {
6
+ constructor(pFable, pOptions, pServiceHash)
7
+ {
8
+ super(pFable, pOptions, pServiceHash);
9
+
10
+ this.serviceType = 'MeadowSyncEntityInitial';
11
+
12
+ if (!this.options.hasOwnProperty('MeadowEntitySchema'))
13
+ {
14
+ throw new Error('MeadowSyncEntityInitial requires a valid MeadowEntitySchema option.');
15
+ }
16
+ if (typeof(this.options.MeadowEntitySchema) != 'object')
17
+ {
18
+ throw new Error(`MeadowSyncEntityInitial 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('MeadowSyncEntityInitial 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('MeadowSyncEntityInitial 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('MeadowSyncEntityInitial 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
+
43
+ this.Meadow = false;
44
+
45
+ this.operation = new libMeadowOperation(this.fable);
46
+ }
47
+
48
+ initialize(fCallback)
49
+ {
50
+ if (this.fable.hasOwnProperty('Meadow'))
51
+ {
52
+ this.Meadow = this.fable.Meadow.loadFromPackageObject(this.EntitySchema.MeadowSchema);
53
+ }
54
+
55
+ this.log.info(`Sync for ${this.EntitySchema.TableName} creating table if it doesn't exist...`);
56
+
57
+ if (this.Meadow && this.Meadow.provider)
58
+ {
59
+ return this.Meadow.provider.getProvider().createTable(this.EntitySchema, (pCreateError) =>
60
+ {
61
+ const tmpGUIDColumn = this.EntitySchema.Columns.find((c) => c.DataType == 'GUID');
62
+ const tmpDeletedColumn = this.EntitySchema.Columns.find((c) => c.Column == 'Deleted');
63
+
64
+ if (!tmpGUIDColumn && !tmpDeletedColumn)
65
+ {
66
+ this.log.info(`No GUID or Deleted columns for ${this.EntitySchema.TableName}; skipping index creation`);
67
+ return fCallback(pCreateError);
68
+ }
69
+
70
+ if (!this.fable.MeadowConnectionManager || !this.fable.MeadowConnectionManager.ConnectionPool)
71
+ {
72
+ this.log.info(`No connection manager available; skipping index creation for ${this.EntitySchema.TableName}`);
73
+ return fCallback(pCreateError);
74
+ }
75
+
76
+ let tmpAnticipate = this.fable.newAnticipate();
77
+ if (tmpGUIDColumn)
78
+ {
79
+ tmpAnticipate.anticipate(
80
+ (fNext) =>
81
+ {
82
+ return this.fable.MeadowConnectionManager.createIndex(this.EntitySchema, tmpGUIDColumn, true, fNext);
83
+ });
84
+ }
85
+ if (tmpDeletedColumn)
86
+ {
87
+ tmpAnticipate.anticipate(
88
+ (fNext) =>
89
+ {
90
+ return this.fable.MeadowConnectionManager.createIndex(this.EntitySchema, tmpDeletedColumn, false, fNext);
91
+ });
92
+ }
93
+ tmpAnticipate.wait(fCallback);
94
+ });
95
+ }
96
+ return fCallback();
97
+ }
98
+
99
+ marshalRecord(pSourceRecord)
100
+ {
101
+ const tmpRecordToCommit = {};
102
+
103
+ for (const tmpColumn of this.EntitySchema.Columns)
104
+ {
105
+ if (pSourceRecord.hasOwnProperty(tmpColumn.Column))
106
+ {
107
+ switch (typeof(pSourceRecord[tmpColumn.Column]))
108
+ {
109
+ case 'null':
110
+ case 'undefined':
111
+ break;
112
+ case 'object':
113
+ if (pSourceRecord[tmpColumn.Column])
114
+ {
115
+ tmpRecordToCommit[tmpColumn.Column] = JSON.stringify(pSourceRecord[tmpColumn.Column]);
116
+ }
117
+ break;
118
+ default:
119
+ if (tmpColumn.DataType == 'DateTime')
120
+ {
121
+ if ((typeof(pSourceRecord[tmpColumn.Column]) == 'string') && (pSourceRecord[tmpColumn.Column].length > 0))
122
+ {
123
+ tmpRecordToCommit[tmpColumn.Column] = this.fable.Dates.dayJS.utc(pSourceRecord[tmpColumn.Column]).format('YYYY-MM-DD HH:mm:ss.SSS');
124
+ }
125
+ }
126
+ else if (pSourceRecord[tmpColumn.Column] !== '')
127
+ {
128
+ tmpRecordToCommit[tmpColumn.Column] = pSourceRecord[tmpColumn.Column];
129
+ }
130
+ break;
131
+ }
132
+ }
133
+ else if (tmpColumn.Column.endsWith('JSON') && typeof pSourceRecord[tmpColumn.Column.substring(0, tmpColumn.Column.length - 4)] === 'object')
134
+ {
135
+ tmpRecordToCommit[tmpColumn.Column] = JSON.stringify(pSourceRecord[tmpColumn.Column.substring(0, tmpColumn.Column.length - 4)]);
136
+ }
137
+ }
138
+
139
+ return tmpRecordToCommit;
140
+ }
141
+
142
+ sync(fCallback)
143
+ {
144
+ this.operation.createTimeStamp('EntityInitialSync');
145
+
146
+ const tmpSyncState = (
147
+ {
148
+ Local: { MaxIDEntity: -1, RecordCount: 0 },
149
+ Server: { MaxIDEntity: -1, RecordCount: 0 },
150
+ });
151
+
152
+ this.fable.Utility.waterfall(
153
+ [
154
+ (fStageComplete) =>
155
+ {
156
+ // Get the Max ID from local database
157
+ const tmpQuery = this.Meadow.query;
158
+ tmpQuery.setSort({ Column: this.DefaultIdentifier, Direction: 'Descending' });
159
+ tmpQuery.setCap(1);
160
+ this.Meadow.doRead(tmpQuery,
161
+ (pReadError, pQuery, pRecord) =>
162
+ {
163
+ if (pReadError)
164
+ {
165
+ this.fable.log.error(`Error reading local max entity ID ${this.EntitySchema.TableName}: ${pReadError}`, { Error: pReadError });
166
+ return fStageComplete(`Error reading local max entity ID ${this.EntitySchema.TableName}: ${pReadError}`);
167
+ }
168
+ if (!pRecord)
169
+ {
170
+ this.fable.log.warn(`No records found in local ${this.EntitySchema.TableName}.`);
171
+ return fStageComplete();
172
+ }
173
+ this.fable.log.info(`Found local max entity ID ${this.EntitySchema.TableName}: ${pRecord[this.DefaultIdentifier]}`);
174
+ tmpSyncState.Local.MaxIDEntity = pRecord[this.DefaultIdentifier];
175
+ return fStageComplete();
176
+ });
177
+ },
178
+ (fStageComplete) =>
179
+ {
180
+ // Get the count from local database
181
+ const tmpQuery = this.Meadow.query;
182
+ this.Meadow.doCount(tmpQuery,
183
+ (pCountError, pQuery, pCount) =>
184
+ {
185
+ if (pCountError)
186
+ {
187
+ this.fable.log.error(`Error getting local count of ${this.EntitySchema.TableName}: ${pCountError}`, { Error: pCountError });
188
+ return fStageComplete(`Error getting local count of ${this.EntitySchema.TableName}: ${pCountError}`);
189
+ }
190
+ tmpSyncState.Local.RecordCount = pCount;
191
+ return fStageComplete();
192
+ });
193
+ },
194
+ (fStageComplete) =>
195
+ {
196
+ // Get the Max ID from the server
197
+ this.fable.MeadowCloneRestClient.getJSON(`${this.EntitySchema.TableName}/Max/${this.DefaultIdentifier}`,
198
+ (pError, pResponse, pBody) =>
199
+ {
200
+ if (pError)
201
+ {
202
+ this.fable.log.error(`Error getting server max entity ID ${this.EntitySchema.TableName}: ${pError}`, { Error: pError });
203
+ return fStageComplete(`Error getting server max entity ID ${this.EntitySchema.TableName}: ${pError}`);
204
+ }
205
+ if (pBody && pBody.hasOwnProperty(this.DefaultIdentifier))
206
+ {
207
+ this.fable.log.info(`Found server max entity ID ${this.EntitySchema.TableName}: ${pBody[this.DefaultIdentifier]}`);
208
+ tmpSyncState.Server.MaxIDEntity = pBody[this.DefaultIdentifier];
209
+ }
210
+ else
211
+ {
212
+ this.fable.log.warn(`No records found in server for max entity ID of ${this.EntitySchema.TableName}.`);
213
+ }
214
+ return fStageComplete();
215
+ });
216
+ },
217
+ (fStageComplete) =>
218
+ {
219
+ // Get the count from the server
220
+ this.fable.MeadowCloneRestClient.getJSON(`${this.EntitySchema.TableName}s/Count`,
221
+ (pError, pResponse, pBody) =>
222
+ {
223
+ if (pError)
224
+ {
225
+ this.fable.log.error(`Error getting server count for ${this.EntitySchema.TableName}: ${pError}`, { Error: pError });
226
+ return fStageComplete(`Error getting server count for ${this.EntitySchema.TableName}: ${pError}`);
227
+ }
228
+ if (pBody && pBody.hasOwnProperty('Count'))
229
+ {
230
+ this.fable.log.info(`Found server count for ${this.EntitySchema.TableName}: ${pBody.Count}`);
231
+ tmpSyncState.Server.RecordCount = pBody.Count;
232
+ }
233
+ else
234
+ {
235
+ this.fable.log.warn(`No records found in server based on count for ${this.EntitySchema.TableName}.`);
236
+ }
237
+ return fStageComplete();
238
+ });
239
+ },
240
+ (fStageComplete) =>
241
+ {
242
+ tmpSyncState.EstimatedRecordCount = tmpSyncState.Server.RecordCount - tmpSyncState.Local.RecordCount;
243
+
244
+ this.operation.createProgressTracker(tmpSyncState.EstimatedRecordCount, `FullSync-${this.EntitySchema.TableName}`);
245
+ this.operation.printProgressTrackerStatus(`FullSync-${this.EntitySchema.TableName}`);
246
+
247
+ // Generate paginated URL partials
248
+ tmpSyncState.URLPartials = [];
249
+ for (let i = 0; i < tmpSyncState.Server.RecordCount; i += this.PageSize)
250
+ {
251
+ tmpSyncState.URLPartials.push(`${this.EntitySchema.TableName}s/FilteredTo/FBV~${this.DefaultIdentifier}~GT~${tmpSyncState.Local.MaxIDEntity}~FSF~${this.DefaultIdentifier}~ASC~ASC/${i}/${this.PageSize}`);
252
+ }
253
+
254
+ this.fable.log.info(`Syncing with ${tmpSyncState.URLPartials.length} requests for ${this.EntitySchema.TableName} with local max ID ${tmpSyncState.Local.MaxIDEntity} and server max ID ${tmpSyncState.Server.MaxIDEntity}; estimated ${tmpSyncState.EstimatedRecordCount} records to sync.`);
255
+
256
+ return fStageComplete();
257
+ },
258
+ (fStageComplete) =>
259
+ {
260
+ this.fable.Utility.eachLimit(tmpSyncState.URLPartials, 1,
261
+ (pURLPartial, fDownloadComplete) =>
262
+ {
263
+ this.fable.MeadowCloneRestClient.getJSON(pURLPartial,
264
+ (pDownloadError, pResponse, pBody) =>
265
+ {
266
+ if (pDownloadError)
267
+ {
268
+ this.fable.log.error(`Error getting URL Partial [${pURLPartial}]: ${pDownloadError}`, { Error: pDownloadError });
269
+ return fDownloadComplete();
270
+ }
271
+ if (pBody && pBody.length > 0)
272
+ {
273
+ this.fable.Utility.eachLimit(pBody, 5,
274
+ (pEntityRecord, fEntitySyncComplete) =>
275
+ {
276
+ const tmpRecord = pEntityRecord;
277
+ const tmpQuery = this.Meadow.query;
278
+
279
+ if ((typeof(tmpRecord[this.DefaultIdentifier]) !== 'undefined') && (tmpRecord[this.DefaultIdentifier] > 0))
280
+ {
281
+ tmpQuery.addFilter(this.DefaultIdentifier, tmpRecord[this.DefaultIdentifier]);
282
+ }
283
+
284
+ this.Meadow.doRead(tmpQuery,
285
+ (pReadError, pQuery, pRecord) =>
286
+ {
287
+ if (pReadError)
288
+ {
289
+ this.fable.log.error(`Error reading record ${this.EntitySchema.TableName}: ${pReadError}`, { Error: pReadError, PassedRecord: tmpRecord });
290
+ return fEntitySyncComplete();
291
+ }
292
+ if (!pRecord)
293
+ {
294
+ // Record not found -- create it
295
+ const tmpRecordToCommit = this.marshalRecord(tmpRecord);
296
+
297
+ const tmpCreateQuery = this.Meadow.query.addRecord(tmpRecordToCommit);
298
+
299
+ tmpCreateQuery.setDisableAutoIdentity(true);
300
+ tmpCreateQuery.setDisableAutoDateStamp(true);
301
+ tmpCreateQuery.setDisableAutoUserStamp(true);
302
+ tmpCreateQuery.setDisableDeleteTracking(true);
303
+
304
+ tmpCreateQuery.AllowIdentityInsert = true;
305
+
306
+ this.Meadow.doCreate(tmpCreateQuery,
307
+ (pCreateError) =>
308
+ {
309
+ if (pCreateError)
310
+ {
311
+ this.log.error(`Error creating record ${this.EntitySchema.TableName}: ${pCreateError}`, pCreateError);
312
+ return fEntitySyncComplete();
313
+ }
314
+ this.operation.incrementProgressTrackerStatus(`FullSync-${this.EntitySchema.TableName}`, 1);
315
+ return fEntitySyncComplete();
316
+ });
317
+ }
318
+ else
319
+ {
320
+ return fEntitySyncComplete();
321
+ }
322
+ });
323
+ },
324
+ (pEntitySyncError) =>
325
+ {
326
+ this.operation.printProgressTrackerStatus(`FullSync-${this.EntitySchema.TableName}`);
327
+ if (pEntitySyncError)
328
+ {
329
+ this.log.error(`Problem or early completion syncing entity ${this.EntitySchema.TableName}: ${pEntitySyncError}`, pEntitySyncError);
330
+ }
331
+ return fDownloadComplete();
332
+ });
333
+ }
334
+ else
335
+ {
336
+ if (Array.isArray(pBody) && pBody.length == 0)
337
+ {
338
+ return fDownloadComplete(new Error('Records depleted!'));
339
+ }
340
+ return fDownloadComplete();
341
+ }
342
+ });
343
+ },
344
+ (pDownloadError) =>
345
+ {
346
+ if (pDownloadError)
347
+ {
348
+ this.fable.log.error(`Error returned URL Partial .. this may not be an error: ${pDownloadError}`);
349
+ }
350
+ fStageComplete();
351
+ });
352
+ },
353
+ ],
354
+ (pError) =>
355
+ {
356
+ if (pError)
357
+ {
358
+ this.fable.log.error(`Error performing sync ${this.EntitySchema.TableName}: ${pError}`, { Error: pError });
359
+ return fCallback();
360
+ }
361
+
362
+ return fCallback();
363
+ });
364
+ }
365
+ }
366
+
367
+ module.exports = MeadowSyncEntityInitial;