idGetter.js 504 B

12345678910111213141516171819202122232425262728293031
  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. schema.virtual('id').get(idGetter);
  14. return schema;
  15. };
  16. /*!
  17. * Returns this documents _id cast to a string.
  18. */
  19. function idGetter() {
  20. if (this._id != null) {
  21. return String(this._id);
  22. }
  23. return null;
  24. }