idGetter.js 573 B

12345678910111213141516171819202122232425262728293031323334
  1. 'use strict';
  2. /*!
  3. * ignore
  4. */
  5. module.exports = function addIdGetter(schema) {
  6. // ensure the documents receive an id getter unless disabled
  7. const autoIdGetter = !schema.paths['id'] &&
  8. schema.paths['_id'] &&
  9. schema.options.id;
  10. if (!autoIdGetter) {
  11. return schema;
  12. }
  13. if (schema.aliases?.id) {
  14. return schema;
  15. }
  16. schema.virtual('id').get(idGetter);
  17. return schema;
  18. };
  19. /**
  20. * Returns this documents _id cast to a string.
  21. * @api private
  22. */
  23. function idGetter() {
  24. if (this._id != null) {
  25. return this._id.toString();
  26. }
  27. return null;
  28. }