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

Content-Range when there is association, counts the inner object as well. #198

Open
zivchen opened this issue Nov 26, 2016 · 6 comments
Open

Comments

@zivchen
Copy link

zivchen commented Nov 26, 2016

Hi
I have 2 object, Test and Step.
Test has many Steps.
I have 4 Tests and 200 steps.
model definition:
endpoints.testResource = epilogue.resource({
model: models.Test,
endpoints: ['/admin/api/tests', '/admin/api/tests/:id'],
associations: true
});

when doing get-> /tests i get content range 0-4/ 204.
so my admin shows pagination when there are none.
if i remove the association its great.
thanks.

Ziv

@xtr0
Copy link

xtr0 commented Dec 2, 2016

@zivchen
As a workaround you can try the following:

endpoints.testResource.list.fetch.before = function (req, res, context) {
    context.options = context.options || {};
    context.options.distinct = true;
    
    return context.continue;
}

At least it works for me

@zlatinejc
Copy link

Distinct could be set as default and mentioned in the docs.

@kvanbere
Copy link

kvanbere commented Jul 14, 2017

This workaround isn't working for me, here's what I did:

	let accounts = epilogue.resource({
		model: models.Account,
		endpoints: ['/accounts', '/accounts/:id'],
		associations: true
	});
	accounts.list.fetch.before = function (req, res, context) {
		context.options = context.options || {};
		context.options.distinct = true;
		
		return context.continue;
	};

Request is i.e. accounts?offset=70&count=10

I'm getting a response Content-Range:items 70-79/153 but there are only 111 rows.

@edrpls
Copy link

edrpls commented Jul 14, 2017

@kvanberendonck it didn't work for me either, I think it's also a bug in sequelize, at least with mysql, I ended up doing two queries and merging the associations.

@kvanbere
Copy link

I made it work ! I'll share solution when I get off lunch

@kvanbere
Copy link

kvanbere commented Jul 14, 2017

@edrpls

	let fixPagination = (endpoint) => {
		endpoint.use({ list: { fetch: { before: function(req, res, context) {
			context.options = context.options || {};
			context.options.distinct = true;
			return context.continue;
		}}}});
	};

//...

	let foo = epilogue.resource({
		model: models.Foo,
		endpoints: ['/foo', '/foo/:id'],
		associations: true
	});
	fixPagination(foo);

Using foo.list.fetch.before for example wasn't actually working as a hook. Need to use .use() for some reason.

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

5 participants