Skip to content

Commit

Permalink
fixup! Optimised queries made by get helper for posts
Browse files Browse the repository at this point in the history
  • Loading branch information
allouis committed Mar 14, 2024
1 parent f14ba03 commit 5588458
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 5 deletions.
14 changes: 9 additions & 5 deletions ghost/core/core/frontend/helpers/get.js
Expand Up @@ -140,7 +140,7 @@ async function makeAPICall(resource, controllerName, action, apiOptions) {
const parsedFilter = nqlLang.parse(apiOptions.filter);
// Support either `id:blah` or `id:blah+other:stuff`
if (parsedFilter.$and || parsedFilter.id) {
const queries = parsedFilter.$and || [parsedFilter.id];
const queries = parsedFilter.$and || [parsedFilter];

for (const query of queries) {
if ('id' in query) {
Expand All @@ -152,7 +152,9 @@ async function makeAPICall(resource, controllerName, action, apiOptions) {

// The default limit is 15, the addition order is to cast apiOptions.limit to a number
let limit = apiOptions.limit;
limit = apiOptions.limit && apiOptions.limit !== 'all' ? 1 + apiOptions.limit : 16;
if (apiOptions.limit !== 'all') {
limit = apiOptions.limit ? 1 + parseInt(apiOptions.limit, 10) : 16;
}

// We replace with id:-null so we don't have to deal with leading/trailing AND operators
const filter = apiOptions.filter.replace(/id:-[a-f0-9A-F]{24}/, 'id:-null');
Expand All @@ -164,11 +166,13 @@ async function makeAPICall(resource, controllerName, action, apiOptions) {
filter
});

const filteredPosts = result?.posts?.filter((post) => {
return post.id !== idToFilter;
}) || [];

return {
...result,
posts: result?.posts?.filter((post) => {
return post.id !== idToFilter;
}).slice(0, limit - 1) || []
posts: limit === 'all' ? filteredPosts : filteredPosts.slice(0, limit - 1)
};
};
}
Expand Down
78 changes: 78 additions & 0 deletions ghost/core/test/e2e-frontend/helpers/get.test.js
Expand Up @@ -100,6 +100,83 @@ describe('e2e {{#get}} helper', function () {
});

describe('Filter optimisation', function () {
it('Returns the correct posts with limit as a string', async function () {
await get.call({}, 'posts', {
hash: {
limit: '5'
},
data: {},
locals,
fn,
inverse
});
const firstPostUsually = fn.firstCall.args[0].posts[0];
const firstLimit = fn.firstCall.args[0].meta.pagination.limit;
assert.equal(firstLimit, 5);
await get.call({}, 'posts', {
hash: {
filter: `id:-${firstPostUsually.id}`,
limit: '5'
},
data: {},
locals,
fn,
inverse
});
assert.equal(fn.secondCall.args[0].meta.pagination.limit, firstLimit + 1);
const foundFilteredPost = fn.secondCall.args[0].posts.find(post => post.id === firstPostUsually.id);
assert.equal(foundFilteredPost, undefined);
});
it('Returns the correct posts with default limit', async function () {
await get.call({}, 'posts', {
hash: {},
data: {},
locals,
fn,
inverse
});
const firstPostUsually = fn.firstCall.args[0].posts[0];
const defaultLimit = fn.firstCall.args[0].meta.pagination.limit;
await get.call({}, 'posts', {
hash: {
filter: `id:-${firstPostUsually.id}`
},
data: {},
locals,
fn,
inverse
});
assert.equal(fn.secondCall.args[0].meta.pagination.limit, defaultLimit + 1);
const foundFilteredPost = fn.secondCall.args[0].posts.find(post => post.id === firstPostUsually.id);
assert.equal(foundFilteredPost, undefined);
});
it('Returns the correct posts with a limit all', async function () {
await get.call({}, 'posts', {
hash: {
limit: 'all'
},
data: {},
locals,
fn,
inverse
});
const firstPostUsually = fn.firstCall.args[0].posts[0];
const initialCount = fn.firstCall.args[0].posts.length;
await get.call({}, 'posts', {
hash: {
filter: `id:-${firstPostUsually.id}`,
limit: 'all'
},
data: {},
locals,
fn,
inverse
});
assert.equal(fn.secondCall.args[0].posts.length, initialCount - 1);
assert.equal(fn.secondCall.args[0].meta.pagination.limit, 'all');
const foundFilteredPost = fn.secondCall.args[0].posts.find(post => post.id === firstPostUsually.id);
assert.equal(foundFilteredPost, undefined);
});
it('Returns the correct posts with a solo negative filter', async function () {
await get.call({}, 'posts', {
hash: {
Expand All @@ -122,6 +199,7 @@ describe('e2e {{#get}} helper', function () {
inverse
});
assert.equal(fn.secondCall.args[0].posts.length, 5);
assert.equal(fn.secondCall.args[0].meta.pagination.limit, 6);
const foundFilteredPost = fn.secondCall.args[0].posts.find(post => post.id === firstPostUsually.id);
assert.equal(foundFilteredPost, undefined);
});
Expand Down

0 comments on commit 5588458

Please sign in to comment.