JKHeadley / rest-hapi

🚀 A RESTful API generator for Node.js
https://resthapi.com
MIT License
1.19k stars 153 forks source link

Associations within subdocs? #102

Open tamias opened 6 years ago

tamias commented 6 years ago

(This is more of a question than an issue, but it could turn into an issue.)

Is it possible, in rest-hapi, to have an association within a subdocument?

For example, I can do this in Mongoose, where the association is at subdoc.pets, instead of being at the top level of the document:

const ownerSchema = new Schema({
  name: Schema.Types.String,
  subdoc: {
    pets: [{ type: Schema.Types.ObjectId, ref: 'pet' }],
  },
});

Owner.findOne().populate('subdoc.pets');

If it were at the top level, I would implement this in rest-hapi by adding an association under routeOptions. I'm not sure how or if I can do this within a subdocument, however.

I don't actually need endpoints for this particular association; I just need to be able to populate it when requesting the document, if that makes a difference.

Thanks!

JKHeadley commented 6 years ago

It might be possible to accomplish something like this using rest-hapi, but it would be more of a hack since it is not part of the intended functionality.

However, keep in mind that rest-hapi is built on top of Mongoose, so you can still take advantage of populating docs directly.

tamias commented 6 years ago

Okay, thank you for the response!

tamias commented 6 years ago

A quick followup. The difficulty I'm running into is that if I want to call .populate() myself, I can't use Rest-Hapi's handler helpers in that flow, because they all call .lean(). The solution we went with for now is to call RestHapi.find(), then retrieve the associated documents with a separate query and populate them manually.

It would be great if there were some way to specify an embed of 'subdoc.pets' that made populateEmbeddedDocs call .populate({ path: 'subdoc.pets' }) instead of .populate({ path: 'subdoc', populate: { path: 'pets' } }).

JKHeadley commented 6 years ago

@tamias thanks for pointing this out. I agree! I think I'll add two more feature requests: one to have an option of disabling .lean() and one for populating subdocs.

fabripeco commented 5 years ago

Populating subdocs would be a very important feature for an association. From the Swagger documentation it seems to be possible image

but server returns 500 with this error — Error: Association not found. [08:46:24.493] 5437 ERROR node_modules/rest-hapi/utilities/handler-helper.js:1677:9 api/user/GetAll — at nestPopulate (/home/fabrizio/Projects/gse-bip/gse-server/backend/node_modules/rest-hapi/utilities/query-helper.js:523:13) [08:46:24.493] 5437 ERROR node_modules/rest-hapi/utilities/handler-helper.js:1677:9 api/user/GetAll — at /home/fabrizio/Projects/gse-bip/gse-server/backend/node_modules/rest-hapi/utilities/query-helper.js:408:20 [08:46:24.493] 5437 ERROR node_modules/rest-hapi/utilities/handler-helper.js:1677:9 api/user/GetAll — at Array.forEach (<anonymous>) [08:46:24.493] 5437 ERROR node_modules/rest-hapi/utilities/handler-helper.js:1677:9 api/user/GetAll — at Object.populateEmbeddedDocs (/home/fabrizio/Projects/gse-bip/gse-server/backend/node_modules/rest-hapi/utilities/query-helper.js:404:20) [08:46:24.493] 5437 ERROR node_modules/rest-hapi/utilities/handler-helper.js:1677:9 api/user/GetAll — at Object.createMongooseQuery (/home/fabrizio/Projects/gse-bip/gse-server/backend/node_modules/rest-hapi/utilities/query-helper.js:88:23) [08:46:24.493] 5437 ERROR node_modules/rest-hapi/utilities/handler-helper.js:1677:9 api/user/GetAll — at _listHandler (/home/fabrizio/Projects/gse-bip/gse-server/backend/node_modules/rest-hapi/utilities/handler-helper.js:115:33)

It is a Mongoose feature, why is it not possibile with RestHapi?. Or am I wrong in something? Thanks!