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

Separate accessor for referenced object from foreign key accessor #576

Closed
lackac opened this issue Oct 24, 2011 · 4 comments
Closed

Separate accessor for referenced object from foreign key accessor #576

lackac opened this issue Oct 24, 2011 · 4 comments

Comments

@lackac
Copy link
Contributor

lackac commented Oct 24, 2011

I like the new populate functionality. It makes our code so much more readable. Our schema doesn't map well to how it works though.

Consider this schema:

var BrandSchema = new Schema({ name: String });
var ProductSchema = new Schema({
    brand_id: { type: Schema.ObjectId, ref: 'Brand' }
  , name: String
  , description: String
});

Now if I use populate with this schema, it starts to look really weird:

Product.findOne().populate('brand_id').run(function(err, product) {

  console.log(product.brand_id); // <-- this is now a brand object

  var brand = new Brand({_id: 'levis', name: "Levi's"});
  product.brand_id = brand;      // <-- I'm assigning a brand object to the brand_id property

  console.log(product.brand_id); // <-- this is now a brand id

  product.save(function() {});
});

I've created a monkey patch to resolve this situation. It restores populated references to their 'id state' and creates an accessor for the populated object. It only supports references which end in _id (e.g. brand_id).

I would be happy to integrate this into core or create a plugin for it. Whichever is more appropriate.

Some limitations of my current approach:

  • it only supports references the names of which end in _id
  • it's not clear how this should handle collections
  • probably doesn't handle many cases

Further ideas for a more complete implementation:

  • ref accessors could be declared in the schema definition with something like this: brand: { refProp: 'brand_id' }
  • if the query wasn't run with populate the accessor could do an implicit fetch on first get and return a promise for the object
  • alternatively it could return null and a separate fetch method would be available for populating references on an initialized object
@lackac
Copy link
Contributor Author

lackac commented Oct 24, 2011

this is related to #570

@trungpham
Copy link

what is the status of this? i am running into the same problem too. thanks.

@wookets
Copy link

wookets commented Jun 25, 2012

I have the same issue. What is the solution? It seems like this is a pretty common use case when using populate. ID -> populated object, go to save and it says, "Error: Invalid ObjectID"

Is everyone just rolling a custom solution to get around this?

@aheckmann
Copy link
Collaborator

In 3.6, setting a populated path to a document will no longer cast to its _id.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants