[Flexible-schema] Add findOne and fix findMany pagination + soft-delete for graphql-query-runner (#6978)

This commit is contained in:
Weiko
2024-09-11 11:29:56 +02:00
committed by GitHub
parent 425eb040f7
commit 1317e1c4f2
11 changed files with 355 additions and 109 deletions

View File

@ -1,28 +1,15 @@
import {
FindOptionsOrderValue,
FindOptionsWhere,
LessThan,
MoreThan,
ObjectLiteral,
} from 'typeorm';
import { FindOptionsWhere, LessThan, MoreThan, ObjectLiteral } from 'typeorm';
export const applyRangeFilter = (
where: FindOptionsWhere<ObjectLiteral>,
order: Record<string, FindOptionsOrderValue> | undefined,
cursor: Record<string, any>,
isForwardPagination = true,
): FindOptionsWhere<ObjectLiteral> => {
if (!order) return where;
const orderEntries = Object.entries(order);
orderEntries.forEach(([column, order], index) => {
if (typeof order !== 'object' || !('direction' in order)) {
Object.entries(cursor ?? {}).forEach(([key, value]) => {
if (key === 'id') {
return;
}
where[column] =
order.direction === 'ASC'
? MoreThan(cursor[index])
: LessThan(cursor[index]);
where[key] = isForwardPagination ? MoreThan(value) : LessThan(value);
});
return where;

View File

@ -7,7 +7,11 @@ import {
GraphqlQueryRunnerExceptionCode,
} from 'src/engine/api/graphql/graphql-query-runner/errors/graphql-query-runner.exception';
export const decodeCursor = (cursor: string): Record<string, any> => {
export interface CursorData {
[key: string]: any;
}
export const decodeCursor = (cursor: string): CursorData => {
try {
return JSON.parse(Buffer.from(cursor, 'base64').toString());
} catch (err) {
@ -22,13 +26,16 @@ export const encodeCursor = <ObjectRecord extends IRecord = IRecord>(
objectRecord: ObjectRecord,
order: Record<string, FindOptionsOrderValue> | undefined,
): string => {
const cursor = {};
const orderByValues: Record<string, any> = {};
Object.keys(order ?? []).forEach((key) => {
cursor[key] = objectRecord[key];
Object.keys(order ?? {}).forEach((key) => {
orderByValues[key] = objectRecord[key];
});
cursor['id'] = objectRecord.id;
const cursorData: CursorData = {
...orderByValues,
id: objectRecord.id,
};
return Buffer.from(JSON.stringify(Object.values(cursor))).toString('base64');
return Buffer.from(JSON.stringify(cursorData)).toString('base64');
};