@directus/api 12.0.0 → 12.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  import { createInspector } from '@directus/schema';
2
2
  import { addFieldFlag } from '@directus/utils';
3
- import { chunk, omit } from 'lodash-es';
3
+ import { chunk, groupBy, merge, omit } from 'lodash-es';
4
4
  import { clearSystemCache, getCache } from '../cache.js';
5
5
  import { ALIAS_TYPES } from '../constants.js';
6
6
  import { getHelpers } from '../database/helpers/index.js';
@@ -108,7 +108,22 @@ export class CollectionsService {
108
108
  schema: this.schema,
109
109
  });
110
110
  const fieldPayloads = payload.fields.filter((field) => field.meta).map((field) => field.meta);
111
- await fieldItemsService.createMany(fieldPayloads, {
111
+ // Sort new fields that does not have any group defined, in ascending order.
112
+ // Lodash merge is used so that the "sort" can be overridden if defined.
113
+ let sortedFieldPayloads = fieldPayloads
114
+ .filter((field) => field?.group === undefined || field?.group === null)
115
+ .map((field, index) => merge({ sort: index + 1 }, field));
116
+ // Sort remaining new fields with group defined, if any, in ascending order.
117
+ // sortedFieldPayloads will be less than fieldPayloads if it filtered out any fields with group defined.
118
+ if (sortedFieldPayloads.length < fieldPayloads.length) {
119
+ const fieldsWithGroups = groupBy(fieldPayloads.filter((field) => field?.group), (field) => field?.group);
120
+ // The sort order is restarted from 1 for fields in each group and appended to sortedFieldPayloads.
121
+ // Lodash merge is used so that the "sort" can be overridden if defined.
122
+ for (const [_group, fields] of Object.entries(fieldsWithGroups)) {
123
+ sortedFieldPayloads = sortedFieldPayloads.concat(fields.map((field, index) => merge({ sort: index + 1 }, field)));
124
+ }
125
+ }
126
+ await fieldItemsService.createMany(sortedFieldPayloads, {
112
127
  bypassEmitAction: (params) => opts?.bypassEmitAction ? opts.bypassEmitAction(params) : nestedActionEvents.push(params),
113
128
  bypassLimits: true,
114
129
  });
@@ -1,7 +1,7 @@
1
1
  import { KNEX_TYPES, REGEX_BETWEEN_PARENS } from '@directus/constants';
2
2
  import { createInspector } from '@directus/schema';
3
3
  import { addFieldFlag, toArray } from '@directus/utils';
4
- import { isEqual, isNil } from 'lodash-es';
4
+ import { isEqual, isNil, merge } from 'lodash-es';
5
5
  import { clearSystemCache, getCache } from '../cache.js';
6
6
  import { ALIAS_TYPES } from '../constants.js';
7
7
  import { translateDatabaseError } from '../database/errors/translate.js';
@@ -241,8 +241,14 @@ export class FieldsService {
241
241
  }
242
242
  }
243
243
  if (hookAdjustedField.meta) {
244
+ const existingSortRecord = await trx
245
+ .from('directus_fields')
246
+ .where(hookAdjustedField.meta?.group ? { collection, group: hookAdjustedField.meta.group } : { collection })
247
+ .max('sort', { as: 'max' })
248
+ .first();
249
+ const newSortValue = existingSortRecord?.max ? existingSortRecord.max + 1 : 1;
244
250
  await itemsService.createOne({
245
- ...hookAdjustedField.meta,
251
+ ...merge({ sort: newSortValue }, hookAdjustedField.meta),
246
252
  collection: collection,
247
253
  field: hookAdjustedField.field,
248
254
  }, { emitEvents: false });
@@ -10,7 +10,6 @@ export class MetaService {
10
10
  this.accountability = options.accountability || null;
11
11
  this.schema = options.schema;
12
12
  }
13
- // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
14
13
  async getMetaForQuery(collection, query) {
15
14
  if (!query || !query.meta)
16
15
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@directus/api",
3
- "version": "12.0.0",
3
+ "version": "12.0.1",
4
4
  "description": "Directus is a real-time API and App dashboard for managing SQL database content",
5
5
  "keywords": [
6
6
  "directus",
@@ -143,7 +143,7 @@
143
143
  "ws": "8.12.1",
144
144
  "zod": "3.21.4",
145
145
  "zod-validation-error": "1.0.1",
146
- "@directus/app": "10.4.0",
146
+ "@directus/app": "10.4.1",
147
147
  "@directus/constants": "10.2.1",
148
148
  "@directus/errors": "0.0.1",
149
149
  "@directus/extensions-sdk": "10.1.5",