Skip to content

Commit

Permalink
Merge pull request #919 from activepieces/chore/timestamp
Browse files Browse the repository at this point in the history
fix: pagination
  • Loading branch information
AbdulTheActivePiecer committed Mar 29, 2023
2 parents df29643 + 13fca3b commit 590f816
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export const appConnectionService = {
const decodedCursor = paginationHelper.decodeCursor(cursorRequest);
const paginator = buildPaginator({
entity: AppConnectionEntity,
paginationKeys: ["created"],
query: {
limit,
order: "ASC",
Expand Down
1 change: 0 additions & 1 deletion packages/backend/src/app/collections/collection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export const collectionService = {
const decodedCursor = paginationHelper.decodeCursor(cursorRequest);
const paginator = buildPaginator({
entity: CollectionEntity,
paginationKeys: ["created"],
query: {
limit,
order: "ASC",
Expand Down
1 change: 0 additions & 1 deletion packages/backend/src/app/flow-run/flow-run-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export const flowRunService = {
const decodedCursor = paginationHelper.decodeCursor(cursor);
const paginator = buildPaginator({
entity: FlowRunEntity,
paginationKeys: ["created"],
query: {
limit,
order: Order.DESC,
Expand Down
1 change: 0 additions & 1 deletion packages/backend/src/app/flows/flow.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ export const flowService = {
const decodedCursor = paginationHelper.decodeCursor(cursorRequest);
const paginator = buildPaginator({
entity: FlowEntity,
paginationKeys: ["created"],
query: {
limit,
order: "ASC",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export const triggerEventService = {
const flowId = flowVersion.flowId;
const paginator = buildPaginator({
entity: TriggerEventEntity,
paginationKeys: ["created"],
query: {
limit,
order: Order.DESC,
Expand Down
5 changes: 2 additions & 3 deletions packages/backend/src/app/helper/pagination/build-paginator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ export interface PaginationOptions<Entity> {
entity: EntitySchema<Entity>;
alias?: string;
query?: PagingQuery;
paginationKeys: Array<Extract<keyof Entity, string>>;
}

export function buildPaginator<Entity extends ObjectLiteral>(options: PaginationOptions<Entity>): Paginator<Entity> {
const { entity, query = {}, alias = entity.options.name.toLowerCase(), paginationKeys } = options;
const { entity, query = {}, alias = entity.options.name.toLowerCase() } = options;

const paginator = new Paginator(entity, paginationKeys);
const paginator = new Paginator<Entity>(entity);

paginator.setAlias(alias);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function encodeByType(type: string, value: any): string | null {
switch (type) {
case "timestamp with time zone":
case "date": {
return (value as Date).getTime().toString();
return new Date(value).valueOf().toString();
}
case "number": {
return `${value}`;
Expand Down Expand Up @@ -47,12 +47,10 @@ export function decodeByType(type: string, value: string): string | number | Dat
case "timestamp with time zone":
case "date": {
const timestamp = parseInt(value, 10);

if (Number.isNaN(timestamp)) {
throw new Error("date column in cursor should be a valid timestamp");
}

return new Date(timestamp);
return new Date(timestamp).toISOString();
}

case "number": {
Expand Down
41 changes: 15 additions & 26 deletions packages/backend/src/app/helper/pagination/paginator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@ import {
import { atob, btoa, decodeByType, encodeByType } from "./pagination-utils";

export enum Order {
ASC = "ASC",
DESC = "DESC",
ASC = "ASC",
DESC = "DESC",
}

export type CursorParam = Record<string, any>;

export interface CursorResult {
beforeCursor: string | null;
afterCursor: string | null;
beforeCursor: string | null;
afterCursor: string | null;
}

export interface PagingResult<Entity> {
data: Entity[];
cursor: CursorResult;
data: Entity[];
cursor: CursorResult;
}

const PAGINATION_KEY = "created";

export default class Paginator<Entity extends ObjectLiteral> {
private afterCursor: string | null = null;

Expand All @@ -41,9 +43,8 @@ export default class Paginator<Entity extends ObjectLiteral> {
private order: Order = Order.DESC;

public constructor(
private readonly entity: EntitySchema,
private readonly paginationKeys: Array<Extract<keyof Entity, string>>
) {}
private readonly entity: EntitySchema,
) { }

public setAlias(alias: string): void {
this.alias = alias;
Expand Down Expand Up @@ -122,13 +123,7 @@ export default class Paginator<Entity extends ObjectLiteral> {

private buildCursorQuery(where: WhereExpressionBuilder, cursors: CursorParam): void {
const operator = this.getOperator();
const params: CursorParam = {};
let query = "";
this.paginationKeys.forEach((key) => {
params[key] = cursors[key];
where.orWhere(`${query}${this.alias}.${key} ${operator} :${key}`, params);
query = `${query}${this.alias}.${key} = :${key} AND `;
});
where.orWhere(`DATE_TRUNC('second', ${this.alias}.${PAGINATION_KEY}) ${operator} DATE_TRUNC('second', :${PAGINATION_KEY}::timestamp)`, cursors);
}

private getOperator(): string {
Expand All @@ -151,9 +146,7 @@ export default class Paginator<Entity extends ObjectLiteral> {
}

const orderByCondition: OrderByCondition = {};
this.paginationKeys.forEach((key) => {
orderByCondition[`${this.alias}.${key}`] = order;
});
orderByCondition[`${this.alias}.${PAGINATION_KEY}`] = order;

return orderByCondition;
}
Expand All @@ -167,13 +160,9 @@ export default class Paginator<Entity extends ObjectLiteral> {
}

private encode(entity: Entity): string {
const payload = this.paginationKeys
.map((key) => {
const type = this.getEntityPropertyType(key);
const value = encodeByType(type, entity[key]);
return `${key}:${value}`;
})
.join(",");
const type = this.getEntityPropertyType(PAGINATION_KEY);
const value = encodeByType(type, entity[PAGINATION_KEY]);
const payload = `${PAGINATION_KEY}:${value}`

return btoa(payload);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export const appCredentialService = {
const decodedCursor = paginationHelper.decodeCursor(cursorRequest ?? null);
const paginator = buildPaginator({
entity: AppCredentialEntity,
paginationKeys: ["created"],
query: {
limit,
order: "ASC",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ export const connectionKeyService = {
const decodedCursor = paginationHelper.decodeCursor(cursorRequest ?? null);
const paginator = buildPaginator({
entity: ConnectionKeyEntity,
paginationKeys: ["created"],
query: {
limit,
order: "ASC",
Expand Down

0 comments on commit 590f816

Please sign in to comment.