openchs-models 1.31.38 → 1.31.40

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 (2) hide show
  1. package/dist/Schema.js +69 -42
  2. package/package.json +1 -1
package/dist/Schema.js CHANGED
@@ -3,6 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.createTransactionDataMapForEmbeddedFields = createTransactionDataMapForEmbeddedFields;
6
7
  exports.default = void 0;
7
8
 
8
9
  var _Settings = _interopRequireDefault(require("./Settings"));
@@ -189,6 +190,8 @@ var _MigrationsHelper = _interopRequireDefault(require("./MigrationsHelper"));
189
190
 
190
191
  var _MetaDataService = _interopRequireDefault(require("./service/MetaDataService"));
191
192
 
193
+ var _moment = _interopRequireDefault(require("moment"));
194
+
192
195
  function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
193
196
 
194
197
  function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
@@ -285,67 +288,95 @@ function shouldFlush(numberOfRecords) {
285
288
  return numberOfRecords % batchSize === batchSize - 1;
286
289
  }
287
290
 
288
- function migrateGeoLocation(oldDB, newDB) {
289
- flush(newDB);
290
- let recordCounter = 0;
291
-
292
- _MetaDataService.default.forEachPointField((field, schemaName) => {
293
- console.log(`schema: ${schemaName}, field: ${field}`);
294
- newDB.objects(schemaName).forEach(newDbParentEntity => {
295
- if (shouldFlush(recordCounter)) {
296
- flush(newDB);
297
- }
298
-
299
- const oldFieldValue = oldDB.objects(schemaName).filtered(`uuid = "${newDbParentEntity.uuid}"`)[0][field];
291
+ function createTransactionDataMapForEmbeddedFields() {
292
+ const map = new Map();
300
293
 
301
- if (!_lodash.default.isNil(oldFieldValue)) {
302
- newDbParentEntity[field] = {
303
- x: oldFieldValue.x,
304
- y: oldFieldValue.y
305
- };
306
- }
294
+ _MetaDataService.default.forEachPointField((fieldName, schemaName) => {
295
+ if (map.has(schemaName)) {
296
+ map.get(schemaName).push({
297
+ fieldName,
298
+ fieldType: "Point"
299
+ });
300
+ } else {
301
+ map.set(schemaName, [{
302
+ fieldName,
303
+ fieldType: "Point"
304
+ }]);
305
+ }
306
+ });
307
307
 
308
- recordCounter++;
309
- });
308
+ _MetaDataService.default.forEachObservationField((fieldName, schemaName) => {
309
+ if (map.has(schemaName)) {
310
+ map.get(schemaName).push({
311
+ fieldName,
312
+ fieldType: "Obs"
313
+ });
314
+ } else {
315
+ map.set(schemaName, [{
316
+ fieldName,
317
+ fieldType: "Obs"
318
+ }]);
319
+ }
310
320
  });
311
321
 
312
- flush(newDB);
313
- newDB.deleteModel("Point");
322
+ return map;
314
323
  }
315
324
 
316
- function migrateObservationsToEmbedded(oldDB, newDB) {
325
+ function migrateAllEmbeddedForTxnData(oldDB, newDB) {
326
+ const startTime = new Date();
317
327
  flush(newDB);
328
+ const map = createTransactionDataMapForEmbeddedFields();
318
329
  let recordCounter = 0;
319
-
320
- _MetaDataService.default.forEachObservationField((observationField, schemaName) => {
321
- console.log(`schema: ${schemaName}, field: ${observationField}`);
330
+ const conceptMap = new Map();
331
+ map.forEach((fields, schemaName) => {
332
+ console.log(`schema: ${schemaName}, fields: ${fields.length}`);
322
333
  newDB.objects(schemaName).forEach(newDbParentEntity => {
323
334
  if (shouldFlush(recordCounter)) {
324
335
  flush(newDB);
325
336
  }
326
337
 
327
- const newList = [];
328
- const oldFieldValues = oldDB.objects(schemaName).filtered(`uuid = "${newDbParentEntity.uuid}"`)[0][observationField];
329
- oldFieldValues.forEach(oldFieldValue => {
330
- const newConcept = newDB.objects("Concept").filtered(`uuid = "${oldFieldValue.concept.uuid}"`)[0];
331
- newList.push({
332
- concept: newConcept,
333
- valueJSON: oldFieldValue.valueJSON
334
- });
338
+ fields.forEach(field => {
339
+ const oldEntity = oldDB.objects(schemaName).filtered(`uuid = "${newDbParentEntity.uuid}"`)[0];
340
+ const oldValue = oldEntity[field.fieldName];
341
+
342
+ if (!_lodash.default.isNil(oldValue)) {
343
+ if (field.fieldType === "Point") newDbParentEntity[field.fieldName] = {
344
+ x: oldValue.x,
345
+ y: oldValue.y
346
+ };else {
347
+ const newObsList = [];
348
+ oldValue.forEach(oldItemValue => {
349
+ let newConcept = conceptMap.get(oldItemValue.concept.uuid);
350
+
351
+ if (_lodash.default.isNil(newConcept)) {
352
+ newConcept = newDB.objects("Concept").filtered(`uuid = "${oldItemValue.concept.uuid}"`)[0];
353
+ conceptMap.set(oldItemValue.concept.uuid, newConcept);
354
+ }
355
+
356
+ newObsList.push({
357
+ concept: newConcept,
358
+ valueJSON: oldItemValue.valueJSON
359
+ });
360
+ });
361
+ newDbParentEntity[field.fieldName] = newObsList;
362
+ }
363
+ }
335
364
  });
336
- newDbParentEntity[observationField] = newList;
337
365
  recordCounter++;
338
366
  });
339
367
  });
340
-
341
368
  flush(newDB);
369
+ newDB.deleteModel("Point");
342
370
  newDB.deleteModel("Observation");
371
+ const endTime = new Date();
372
+ const diff = (0, _moment.default)(endTime).diff(startTime, "seconds", true);
373
+ console.log("Total Time Taken", diff, "seconds");
343
374
  }
344
375
 
345
376
  function createRealmConfig() {
346
377
  return {
347
378
  //order is important, should be arranged according to the dependency
348
- schemaVersion: 186,
379
+ schemaVersion: 185,
349
380
  onMigration: function (oldDB, newDB) {
350
381
  console.log("[AvniModels.Schema]", `Running migration with old schema version: ${oldDB.schemaVersion} and new schema version: ${newDB.schemaVersion}`);
351
382
 
@@ -959,11 +990,7 @@ function createRealmConfig() {
959
990
  }
960
991
 
961
992
  if (oldDB.schemaVersion < 185) {
962
- migrateGeoLocation(oldDB, newDB);
963
- }
964
-
965
- if (oldDB.schemaVersion < 186) {
966
- migrateObservationsToEmbedded(oldDB, newDB);
993
+ migrateAllEmbeddedForTxnData(oldDB, newDB);
967
994
  }
968
995
  }
969
996
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "openchs-models",
3
3
  "description": "OpenCHS data model to be used by front end clients",
4
- "version": "1.31.38",
4
+ "version": "1.31.40",
5
5
  "private": false,
6
6
  "repository": {
7
7
  "type": "git",