@dhyasama/totem-models 12.3.0 → 12.4.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.
@@ -23,9 +23,7 @@ module.exports = function(mongoose, config) {
23
23
 
24
24
  });
25
25
 
26
- DiffbotArticle.statics.getByOrgId = function getByOrgId(id, options, cb) {
27
-
28
- const self = this;
26
+ DiffbotArticle.statics.getByOrgId = async function getByOrgId(id, options, cb) {
29
27
 
30
28
  if (!cb) { throw new Error('cb is required'); }
31
29
  if (!id) { return cb(new Error('ids is required'), null); }
@@ -33,16 +31,16 @@ module.exports = function(mongoose, config) {
33
31
 
34
32
  options = helpers.getDefaultOptions(options);
35
33
 
36
- self
37
- .find({ org: id })
38
- .populate('org')
39
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
34
+ try {
35
+ const result = await this.find({ org: id }).populate('org');
36
+ return cb(null, result);
37
+ } catch(err) {
38
+ return cb(err);
39
+ }
40
40
 
41
41
  };
42
42
 
43
- DiffbotArticle.statics.getByOrgIdSkinny = function getByOrgIdSkinny(id, options, cb) {
44
-
45
- const self = this;
43
+ DiffbotArticle.statics.getByOrgIdSkinny = async function getByOrgIdSkinny(id, options, cb) {
46
44
 
47
45
  if (!cb) { throw new Error('cb is required'); }
48
46
  if (!id) { return cb(new Error('ids is required'), null); }
@@ -50,16 +48,24 @@ module.exports = function(mongoose, config) {
50
48
 
51
49
  options = helpers.getDefaultOptions(options);
52
50
 
53
- self
54
- .find({ org: id })
55
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
51
+ try {
52
+ const result = await this.find({ org: id });
53
+ return cb(null, result);
54
+ } catch(err) {
55
+ return cb(err);
56
+ }
56
57
 
57
58
  };
58
59
 
59
- DiffbotArticle.statics.upsert = function (doc, cb) {
60
+ DiffbotArticle.statics.upsert = async function (doc, cb) {
60
61
  doc.updatedOn = new Date();
61
62
  doc.markModified('entity');
62
- doc.save().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
63
+ try {
64
+ const result = await doc.save();
65
+ return cb(null, result);
66
+ } catch(err) {
67
+ return cb(err);
68
+ }
63
69
  };
64
70
  DiffbotArticle.set('autoIndex', true);
65
71
  DiffbotArticle.on('index', function(err) { console.log('error building DiffbotArticle indexes: ' + err); });
@@ -22,9 +22,7 @@ module.exports = function(mongoose, config) {
22
22
 
23
23
  });
24
24
 
25
- DiffbotOrganization.statics.getByOrgId = function getByOrgId(id, options, cb) {
26
-
27
- const self = this;
25
+ DiffbotOrganization.statics.getByOrgId = async function getByOrgId(id, options, cb) {
28
26
 
29
27
  if (!cb) { throw new Error('cb is required'); }
30
28
  if (!id) { return cb(new Error('ids is required'), null); }
@@ -32,16 +30,16 @@ module.exports = function(mongoose, config) {
32
30
 
33
31
  options = helpers.getDefaultOptions(options);
34
32
 
35
- self
36
- .findOne({ org: id })
37
- .populate('org')
38
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
33
+ try {
34
+ const result = await this.findOne({ org: id }).populate('org');
35
+ return cb(null, result);
36
+ } catch(err) {
37
+ return cb(err);
38
+ }
39
39
 
40
40
  };
41
41
 
42
- DiffbotOrganization.statics.getByOrgIdSkinny = function getByOrgIdSkinny(id, options, cb) {
43
-
44
- const self = this;
42
+ DiffbotOrganization.statics.getByOrgIdSkinny = async function getByOrgIdSkinny(id, options, cb) {
45
43
 
46
44
  if (!cb) { throw new Error('cb is required'); }
47
45
  if (!id) { return cb(new Error('ids is required'), null); }
@@ -49,16 +47,24 @@ module.exports = function(mongoose, config) {
49
47
 
50
48
  options = helpers.getDefaultOptions(options);
51
49
 
52
- self
53
- .findOne({ org: id })
54
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
50
+ try {
51
+ const result = await this.findOne({ org: id });
52
+ return cb(null, result);
53
+ } catch(err) {
54
+ return cb(err);
55
+ }
55
56
 
56
57
  };
57
58
 
58
- DiffbotOrganization.statics.upsert = function (doc, cb) {
59
+ DiffbotOrganization.statics.upsert = async function (doc, cb) {
59
60
  doc.updatedOn = new Date();
60
61
  doc.markModified('entity');
61
- doc.save().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
62
+ try {
63
+ const result = await doc.save();
64
+ return cb(null, result);
65
+ } catch(err) {
66
+ return cb(err);
67
+ }
62
68
  };
63
69
  DiffbotOrganization.set('autoIndex', false);
64
70
  DiffbotOrganization.on('index', function(err) { console.log('error building DiffbotOrganization indexes: ' + err); });
package/lib/Document.js CHANGED
@@ -111,31 +111,26 @@ module.exports = function(mongoose, config) {
111
111
  // Statics operate on the entire collection
112
112
  //////////////////////////////////////////////////////
113
113
 
114
- Document.statics.createForModel = function(doc, Model, modelId, cb) {
114
+ Document.statics.createForModel = async function(doc, Model, modelId, cb) {
115
115
 
116
116
  // No population so no need to scrub
117
117
 
118
- var addToModel = function(err, createdDoc) {
119
- if (err) return cb(err, null);
120
- Model.findByIdAndUpdate(modelId, { $push: { documents: createdDoc }}, { new: true, upsert: false }).then(function(addedTo) {
121
- return cb(null, {
122
- document: createdDoc,
123
- addedTo: addedTo
124
- });
125
- }).catch(function(err) {
126
- return cb(err);
127
- });
128
- };
129
-
130
118
  doc.createdOn = new Date();
131
119
 
132
- this.create(doc).then(function(created) { addToModel(null, created); }).catch(function(err) { addToModel(err); });
120
+ try {
121
+ const createdDoc = await this.create(doc);
122
+ const addedTo = await Model.findByIdAndUpdate(modelId, { $push: { documents: createdDoc }}, { new: true, upsert: false });
123
+ return cb(null, {
124
+ document: createdDoc,
125
+ addedTo: addedTo
126
+ });
127
+ } catch(err) {
128
+ return cb(err);
129
+ }
133
130
 
134
131
  };
135
132
 
136
- Document.statics.delete = function(id, options, cb) {
137
-
138
- let self = this;
133
+ Document.statics.delete = async function(id, options, cb) {
139
134
 
140
135
  if (!cb) { throw new Error('cb is required'); }
141
136
  if (!id) { return cb(new Error('id is required'), null); }
@@ -146,25 +141,26 @@ module.exports = function(mongoose, config) {
146
141
 
147
142
  // Not strictly necessary but provides verification that the document being deleted belongs to the customer doing the deleting
148
143
 
149
- let query = self.findOne({
144
+ let query = this.findOne({
150
145
  '_id': id,
151
146
  'customer': options.CUSTOMER_ID
152
147
  });
153
148
 
154
- query.exec().then(function(doc) {
149
+ try {
150
+ const doc = await query;
155
151
  if (!doc) return cb(null, null);
156
152
 
157
153
  // No population so no need to scrub
158
154
 
159
- doc.deleteOne().then(function() { cb(null, doc); }).catch(function(err) { cb(err); });
160
-
161
- }).catch(function(err) { cb(err); });
155
+ await doc.deleteOne();
156
+ return cb(null, doc);
157
+ } catch(err) {
158
+ return cb(err);
159
+ }
162
160
 
163
161
  };
164
162
 
165
- Document.statics.getById = function (id, options, cb) {
166
-
167
- let self = this;
163
+ Document.statics.getById = async function (id, options, cb) {
168
164
 
169
165
  if (!cb) { throw new Error('cb is required'); }
170
166
  if (!id) { return cb(new Error('id is required'), null); }
@@ -177,36 +173,43 @@ module.exports = function(mongoose, config) {
177
173
  }
178
174
 
179
175
  let queryOptions = { '_id': id };
180
-
176
+
181
177
  if (!options.isWorkerProcess) {
182
178
  queryOptions.customer = options.CUSTOMER_ID;
183
179
  }
184
180
 
185
181
  let query = this.findOne(queryOptions);
186
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
187
182
 
188
- };
183
+ try {
184
+ const result = await query;
185
+ return cb(null, result);
186
+ } catch(err) {
187
+ return cb(err);
188
+ }
189
189
 
190
- Document.statics.findByS3Info = function (bucket, key, cb) {
190
+ };
191
191
 
192
- let self = this;
192
+ Document.statics.findByS3Info = async function (bucket, key, cb) {
193
193
 
194
194
  if (!cb) { throw new Error('cb is required'); }
195
195
  if (!bucket) { return cb(new Error('bucket is required'), null); }
196
196
  if (!key) { return cb(new Error('key is required'), null); }
197
197
 
198
- let query = self.findOne({
198
+ let query = this.findOne({
199
199
  's3.bucket': bucket,
200
200
  's3.key': key
201
201
  });
202
202
 
203
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
203
+ try {
204
+ const result = await query;
205
+ return cb(null, result);
206
+ } catch(err) {
207
+ return cb(err);
208
+ }
204
209
 
205
210
  };
206
211
 
207
- Document.statics.modifyById2 = function(id, update, options, cb) {
208
-
209
- let self = this;
212
+ Document.statics.modifyById2 = async function(id, update, options, cb) {
210
213
 
211
214
  // VERY IMPORTANT NOTE
212
215
  // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
@@ -221,17 +224,20 @@ module.exports = function(mongoose, config) {
221
224
  // options runValidators defaults false which is ok since we have upsert false
222
225
  // new returns the updated document
223
226
 
224
- let query = self.findOneAndUpdate({ _id: id }, update, { upsert: false, new: true });
227
+ let query = this.findOneAndUpdate({ _id: id }, update, { upsert: false, new: true });
225
228
 
226
229
  // No population so no need to scrub
227
230
 
228
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
231
+ try {
232
+ const result = await query;
233
+ return cb(null, result);
234
+ } catch(err) {
235
+ return cb(err);
236
+ }
229
237
 
230
238
  };
231
239
 
232
- Document.statics.modifyById = function(id, update, options, cb) {
233
-
234
- let self = this;
240
+ Document.statics.modifyById = async function(id, update, options, cb) {
235
241
 
236
242
  // VERY IMPORTANT NOTE
237
243
  // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
@@ -248,21 +254,31 @@ module.exports = function(mongoose, config) {
248
254
  // options runValidators defaults false which is ok since we have upsert false
249
255
  // new returns the updated document
250
256
 
251
- let query = self.findOneAndUpdate({ _id: id, customer: options.CUSTOMER_ID }, update, { upsert: false, new: true });
257
+ let query = this.findOneAndUpdate({ _id: id, customer: options.CUSTOMER_ID }, update, { upsert: false, new: true });
252
258
 
253
259
  // No population so no need to scrub
254
260
 
255
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
261
+ try {
262
+ const result = await query;
263
+ return cb(null, result);
264
+ } catch(err) {
265
+ return cb(err);
266
+ }
256
267
 
257
268
  };
258
269
 
259
- Document.statics.upsert = function upsert(doc, cb) {
270
+ Document.statics.upsert = async function upsert(doc, cb) {
260
271
 
261
272
  if (!doc) { return cb(new Error('doc is required'), null); }
262
273
 
263
274
  // No population so no need to scrub
264
275
 
265
- doc.save().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
276
+ try {
277
+ const result = await doc.save();
278
+ return cb(null, result);
279
+ } catch(err) {
280
+ return cb(err);
281
+ }
266
282
 
267
283
  };
268
284
 
package/lib/Event.js CHANGED
@@ -42,15 +42,18 @@ module.exports = function(mongoose, config) {
42
42
  foreignField: 'Event'
43
43
  });
44
44
 
45
- Event.statics.delete = function (id, options, cb) {
46
-
47
- const self = this;
45
+ Event.statics.delete = async function (id, options, cb) {
48
46
 
49
47
  if (!cb) { throw new Error('cb is required'); }
50
48
  if (!id) { return cb(new Error('id is required'), null); }
51
49
  if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
52
50
 
53
- self.findByIdAndDelete(id).then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
51
+ try {
52
+ const result = await this.findByIdAndDelete(id);
53
+ return cb(null, result);
54
+ } catch(err) {
55
+ return cb(err);
56
+ }
54
57
 
55
58
  };
56
59
 
@@ -58,9 +61,7 @@ module.exports = function(mongoose, config) {
58
61
  // Double check customer id
59
62
  // Always populates attendees
60
63
  // Optionally populate customer
61
- Event.statics.getById = function (id, customerId, options, cb) {
62
-
63
- const self = this;
64
+ Event.statics.getById = async function (id, customerId, options, cb) {
64
65
 
65
66
  if (!cb) { throw new Error('cb is required'); }
66
67
  if (!id) { return cb(new Error('id is required'), null); }
@@ -68,7 +69,7 @@ module.exports = function(mongoose, config) {
68
69
  if (!customerId) { return cb(new Error('customerId is required'), null); }
69
70
  if (!mongoose.Types.ObjectId.isValid(customerId)) { return cb(new Error('customerId is not a valid ObjectId'), null); }
70
71
 
71
- let query = self.findOne({
72
+ let query = this.findOne({
72
73
  '_id': id,
73
74
  'customer': customerId
74
75
  });
@@ -77,7 +78,12 @@ module.exports = function(mongoose, config) {
77
78
 
78
79
  query.populate('Attendees');
79
80
 
80
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
81
+ try {
82
+ const result = await query;
83
+ return cb(null, result);
84
+ } catch(err) {
85
+ return cb(err);
86
+ }
81
87
 
82
88
  };
83
89
 
@@ -85,16 +91,14 @@ module.exports = function(mongoose, config) {
85
91
  // Double check customer id
86
92
  // Always populates attendees
87
93
  // Optionally populate customer
88
- Event.statics.getByProviderEventId = function (id, customerId, options, cb) {
89
-
90
- const self = this;
94
+ Event.statics.getByProviderEventId = async function (id, customerId, options, cb) {
91
95
 
92
96
  if (!cb) { throw new Error('cb is required'); }
93
97
  if (!id) { return cb(new Error('id is required'), null); }
94
98
  if (!customerId) { return cb(new Error('customerId is required'), null); }
95
99
  if (!mongoose.Types.ObjectId.isValid(customerId)) { return cb(new Error('customerId is not a valid ObjectId'), null); }
96
100
 
97
- let query = self.findOne({
101
+ let query = this.findOne({
98
102
  'EventId': id,
99
103
  'customer': customerId
100
104
  });
@@ -103,40 +107,53 @@ module.exports = function(mongoose, config) {
103
107
 
104
108
  query.populate('Attendees');
105
109
 
106
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
110
+ try {
111
+ const result = await query;
112
+ return cb(null, result);
113
+ } catch(err) {
114
+ return cb(err);
115
+ }
107
116
 
108
117
  };
109
118
 
110
119
  // Get all events a customer has held
111
120
  // Always populate attendees
112
121
  // Optionally populate customer
113
- Event.statics.list = function(customerId, options, cb) {
114
-
115
- const self = this;
122
+ Event.statics.list = async function(customerId, options, cb) {
116
123
 
117
124
  if (!cb) throw new Error('callback is required');
118
125
  if (!options) options = {};
119
126
  if (!customerId) throw new Error('customerId is required');
120
127
  if (!mongoose.Types.ObjectId.isValid(customerId)) { return cb(new Error('customerId is not a valid ObjectId'), null); }
121
128
 
122
- let query = self.find({ customer: customerId });
129
+ let query = this.find({ customer: customerId });
123
130
 
124
131
  if (options.populate) { query.populate('customer'); }
125
132
 
126
133
  query.populate('Attendees');
127
134
 
128
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
135
+ try {
136
+ const result = await query;
137
+ return cb(null, result);
138
+ } catch(err) {
139
+ return cb(err);
140
+ }
129
141
 
130
142
  };
131
143
 
132
144
  // Create or update an event
133
- Event.statics.upsert = function(event, options, cb) {
145
+ Event.statics.upsert = async function(event, options, cb) {
134
146
 
135
147
  if (!cb) throw new Error('cb is required');
136
148
  if (!event) throw new Error('event is required');
137
149
  if (!options) { return cb(new Error('options is required'), null); }
138
150
 
139
- event.save().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
151
+ try {
152
+ const result = await event.save();
153
+ return cb(null, result);
154
+ } catch(err) {
155
+ return cb(err);
156
+ }
140
157
 
141
158
  };
142
159
  Event.set('autoIndex', false);
@@ -37,16 +37,19 @@ module.exports = function(mongoose, config) {
37
37
  });
38
38
 
39
39
  // Delete an attendee of an event
40
- EventAttendee.statics.delete = function (eventId, email, options, cb) {
41
-
42
- const self = this;
40
+ EventAttendee.statics.delete = async function (eventId, email, options, cb) {
43
41
 
44
42
  if (!cb) { throw new Error('cb is required'); }
45
43
  if (!eventId) { return cb(new Error('eventId is required'), null); }
46
44
  if (!mongoose.Types.ObjectId.isValid(eventId)) { return cb(new Error('eventId is not a valid ObjectId'), null); }
47
45
  if (!email) { return cb(new Error('email is required'), null); }
48
46
 
49
- self.findOneAndDelete({ Event: eventId, Email: email }).then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
47
+ try {
48
+ const result = await this.findOneAndDelete({ Event: eventId, Email: email });
49
+ return cb(null, result);
50
+ } catch(err) {
51
+ return cb(err);
52
+ }
50
53
 
51
54
  };
52
55
 
@@ -62,9 +65,7 @@ module.exports = function(mongoose, config) {
62
65
 
63
66
  // Get all the events an attendee attended (that belong to the specified customer)
64
67
  // Email can be a single email or an array of emails
65
- EventAttendee.statics.listAllEventsForAttendee = function (email, customerId, options, cb) {
66
-
67
- const self = this;
68
+ EventAttendee.statics.listAllEventsForAttendee = async function (email, customerId, options, cb) {
68
69
 
69
70
  if (!cb) { throw new Error('cb is required'); }
70
71
  if (!email) { return cb(new Error('email is required'), null); }
@@ -76,7 +77,7 @@ module.exports = function(mongoose, config) {
76
77
  if (Array.isArray(email)) { params = { Email: { $in : email }}; }
77
78
  else { params = { Email: email }; }
78
79
 
79
- let query = self.find(params);
80
+ let query = this.find(params);
80
81
 
81
82
  // Get customer id so we can filter by it
82
83
  query.populate({
@@ -84,21 +85,22 @@ module.exports = function(mongoose, config) {
84
85
  match: { customer: customerId },
85
86
  });
86
87
 
87
- query.exec().then(function(result) {// Reject anything that didn't match on customer id
88
+ try {
89
+ let result = await query;
90
+ // Reject anything that didn't match on customer id
88
91
  result = _.reject(result, function(r) {
89
92
  return !r.Event || !r.Event.customer;
90
93
  });
91
94
 
92
95
  return cb(null, result);
93
-
94
- }).catch(function(err) { cb(err); });
96
+ } catch(err) {
97
+ return cb(err);
98
+ }
95
99
 
96
100
  };
97
101
 
98
102
  // Update status for an attendee of an event
99
- EventAttendee.statics.setGuestListStatus = function(eventId, email, status, options, cb) {
100
-
101
- const self = this;
103
+ EventAttendee.statics.setGuestListStatus = async function(eventId, email, status, options, cb) {
102
104
 
103
105
  if (!cb) throw new Error('cb is required');
104
106
  if (!eventId) throw new Error('eventId is required');
@@ -117,17 +119,27 @@ module.exports = function(mongoose, config) {
117
119
  }
118
120
  };
119
121
 
120
- self.findOneAndUpdate(conditions, update, { upsert: false, new: true }).then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
122
+ try {
123
+ const result = await this.findOneAndUpdate(conditions, update, { upsert: false, new: true });
124
+ return cb(null, result);
125
+ } catch(err) {
126
+ return cb(err);
127
+ }
121
128
 
122
129
  };
123
130
 
124
131
  // Update an attendee of an event
125
- EventAttendee.statics.upsert = function(eventAttendee, options, cb) {
132
+ EventAttendee.statics.upsert = async function(eventAttendee, options, cb) {
126
133
 
127
134
  if (!cb) throw new Error('cb is required');
128
135
  if (!eventAttendee) throw new Error('eventAttendee is required');
129
136
 
130
- eventAttendee.save().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
137
+ try {
138
+ const result = await eventAttendee.save();
139
+ return cb(null, result);
140
+ } catch(err) {
141
+ return cb(err);
142
+ }
131
143
 
132
144
  };
133
145
  EventAttendee.set('autoIndex', false);