Add pagination to orders and products (#106)

* Add pagination to orders and products

* remove comment

Co-authored-by: Anders Gustafsson <anders@photowall.se>
This commit is contained in:
Niklas Fondberg
2022-02-16 16:20:35 +01:00
committed by GitHub
co-authored by Anders Gustafsson
parent 3d7a36afc4
commit be5471f958
7 changed files with 23 additions and 14 deletions
+10
View File
@@ -10,8 +10,18 @@ services:
ports:
- '4000:4000'
environment:
- ENVIRONMENT_NAME=${ENVIRONMENT_NAME}
- COGNITO_POOL_ID=eu-west-1_3O4VfvPn7
- DATABASE_URL=${DATABASE_URL}
- INTERIORS_TOKEN=${INTERIORS_TOKEN}
- INTERIORS_URL=${INTERIORS_URL}
- AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
- AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
- AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION}
- NODE_ENV=development
- BERNARD_QUEUE_URL=${BERNARD_QUEUE_URL}
- NEW_RELIC_LICENSE_KEY=${NEW_RELIC_LICENSE_KEY}
- NEW_RELIC_APP_NAME=${NEW_RELIC_APP_NAME}
networks:
default:
+6 -5
View File
@@ -29,19 +29,20 @@ export class OrderAPI extends BaseSQLDataSource {
: null;
}
async getOrdersTotal(input: Maybe<GeneralInput>): Promise<number> {
async getOrdersTotal(input: GeneralInput): Promise<number> {
const res = await this.getOrdersQuery(input)
.clone()
.count()
.first()
.cache(MINUTE * 60);
.cache(MINUTE * 5);
return res['count'] as number; // Optimize later to return Promise
}
async getOrders(input: Maybe<GeneralInput>): Promise<Array<Order>> {
let query = this.getOrdersQuery(input).limit(input?.pagination.limit);
async getOrders(input: GeneralInput): Promise<Array<Order>> {
const offset = input.pagination.offset ?? 0;
const limit = input.pagination.limit ?? 100;
let query = this.getOrdersQuery(input).limit(limit).offset(offset);
// TODO: handle offset
query = query.orderBy('inserted', 'ASC');
return query.cache(MINUTE);
}
+1 -2
View File
@@ -168,13 +168,12 @@ export class ProductAPI extends BaseSQLDataSource {
async getProductsTotal(input: ProductsFilterInput): Promise<number> {
const query = sql.productsTotal(input);
const res = await this.cachedRaw(query)
.cache(MINUTE * 60)
.cache(MINUTE * 5)
.then((data) => data.rows);
return res[0]['count'] as number; // Optimize later to return Promise
}
async getProducts(input: ProductsFilterInput): Promise<Array<Product>> {
// TODO: handle offset
const query = sql.products(input);
return this.knex
+2 -1
View File
@@ -77,6 +77,7 @@ FROM "product-products" products
export function products(input: ProductsFilterInput) {
const limit = input.pagination.limit ?? 100;
const offset = input.pagination.offset ?? 0;
const filter = input.filter ?? null;
return (
baseQuery +
@@ -89,7 +90,7 @@ export function products(input: ProductsFilterInput) {
: ``
}
ORDER BY products.inserted DESC
LIMIT ${limit}
LIMIT ${limit} OFFSET ${offset}
`
);
}
+2 -1
View File
@@ -41,12 +41,13 @@ async function getOrdersResult(
checkAccess(['orders.read'], auth);
const total = (<OrderAPI>dataSources.orderApi).getOrdersTotal(input);
const items = (<OrderAPI>dataSources.orderApi).getOrders(input);
const offset = input.pagination?.offset ?? 0;
return {
items: items,
pagination: {
total: total,
limit: input.pagination.limit,
offset: 0, // TODO: this will be implemented later if the client app needs it
offset: offset,
},
};
}
+2 -2
View File
@@ -68,11 +68,11 @@ async function getProductsResult(
checkAccess(['products.read'], auth);
const total = (<ProductAPI>dataSources.productApi).getProductsTotal(input);
const items = (<ProductAPI>dataSources.productApi).getProducts(input);
const offset = input.pagination?.offset ?? 0;
return {
pagination: {
limit: input.pagination.limit,
offset: 0, // TODO: this will be implemented later if the client app needs it
offset: offset,
total: total,
},
items: items,
-3
View File
@@ -58,9 +58,6 @@ input SignedUploadURLInput {
input PaginationInput {
limit: Int!
"""
offset is not implemented yet and result will always return as if it was 0. Implement when needed.
"""
offset: Int
}