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:
co-authored by
Anders Gustafsson
parent
3d7a36afc4
commit
be5471f958
@@ -10,8 +10,18 @@ services:
|
|||||||
ports:
|
ports:
|
||||||
- '4000:4000'
|
- '4000:4000'
|
||||||
environment:
|
environment:
|
||||||
|
- ENVIRONMENT_NAME=${ENVIRONMENT_NAME}
|
||||||
- COGNITO_POOL_ID=eu-west-1_3O4VfvPn7
|
- COGNITO_POOL_ID=eu-west-1_3O4VfvPn7
|
||||||
- DATABASE_URL=${DATABASE_URL}
|
- 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:
|
networks:
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -29,19 +29,20 @@ export class OrderAPI extends BaseSQLDataSource {
|
|||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getOrdersTotal(input: Maybe<GeneralInput>): Promise<number> {
|
async getOrdersTotal(input: GeneralInput): Promise<number> {
|
||||||
const res = await this.getOrdersQuery(input)
|
const res = await this.getOrdersQuery(input)
|
||||||
.clone()
|
.clone()
|
||||||
.count()
|
.count()
|
||||||
.first()
|
.first()
|
||||||
.cache(MINUTE * 60);
|
.cache(MINUTE * 5);
|
||||||
return res['count'] as number; // Optimize later to return Promise
|
return res['count'] as number; // Optimize later to return Promise
|
||||||
}
|
}
|
||||||
|
|
||||||
async getOrders(input: Maybe<GeneralInput>): Promise<Array<Order>> {
|
async getOrders(input: GeneralInput): Promise<Array<Order>> {
|
||||||
let query = this.getOrdersQuery(input).limit(input?.pagination.limit);
|
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');
|
query = query.orderBy('inserted', 'ASC');
|
||||||
return query.cache(MINUTE);
|
return query.cache(MINUTE);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -168,13 +168,12 @@ export class ProductAPI extends BaseSQLDataSource {
|
|||||||
async getProductsTotal(input: ProductsFilterInput): Promise<number> {
|
async getProductsTotal(input: ProductsFilterInput): Promise<number> {
|
||||||
const query = sql.productsTotal(input);
|
const query = sql.productsTotal(input);
|
||||||
const res = await this.cachedRaw(query)
|
const res = await this.cachedRaw(query)
|
||||||
.cache(MINUTE * 60)
|
.cache(MINUTE * 5)
|
||||||
.then((data) => data.rows);
|
.then((data) => data.rows);
|
||||||
return res[0]['count'] as number; // Optimize later to return Promise
|
return res[0]['count'] as number; // Optimize later to return Promise
|
||||||
}
|
}
|
||||||
|
|
||||||
async getProducts(input: ProductsFilterInput): Promise<Array<Product>> {
|
async getProducts(input: ProductsFilterInput): Promise<Array<Product>> {
|
||||||
// TODO: handle offset
|
|
||||||
const query = sql.products(input);
|
const query = sql.products(input);
|
||||||
|
|
||||||
return this.knex
|
return this.knex
|
||||||
|
|||||||
@@ -77,6 +77,7 @@ FROM "product-products" products
|
|||||||
|
|
||||||
export function products(input: ProductsFilterInput) {
|
export function products(input: ProductsFilterInput) {
|
||||||
const limit = input.pagination.limit ?? 100;
|
const limit = input.pagination.limit ?? 100;
|
||||||
|
const offset = input.pagination.offset ?? 0;
|
||||||
const filter = input.filter ?? null;
|
const filter = input.filter ?? null;
|
||||||
return (
|
return (
|
||||||
baseQuery +
|
baseQuery +
|
||||||
@@ -89,7 +90,7 @@ export function products(input: ProductsFilterInput) {
|
|||||||
: ``
|
: ``
|
||||||
}
|
}
|
||||||
ORDER BY products.inserted DESC
|
ORDER BY products.inserted DESC
|
||||||
LIMIT ${limit}
|
LIMIT ${limit} OFFSET ${offset}
|
||||||
`
|
`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,12 +41,13 @@ async function getOrdersResult(
|
|||||||
checkAccess(['orders.read'], auth);
|
checkAccess(['orders.read'], auth);
|
||||||
const total = (<OrderAPI>dataSources.orderApi).getOrdersTotal(input);
|
const total = (<OrderAPI>dataSources.orderApi).getOrdersTotal(input);
|
||||||
const items = (<OrderAPI>dataSources.orderApi).getOrders(input);
|
const items = (<OrderAPI>dataSources.orderApi).getOrders(input);
|
||||||
|
const offset = input.pagination?.offset ?? 0;
|
||||||
return {
|
return {
|
||||||
items: items,
|
items: items,
|
||||||
pagination: {
|
pagination: {
|
||||||
total: total,
|
total: total,
|
||||||
limit: input.pagination.limit,
|
limit: input.pagination.limit,
|
||||||
offset: 0, // TODO: this will be implemented later if the client app needs it
|
offset: offset,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,11 +68,11 @@ async function getProductsResult(
|
|||||||
checkAccess(['products.read'], auth);
|
checkAccess(['products.read'], auth);
|
||||||
const total = (<ProductAPI>dataSources.productApi).getProductsTotal(input);
|
const total = (<ProductAPI>dataSources.productApi).getProductsTotal(input);
|
||||||
const items = (<ProductAPI>dataSources.productApi).getProducts(input);
|
const items = (<ProductAPI>dataSources.productApi).getProducts(input);
|
||||||
|
const offset = input.pagination?.offset ?? 0;
|
||||||
return {
|
return {
|
||||||
pagination: {
|
pagination: {
|
||||||
limit: input.pagination.limit,
|
limit: input.pagination.limit,
|
||||||
offset: 0, // TODO: this will be implemented later if the client app needs it
|
offset: offset,
|
||||||
total: total,
|
total: total,
|
||||||
},
|
},
|
||||||
items: items,
|
items: items,
|
||||||
|
|||||||
@@ -58,9 +58,6 @@ input SignedUploadURLInput {
|
|||||||
|
|
||||||
input PaginationInput {
|
input PaginationInput {
|
||||||
limit: Int!
|
limit: Int!
|
||||||
"""
|
|
||||||
offset is not implemented yet and result will always return as if it was 0. Implement when needed.
|
|
||||||
"""
|
|
||||||
offset: Int
|
offset: Int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user