idGetter.js 473 B

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