@directus/api 22.1.1 → 22.2.0

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.
Files changed (72) hide show
  1. package/dist/app.js +1 -1
  2. package/dist/cache.d.ts +2 -2
  3. package/dist/cache.js +2 -2
  4. package/dist/constants.d.ts +1 -0
  5. package/dist/constants.js +1 -0
  6. package/dist/database/helpers/index.d.ts +2 -0
  7. package/dist/database/helpers/index.js +2 -0
  8. package/dist/database/helpers/nullable-update/dialects/default.d.ts +3 -0
  9. package/dist/database/helpers/nullable-update/dialects/default.js +3 -0
  10. package/dist/database/helpers/nullable-update/dialects/oracle.d.ts +12 -0
  11. package/dist/database/helpers/nullable-update/dialects/oracle.js +16 -0
  12. package/dist/database/helpers/nullable-update/index.d.ts +7 -0
  13. package/dist/database/helpers/nullable-update/index.js +7 -0
  14. package/dist/database/helpers/nullable-update/types.d.ts +7 -0
  15. package/dist/database/helpers/nullable-update/types.js +12 -0
  16. package/dist/database/helpers/schema/dialects/cockroachdb.d.ts +3 -1
  17. package/dist/database/helpers/schema/dialects/cockroachdb.js +17 -0
  18. package/dist/database/helpers/schema/dialects/mssql.d.ts +2 -1
  19. package/dist/database/helpers/schema/dialects/mssql.js +20 -0
  20. package/dist/database/helpers/schema/dialects/mysql.d.ts +2 -1
  21. package/dist/database/helpers/schema/dialects/mysql.js +33 -0
  22. package/dist/database/helpers/schema/dialects/oracle.d.ts +3 -1
  23. package/dist/database/helpers/schema/dialects/oracle.js +21 -0
  24. package/dist/database/helpers/schema/dialects/postgres.d.ts +3 -1
  25. package/dist/database/helpers/schema/dialects/postgres.js +23 -0
  26. package/dist/database/helpers/schema/dialects/sqlite.d.ts +1 -0
  27. package/dist/database/helpers/schema/dialects/sqlite.js +3 -0
  28. package/dist/database/helpers/schema/types.d.ts +5 -0
  29. package/dist/database/helpers/schema/types.js +3 -0
  30. package/dist/database/helpers/schema/utils/preprocess-bindings.d.ts +5 -1
  31. package/dist/database/helpers/schema/utils/preprocess-bindings.js +23 -17
  32. package/dist/database/migrations/20240817A-update-icon-fields-length.d.ts +3 -0
  33. package/dist/database/migrations/20240817A-update-icon-fields-length.js +55 -0
  34. package/dist/database/run-ast/lib/get-db-query.js +14 -8
  35. package/dist/extensions/manager.js +2 -2
  36. package/dist/logger/index.d.ts +6 -0
  37. package/dist/logger/index.js +79 -28
  38. package/dist/logger/logs-stream.d.ts +11 -0
  39. package/dist/logger/logs-stream.js +41 -0
  40. package/dist/middleware/respond.js +1 -0
  41. package/dist/request/is-denied-ip.js +7 -1
  42. package/dist/server.js +4 -2
  43. package/dist/services/fields.js +58 -20
  44. package/dist/services/mail/index.js +1 -5
  45. package/dist/services/notifications.d.ts +0 -4
  46. package/dist/services/notifications.js +8 -6
  47. package/dist/services/server.js +8 -1
  48. package/dist/services/specifications.js +7 -7
  49. package/dist/services/users.js +4 -1
  50. package/dist/utils/get-address.d.ts +1 -1
  51. package/dist/utils/get-address.js +6 -1
  52. package/dist/utils/get-allowed-log-levels.d.ts +3 -0
  53. package/dist/utils/get-allowed-log-levels.js +11 -0
  54. package/dist/utils/get-schema.js +19 -24
  55. package/dist/utils/parse-filter-key.js +1 -5
  56. package/dist/utils/sanitize-schema.d.ts +1 -1
  57. package/dist/websocket/controllers/base.d.ts +10 -10
  58. package/dist/websocket/controllers/base.js +22 -3
  59. package/dist/websocket/controllers/graphql.js +3 -1
  60. package/dist/websocket/controllers/index.d.ts +4 -0
  61. package/dist/websocket/controllers/index.js +12 -0
  62. package/dist/websocket/controllers/logs.d.ts +18 -0
  63. package/dist/websocket/controllers/logs.js +50 -0
  64. package/dist/websocket/controllers/rest.js +3 -1
  65. package/dist/websocket/handlers/index.d.ts +1 -0
  66. package/dist/websocket/handlers/index.js +21 -3
  67. package/dist/websocket/handlers/logs.d.ts +31 -0
  68. package/dist/websocket/handlers/logs.js +121 -0
  69. package/dist/websocket/messages.d.ts +26 -0
  70. package/dist/websocket/messages.js +9 -0
  71. package/dist/websocket/types.d.ts +7 -0
  72. package/package.json +25 -25
