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
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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}
|
||||
`
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user