diff --git a/api/src/database/migrations/20210225A-add-relations-sort-field.ts b/api/src/database/migrations/20210225A-add-relations-sort-field.ts new file mode 100644 index 0000000000..53fd1e8cb9 --- /dev/null +++ b/api/src/database/migrations/20210225A-add-relations-sort-field.ts @@ -0,0 +1,33 @@ +import Knex from 'knex'; + +export async function up(knex: Knex) { + await knex.schema.alterTable('directus_relations', (table) => { + table.string('sort_field'); + }); + + const fieldsWithSort = await knex + .select('collection', 'field', 'options') + .from('directus_fields') + .whereIn('interface', ['one-to-many', 'm2a-builder', 'many-to-many']); + + for (const field of fieldsWithSort) { + const options = typeof field.options === 'string' ? JSON.parse(field.options) : field.options ?? {}; + + if ('sortField' in options) { + await knex('directus_relations') + .update({ + sort_field: options.sortField, + }) + .where({ + one_collection: field.collection, + one_field: field.field, + }); + } + } +} + +export async function down(knex: Knex) { + await knex.schema.alterTable('directus_relations', (table) => { + table.dropColumn('sort_field'); + }); +} diff --git a/api/src/database/system-data/relations/relations.yaml b/api/src/database/system-data/relations/relations.yaml index 70810149a0..ae9cbac0a6 100644 --- a/api/src/database/system-data/relations/relations.yaml +++ b/api/src/database/system-data/relations/relations.yaml @@ -8,6 +8,7 @@ defaults: one_field: null one_primary: null junction_field: null + sort_field: null data: - many_collection: directus_users diff --git a/api/src/types/relation.ts b/api/src/types/relation.ts index b02d42af73..f180fcbd98 100644 --- a/api/src/types/relation.ts +++ b/api/src/types/relation.ts @@ -11,4 +11,7 @@ export type Relation = { one_collection_field: string | null; one_allowed_collections: string | null; + + junction_field: string | null; + sort_field: string | null; }; diff --git a/app/src/interfaces/files/files.vue b/app/src/interfaces/files/files.vue index 95388a8581..5e05b191b0 100644 --- a/app/src/interfaces/files/files.vue +++ b/app/src/interfaces/files/files.vue @@ -12,8 +12,8 @@ :disabled="disabled" @update:items="sortItems($event)" @click:row="editItem" - :show-manual-sort="sortField !== null" - :manual-sort-key="sortField" + :show-manual-sort="relationInfo.sortField !== null" + :manual-sort-key="relationInfo.sortField" >