@dhyasama/totem-merge 6.16.0 → 6.18.0

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/lib/helpers.js CHANGED
@@ -24,17 +24,32 @@ var mergeObjects = function mergeObjects(target, source, doNotMergeKeys) {
24
24
  if (source == 'undefined') return;
25
25
  else if (source == 'MongooseDocument { undefined }') return;
26
26
 
27
- var jsonSource = JSON.parse(JSON.stringify(source));
27
+ // Safely convert potential Mongoose documents to plain objects without
28
+ // invoking virtual getters, which can throw during serialization
29
+ var toPlainObject = function toPlainObject(value) {
30
+ if (!value) return value;
31
+ if (typeof value.toObject === 'function') {
32
+ try {
33
+ return value.toObject({ virtuals: false, getters: false, depopulate: true, minimize: false });
34
+ }
35
+ catch (e) {
36
+ // fall through to JSON stringify fallback below
37
+ }
38
+ }
39
+ try { return JSON.parse(JSON.stringify(value)); } catch (e) { return {}; }
40
+ };
41
+
42
+ var jsonSource = toPlainObject(source);
28
43
 
29
44
  for (var key in jsonSource) {
30
45
 
31
46
  if (doNotMergeKeys.indexOf(key) >= 0) continue;
32
47
 
33
- var sourceItem = source[key];
48
+ var sourceItem = jsonSource[key];
34
49
  if (!sourceItem) continue;
35
50
 
36
51
  var type = typeOf(target[key]);
37
- if (type == 'null') type = typeOf(source[key]);
52
+ if (type == 'null') type = typeOf(sourceItem);
38
53
 
39
54
  if (type == 'object') {
40
55
  mergeObjects(target[key], sourceItem, doNotMergeKeys);
@@ -71,22 +86,11 @@ var typeOf = function typeOf(input) {
71
86
  // Deduping with some type support
72
87
  var unique = function unique(array, key) {
73
88
 
74
- // translate each of these to "company"
75
- var companyKeys = [
76
- 'first',
77
- 'second',
78
- 'boards',
79
- 'leadership',
80
- 'pcs',
81
- 'investments'
82
- ];
89
+ // legacy keys mapped to 'company' removed as schemas evolved
83
90
 
84
91
  // for arrays of objects, specify the object key to use for comparison
85
92
  if (key == 'people') key = 'person';
86
93
  else if (key == 'phone') key = 'number';
87
- else if (companyKeys.indexOf(key) >= 0) key = 'company';
88
- else if (key == 'orgs') key = 'org';
89
- else if (key == 'investedIn') key = 'id';
90
94
 
91
95
  array = _.compact(array);
92
96
 
@@ -109,7 +109,7 @@ var mergeSourceIntoTarget = function mergeEntities(target, source, cb) {
109
109
  '__v'
110
110
  ];
111
111
 
112
- helpers.mergeObjects(target.toObject(), source.toObject(), doNotMergeKeys);
112
+ helpers.mergeObjects(target, source, doNotMergeKeys);
113
113
 
114
114
  // keep a list of copied orgs
115
115
  target.merge(source.id);
package/lib/people.js CHANGED
@@ -116,7 +116,7 @@ var mergeSourceIntoTarget = function mergeEntities(target, source, cb) {
116
116
 
117
117
  };
118
118
 
119
- helpers.mergeObjects(target.toObject(), source.toObject(), doNotMergeKeys);
119
+ helpers.mergeObjects(target, source, doNotMergeKeys);
120
120
 
121
121
  target.sources = mergeSources(target, source);
122
122
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhyasama/totem-merge",
3
- "version": "6.16.0",
3
+ "version": "6.18.0",
4
4
  "description": "Org and person merging package",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -22,7 +22,7 @@
22
22
  "underscore": "^1.13.6"
23
23
  },
24
24
  "devDependencies": {
25
- "@dhyasama/totem-models": "^11.110.0",
25
+ "@dhyasama/totem-models": "^11.111.0",
26
26
  "mocha": "^5.2.0",
27
27
  "mocha-mongoose": "^1.1.1",
28
28
  "should": "^8.2.2"
package/test/index.js CHANGED
@@ -179,7 +179,6 @@ describe('Merge', function() {
179
179
  should.not.exist(err);
180
180
  should.exist(newOrg1);
181
181
 
182
- // getbyid to populate investedIn
183
182
  Organization.getById(newOrg1.id, { CUSTOMER_ID: customer._id, role: 'none' }, function(err, result1) {
184
183
 
185
184
  should.not.exist(err);
@@ -329,8 +328,6 @@ describe('Merge', function() {
329
328
  config.flag = false;
330
329
  merge = new Merge(mongoose, config);
331
330
 
332
- org1.investedIn = [];
333
-
334
331
  Organization.upsert(org2, 'testy', function(err, result) {
335
332
 
336
333
  should.not.exist(err);