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

Added "createPaginationMeta" helper function for manual creation of pagination meta #265

Merged
merged 1 commit into from
Oct 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow
import type { Entity, EntityCollection } from "webiny-entity";
import { createPaginationMeta } from "webiny-entity";
import { ListResponse } from "webiny-api/graphql/responses";

type EntityFetcher = (context: Object) => Class<Entity>;
Expand Down Expand Up @@ -39,19 +40,12 @@ export default (entityFetcher: EntityFetcher) => async (
};

const pages: EntityCollection<Entity> = await entityClass.find({ sql });

const meta: Object = {
page,
perPage,
count: pages.length,
totalCount: pages.getMeta().totalCount
};

meta.totalPages = Math.ceil(meta.totalCount / meta.perPage);
meta.to = (meta.page - 1) * meta.perPage + meta.count;
meta.from = meta.to - meta.count + 1;
meta.nextPage = meta.page < meta.totalPages ? meta.page + 1 : null;
meta.previousPage = meta.page === 1 ? null : meta.page - 1;

return new ListResponse(pages, meta);
return new ListResponse(
pages,
createPaginationMeta({
page,
perPage,
totalCount: pages.getMeta().totalCount
})
);
};
11 changes: 1 addition & 10 deletions packages/webiny-api/src/graphql/crudResolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,7 @@ export const resolveList = (entityFetcher: EntityFetcher) => async (
sort: args.sort
});

const meta = data.getParams();
meta.count = data.length;
meta.totalCount = data.getMeta().totalCount;
meta.totalPages = Math.ceil(meta.totalCount / meta.perPage);
meta.to = (meta.page - 1) * meta.perPage + meta.count;
meta.from = meta.to - meta.count + 1;
meta.nextPage = meta.page < meta.totalPages ? meta.page + 1 : null;
meta.previousPage = meta.page === 1 ? null : meta.page - 1;

return new ListResponse(data, meta);
return new ListResponse(data, data.getMeta());
};

