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

Allow feature to populate embedded data #189

Open
numerical25 opened this issue Feb 14, 2019 · 2 comments
Open

Allow feature to populate embedded data #189

numerical25 opened this issue Feb 14, 2019 · 2 comments

Comments

@numerical25
Copy link

numerical25 commented Feb 14, 2019

Is there a reason JSON:API server doesn't allow the ability to populate embedded data ? Right now it only allows you to populate reference data and it does not allow for Multi Level Support .

I was easily able to get this to work by altering this code to MongooseAdapter.js

I am not suggesting the following code!, I am just demostrating how easy a simple feature can be added but is instead disregarded.

            queryBuilder.populate(includePaths[0]);
            includePaths.map((it) => it.split(".")).forEach((pathParts) => {
                if (!refPaths.includes(pathParts[0])) {
                    // throw Errors.invalidIncludePath({
                    //     detail: `Resources of type "${type}" don't have a(n) "${pathParts[0]}" relationship.`
                    // });
                }
                if (pathParts.length > 1) {
                    // throw Errors.unsupportedIncludePath({
                    //     detail: `Multi-level include paths like ${pathParts.join('.')} aren't yet supported.`
                    // });
                }
                //populatedPaths.push(pathParts[0]);
                //queryBuilder.populate(pathParts[0]);
            });

Can I suggest a Code Change to allow for this feature ?

@ethanresnick
Copy link
Owner

Hey!

I'm not quite sure I understand what you're asking for. Can you provide some examples of how this would work? In the examples, it would help if you could include: how the document would be structured in mongo; what the incoming request would look like; and then what the desired response would be.

@numerical25
Copy link
Author

numerical25 commented Feb 15, 2019

Im saying in the router when someone pushes a field to the populates array. Don't you think the json-server should check for the reference path and the embedded path ? Right now it only checks for a field that references another table. In my case, I am trying to populate a field called users. Here is the schema

Below The Venue Has a Embedded EventSchema assigned to it

var VenueSchema = new mongoose.Schema({
    name: String,
    address: String,
    city: String,
    state: String,
    zip: Number,
    location: {
        type: mongoose.Schema.Types.Mixed ,
        coordinates: []
    },
    created_at: {type:Date,default:Date.now},
    updated_at: {type:Date,default:Date.now, set:v => Date.now},
    events: [Event.schema]
});

The Event Schema has a ref to the User Schema

var EventSchema = new mongoose.Schema({
    name: String,
    user:{ type: mongoose.Schema.Types.ObjectId, ref: 'User' },
    start_date: Date,
    end_date: Date
});

I would just like populate the user information

router.get("/:type(venues)", Front.customAPIRequest({
    queryFactory: async (opts) => {
        const origQuery = await opts.makeQuery(opts);
        origQuery.populates.push('events.user');
        return origQuery;
    }
}));

This could easily be achieved simply by adding the includesPath to the populate. queryBuilder.populate(includePaths[0]);

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

2 participants