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