Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support mongoose.SchemaType.get() : custom getters across all instances of a schematype #6912

Closed
vkarpov15 opened this issue Aug 22, 2018 · 9 comments
Labels
new feature This change adds new functionality, like a new method or class
Milestone

Comments

@vkarpov15
Copy link
Collaborator

vkarpov15 commented Aug 22, 2018

Re discussion on #6849

Example:

mongoose.Schema.Date.set(v => v === '' ? Date.now() : v); // Convert '' to `new Date()` for all date paths
@vkarpov15 vkarpov15 added the new feature This change adds new functionality, like a new method or class label Aug 22, 2018
@vkarpov15 vkarpov15 added this to the 5.x Unprioritized milestone Aug 22, 2018
@vkarpov15 vkarpov15 modified the milestones: 5.x Unprioritized, 5.4 Oct 29, 2018
@vkarpov15 vkarpov15 changed the title Schema type setters Support mongoose.SchemaType.get() and .set() : custom getters/setters across all instances of a schematype Oct 29, 2018
@Lanfei
Copy link

Lanfei commented Oct 31, 2018

@vkarpov15 What if I want to get the original object in some cases?

@vkarpov15
Copy link
Collaborator Author

@Lanfei can you provide an example case? You can do something like toObject({ getters: false }) to get the raw doc without any getters applied.

@Lanfei
Copy link

Lanfei commented Nov 7, 2018

@vkarpov15 Actually I don't have any example case now, just wanted to know the solution if I met this situation in the future :)

Maybe we can also have something like .get(key, { getter: false })?

@vkarpov15
Copy link
Collaborator Author

Love it, great idea 👍

@vkarpov15 vkarpov15 changed the title Support mongoose.SchemaType.get() and .set() : custom getters/setters across all instances of a schematype Support mongoose.SchemaType.get() : custom getters across all instances of a schematype Dec 14, 2018
@vkarpov15
Copy link
Collaborator Author

The syntax will be .get(key, null, { getters: false }) in 5.5.0. The 2nd arg to .get() is for adhoc type casting.

@GlennyDC
Copy link

Hi

This does not seem to work in v5.10.3:

mongoose.Schema.ObjectId.get(v => v.toString());

And neither does this:

mongoose.ObjectId.get(v => v.toString());

Full code:

import mongoose from 'mongoose';

export const installDatabaseConnection = async (): Promise<void> => {
  try {
    await mongoose.connect('mongodb://db:27017', {
      useNewUrlParser: true,
      useFindAndModify: false,
      useUnifiedTopology: true,
    });

    mongoose.Schema.ObjectId.get((v) => v.toString());
  } catch (err) {
    console.log(err);
  }
};

Kind regards

Glenny

@vkarpov15
Copy link
Collaborator Author

@GlennyDC can you please define what you mean by "does not seem to work"? What is the behavior you're seeing and what is the behavior you expect?

@GlennyDC
Copy link

GlennyDC commented Oct 29, 2020

@vkarpov15 I am referring to this functionality: https://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-get

Also described / mentioned in:

mongoose.Schema.ObjectId.get((v) => v.toString());

Expected behaviour:
When querying, the Schema should convert any ObjectId type to type String for the returned Documents.
Much like setting a getter for every ObjectId for every Schema:

ownerId: {
      type: mongoose.Schema.Types.ObjectId,
      get: (v) => v.toString(),
      required: true,
},

Actual behaviour:
The returned Documents do not contain the converted values. Every property with type ObjectId is still type ObjectId and not type String.

@vkarpov15
Copy link
Collaborator Author

I opened up a separate issue for this #9509 @GlennyDC

@Automattic Automattic locked and limited conversation to collaborators Oct 29, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
new feature This change adds new functionality, like a new method or class
Projects
None yet
Development

No branches or pull requests

4 participants