✨ Sort files chronologically + infinite scroll (#535)
This commit is contained in:
+16
-2
@@ -264,12 +264,22 @@ export class MongoConnector extends AbstractConnector<MongoConnectionOptions> {
|
||||
options,
|
||||
);
|
||||
|
||||
const sort: any = {};
|
||||
let sort: any = {};
|
||||
for (const key of entityDefinition.options.primaryKey.slice(1)) {
|
||||
const defaultOrder =
|
||||
(columnsDefinition[key as string].options.order || "ASC") === "ASC" ? 1 : -1;
|
||||
sort[key as string] = (options?.pagination?.reversed ? -1 : 1) * defaultOrder;
|
||||
}
|
||||
if (options?.sort) {
|
||||
sort = options.sort
|
||||
? Object.fromEntries(
|
||||
Object.entries(options.sort).map(([field, direction]) => [
|
||||
field,
|
||||
direction === "asc" ? 1 : -1,
|
||||
]),
|
||||
)
|
||||
: {};
|
||||
}
|
||||
|
||||
logger.debug(`services.database.orm.mongodb.find - Query: ${JSON.stringify(query)}`);
|
||||
|
||||
@@ -277,7 +287,11 @@ export class MongoConnector extends AbstractConnector<MongoConnectionOptions> {
|
||||
.find(query)
|
||||
.sort(sort)
|
||||
.skip(Math.max(0, parseInt(options.pagination.page_token || "0")))
|
||||
.limit(Math.max(0, parseInt(options.pagination.limitStr || "100")));
|
||||
.limit(Math.max(0, parseInt(options.pagination.limitStr || "100")))
|
||||
.collation({
|
||||
locale: "en_US",
|
||||
numericOrdering: true,
|
||||
});
|
||||
|
||||
const entities: Table[] = [];
|
||||
while (await cursor.hasNext()) {
|
||||
|
||||
+8
-1
@@ -88,13 +88,20 @@ export class PostgresQueryBuilder {
|
||||
if (whereClause && whereClause.endsWith("AND ")) whereClause = whereClause.slice(0, -4);
|
||||
|
||||
// ==== ORDER BY =====
|
||||
const orderByClause = `${entityDefinition.options.primaryKey
|
||||
let orderByClause = `${entityDefinition.options.primaryKey
|
||||
.slice(1)
|
||||
.map(
|
||||
(key: string) =>
|
||||
`${key} ${(columnsDefinition[key].options.order || "ASC") === "ASC" ? "DESC" : "ASC"}`,
|
||||
)}`;
|
||||
|
||||
// ==== ORDER BY CUSTOM COLUMN =====
|
||||
if (options?.sort) {
|
||||
orderByClause = Object.keys(options.sort)
|
||||
.map(key => `${key} ${options.sort[key].toUpperCase()}`)
|
||||
.join(", ");
|
||||
}
|
||||
|
||||
// ==== PAGING =====
|
||||
let limit = 100;
|
||||
let offset = 0;
|
||||
|
||||
+7
@@ -22,6 +22,12 @@ export type inType = [string, Array<any>];
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
type likeType = [string, any];
|
||||
|
||||
type SortDirection = "asc" | "desc";
|
||||
|
||||
type SortOption = {
|
||||
[field: string]: SortDirection;
|
||||
};
|
||||
|
||||
export type FindOptions = {
|
||||
pagination?: Pagination;
|
||||
$lt?: comparisonType[];
|
||||
@@ -34,6 +40,7 @@ export type FindOptions = {
|
||||
$in?: inType[];
|
||||
$nin?: inType[];
|
||||
$like?: likeType[];
|
||||
sort?: SortOption;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user