package/dist/app.js CHANGED
@@ -125,7 +125,7 @@ export default async function createApp() {
125
125
  'https://avatars.githubusercontent.com',
126
126
  ],
127
127
  mediaSrc: ["'self'"],
128
- connectSrc: ["'self'", 'https://*'],
128
+ connectSrc: ["'self'", 'https://*', 'wss://*'],
129
129
  },
130
130
  }, getConfigFromEnv('CONTENT_SECURITY_POLICY_'))));
131
131
  if (env['HSTS_ENABLED']) {
package/dist/cache.d.ts CHANGED
@@ -13,7 +13,7 @@ export declare function clearSystemCache(opts?: {
13
13
  }): Promise<void>;
14
14
  export declare function setSystemCache(key: string, value: any, ttl?: number): Promise<void>;
15
15
  export declare function getSystemCache(key: string): Promise<Record<string, any>>;
16
- export declare function setSchemaCache(schema: SchemaOverview): Promise<void>;
17
- export declare function getSchemaCache(): Promise<SchemaOverview | undefined>;
16
+ export declare function setLocalSchemaCache(schema: SchemaOverview): Promise<void>;
17
+ export declare function getLocalSchemaCache(): Promise<SchemaOverview | undefined>;
18
18
  export declare function setCacheValue(cache: Keyv, key: string, value: Record<string, any> | Record<string, any>[], ttl?: number): Promise<void>;
19
19
  export declare function getCacheValue(cache: Keyv, key: string): Promise<any>;
package/dist/cache.js CHANGED
@@ -75,11 +75,11 @@ export async function getSystemCache(key) {
75
75
  const { systemCache } = getCache();
76
76
  return await getCacheValue(systemCache, key);
77
77
  }
78
- export async function setSchemaCache(schema) {
78
+ export async function setLocalSchemaCache(schema) {
79
79
  const { localSchemaCache } = getCache();
80
80
  await localSchemaCache.set('schema', schema);
81
81
  }
82
- export async function getSchemaCache() {
82
+ export async function getLocalSchemaCache() {
83
83
  const { localSchemaCache } = getCache();
84
84
  return await localSchemaCache.get('schema');
85
85
  }
@@ -23,3 +23,4 @@ export declare const RESUMABLE_UPLOADS: {
23
23
  EXPIRATION_TIME: number;
24
24
  SCHEDULE: string;
25
25
  };
26
+ export declare const ALLOWED_DB_DEFAULT_FUNCTIONS: string[];
package/dist/constants.js CHANGED
@@ -87,3 +87,4 @@ export const RESUMABLE_UPLOADS = {
87
87
  EXPIRATION_TIME: getMilliseconds(env['TUS_UPLOAD_EXPIRATION'], 600_000 /* 10min */),
88
88
  SCHEDULE: String(env['TUS_CLEANUP_SCHEDULE']),
89
89
  };
90
+ export const ALLOWED_DB_DEFAULT_FUNCTIONS = ['gen_random_uuid()'];
@@ -6,12 +6,14 @@ import * as geometryHelpers from './geometry/index.js';
6
6
  import * as schemaHelpers from './schema/index.js';
7
7
  import * as sequenceHelpers from './sequence/index.js';
8
8
  import * as numberHelpers from './number/index.js';
9
+ import * as nullableUpdateHelper from './nullable-update/index.js';
9
10
  export declare function getHelpers(database: Knex): {
10
11
  date: dateHelpers.postgres | dateHelpers.oracle | dateHelpers.mysql | dateHelpers.mssql | dateHelpers.sqlite;
11
12
  st: geometryHelpers.postgres | geometryHelpers.mssql | geometryHelpers.mysql | geometryHelpers.sqlite | geometryHelpers.oracle | geometryHelpers.redshift;
12
13
  schema: schemaHelpers.cockroachdb | schemaHelpers.mssql | schemaHelpers.mysql | schemaHelpers.postgres | schemaHelpers.sqlite | schemaHelpers.oracle | schemaHelpers.redshift;
13
14
  sequence: sequenceHelpers.mysql | sequenceHelpers.postgres;
14
15
  number: numberHelpers.cockroachdb | numberHelpers.mssql | numberHelpers.postgres | numberHelpers.sqlite | numberHelpers.oracle;
16
+ nullableUpdate: nullableUpdateHelper.postgres | nullableUpdateHelper.oracle;
15
17
  };
16
18
  export declare function getFunctions(database: Knex, schema: SchemaOverview): fnHelpers.postgres | fnHelpers.mssql | fnHelpers.mysql | fnHelpers.sqlite | fnHelpers.oracle;
17
19
  export type Helpers = ReturnType<typeof getHelpers>;
@@ -5,6 +5,7 @@ import * as geometryHelpers from './geometry/index.js';
5
5
  import * as schemaHelpers from './schema/index.js';
6
6
  import * as sequenceHelpers from './sequence/index.js';
7
7
  import * as numberHelpers from './number/index.js';
8
+ import * as nullableUpdateHelper from './nullable-update/index.js';
8
9
  export function getHelpers(database) {
9
10
  const client = getDatabaseClient(database);
10
11
  return {
@@ -13,6 +14,7 @@ export function getHelpers(database) {
13
14
  schema: new schemaHelpers[client](database),
14
15
  sequence: new sequenceHelpers[client](database),
15
16
  number: new numberHelpers[client](database),
17
+ nullableUpdate: new nullableUpdateHelper[client](database),
16
18
  };
17
19
  }
18
20
  export function getFunctions(database, schema) {
@@ -0,0 +1,3 @@
1
+ import { NullableFieldUpdateHelper } from '../types.js';
2
+ export declare class NullableFieldUpdateHelperDefault extends NullableFieldUpdateHelper {
3
+ }
@@ -0,0 +1,3 @@
1
+ import { NullableFieldUpdateHelper } from '../types.js';
2
+ export class NullableFieldUpdateHelperDefault extends NullableFieldUpdateHelper {
3
+ }
@@ -0,0 +1,12 @@
1
+ import type { Column } from '@directus/schema';
2
+ import type { Field, RawField } from '@directus/types';
3
+ import type { Knex } from 'knex';
4
+ import { NullableFieldUpdateHelper } from '../types.js';
5
+ /**
6
+ * Oracle throws an error when overwriting the nullable option with same value.
7
+ * Therefore we need to check if the nullable option has changed and only then apply it.
8
+ * The default value can be set regardless of the previous value.
9
+ */
10
+ export declare class NullableFieldUpdateHelperOracle extends NullableFieldUpdateHelper {
11
+ updateNullableValue(column: Knex.ColumnBuilder, field: RawField | Field, existing: Column): void;
12
+ }
@@ -0,0 +1,16 @@
1
+ import { NullableFieldUpdateHelper } from '../types.js';
2
+ /**
3
+ * Oracle throws an error when overwriting the nullable option with same value.
4
+ * Therefore we need to check if the nullable option has changed and only then apply it.
5
+ * The default value can be set regardless of the previous value.
6
+ */
7
+ export class NullableFieldUpdateHelperOracle extends NullableFieldUpdateHelper {
8
+ updateNullableValue(column, field, existing) {
9
+ if (field.schema?.is_nullable === false && existing.is_nullable === true) {
10
+ column.notNullable();
11
+ }
12
+ else if (field.schema?.is_nullable === true && existing.is_nullable === false) {
13
+ column.nullable();
14
+ }
15
+ }
16
+ }
@@ -0,0 +1,7 @@
1
+ export { NullableFieldUpdateHelperOracle as oracle } from './dialects/oracle.js';
2
+ export { NullableFieldUpdateHelperDefault as postgres } from './dialects/default.js';
3
+ export { NullableFieldUpdateHelperDefault as mysql } from './dialects/default.js';
4
+ export { NullableFieldUpdateHelperDefault as cockroachdb } from './dialects/default.js';
5
+ export { NullableFieldUpdateHelperDefault as redshift } from './dialects/default.js';
6
+ export { NullableFieldUpdateHelperDefault as sqlite } from './dialects/default.js';
7
+ export { NullableFieldUpdateHelperDefault as mssql } from './dialects/default.js';
@@ -0,0 +1,7 @@
1
+ export { NullableFieldUpdateHelperOracle as oracle } from './dialects/oracle.js';
2
+ export { NullableFieldUpdateHelperDefault as postgres } from './dialects/default.js';
3
+ export { NullableFieldUpdateHelperDefault as mysql } from './dialects/default.js';
4
+ export { NullableFieldUpdateHelperDefault as cockroachdb } from './dialects/default.js';
5
+ export { NullableFieldUpdateHelperDefault as redshift } from './dialects/default.js';
6
+ export { NullableFieldUpdateHelperDefault as sqlite } from './dialects/default.js';
7
+ export { NullableFieldUpdateHelperDefault as mssql } from './dialects/default.js';
@@ -0,0 +1,7 @@
1
+ import type { Knex } from 'knex';
2
+ import { DatabaseHelper } from '../types.js';
3
+ import type { Column } from '@directus/schema';
4
+ import type { Field, RawField } from '@directus/types';
5
+ export declare class NullableFieldUpdateHelper extends DatabaseHelper {
6
+ updateNullableValue(column: Knex.ColumnBuilder, field: RawField | Field, existing: Column): void;
7
+ }
@@ -0,0 +1,12 @@
1
+ import { DatabaseHelper } from '../types.js';
2
+ export class NullableFieldUpdateHelper extends DatabaseHelper {
3
+ updateNullableValue(column, field, existing) {
4
+ const isNullable = field.schema?.is_nullable ?? existing?.is_nullable ?? true;
5
+ if (isNullable) {
6
+ column.nullable();
7
+ }
8
+ else {
9
+ column.notNullable();
10
+ }
11
+ }
12
+ }
@@ -1,9 +1,11 @@
1
1
  import type { KNEX_TYPES } from '@directus/constants';
2
- import type { Options, Sql } from '../types.js';
2
+ import { type Knex } from 'knex';
3
+ import type { Options, SortRecord, Sql } from '../types.js';
3
4
  import { SchemaHelper } from '../types.js';
4
5
  export declare class SchemaHelperCockroachDb extends SchemaHelper {
5
6
  changeToType(table: string, column: string, type: (typeof KNEX_TYPES)[number], options?: Options): Promise<void>;
6
7
  constraintName(existingName: string): string;
7
8
  getDatabaseSize(): Promise<number | null>;
8
9
  preprocessBindings(queryParams: Sql): Sql;
10
+ addInnerSortFieldsToGroupBy(groupByFields: (string | Knex.Raw)[], sortRecords: SortRecord[], hasMultiRelationalSort: boolean): void;
9
11
  }
@@ -1,3 +1,4 @@
1
+ import {} from 'knex';
1
2
  import { SchemaHelper } from '../types.js';
2
3
  import { useEnv } from '@directus/env';
3
4
  import { preprocessBindings } from '../utils/preprocess-bindings.js';
@@ -31,4 +32,20 @@ export class SchemaHelperCockroachDb extends SchemaHelper {
31
32
  preprocessBindings(queryParams) {
32
33
  return preprocessBindings(queryParams, { format: (index) => `$${index + 1}` });
33
34
  }
35
+ addInnerSortFieldsToGroupBy(groupByFields, sortRecords, hasMultiRelationalSort) {
36
+ if (hasMultiRelationalSort) {
37
+ /*
38
+ Cockroach allows aliases to be used in the GROUP BY clause and only needs columns in the GROUP BY clause that
39
+ are not functionally dependent on the primary key.
40
+
41
+ > You can group columns by an alias (i.e., a label assigned to the column with an AS clause) rather than the column name.
42
+
43
+ > If aggregate groups are created on a full primary key, any column in the table can be selected as a target_elem,
44
+ or specified in a HAVING clause.
45
+
46
+ https://www.cockroachlabs.com/docs/stable/select-clause#parameters
47
+ */
48
+ groupByFields.push(...sortRecords.map(({ alias }) => alias));
49
+ }
50
+ }
34
51
  }
@@ -1,9 +1,10 @@
1
1
  import type { Knex } from 'knex';
2
- import { SchemaHelper, type Sql } from '../types.js';
2
+ import { SchemaHelper, type SortRecord, type Sql } from '../types.js';
3
3
  export declare class SchemaHelperMSSQL extends SchemaHelper {
4
4
  applyLimit(rootQuery: Knex.QueryBuilder, limit: number): void;
5
5
  applyOffset(rootQuery: Knex.QueryBuilder, offset: number): void;
6
6
  formatUUID(uuid: string): string;
7
7
  getDatabaseSize(): Promise<number | null>;
8
8
  preprocessBindings(queryParams: Sql): Sql;
9
+ addInnerSortFieldsToGroupBy(groupByFields: (string | Knex.Raw)[], sortRecords: SortRecord[], _hasMultiRelationalSort: boolean): void;
9
10
  }
@@ -30,4 +30,24 @@ export class SchemaHelperMSSQL extends SchemaHelper {
30
30
  preprocessBindings(queryParams) {
31
31
  return preprocessBindings(queryParams, { format: (index) => `@p${index}` });
32
32
  }
33
+ addInnerSortFieldsToGroupBy(groupByFields, sortRecords, _hasMultiRelationalSort) {
34
+ /*
35
+ MSSQL requires all selected columns that are not aggregated over are to be present in the GROUP BY clause
36
+
37
+ > When the select list has no aggregations, each column in the select list must be included in the GROUP BY list.
38
+
39
+ https://learn.microsoft.com/en-us/sql/t-sql/queries/select-group-by-transact-sql?view=sql-server-ver16#g-syntax-variations-for-group-by
40
+
41
+ MSSQL does not support aliases in the GROUP BY clause
42
+
43
+ > The column expression cannot contain:
44
+ A column alias that is defined in the SELECT list. It can use a column alias for a derived table that is defined
45
+ in the FROM clause.
46
+
47
+ https://learn.microsoft.com/en-us/sql/t-sql/queries/select-group-by-transact-sql?view=sql-server-ver16
48
+ */
49
+ if (sortRecords.length > 0) {
50
+ groupByFields.push(...sortRecords.map(({ column }) => column));
51
+ }
52
+ }
33
53
  }
@@ -1,6 +1,7 @@
1
1
  import type { Knex } from 'knex';
2
- import { SchemaHelper } from '../types.js';
2
+ import { SchemaHelper, type SortRecord } from '../types.js';
3
3
  export declare class SchemaHelperMySQL extends SchemaHelper {
4
4
  applyMultiRelationalSort(knex: Knex, dbQuery: Knex.QueryBuilder, table: string, primaryKey: string, orderByString: string, orderByFields: Knex.Raw[]): Knex.QueryBuilder;
5
5
  getDatabaseSize(): Promise<number | null>;
6
+ addInnerSortFieldsToGroupBy(groupByFields: (string | Knex.Raw)[], sortRecords: SortRecord[], hasMultiRelationalSort: boolean): void;
6
7
  }
@@ -28,4 +28,37 @@ export class SchemaHelperMySQL extends SchemaHelper {
28
28
  return null;
29
29
  }
30
30
  }
31
+ addInnerSortFieldsToGroupBy(groupByFields, sortRecords, hasMultiRelationalSort) {
32
+ if (hasMultiRelationalSort) {
33
+ /*
34
+ ** MySQL **
35
+
36
+ MySQL only requires all selected sort columns that are not functionally dependent on the primary key to be included.
37
+
38
+ > If the ONLY_FULL_GROUP_BY SQL mode is enabled (which it is by default),
39
+ MySQL rejects queries for which the select list, HAVING condition, or ORDER BY list refer to
40
+ nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on them.
41
+
42
+ https://dev.mysql.com/doc/refman/8.4/en/group-by-handling.html
43
+
44
+ MySQL allows aliases to be used in the GROUP BY clause
45
+
46
+ > You can use the alias in GROUP BY, ORDER BY, or HAVING clauses to refer to the column:
47
+
48
+ https://dev.mysql.com/doc/refman/8.4/en/problems-with-alias.html
49
+
50
+ ** MariaDB **
51
+
52
+ MariaDB does not document how it supports functional dependent columns in GROUP BY clauses.
53
+ But testing shows that it does support the same features as MySQL in this area.
54
+
55
+ MariaDB allows aliases to be used in the GROUP BY clause
56
+
57
+ > The GROUP BY expression can be a computed value, and can refer back to an identifer specified with AS.
58
+
59
+ https://mariadb.com/kb/en/group-by/#group-by-examples
60
+ */
61
+ groupByFields.push(...sortRecords.map(({ alias }) => alias));
62
+ }
63
+ }
31
64
  }
@@ -1,6 +1,7 @@
1
1
  import type { KNEX_TYPES } from '@directus/constants';
2
2
  import type { Field, Relation, Type } from '@directus/types';
3
- import type { Options, Sql } from '../types.js';
3
+ import type { Knex } from 'knex';
4
+ import type { Options, SortRecord, Sql } from '../types.js';
4
5
  import { SchemaHelper } from '../types.js';
5
6
  export declare class SchemaHelperOracle extends SchemaHelper {
6
7
  changeToType(table: string, column: string, type: (typeof KNEX_TYPES)[number], options?: Options): Promise<void>;
@@ -9,4 +10,5 @@ export declare class SchemaHelperOracle extends SchemaHelper {
9
10
  processFieldType(field: Field): Type;
10
11
  getDatabaseSize(): Promise<number | null>;
11
12
  preprocessBindings(queryParams: Sql): Sql;
13
+ addInnerSortFieldsToGroupBy(groupByFields: (string | Knex.Raw)[], sortRecords: SortRecord[], _hasMultiRelationalSort: boolean): void;
12
14
  }
@@ -42,4 +42,25 @@ export class SchemaHelperOracle extends SchemaHelper {
42
42
  preprocessBindings(queryParams) {
43
43
  return preprocessBindings(queryParams, { format: (index) => `:${index + 1}` });
44
44
  }
45
+ addInnerSortFieldsToGroupBy(groupByFields, sortRecords, _hasMultiRelationalSort) {
46
+ /*
47
+ Oracle requires all selected columns that are not aggregated over to be present in the GROUP BY clause
48
+ aliases can not be used before version 23c.
49
+
50
+ > If you also specify a group_by_clause in this statement, then this select list can contain only the following
51
+ types of expressions:
52
+ * Constants
53
+ * Aggregate functions and the functions USER, UID, and SYSDATE
54
+ * Expressions identical to those in the group_by_clause. If the group_by_clause is in a subquery,
55
+ then all columns in the select list of the subquery must match the GROUP BY columns in the subquery.
56
+ If the select list and GROUP BY columns of a top-level query or of a subquery do not match,
57
+ then the statement results in ORA-00979.
58
+ * Expressions involving the preceding expressions that evaluate to the same value for all rows in a group
59
+
60
+ https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/SELECT.html
61
+ */
62
+ if (sortRecords.length > 0) {
63
+ groupByFields.push(...sortRecords.map(({ column }) => column));
64
+ }
65
+ }
45
66
  }
@@ -1,5 +1,7 @@
1
- import { SchemaHelper, type Sql } from '../types.js';
1
+ import type { Knex } from 'knex';
2
+ import { SchemaHelper, type SortRecord, type Sql } from '../types.js';
2
3
  export declare class SchemaHelperPostgres extends SchemaHelper {
3
4
  getDatabaseSize(): Promise<number | null>;
4
5
  preprocessBindings(queryParams: Sql): Sql;
6
+ addInnerSortFieldsToGroupBy(groupByFields: (string | Knex.Raw)[], sortRecords: SortRecord[], hasMultiRelationalSort: boolean): void;
5
7
  }
@@ -15,4 +15,27 @@ export class SchemaHelperPostgres extends SchemaHelper {
15
15
  preprocessBindings(queryParams) {
16
16
  return preprocessBindings(queryParams, { format: (index) => `$${index + 1}` });
17
17
  }
18
+ addInnerSortFieldsToGroupBy(groupByFields, sortRecords, hasMultiRelationalSort) {
19
+ if (hasMultiRelationalSort) {
20
+ /*
21
+ Postgres only requires selected columns that are not functionally dependent on the primary key to be
22
+ included in the GROUP BY clause. Since the results are already grouped by the primary key, we don't need to
23
+ always include the sort columns in the GROUP BY but only if there is a multi relational sort involved, eg.
24
+ a sort column that comes from a related M2O relation.
25
+
26
+ > When GROUP BY is present, or any aggregate functions are present, it is not valid for the SELECT list
27
+ expressions to refer to ungrouped columns except within aggregate functions or when the ungrouped column is
28
+ functionally dependent on the grouped columns, since there would otherwise be more than one possible value to
29
+ return for an ungrouped column.
30
+ https://www.postgresql.org/docs/current/sql-select.html
31
+
32
+ Postgres allows aliases to be used in the GROUP BY clause
33
+
34
+ > In strict SQL, GROUP BY can only group by columns of the source table but PostgreSQL extends this to also allow
35
+ GROUP BY to group by columns in the select list.
36
+ https://www.postgresql.org/docs/16/queries-table-expressions.html#QUERIES-GROUP
37
+ */
38
+ groupByFields.push(...sortRecords.map(({ alias }) => alias));
39
+ }
40
+ }
18
41
  }
@@ -3,4 +3,5 @@ export declare class SchemaHelperSQLite extends SchemaHelper {
3
3
  preColumnChange(): Promise<boolean>;
4
4
  postColumnChange(): Promise<void>;
5
5
  getDatabaseSize(): Promise<number | null>;
6
+ addInnerSortFieldsToGroupBy(): void;
6
7
  }
@@ -19,4 +19,7 @@ export class SchemaHelperSQLite extends SchemaHelper {
19
19
  return null;
20
20
  }
21
21
  }
22
+ addInnerSortFieldsToGroupBy() {
23
+ // SQLite does not need any special handling for inner query sort columns
24
+ }
22
25
  }
@@ -12,6 +12,10 @@ export type Sql = {
12
12
  sql: string;
13
13
  bindings: readonly Knex.Value[];
14
14
  };
15
+ export type SortRecord = {
16
+ alias: string;
17
+ column: Knex.Raw;
18
+ };
15
19
  export declare abstract class SchemaHelper extends DatabaseHelper {
16
20
  isOneOfClients(clients: DatabaseClient[]): boolean;
17
21
  changeNullable(table: string, column: string, nullable: boolean): Promise<void>;
@@ -32,4 +36,5 @@ export declare abstract class SchemaHelper extends DatabaseHelper {
32
36
  */
33
37
  getDatabaseSize(): Promise<number | null>;
34
38
  preprocessBindings(queryParams: Sql): Sql;
39
+ addInnerSortFieldsToGroupBy(_groupByFields: (string | Knex.Raw)[], _sortRecords: SortRecord[], _hasMultiRelationalSort: boolean): void;
35
40
  }
@@ -97,4 +97,7 @@ export class SchemaHelper extends DatabaseHelper {
97
97
  preprocessBindings(queryParams) {
98
98
  return queryParams;
99
99
  }
100
+ addInnerSortFieldsToGroupBy(_groupByFields, _sortRecords, _hasMultiRelationalSort) {
101
+ // no-op by default
102
+ }
100
103
  }
@@ -1,8 +1,12 @@
1
+ import type { Knex } from 'knex';
1
2
  import type { Sql } from '../types.js';
2
3
  export type PreprocessBindingsOptions = {
3
4
  format(index: number): string;
4
5
  };
6
+ /**
7
+ * Preprocess a SQL query, such that repeated binding values are bound to the same binding index.
8
+ **/
5
9
  export declare function preprocessBindings(queryParams: (Partial<Sql> & Pick<Sql, 'sql'>) | string, options: PreprocessBindingsOptions): {
6
10
  sql: string;
7
- bindings: import("knex").Knex.Value[];
11
+ bindings: Knex.Value[];
8
12
  };
@@ -1,30 +1,36 @@
1
1
  import { isString } from 'lodash-es';
2
+ /**
3
+ * Preprocess a SQL query, such that repeated binding values are bound to the same binding index.
4
+ **/
2
5
  export function preprocessBindings(queryParams, options) {
3
6
  const query = { bindings: [], ...(isString(queryParams) ? { sql: queryParams } : queryParams) };
4
- const bindingIndices = new Array(query.bindings.length);
5
- for (let i = 0; i < query.bindings.length; i++) {
6
- const binding = query.bindings[i];
7
- const prevIndex = query.bindings.findIndex((b, j) => j < i && b === binding);
8
- if (prevIndex !== -1) {
9
- bindingIndices[i] = prevIndex;
10
- }
11
- else {
12
- bindingIndices[i] = i;
13
- }
14
- }
7
+ // bindingIndices[i] is the index of the first occurrence of query.bindings[i]
8
+ const bindingIndices = new Map();
9
+ // The new, deduplicated bindings
10
+ const bindings = [];
15
11
  let matchIndex = 0;
16
- let currentBindingIndex = 0;
17
- const sql = query.sql.replace(/(\\*)(\?)/g, function (_, escapes) {
12
+ let nextBindingIndex = 0;
13
+ const sql = query.sql.replace(/(\\*)(\?)/g, (_, escapes) => {
18
14
  if (escapes.length % 2) {
19
15
  // Return an escaped question mark, so it stays escaped
20
16
  return `${'\\'.repeat(escapes.length)}?`;
21
17
  }
18
+ const binding = query.bindings[matchIndex];
19
+ let bindingIndex;
20
+ if (bindingIndices.has(binding)) {
21
+ // This index belongs to a binding that has been encountered before.
22
+ bindingIndex = bindingIndices.get(binding);
23
+ }
22
24
  else {
23
- const bindingIndex = bindingIndices[matchIndex] === matchIndex ? currentBindingIndex++ : bindingIndices[matchIndex];
24
- matchIndex++;
25
- return options.format(bindingIndex);
25
+ // The first time the value is encountered, set the index lookup to the current index
26
+ // Use the nextBindingIndex to get the next unused binding index that is used in the new, deduplicated bindings
27
+ bindingIndex = nextBindingIndex++;
28
+ bindingIndices.set(binding, bindingIndex);
29
+ bindings.push(binding);
26
30
  }
31
+ // Increment the loop counter
32
+ matchIndex++;
33
+ return options.format(bindingIndex);
27
34
  });
28
- const bindings = query.bindings.filter((_, i) => bindingIndices[i] === i);
29
35
  return { ...query, sql, bindings };
30
36
  }
@@ -0,0 +1,3 @@
1
+ import type { Knex } from 'knex';
2
+ export declare function up(knex: Knex): Promise<void>;
3
+ export declare function down(knex: Knex): Promise<void>;
@@ -0,0 +1,55 @@
1
+ import { getHelpers } from '../helpers/index.js';
2
+ export async function up(knex) {
3
+ const helper = getHelpers(knex).schema;
4
+ await helper.changeToType('directus_collections', 'icon', 'string', {
5
+ length: 64,
6
+ });
7
+ await helper.changeToType('directus_dashboards', 'icon', 'string', {
8
+ nullable: false,
9
+ default: 'dashboard',
10
+ length: 64,
11
+ });
12
+ await helper.changeToType('directus_flows', 'icon', 'string', {
13
+ length: 64,
14
+ });
15
+ await helper.changeToType('directus_panels', 'icon', 'string', {
16
+ default: null,
17
+ length: 64,
18
+ });
19
+ await helper.changeToType('directus_presets', 'icon', 'string', {
20
+ default: 'bookmark',
21
+ length: 64,
22
+ });
23
+ await helper.changeToType('directus_roles', 'icon', 'string', {
24
+ nullable: false,
25
+ default: 'supervised_user_circle',
26
+ length: 64,
27
+ });
28
+ }
29
+ export async function down(knex) {
30
+ const helper = getHelpers(knex).schema;
31
+ await helper.changeToType('directus_collections', 'icon', 'string', {
32
+ length: 30,
33
+ });
34
+ await helper.changeToType('directus_dashboards', 'icon', 'string', {
35
+ nullable: false,
36
+ default: 'dashboard',
37
+ length: 30,
38
+ });
39
+ await helper.changeToType('directus_flows', 'icon', 'string', {
40
+ length: 30,
41
+ });
42
+ await helper.changeToType('directus_panels', 'icon', 'string', {
43
+ default: null,
44
+ length: 30,
45
+ });
46
+ await helper.changeToType('directus_presets', 'icon', 'string', {
47
+ default: 'bookmark',
48
+ length: 30,
49
+ });
50
+ await helper.changeToType('directus_roles', 'icon', 'string', {
51
+ nullable: false,
52
+ default: 'supervised_user_circle',
53
+ length: 30,
54
+ });
55
+ }
@@ -86,19 +86,21 @@ export function getDBQuery(schema, knex, table, fieldNodes, o2mNodes, query, cas
86
86
  orderByString += ', ';
87
87
  }
88
88
  const sortAlias = `sort_${generateAlias()}`;
89
+ let orderByColumn;
89
90
  if (sortRecord.column.includes('.')) {
90
91
  const [alias, field] = sortRecord.column.split('.');
91
92
  const originalCollectionName = getCollectionFromAlias(alias, aliasMap);
92
93
  dbQuery.select(getColumn(knex, alias, field, sortAlias, schema, { originalCollectionName }));
93
94
  orderByString += `?? ${sortRecord.order}`;
94
- orderByFields.push(getColumn(knex, alias, field, false, schema, { originalCollectionName }));
95
+ orderByColumn = getColumn(knex, alias, field, false, schema, { originalCollectionName });
95
96
  }
96
97
  else {
97
98
  dbQuery.select(getColumn(knex, table, sortRecord.column, sortAlias, schema));
98
99
  orderByString += `?? ${sortRecord.order}`;
99
- orderByFields.push(getColumn(knex, table, sortRecord.column, false, schema));
100
+ orderByColumn = getColumn(knex, table, sortRecord.column, false, schema);
100
101
  }
101
- innerQuerySortRecords.push({ alias: sortAlias, order: sortRecord.order });
102
+ orderByFields.push(orderByColumn);
103
+ innerQuerySortRecords.push({ alias: sortAlias, order: sortRecord.order, column: orderByColumn });
102
104
  });
103
105
  if (hasMultiRelationalSort) {
104
106
  dbQuery = helpers.schema.applyMultiRelationalSort(knex, dbQuery, table, primaryKey, orderByString, orderByFields);
@@ -171,11 +173,15 @@ export function getDBQuery(schema, knex, table, fieldNodes, o2mNodes, query, cas
171
173
  // based on the case/when of that field.
172
174
  dbQuery.select(o2mNodes.map(innerPreprocess).filter((x) => x !== null));
173
175
  const groupByFields = [knex.raw('??.??', [table, primaryKey])];
174
- if (hasMultiRelationalSort) {
175
- // Sort fields that are not directly in the table the primary key is from need to be included in the group
176
- // by clause, otherwise this causes problems on some DBs
177
- groupByFields.push(...innerQuerySortRecords.map(({ alias }) => knex.raw('??', alias)));
178
- }
176
+ // For some DB vendors sort fields need to be included in the group by clause, otherwise this causes problems those DBs
177
+ // since sort fields are selected in the inner query, and they expect all selected columns to be in
178
+ // the group by clause or aggregated over.
179
+ // For some DBs the field needs to be the actual raw column expression, since aliases are not available in the
180
+ // group by clause.
181
+ // Since the fields are expected to be the same for a single primary key it is safe to include them in the
182
+ // group by without influencing the result.
183
+ // This inclusion depends on the DB vendor, as such it is handled in a dialect specific helper.
184
+ helpers.schema.addInnerSortFieldsToGroupBy(groupByFields, innerQuerySortRecords, hasMultiRelationalSort ?? false);
179
185
  dbQuery.groupBy(groupByFields);
180
186
  }
181
187
  const wrapperQuery = knex
@@ -156,7 +156,7 @@ export class ExtensionManager {
156
156
  }
157
157
  }
158
158
  if (this.options.watch && !wasWatcherInitialized) {
159
- this.updateWatchedExtensions(Array.from(this.localExtensions.values()));
159
+ this.updateWatchedExtensions([...this.extensions]);
160
160
  }
161
161
  this.messenger.subscribe(this.reloadChannel, (payload) => {
162
162
  // Ignore requests for reloading that were published by the current process
@@ -327,7 +327,7 @@ export class ExtensionManager {
327
327
  const extensionDir = path.resolve(getExtensionsPath());
328
328
  const registryDir = path.join(extensionDir, '.registry');
329
329
  const toPackageExtensionPaths = (extensions) => extensions
330
- .filter((extension) => extension.local && extension.path.startsWith(extensionDir) && !extension.path.startsWith(registryDir))
330
+ .filter((extension) => extension.local && !extension.path.startsWith(registryDir))
331
331
  .flatMap((extension) => isTypeIn(extension, HYBRID_EXTENSION_TYPES) || extension.type === 'bundle'
332
332
  ? [
333
333
  path.resolve(extension.path, extension.entrypoint.app),
@@ -1,9 +1,15 @@
1
1
  /// <reference types="qs" />
2
2
  import type { RequestHandler } from 'express';
3
3
  import { type Logger } from 'pino';
4
+ import { LogsStream } from './logs-stream.js';
4
5
  export declare const _cache: {
5
6
  logger: Logger<never> | undefined;
7
+ logsStream: LogsStream | undefined;
8
+ httpLogsStream: LogsStream | undefined;
6
9
  };
7
10
  export declare const useLogger: () => Logger<never>;
11
+ export declare const getLogsStream: (pretty: boolean) => LogsStream;
12
+ export declare const getHttpLogsStream: (pretty: boolean) => LogsStream;
13
+ export declare const getLoggerLevelValue: (level: string) => number;
8
14
  export declare const createLogger: () => Logger<never>;
9
15
  export declare const createExpressLogger: () => RequestHandler<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>;