dchester / epilogue

Create flexible REST endpoints and controllers from Sequelize models in your Express app
846 stars 116 forks source link

/<parent entity>/:parent_id/<entity>/[:id] #102

Closed mark-lester closed 8 years ago

mark-lester commented 9 years ago

sequelize-restful just bit the dust with the latest sequelize. He told me to come here. I was using /parent entity/:parent_id/entity/[:id] that's not listed in the instructions. Do I need to implement this / salvage from sequelize-restful ?

mbroadst commented 9 years ago

Hey, I think what you're referring to is if you have, for example, a one-to-many relation between Song and Album, being able to look up like /album/12/song/3? We had sort of a long debate about this over on #34, and came to the conclusion at the time that in that case there's no real need for the /album/12 part of the route; if you're already working with a list of Songs from that Album, this would essentially be the same as /songs/:id.

To that end, and please correct me if the assumption I made earlier is incorrect, no that is not currently implemented in epilogue. I'm not completely against incorporating the feature into the module, but I think some discussion about the merits of that approach is warranted first.

mark-lester commented 9 years ago

Hi, I want a relational model on the front end so that parent collections can have a currently selected model, and thus the child collection need to be the set of members of that parent. So yes I want the songs on album 12. How do I ask for that ?

mark-lester commented 9 years ago

boy that thread's long and it's late here, I'm sure you do this, I dont need arbitrary nesting (I got as far as "that's an anti-pattern"), but that's such a long discussion I should read as soon as I get up. I also need a callback to implement some filter on posted and getted records as an access cotnrol mechanism, I think this middleware stuff you've done must handle that. much obliged.

mbroadst commented 9 years ago

@mark-lester to get the songs on album 12 you would first enable associations: true on your resource to trigger automatic association generation. Then you would point your browser to like /albums/12/songs

mark-lester commented 9 years ago

@mbroadst after some thought (and editing what I said earlier) I realised that my problem now is that DataSet owns everything, apart from User which owns it. I am getting an outer join on 10 tables when I just go /DataSets, and that breaks. /DataSets/1/Agencies works, which is why I thought it was because I'd declared a resource on everything it had got upset. All I want /DataSets to do is list datasets, i dont want a join.

mark-lester commented 9 years ago

OK, so it was that nasty options.include array. I've just brutalised that for now, is there a switch to turn it off or just explicitly specify the models you want.

  if (Object.keys(criteria).length)
    options.where = criteria;
options.include=[]; // brutalisation
  return model
    .findAndCountAll(options)
...
mbroadst commented 9 years ago

Hm, I haven't really experienced a situation like this yet (this might be a smell that your DataSet is doing a bit too much work, however that's most likely not changeable). If you look here you'll see that we're using this.include to cache the includes on a per-resource basis. Theoretically you could create the resource, and immediately afterwards do something like myResource.controllers.list.include = [] though this might cause some other sort of havoc considering we don't test these types of scenarios.

mark-lester commented 9 years ago

Yeah my DataSet is sort of a DB instance, so it owns everything. Things seem to be working, but I havent done any posting or anything yet, so I'll let you know. Thanks a lot for this stuff.

carrbrpoa commented 8 years ago

Hello,

I'm new to epilogue and sequelize and wish to achieve the same thing. Tried with this:

var Project = database.define('Projects', {
    name: {
        type: Sequelize.STRING
    },
    archived: {
        type: Sequelize.BOOLEAN
    }
});

var Sprint = database.define('Sprints', {
    name: {
        type: Sequelize.STRING
    },
    startsAt: {
        type: Sequelize.DATE
    },
    endsAt: {
        type: Sequelize.DATE
    }
});

Project.hasMany(Sprint, {as: 'Sprints'});

(...)

var projectResource = epilogue.resource({
        model : Project,
        associations : true,
        endpoints : ['/projects', '/projects/:id']
    });

I noticed that I can request, as example, GET to projects/1/sprints (project with id=1 exists), which returns an empty array, but I can't POST to the same address, coming status 404 as response.

Is it possible to achieve it? I'm I missing something?

Thanks in advance