|
|
|
@ -25,7 +25,13 @@ class ModelsBase extends EventEmitter { |
|
|
|
|
this.tryEnsureIndex({ '_updatedAt': 1 }); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
setUpdatedAt(record = {}) { |
|
|
|
|
setUpdatedAt(record = {}, checkQuery = false, query) { |
|
|
|
|
if (checkQuery === true) { |
|
|
|
|
if (!query || Object.keys(query).length === 0) { |
|
|
|
|
throw new Meteor.Error('Models._Base: Empty query'); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (/(^|,)\$/.test(Object.keys(record).join(','))) { |
|
|
|
|
record.$set = record.$set || {}; |
|
|
|
|
record.$set._updatedAt = new Date; |
|
|
|
@ -55,7 +61,7 @@ class ModelsBase extends EventEmitter { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
update(query, update, options = {}) { |
|
|
|
|
this.setUpdatedAt(update); |
|
|
|
|
this.setUpdatedAt(update, true, query); |
|
|
|
|
|
|
|
|
|
if (options.upsert) { |
|
|
|
|
return this.upsert(query, update); |
|
|
|
@ -82,7 +88,7 @@ class ModelsBase extends EventEmitter { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
upsert(query, update) { |
|
|
|
|
this.setUpdatedAt(update); |
|
|
|
|
this.setUpdatedAt(update, true, query); |
|
|
|
|
|
|
|
|
|
const id = this.model.findOne(query, { fields: { _id: 1 } }); |
|
|
|
|
const result = this.model.upsert(...arguments); |
|
|
|
|