mirror of
https://github.com/directus/directus.git
synced 2026-01-29 18:48:04 -05:00
Support *.* notation
This commit is contained in:
@@ -1,5 +1,15 @@
|
||||
import database, { schemaInspector } from '../database';
|
||||
import { Field } from '../types/field';
|
||||
import { uniq } from 'lodash';
|
||||
|
||||
export const fieldsInCollection = async (collection: string) => {
|
||||
const [fields, columns] = await Promise.all([
|
||||
database.select('field').from('directus_fields').where({ collection }),
|
||||
schemaInspector.columns(collection),
|
||||
]);
|
||||
|
||||
return uniq([...fields.map(({ field }) => field), ...columns.map(({ column }) => column)]);
|
||||
};
|
||||
|
||||
export const readAll = async (collection?: string) => {
|
||||
const fieldsQuery = database.select('*').from('directus_fields');
|
||||
|
||||
@@ -6,6 +6,7 @@ import { Query } from '../types/query';
|
||||
import { Relation } from '../types/relation';
|
||||
import { AST, NestedCollectionAST, FieldAST } from '../types/ast';
|
||||
import database from '../database';
|
||||
import * as FieldsService from '../services/fields';
|
||||
|
||||
export default async function getAST(collection: string, query: Query): Promise<AST> {
|
||||
const ast: AST = {
|
||||
@@ -40,8 +41,6 @@ export default async function getAST(collection: string, query: Query): Promise<
|
||||
|
||||
ast.children = await parseFields(collection, query.fields);
|
||||
|
||||
console.log(JSON.stringify(ast, null, 2));
|
||||
|
||||
return ast;
|
||||
|
||||
async function parseFields(parentCollection: string, fields: string[]) {
|
||||
@@ -49,6 +48,27 @@ export default async function getAST(collection: string, query: Query): Promise<
|
||||
|
||||
const relationalStructure: Record<string, string[]> = {};
|
||||
|
||||
// Swap *.* case for *,<relational-field>.*
|
||||
for (let i = 0; i < fields.length; i++) {
|
||||
const fieldKey = fields[i];
|
||||
|
||||
if (fieldKey.includes('.') === false) continue;
|
||||
|
||||
const parts = fieldKey.split('.');
|
||||
|
||||
if (parts[0] === '*') {
|
||||
const availableFields = await FieldsService.fieldsInCollection(parentCollection);
|
||||
fields.splice(
|
||||
i,
|
||||
1,
|
||||
...availableFields
|
||||
.filter((field) => !!getRelation(parentCollection, field))
|
||||
.map((field) => `${field}.${parts.slice(1).join('.')}`)
|
||||
);
|
||||
fields.push('*');
|
||||
}
|
||||
}
|
||||
|
||||
for (const field of fields) {
|
||||
if (field.includes('.') === false) {
|
||||
children.push({ type: 'field', name: field });
|
||||
|
||||
Reference in New Issue
Block a user