Compare commits

...

1 Commits

Author SHA1 Message Date
Dan Ribbens
c02e4df4bc chore: wip sort with 2023-08-29 08:31:59 -04:00
2 changed files with 49 additions and 8 deletions

View File

@@ -1,5 +1,7 @@
import { ArrayField, Block, Field } from 'payload/types';
import { asc, DBQueryConfig, desc } from 'drizzle-orm';
import { FieldAffectingData, fieldAffectsData } from 'payload/dist/fields/config/types';
import flattenFields from 'payload/dist/utilities/flattenTopLevelFields';
import { traverseFields } from './traverseFields';
import { PostgresAdapter } from '../types';
@@ -26,6 +28,30 @@ export const buildFindManyArgs = ({
with: {},
};
// sorting
let sortField: FieldAffectingData;
let sortDirection: typeof asc | typeof desc;
let sortFieldName;
if (sort && typeof sort === 'string' && sort.length > 0) {
// asc or desc types
if (sort[0] === '-') {
sortDirection = desc;
sortFieldName = sort.substring(1);
} else {
sortDirection = asc;
sortFieldName = sort;
}
sortField = flattenFields(fields).find((field) => (fieldAffectsData(field) && field.name === sortFieldName)) as FieldAffectingData;
if (sortField) {
if (sortField.localized) {
// error: column pages.slug does not exist
// result.orderBy = sortDirection(adapter.tables[`${tableName}_locales`][sortField.name]);
} else {
result.orderBy = sortDirection(adapter.tables[tableName][sortField.name]);
}
}
}
const _locales: Result = {
columns: {
id: false,
@@ -33,6 +59,11 @@ export const buildFindManyArgs = ({
},
};
if (sortField && sortField.localized) {
// TODO: this is not sorting the end results
_locales.orderBy = (table) => [sortDirection(table[sortField.name])];
}
if (adapter.tables[`${tableName}_relationships`]) {
result.with._relationships = {
orderBy: ({ order }, { asc: ASC }) => [ASC(order)],
@@ -47,14 +78,6 @@ export const buildFindManyArgs = ({
result.with._locales = _locales;
}
if (sort) {
if (sort[0] === '-') {
result.orderBy = desc(adapter.tables[tableName][sort.substring(1)]);
} else {
result.orderBy = asc(adapter.tables[tableName][sort]);
}
}
const locatedBlocks: Block[] = [];
const locatedArrays: { [path: string]: ArrayField } = {};

View File

@@ -343,6 +343,24 @@ describe('Postgres', () => {
expect(people[0].fullName).toEqual('Elliot DeNolf');
expect(people[1].fullName).toEqual('Dan Ribbens');
});
it('sort asc localized', async () => {
const { docs: pages } = await payload.find({
collection: 'pages',
sort: 'slug',
});
expect(pages[0].slug).toEqual('first');
expect(pages[1].slug).toEqual('second');
});
it('sort desc localized', async () => {
const { docs: pages } = await payload.find({
collection: 'pages',
sort: '-slug',
});
expect(pages[0].slug).toEqual('second');
expect(pages[1].slug).toEqual('first');
});
});
describe('localized arrays', () => {