Add ordering option to populate

This commit is contained in:
Alexandre Bodin 2022-09-19 21:25:39 +02:00 committed by Pierre Noël
parent 7c4b7e02c8
commit 15cb9900b4
6 changed files with 15 additions and 39 deletions

View File

@ -4,7 +4,6 @@ const sqlite = {
filename: '.tmp/data.db',
},
useNullAsDefault: true,
debug: true,
};
const postgres = {

View File

@ -2,23 +2,8 @@ const { createCoreController } = require('@strapi/strapi').factories;
module.exports = createCoreController('api::address.address', {
async find(ctx) {
// const { results } = await strapi.service('api::address.address').find();
const { results } = await strapi.service('api::address.address').find();
const r = await strapi.db.query('api::address.address').load(
{
id: 1,
},
'categories',
{
limit: 2,
orderBy: 'asc|desc',
}
);
console.log(r);
ctx.body = r;
// ctx.body = await this.sanitizeOutput(results);
ctx.body = await this.sanitizeOutput(results);
},
});

View File

@ -1063,10 +1063,6 @@ const createEntityManager = (db) => {
return { ...entity, ...entry };
},
async LoadPage() {
},
// TODO: add lifecycle events
async load(uid, entity, fields, params) {
const { attributes } = db.metadata.get(uid);

View File

@ -33,12 +33,7 @@ const processOrderBy = (orderBy, ctx) => {
const attribute = attributes[key];
if (!attribute) {
// throw new Error(`Attribute ${key} not found on model ${uid}`);
return {
column: key,
order: direction,
};
throw new Error(`Attribute ${key} not found on model ${uid}`);
}
if (types.isScalar(attribute.type)) {

View File

@ -5,7 +5,7 @@ const _ = require('lodash/fp');
const { fromRow } = require('../transform');
/**
* Populate X to One relation
* Populate oneToOne and manyToOne relation
* @param {*} input
* @param {*} ctx
* @returns
@ -193,7 +193,7 @@ const oneToMany = async (input, ctx) => {
rootColumn: joinTable.inverseJoinColumn.referencedColumn,
rootTable: qb.alias,
on: joinTable.on,
orderBy: joinTable.orderBy,
orderBy: _.mapValues((v) => populateValue.ordering || v, joinTable.orderBy),
})
.addSelect(joinColAlias)
.where({ [joinColAlias]: referencedValues })
@ -276,7 +276,7 @@ const manyToMany = async (input, ctx) => {
rootColumn: joinTable.inverseJoinColumn.referencedColumn,
rootTable: populateQb.alias,
on: joinTable.on,
orderBy: joinTable.orderBy,
orderBy: _.mapValues((v) => populateValue.ordering || v, joinTable.orderBy),
})
.addSelect(joinColAlias)
.where({ [joinColAlias]: referencedValues })
@ -367,7 +367,7 @@ const morphX = async (input, ctx) => {
...(joinTable.on || {}),
field: attributeName,
},
orderBy: joinTable.orderBy,
orderBy: _.mapValues((v) => populateValue.ordering || v, joinTable.orderBy),
})
.addSelect([`${alias}.${idColumn.name}`, `${alias}.${typeColumn.name}`])
.where({
@ -552,6 +552,7 @@ const pickPopulateParams = _.pick([
'limit',
'offset',
'filters',
'ordering',
]);
const applyPopulate = async (results, populate, ctx) => {

View File

@ -326,14 +326,14 @@ const createQueryBuilder = (uid, db, initialState = {}) => {
processSelect() {
state.select = state.select.map((field) => helpers.toColumnName(meta, field));
// if (this.shouldUseDistinct()) {
// const joinsOrderByColumns = state.joins.flatMap((join) => {
// return _.keys(join.orderBy).map((key) => this.aliasColumn(key, join.alias));
// });
// const orderByColumns = state.orderBy.map(({ column }) => column);
if (this.shouldUseDistinct()) {
const joinsOrderByColumns = state.joins.flatMap((join) => {
return _.keys(join.orderBy).map((key) => this.aliasColumn(key, join.alias));
});
const orderByColumns = state.orderBy.map(({ column }) => column);
// state.select = _.uniq([...joinsOrderByColumns, ...orderByColumns, ...state.select]);
// }
state.select = _.uniq([...joinsOrderByColumns, ...orderByColumns, ...state.select]);
}
},
getKnexQuery() {