setDocumentTimestamps.js 842 B

1234567891011121314151617181920212223242526
  1. 'use strict';
  2. module.exports = function setDocumentTimestamps(doc, timestampOption, currentTime, createdAt, updatedAt) {
  3. const skipUpdatedAt = timestampOption?.updatedAt === false;
  4. const skipCreatedAt = timestampOption?.createdAt === false;
  5. const defaultTimestamp = currentTime != null ?
  6. currentTime() :
  7. doc.ownerDocument().constructor.base.now();
  8. if (!skipCreatedAt &&
  9. (doc.isNew || doc.$isSubdocument) &&
  10. createdAt &&
  11. !doc.$__getValue(createdAt) &&
  12. doc.$__isSelected(createdAt)) {
  13. doc.$set(createdAt, defaultTimestamp, undefined, { overwriteImmutable: true });
  14. }
  15. if (!skipUpdatedAt && updatedAt && (doc.isNew || doc.$isModified())) {
  16. let ts = defaultTimestamp;
  17. if (doc.isNew && createdAt != null) {
  18. ts = doc.$__getValue(createdAt);
  19. }
  20. doc.$set(updatedAt, ts);
  21. }
  22. };