export const resolveCreate = (entityFetcher: EntityFetcher) => async (
Expand Down
3 changes: 1 addition & 2 deletions packages/webiny-api/src/graphql/schema/genericTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export const genericTypes = () => {
}

type ListMeta {
count: Int
totalCount: Int
totalPages: Int
page: Int
Expand All @@ -30,4 +29,4 @@ export const genericTypes = () => {
data: JSON
}
`;
};
};
11 changes: 8 additions & 3 deletions packages/webiny-app-admin/src/views/ApiTokens/graphql.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
// @flow
import gql from "graphql-tag";

const fields = `
id token name description groups { id name } roles { id name }
`;

export const loadApiTokens = gql`
query LoadApiTokens($where: JSON, $sort: JSON, $page: Int, $perPage: Int, $search: SearchInput) {
query LoadApiTokens(
$where: JSON
$sort: JSON
$page: Int
$perPage: Int
$search: SearchInput
) {
security {
tokens: listApiTokens(
where: $where
Expand All @@ -21,9 +28,7 @@ export const loadApiTokens = gql`
createdOn
}
meta {
count
totalCount
totalPages
to
from
nextPage
Expand Down
2 changes: 0 additions & 2 deletions packages/webiny-app-admin/src/views/Groups/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ export const loadGroups = gql`
createdOn
}
meta {
count
totalCount
totalPages
to
from
nextPage
Expand Down
3 changes: 1 addition & 2 deletions packages/webiny-app-admin/src/views/Roles/graphql.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @flow
import gql from "graphql-tag";

const fields = `
Expand Down Expand Up @@ -33,9 +34,7 @@ export const loadRoles = gql`
createdOn
}
meta {
count
totalCount
totalPages
to
from
nextPage
Expand Down
3 changes: 1 addition & 2 deletions packages/webiny-app-admin/src/views/Users/graphql.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @flow
import gql from "graphql-tag";

const fields = `
Expand Down Expand Up @@ -26,9 +27,7 @@ export const loadUsers = gql`
createdOn
}
meta {
count
totalCount
totalPages
to
from
nextPage
Expand Down
7 changes: 2 additions & 5 deletions packages/webiny-app-cms/src/admin/graphql/pages.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @flow
import gql from "graphql-tag";

const error = `
Expand Down Expand Up @@ -45,13 +46,9 @@ export const listPages = gql`
}
}
meta {
count
totalCount
from
to
page
totalPages
perPage
from
nextPage
previousPage
}
Expand Down
11 changes: 8 additions & 3 deletions packages/webiny-app-cms/src/admin/views/Categories/graphql.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @flow
import gql from "graphql-tag";

const fields = `
Expand All @@ -9,7 +10,13 @@ const fields = `
`;

export const loadCategories = gql`
query LoadCategories($where: JSON, $sort: JSON, $page: Int, $perPage: Int, $search: SearchInput) {
query LoadCategories(
$where: JSON
$sort: JSON
$page: Int
$perPage: Int
$search: SearchInput
) {
cms {
categories: listCategories(
where: $where
Expand All @@ -26,9 +33,7 @@ export const loadCategories = gql`
createdOn
}
meta {
count
totalCount
totalPages
to
from
nextPage
Expand Down
10 changes: 8 additions & 2 deletions packages/webiny-entity-mysql/src/mysqlDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import _ from "lodash";
import mdbid from "mdbid";
import type { Connection, Pool } from "mysql";
import { Entity, Driver, QueryResult } from "webiny-entity";
import { Entity, Driver, QueryResult, createPaginationMeta } from "webiny-entity";
import { MySQLConnection } from "webiny-mysql-connection";
import { Attribute } from "webiny-model";
import type {
Expand Down Expand Up @@ -173,7 +173,13 @@ class MySQLDriver extends Driver {
const sql = new Select(clonedOptions, entity).generate();
const results = await this.getConnection().query([sql, "SELECT FOUND_ROWS() as count"]);

return new QueryResult(results[0], { totalCount: results[1][0].count });
const meta = createPaginationMeta({
totalCount: results[1][0].count,
page: options.page,
perPage: options.perPage
});

return new QueryResult(results[0], meta);
}

async findOne(
Expand Down
90 changes: 90 additions & 0 deletions packages/webiny-entity/__tests__/createPaginationMeta.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { createPaginationMeta } from "webiny-entity";

describe("createPaginationMeta test", () => {
test("should return correct pagination meta data", async () => {
let meta = createPaginationMeta({
page: 1,
perPage: 10,
totalCount: 100
});

expect(meta).toEqual({
from: 1,
nextPage: 2,
page: 1,
perPage: 10,
previousPage: null,
to: 10,
totalCount: 100,
totalPages: 10
});

meta = createPaginationMeta({
totalCount: 100,
page: 3,
perPage: 7
});

expect(meta).toEqual({
page: 3,
perPage: 7,
totalCount: 100,
from: 15,
nextPage: 4,
previousPage: 2,
to: 21,
totalPages: 15
});

meta = createPaginationMeta({
totalCount: 100,
page: 3,
perPage: 10
});

expect(meta).toEqual({
from: 21,
nextPage: 4,
page: 3,
perPage: 10,
previousPage: 2,
to: 30,
totalCount: 100,
totalPages: 10
});

meta = createPaginationMeta({
totalCount: 15,
page: 3,
perPage: 6
});

expect(meta).toEqual({
from: 13,
nextPage: null,
page: 3,
perPage: 6,
previousPage: 2,
to: 15,
totalCount: 15,
totalPages: 3
});

meta = createPaginationMeta({
totalCount: 0,
page: 1,
perPage: 10
});

expect(meta).toEqual({
page: 1,
perPage: 10,
nextPage: null,
previousPage: null,
to: 0,
from: 0,
totalCount: 0,
totalPages: 0
});
});
});
6 changes: 0 additions & 6 deletions packages/webiny-entity/__tests__/entityCollection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ describe("EntityCollection test", () => {
expect(collection.getParams().a).toEqual(123);
});

test("setTotalCount/getTotalCount methods must work correctly", async () => {
const collection = new EntityCollection();
collection.setTotalCount(444);
expect(collection.getTotalCount()).toEqual(444);
});

test("setMeta/getMeta methods must work correctly", async () => {
const collection = new EntityCollection();
collection.setMeta({ a: 123 });
Expand Down
1 change: 1 addition & 0 deletions packages/webiny-entity/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export { default as EntityPoolEntry } from "./src/entityPoolEntry";
export { default as EntityCollection } from "./src/entityCollection";
export { default as EntityAttributesContainer } from "./src/entityAttributesContainer";
export { default as QueryResult } from "./src/queryResult";
export { default as createPaginationMeta } from "./src/createPaginationMeta";
export { default as Driver } from "./src/driver";
export { default as EventHandler } from "./src/eventHandler";

Expand Down
52 changes: 52 additions & 0 deletions packages/webiny-entity/src/createPaginationMeta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// @flow
type PaginationMeta = {
page: number,
perPage: number,
totalCount: number,

totalPages: ?number,
from: ?number,
to: ?number,
nextPage: ?number,
previousPage: ?number
};

type Params = {
page: number,
perPage: number,
totalCount: number
};

export default (params: ?Params): PaginationMeta => {
const meta: PaginationMeta = {
page: 0,
perPage: 0,
totalCount: 0,
totalPages: null,
from: null,
to: null,
nextPage: null,
previousPage: null,
...params
};

if (meta.page && meta.perPage) {
meta.totalPages = Math.ceil(meta.totalCount / meta.perPage);

if (meta.totalCount) {
meta.from = 1 + meta.perPage * (meta.page - 1);
} else {
meta.from = 0;
}

meta.to = meta.perPage * meta.page;
if (meta.to > meta.totalCount) {
meta.to = meta.totalCount;
}

meta.nextPage = meta.page < meta.totalPages ? meta.page + 1 : null;
meta.previousPage = meta.page === 1 ? null : meta.page - 1;
}

return meta;
};
Loading