@directus/api 21.0.0-rc.0 → 21.0.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.
- package/dist/app.js +5 -5
- package/dist/auth/drivers/ldap.js +4 -4
- package/dist/auth/drivers/local.js +4 -4
- package/dist/auth/drivers/oauth2.js +4 -4
- package/dist/auth/drivers/openid.js +4 -2
- package/dist/cache.d.ts +0 -1
- package/dist/cache.js +7 -25
- package/dist/cli/commands/bootstrap/index.js +2 -8
- package/dist/cli/commands/init/index.js +10 -9
- package/dist/cli/utils/defaults.d.ts +11 -4
- package/dist/cli/utils/defaults.js +1 -7
- package/dist/constants.d.ts +1 -1
- package/dist/controllers/auth.js +16 -5
- package/dist/controllers/permissions.js +2 -14
- package/dist/controllers/roles.js +1 -22
- package/dist/controllers/tus.js +27 -13
- package/dist/controllers/users.js +55 -0
- package/dist/database/helpers/fn/types.d.ts +1 -2
- package/dist/database/helpers/fn/types.js +1 -1
- package/dist/database/helpers/geometry/dialects/mssql.d.ts +1 -1
- package/dist/database/helpers/geometry/dialects/mssql.js +2 -4
- package/dist/database/helpers/geometry/dialects/mysql.js +1 -1
- package/dist/database/helpers/geometry/dialects/oracle.d.ts +1 -1
- package/dist/database/helpers/geometry/dialects/oracle.js +3 -5
- package/dist/database/helpers/geometry/types.d.ts +1 -1
- package/dist/database/helpers/geometry/types.js +2 -4
- package/dist/database/index.js +11 -8
- package/dist/database/migrations/20240305A-change-useragent-type.js +1 -1
- package/dist/database/migrations/20240716A-update-files-date-fields.js +33 -0
- package/dist/database/{run-ast/types.d.ts → run-ast.d.ts} +9 -3
- package/dist/database/run-ast.js +458 -0
- package/dist/flows.js +4 -3
- package/dist/logger/index.js +1 -1
- package/dist/middleware/authenticate.js +7 -2
- package/dist/middleware/cache.js +1 -1
- package/dist/middleware/check-ip.d.ts +2 -0
- package/dist/middleware/check-ip.js +37 -0
- package/dist/middleware/error-handler.d.ts +2 -2
- package/dist/middleware/error-handler.js +54 -51
- package/dist/middleware/get-permissions.d.ts +3 -0
- package/dist/middleware/get-permissions.js +10 -0
- package/dist/middleware/respond.js +1 -1
- package/dist/services/activity.js +10 -22
- package/dist/services/assets.d.ts +3 -2
- package/dist/services/assets.js +7 -15
- package/dist/services/authentication.js +18 -18
- package/dist/services/authorization.d.ts +17 -0
- package/dist/services/authorization.js +456 -0
- package/dist/services/collections.js +17 -18
- package/dist/services/fields.d.ts +4 -0
- package/dist/services/fields.js +53 -58
- package/dist/services/files/lib/get-sharp-instance.d.ts +2 -0
- package/dist/services/files/lib/get-sharp-instance.js +10 -0
- package/dist/services/files/utils/get-metadata.js +7 -6
- package/dist/services/files.js +8 -10
- package/dist/services/graphql/index.d.ts +3 -3
- package/dist/services/graphql/index.js +22 -126
- package/dist/services/graphql/subscription.js +4 -2
- package/dist/services/import-export.js +4 -18
- package/dist/services/index.d.ts +2 -3
- package/dist/services/index.js +2 -3
- package/dist/services/items.js +44 -115
- package/dist/services/mail/index.d.ts +1 -1
- package/dist/services/mail/index.js +9 -1
- package/dist/services/meta.js +23 -60
- package/dist/services/notifications.js +6 -14
- package/dist/services/payload.d.ts +10 -9
- package/dist/services/payload.js +3 -18
- package/dist/services/{permissions.d.ts → permissions/index.d.ts} +7 -5
- package/dist/services/{permissions.js → permissions/index.js} +54 -30
- package/dist/{permissions → services/permissions}/lib/with-app-minimal-permissions.d.ts +1 -1
- package/dist/services/permissions/lib/with-app-minimal-permissions.js +13 -0
- package/dist/services/relations.d.ts +9 -1
- package/dist/services/relations.js +56 -31
- package/dist/services/roles.d.ts +12 -4
- package/dist/services/roles.js +424 -57
- package/dist/services/shares.d.ts +2 -0
- package/dist/services/shares.js +8 -12
- package/dist/services/specifications.d.ts +2 -2
- package/dist/services/specifications.js +27 -39
- package/dist/services/tus/data-store.js +4 -5
- package/dist/services/tus/server.d.ts +1 -1
- package/dist/services/tus/server.js +9 -2
- package/dist/services/users.d.ts +5 -1
- package/dist/services/users.js +161 -78
- package/dist/services/utils.js +7 -11
- package/dist/services/versions.d.ts +2 -0
- package/dist/services/versions.js +10 -34
- package/dist/telemetry/lib/get-report.js +2 -2
- package/dist/telemetry/utils/check-increased-user-limits.d.ts +7 -0
- package/dist/telemetry/utils/check-increased-user-limits.js +25 -0
- package/dist/telemetry/utils/get-role-counts-by-roles.d.ts +6 -0
- package/dist/telemetry/utils/get-role-counts-by-roles.js +27 -0
- package/dist/telemetry/utils/get-role-counts-by-users.d.ts +11 -0
- package/dist/telemetry/utils/get-role-counts-by-users.js +34 -0
- package/dist/telemetry/utils/get-user-count.d.ts +8 -0
- package/dist/telemetry/utils/get-user-count.js +33 -0
- package/dist/telemetry/utils/get-user-counts-by-roles.d.ts +7 -0
- package/dist/telemetry/utils/get-user-counts-by-roles.js +35 -0
- package/dist/types/ast.d.ts +1 -43
- package/dist/types/items.d.ts +0 -11
- package/dist/utils/apply-query.d.ts +3 -4
- package/dist/utils/apply-query.js +16 -39
- package/dist/utils/get-accountability-for-role.js +25 -16
- package/dist/utils/get-accountability-for-token.js +16 -17
- package/dist/utils/get-ast-from-query.d.ts +13 -0
- package/dist/utils/get-ast-from-query.js +297 -0
- package/dist/utils/get-cache-key.d.ts +1 -1
- package/dist/utils/get-cache-key.js +1 -12
- package/dist/utils/get-column.d.ts +1 -2
- package/dist/utils/get-column.js +0 -1
- package/dist/utils/get-permissions.d.ts +2 -0
- package/dist/utils/get-permissions.js +150 -0
- package/dist/utils/get-schema.js +3 -3
- package/dist/utils/get-service.js +1 -5
- package/dist/utils/merge-permissions-for-share.d.ts +4 -0
- package/dist/utils/merge-permissions-for-share.js +109 -0
- package/dist/utils/merge-permissions.d.ts +3 -0
- package/dist/utils/merge-permissions.js +95 -0
- package/dist/utils/reduce-schema.d.ts +6 -4
- package/dist/utils/reduce-schema.js +32 -16
- package/dist/websocket/authenticate.d.ts +2 -0
- package/dist/websocket/authenticate.js +12 -0
- package/dist/websocket/controllers/graphql.js +4 -1
- package/dist/websocket/controllers/hooks.js +0 -4
- package/dist/websocket/controllers/rest.js +2 -0
- package/dist/websocket/handlers/subscribe.js +2 -0
- package/dist/websocket/utils/items.d.ts +1 -1
- package/package.json +36 -37
- package/dist/controllers/access.d.ts +0 -2
- package/dist/controllers/access.js +0 -148
- package/dist/controllers/policies.d.ts +0 -2
- package/dist/controllers/policies.js +0 -169
- package/dist/database/get-ast-from-query/get-ast-from-query.d.ts +0 -16
- package/dist/database/get-ast-from-query/get-ast-from-query.js +0 -82
- package/dist/database/get-ast-from-query/lib/convert-wildcards.d.ts +0 -13
- package/dist/database/get-ast-from-query/lib/convert-wildcards.js +0 -69
- package/dist/database/get-ast-from-query/lib/parse-fields.d.ts +0 -15
- package/dist/database/get-ast-from-query/lib/parse-fields.js +0 -190
- package/dist/database/get-ast-from-query/utils/get-deep-query.d.ts +0 -14
- package/dist/database/get-ast-from-query/utils/get-deep-query.js +0 -17
- package/dist/database/get-ast-from-query/utils/get-related-collection.d.ts +0 -2
- package/dist/database/get-ast-from-query/utils/get-related-collection.js +0 -13
- package/dist/database/get-ast-from-query/utils/get-relation.d.ts +0 -2
- package/dist/database/get-ast-from-query/utils/get-relation.js +0 -7
- package/dist/database/migrations/20240710A-permissions-policies.js +0 -169
- package/dist/database/run-ast/lib/get-db-query.d.ts +0 -4
- package/dist/database/run-ast/lib/get-db-query.js +0 -208
- package/dist/database/run-ast/lib/parse-current-level.d.ts +0 -7
- package/dist/database/run-ast/lib/parse-current-level.js +0 -41
- package/dist/database/run-ast/run-ast.d.ts +0 -7
- package/dist/database/run-ast/run-ast.js +0 -107
- package/dist/database/run-ast/types.js +0 -1
- package/dist/database/run-ast/utils/apply-case-when.d.ts +0 -16
- package/dist/database/run-ast/utils/apply-case-when.js +0 -26
- package/dist/database/run-ast/utils/apply-parent-filters.d.ts +0 -3
- package/dist/database/run-ast/utils/apply-parent-filters.js +0 -55
- package/dist/database/run-ast/utils/get-column-pre-processor.d.ts +0 -10
- package/dist/database/run-ast/utils/get-column-pre-processor.js +0 -57
- package/dist/database/run-ast/utils/get-field-alias.d.ts +0 -2
- package/dist/database/run-ast/utils/get-field-alias.js +0 -4
- package/dist/database/run-ast/utils/get-inner-query-column-pre-processor.d.ts +0 -5
- package/dist/database/run-ast/utils/get-inner-query-column-pre-processor.js +0 -23
- package/dist/database/run-ast/utils/merge-with-parent-items.d.ts +0 -3
- package/dist/database/run-ast/utils/merge-with-parent-items.js +0 -87
- package/dist/database/run-ast/utils/remove-temporary-fields.d.ts +0 -3
- package/dist/database/run-ast/utils/remove-temporary-fields.js +0 -73
- package/dist/permissions/cache.d.ts +0 -2
- package/dist/permissions/cache.js +0 -23
- package/dist/permissions/lib/fetch-permissions.d.ts +0 -10
- package/dist/permissions/lib/fetch-permissions.js +0 -55
- package/dist/permissions/lib/fetch-policies.d.ts +0 -7
- package/dist/permissions/lib/fetch-policies.js +0 -28
- package/dist/permissions/lib/fetch-roles-tree.d.ts +0 -3
- package/dist/permissions/lib/fetch-roles-tree.js +0 -28
- package/dist/permissions/lib/with-app-minimal-permissions.js +0 -10
- package/dist/permissions/modules/fetch-accountability-collection-access/fetch-accountability-collection-access.d.ts +0 -7
- package/dist/permissions/modules/fetch-accountability-collection-access/fetch-accountability-collection-access.js +0 -56
- package/dist/permissions/modules/fetch-accountability-policy-globals/fetch-accountability-policy-globals.d.ts +0 -3
- package/dist/permissions/modules/fetch-accountability-policy-globals/fetch-accountability-policy-globals.js +0 -16
- package/dist/permissions/modules/fetch-allowed-collections/fetch-allowed-collections.d.ts +0 -8
- package/dist/permissions/modules/fetch-allowed-collections/fetch-allowed-collections.js +0 -24
- package/dist/permissions/modules/fetch-allowed-field-map/fetch-allowed-field-map.d.ts +0 -9
- package/dist/permissions/modules/fetch-allowed-field-map/fetch-allowed-field-map.js +0 -31
- package/dist/permissions/modules/fetch-allowed-fields/fetch-allowed-fields.d.ts +0 -16
- package/dist/permissions/modules/fetch-allowed-fields/fetch-allowed-fields.js +0 -27
- package/dist/permissions/modules/fetch-global-access/fetch-global-access.d.ts +0 -10
- package/dist/permissions/modules/fetch-global-access/fetch-global-access.js +0 -23
- package/dist/permissions/modules/fetch-global-access/lib/fetch-global-access-for-roles.d.ts +0 -5
- package/dist/permissions/modules/fetch-global-access/lib/fetch-global-access-for-roles.js +0 -7
- package/dist/permissions/modules/fetch-global-access/lib/fetch-global-access-for-user.d.ts +0 -5
- package/dist/permissions/modules/fetch-global-access/lib/fetch-global-access-for-user.js +0 -10
- package/dist/permissions/modules/fetch-global-access/types.d.ts +0 -4
- package/dist/permissions/modules/fetch-global-access/types.js +0 -1
- package/dist/permissions/modules/fetch-global-access/utils/fetch-global-access-for-query.d.ts +0 -4
- package/dist/permissions/modules/fetch-global-access/utils/fetch-global-access-for-query.js +0 -27
- package/dist/permissions/modules/fetch-inconsistent-field-map/fetch-inconsistent-field-map.d.ts +0 -12
- package/dist/permissions/modules/fetch-inconsistent-field-map/fetch-inconsistent-field-map.js +0 -32
- package/dist/permissions/modules/fetch-policies-ip-access/fetch-policies-ip-access.d.ts +0 -4
- package/dist/permissions/modules/fetch-policies-ip-access/fetch-policies-ip-access.js +0 -29
- package/dist/permissions/modules/process-ast/lib/extract-fields-from-children.d.ts +0 -4
- package/dist/permissions/modules/process-ast/lib/extract-fields-from-children.js +0 -49
- package/dist/permissions/modules/process-ast/lib/extract-fields-from-query.d.ts +0 -3
- package/dist/permissions/modules/process-ast/lib/extract-fields-from-query.js +0 -56
- package/dist/permissions/modules/process-ast/lib/field-map-from-ast.d.ts +0 -4
- package/dist/permissions/modules/process-ast/lib/field-map-from-ast.js +0 -8
- package/dist/permissions/modules/process-ast/lib/inject-cases.d.ts +0 -9
- package/dist/permissions/modules/process-ast/lib/inject-cases.js +0 -93
- package/dist/permissions/modules/process-ast/process-ast.d.ts +0 -9
- package/dist/permissions/modules/process-ast/process-ast.js +0 -39
- package/dist/permissions/modules/process-ast/types.d.ts +0 -24
- package/dist/permissions/modules/process-ast/types.js +0 -1
- package/dist/permissions/modules/process-ast/utils/collections-in-field-map.d.ts +0 -2
- package/dist/permissions/modules/process-ast/utils/collections-in-field-map.js +0 -7
- package/dist/permissions/modules/process-ast/utils/dedupe-access.d.ts +0 -12
- package/dist/permissions/modules/process-ast/utils/dedupe-access.js +0 -30
- package/dist/permissions/modules/process-ast/utils/extract-paths-from-query.d.ts +0 -15
- package/dist/permissions/modules/process-ast/utils/extract-paths-from-query.js +0 -50
- package/dist/permissions/modules/process-ast/utils/find-related-collection.d.ts +0 -3
- package/dist/permissions/modules/process-ast/utils/find-related-collection.js +0 -9
- package/dist/permissions/modules/process-ast/utils/flatten-filter.d.ts +0 -3
- package/dist/permissions/modules/process-ast/utils/flatten-filter.js +0 -34
- package/dist/permissions/modules/process-ast/utils/format-a2o-key.d.ts +0 -1
- package/dist/permissions/modules/process-ast/utils/format-a2o-key.js +0 -3
- package/dist/permissions/modules/process-ast/utils/get-info-for-path.d.ts +0 -5
- package/dist/permissions/modules/process-ast/utils/get-info-for-path.js +0 -7
- package/dist/permissions/modules/process-ast/utils/has-item-permissions.d.ts +0 -2
- package/dist/permissions/modules/process-ast/utils/has-item-permissions.js +0 -3
- package/dist/permissions/modules/process-ast/utils/stringify-query-path.d.ts +0 -2
- package/dist/permissions/modules/process-ast/utils/stringify-query-path.js +0 -3
- package/dist/permissions/modules/process-ast/utils/validate-path/create-error.d.ts +0 -3
- package/dist/permissions/modules/process-ast/utils/validate-path/create-error.js +0 -16
- package/dist/permissions/modules/process-ast/utils/validate-path/validate-path-existence.d.ts +0 -2
- package/dist/permissions/modules/process-ast/utils/validate-path/validate-path-existence.js +0 -12
- package/dist/permissions/modules/process-ast/utils/validate-path/validate-path-permissions.d.ts +0 -2
- package/dist/permissions/modules/process-ast/utils/validate-path/validate-path-permissions.js +0 -28
- package/dist/permissions/modules/process-payload/lib/is-field-nullable.d.ts +0 -5
- package/dist/permissions/modules/process-payload/lib/is-field-nullable.js +0 -12
- package/dist/permissions/modules/process-payload/process-payload.d.ts +0 -13
- package/dist/permissions/modules/process-payload/process-payload.js +0 -77
- package/dist/permissions/modules/validate-access/lib/validate-collection-access.d.ts +0 -12
- package/dist/permissions/modules/validate-access/lib/validate-collection-access.js +0 -11
- package/dist/permissions/modules/validate-access/lib/validate-item-access.d.ts +0 -9
- package/dist/permissions/modules/validate-access/lib/validate-item-access.js +0 -33
- package/dist/permissions/modules/validate-access/validate-access.d.ts +0 -14
- package/dist/permissions/modules/validate-access/validate-access.js +0 -28
- package/dist/permissions/modules/validate-remaining-admin/validate-remaining-admin-count.d.ts +0 -1
- package/dist/permissions/modules/validate-remaining-admin/validate-remaining-admin-count.js +0 -8
- package/dist/permissions/modules/validate-remaining-admin/validate-remaining-admin-users.d.ts +0 -5
- package/dist/permissions/modules/validate-remaining-admin/validate-remaining-admin-users.js +0 -10
- package/dist/permissions/types.d.ts +0 -6
- package/dist/permissions/types.js +0 -1
- package/dist/permissions/utils/create-default-accountability.d.ts +0 -2
- package/dist/permissions/utils/create-default-accountability.js +0 -11
- package/dist/permissions/utils/extract-required-dynamic-variable-context.d.ts +0 -8
- package/dist/permissions/utils/extract-required-dynamic-variable-context.js +0 -27
- package/dist/permissions/utils/fetch-dynamic-variable-context.d.ts +0 -9
- package/dist/permissions/utils/fetch-dynamic-variable-context.js +0 -43
- package/dist/permissions/utils/filter-policies-by-ip.d.ts +0 -2
- package/dist/permissions/utils/filter-policies-by-ip.js +0 -15
- package/dist/permissions/utils/get-unaliased-field-key.d.ts +0 -5
- package/dist/permissions/utils/get-unaliased-field-key.js +0 -17
- package/dist/permissions/utils/process-permissions.d.ts +0 -7
- package/dist/permissions/utils/process-permissions.js +0 -9
- package/dist/permissions/utils/with-cache.d.ts +0 -10
- package/dist/permissions/utils/with-cache.js +0 -25
- package/dist/services/access.d.ts +0 -10
- package/dist/services/access.js +0 -43
- package/dist/services/policies.d.ts +0 -12
- package/dist/services/policies.js +0 -87
- package/dist/telemetry/utils/check-user-limits.d.ts +0 -5
- package/dist/telemetry/utils/check-user-limits.js +0 -19
- package/dist/utils/fetch-user-count/fetch-access-lookup.d.ts +0 -17
- package/dist/utils/fetch-user-count/fetch-access-lookup.js +0 -22
- package/dist/utils/fetch-user-count/fetch-access-roles.d.ts +0 -16
- package/dist/utils/fetch-user-count/fetch-access-roles.js +0 -37
- package/dist/utils/fetch-user-count/fetch-active-users.d.ts +0 -6
- package/dist/utils/fetch-user-count/fetch-active-users.js +0 -3
- package/dist/utils/fetch-user-count/fetch-user-count.d.ts +0 -12
- package/dist/utils/fetch-user-count/fetch-user-count.js +0 -57
- package/dist/utils/fetch-user-count/get-user-count-query.d.ts +0 -20
- package/dist/utils/fetch-user-count/get-user-count-query.js +0 -17
- package/dist/utils/validate-user-count-integrity.d.ts +0 -13
- package/dist/utils/validate-user-count-integrity.js +0 -29
- /package/dist/database/migrations/{20240710A-permissions-policies.d.ts → 20240716A-update-files-date-fields.d.ts} +0 -0
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
import { REGEX_BETWEEN_PARENS } from '@directus/constants';
|
|
2
|
-
import { isEmpty } from 'lodash-es';
|
|
3
|
-
import { fetchPermissions } from '../../../permissions/lib/fetch-permissions.js';
|
|
4
|
-
import { fetchPolicies } from '../../../permissions/lib/fetch-policies.js';
|
|
5
|
-
import { getRelationType } from '../../../utils/get-relation-type.js';
|
|
6
|
-
import { getDeepQuery } from '../utils/get-deep-query.js';
|
|
7
|
-
import { getRelatedCollection } from '../utils/get-related-collection.js';
|
|
8
|
-
import { getRelation } from '../utils/get-relation.js';
|
|
9
|
-
import { convertWildcards } from './convert-wildcards.js';
|
|
10
|
-
export async function parseFields(options, context) {
|
|
11
|
-
let { fields } = options;
|
|
12
|
-
if (!fields)
|
|
13
|
-
return [];
|
|
14
|
-
fields = await convertWildcards({
|
|
15
|
-
fields,
|
|
16
|
-
parentCollection: options.parentCollection,
|
|
17
|
-
query: options.query,
|
|
18
|
-
accountability: options.accountability,
|
|
19
|
-
}, context);
|
|
20
|
-
if (!fields || !Array.isArray(fields))
|
|
21
|
-
return [];
|
|
22
|
-
const children = [];
|
|
23
|
-
const policies = options.accountability && options.accountability.admin === false
|
|
24
|
-
? await fetchPolicies(options.accountability, context)
|
|
25
|
-
: null;
|
|
26
|
-
const relationalStructure = Object.create(null);
|
|
27
|
-
for (const fieldKey of fields) {
|
|
28
|
-
let name = fieldKey;
|
|
29
|
-
if (options.query.alias) {
|
|
30
|
-
// check for field alias (is one of the key)
|
|
31
|
-
if (name in options.query.alias) {
|
|
32
|
-
name = options.query.alias[fieldKey];
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
const isRelational = name.includes('.') ||
|
|
36
|
-
// We'll always treat top level o2m fields as a related item. This is an alias field, otherwise it won't return
|
|
37
|
-
// anything
|
|
38
|
-
!!context.schema.relations.find((relation) => relation.related_collection === options.parentCollection && relation.meta?.one_field === name);
|
|
39
|
-
if (isRelational) {
|
|
40
|
-
// field is relational
|
|
41
|
-
const parts = fieldKey.split('.');
|
|
42
|
-
let rootField = parts[0];
|
|
43
|
-
let collectionScope = null;
|
|
44
|
-
// a2o related collection scoped field selector `fields=sections.section_id:headings.title`
|
|
45
|
-
if (rootField.includes(':')) {
|
|
46
|
-
const [key, scope] = rootField.split(':');
|
|
47
|
-
rootField = key;
|
|
48
|
-
collectionScope = scope;
|
|
49
|
-
}
|
|
50
|
-
if (rootField in relationalStructure === false) {
|
|
51
|
-
if (collectionScope) {
|
|
52
|
-
relationalStructure[rootField] = { [collectionScope]: [] };
|
|
53
|
-
}
|
|
54
|
-
else {
|
|
55
|
-
relationalStructure[rootField] = [];
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
if (parts.length > 1) {
|
|
59
|
-
const childKey = parts.slice(1).join('.');
|
|
60
|
-
if (collectionScope) {
|
|
61
|
-
if (collectionScope in relationalStructure[rootField] === false) {
|
|
62
|
-
relationalStructure[rootField][collectionScope] = [];
|
|
63
|
-
}
|
|
64
|
-
relationalStructure[rootField][collectionScope].push(childKey);
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
relationalStructure[rootField].push(childKey);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
if (name.includes('(') && name.includes(')')) {
|
|
73
|
-
const columnName = name.match(REGEX_BETWEEN_PARENS)[1];
|
|
74
|
-
const foundField = context.schema.collections[options.parentCollection].fields[columnName];
|
|
75
|
-
if (foundField && foundField.type === 'alias') {
|
|
76
|
-
const foundRelation = context.schema.relations.find((relation) => relation.related_collection === options.parentCollection && relation.meta?.one_field === columnName);
|
|
77
|
-
if (foundRelation) {
|
|
78
|
-
children.push({
|
|
79
|
-
type: 'functionField',
|
|
80
|
-
name,
|
|
81
|
-
fieldKey,
|
|
82
|
-
query: {},
|
|
83
|
-
relatedCollection: foundRelation.collection,
|
|
84
|
-
whenCase: [],
|
|
85
|
-
cases: [],
|
|
86
|
-
});
|
|
87
|
-
continue;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
children.push({ type: 'field', name, fieldKey, whenCase: [] });
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
for (const [fieldKey, nestedFields] of Object.entries(relationalStructure)) {
|
|
95
|
-
let fieldName = fieldKey;
|
|
96
|
-
if (options.query.alias && fieldKey in options.query.alias) {
|
|
97
|
-
fieldName = options.query.alias[fieldKey];
|
|
98
|
-
}
|
|
99
|
-
const relatedCollection = getRelatedCollection(context.schema, options.parentCollection, fieldName);
|
|
100
|
-
const relation = getRelation(context.schema, options.parentCollection, fieldName);
|
|
101
|
-
if (!relation)
|
|
102
|
-
continue;
|
|
103
|
-
const relationType = getRelationType({
|
|
104
|
-
relation,
|
|
105
|
-
collection: options.parentCollection,
|
|
106
|
-
field: fieldName,
|
|
107
|
-
});
|
|
108
|
-
if (!relationType)
|
|
109
|
-
continue;
|
|
110
|
-
let child = null;
|
|
111
|
-
if (relationType === 'a2o') {
|
|
112
|
-
const allowedCollections = relation.meta.one_allowed_collections;
|
|
113
|
-
child = {
|
|
114
|
-
type: 'a2o',
|
|
115
|
-
names: allowedCollections,
|
|
116
|
-
children: {},
|
|
117
|
-
query: {},
|
|
118
|
-
relatedKey: {},
|
|
119
|
-
parentKey: context.schema.collections[options.parentCollection].primary,
|
|
120
|
-
fieldKey: fieldKey,
|
|
121
|
-
relation: relation,
|
|
122
|
-
cases: {},
|
|
123
|
-
whenCase: [],
|
|
124
|
-
};
|
|
125
|
-
for (const relatedCollection of allowedCollections) {
|
|
126
|
-
child.children[relatedCollection] = await parseFields({
|
|
127
|
-
parentCollection: relatedCollection,
|
|
128
|
-
fields: Array.isArray(nestedFields)
|
|
129
|
-
? nestedFields
|
|
130
|
-
: nestedFields[relatedCollection] || [],
|
|
131
|
-
query: options.query,
|
|
132
|
-
deep: options.deep?.[`${fieldKey}:${relatedCollection}`],
|
|
133
|
-
accountability: options.accountability,
|
|
134
|
-
}, context);
|
|
135
|
-
child.query[relatedCollection] = getDeepQuery(options.deep?.[`${fieldKey}:${relatedCollection}`] || {});
|
|
136
|
-
child.relatedKey[relatedCollection] = context.schema.collections[relatedCollection].primary;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
else if (relatedCollection) {
|
|
140
|
-
if (options.accountability && options.accountability.admin === false && policies) {
|
|
141
|
-
const permissions = await fetchPermissions({
|
|
142
|
-
action: 'read',
|
|
143
|
-
collections: [relatedCollection],
|
|
144
|
-
policies: policies,
|
|
145
|
-
accountability: options.accountability,
|
|
146
|
-
}, context);
|
|
147
|
-
// Skip related collection if no permissions
|
|
148
|
-
if (permissions.length === 0) {
|
|
149
|
-
continue;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
// update query alias for children parseFields
|
|
153
|
-
const deepAlias = getDeepQuery(options.deep?.[fieldKey] || {})?.['alias'];
|
|
154
|
-
if (!isEmpty(deepAlias))
|
|
155
|
-
options.query.alias = deepAlias;
|
|
156
|
-
child = {
|
|
157
|
-
type: relationType,
|
|
158
|
-
name: relatedCollection,
|
|
159
|
-
fieldKey: fieldKey,
|
|
160
|
-
parentKey: context.schema.collections[options.parentCollection].primary,
|
|
161
|
-
relatedKey: context.schema.collections[relatedCollection].primary,
|
|
162
|
-
relation: relation,
|
|
163
|
-
query: getDeepQuery(options.deep?.[fieldKey] || {}),
|
|
164
|
-
children: await parseFields({
|
|
165
|
-
parentCollection: relatedCollection,
|
|
166
|
-
fields: nestedFields,
|
|
167
|
-
query: options.query,
|
|
168
|
-
deep: options.deep?.[fieldKey] || {},
|
|
169
|
-
accountability: options.accountability,
|
|
170
|
-
}, context),
|
|
171
|
-
cases: [],
|
|
172
|
-
whenCase: [],
|
|
173
|
-
};
|
|
174
|
-
if (relationType === 'o2m' && !child.query.sort) {
|
|
175
|
-
child.query.sort = [relation.meta?.sort_field || context.schema.collections[relation.collection].primary];
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
if (child) {
|
|
179
|
-
children.push(child);
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
// Deduplicate any children fields that are included both as a regular field, and as a nested m2o field
|
|
183
|
-
const nestedCollectionNodes = children.filter((childNode) => childNode.type !== 'field');
|
|
184
|
-
return children.filter((childNode) => {
|
|
185
|
-
const existsAsNestedRelational = !!nestedCollectionNodes.find((nestedCollectionNode) => childNode.fieldKey === nestedCollectionNode.fieldKey);
|
|
186
|
-
if (childNode.type === 'field' && existsAsNestedRelational)
|
|
187
|
-
return false;
|
|
188
|
-
return true;
|
|
189
|
-
});
|
|
190
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Convert Deep query object to regular query object by ignoring all nested fields and returning the
|
|
3
|
-
* `_` prefixed fields as top level query fields
|
|
4
|
-
*
|
|
5
|
-
* @example
|
|
6
|
-
*
|
|
7
|
-
* ```js
|
|
8
|
-
* getDeepQuery({
|
|
9
|
-
* _sort: ['a']
|
|
10
|
-
* });
|
|
11
|
-
* // => { sort: ['a'] }
|
|
12
|
-
* ```
|
|
13
|
-
*/
|
|
14
|
-
export declare function getDeepQuery(query: Record<string, any>): Record<string, any>;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { mapKeys, omitBy } from 'lodash-es';
|
|
2
|
-
/**
|
|
3
|
-
* Convert Deep query object to regular query object by ignoring all nested fields and returning the
|
|
4
|
-
* `_` prefixed fields as top level query fields
|
|
5
|
-
*
|
|
6
|
-
* @example
|
|
7
|
-
*
|
|
8
|
-
* ```js
|
|
9
|
-
* getDeepQuery({
|
|
10
|
-
* _sort: ['a']
|
|
11
|
-
* });
|
|
12
|
-
* // => { sort: ['a'] }
|
|
13
|
-
* ```
|
|
14
|
-
*/
|
|
15
|
-
export function getDeepQuery(query) {
|
|
16
|
-
return mapKeys(omitBy(query, (_value, key) => key.startsWith('_') === false), (_value, key) => key.substring(1));
|
|
17
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { getRelation } from './get-relation.js';
|
|
2
|
-
export function getRelatedCollection(schema, collection, field) {
|
|
3
|
-
const relation = getRelation(schema, collection, field);
|
|
4
|
-
if (!relation)
|
|
5
|
-
return null;
|
|
6
|
-
if (relation.collection === collection && relation.field === field) {
|
|
7
|
-
return relation.related_collection || null;
|
|
8
|
-
}
|
|
9
|
-
if (relation.related_collection === collection && relation.meta?.one_field === field) {
|
|
10
|
-
return relation.collection || null;
|
|
11
|
-
}
|
|
12
|
-
return null;
|
|
13
|
-
}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export function getRelation(schema, collection, field) {
|
|
2
|
-
const relation = schema.relations.find((relation) => {
|
|
3
|
-
return ((relation.collection === collection && relation.field === field) ||
|
|
4
|
-
(relation.related_collection === collection && relation.meta?.one_field === field));
|
|
5
|
-
});
|
|
6
|
-
return relation;
|
|
7
|
-
}
|
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from 'node:crypto';
|
|
2
|
-
import { processChunk } from '@directus/utils';
|
|
3
|
-
/**
|
|
4
|
-
* The public role used to be `null`, we gotta create a single new policy for the permissions
|
|
5
|
-
* previously attached to the public role (marked through `role = null`).
|
|
6
|
-
*/
|
|
7
|
-
const PUBLIC_POLICY_ID = 'abf8a154-5b1c-4a46-ac9c-7300570f4f17';
|
|
8
|
-
export async function up(knex) {
|
|
9
|
-
/////////////////////////////////////////////////////////////////////////////////////////////////
|
|
10
|
-
// If the policies table already exists the migration has already run
|
|
11
|
-
if (await knex.schema.hasTable('directus_policies')) {
|
|
12
|
-
return;
|
|
13
|
-
}
|
|
14
|
-
/////////////////////////////////////////////////////////////////////////////////////////////////
|
|
15
|
-
// Create new policies table that mirrors previous Roles
|
|
16
|
-
await knex.schema.createTable('directus_policies', (table) => {
|
|
17
|
-
table.uuid('id').primary();
|
|
18
|
-
table.string('name', 100).notNullable();
|
|
19
|
-
table.string('icon', 64).notNullable().defaultTo('badge');
|
|
20
|
-
table.text('description');
|
|
21
|
-
table.text('ip_access');
|
|
22
|
-
table.boolean('enforce_tfa').defaultTo(false).notNullable();
|
|
23
|
-
table.boolean('admin_access').defaultTo(false).notNullable();
|
|
24
|
-
table.boolean('app_access').defaultTo(false).notNullable();
|
|
25
|
-
});
|
|
26
|
-
/////////////////////////////////////////////////////////////////////////////////////////////////
|
|
27
|
-
// Copy over all existing roles into new policies
|
|
28
|
-
const roles = await knex
|
|
29
|
-
.select('id', 'name', 'icon', 'description', 'ip_access', 'enforce_tfa', 'admin_access', 'app_access')
|
|
30
|
-
.from('directus_roles');
|
|
31
|
-
if (roles.length > 0) {
|
|
32
|
-
await processChunk(roles, 100, async (chunk) => {
|
|
33
|
-
await knex('directus_policies').insert(chunk);
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
await knex
|
|
37
|
-
.insert({
|
|
38
|
-
id: PUBLIC_POLICY_ID,
|
|
39
|
-
name: '$t:public_label',
|
|
40
|
-
icon: 'public',
|
|
41
|
-
description: '$t:public_description',
|
|
42
|
-
app_access: false,
|
|
43
|
-
})
|
|
44
|
-
.into('directus_policies');
|
|
45
|
-
// Change the admin policy description to $t:admin_policy_description
|
|
46
|
-
await knex('directus_policies')
|
|
47
|
-
.update({
|
|
48
|
-
description: '$t:admin_policy_description',
|
|
49
|
-
})
|
|
50
|
-
.where('description', 'LIKE', '$t:admin_description');
|
|
51
|
-
/////////////////////////////////////////////////////////////////////////////////////////////////
|
|
52
|
-
// Remove access control + add nesting to roles
|
|
53
|
-
await knex.schema.alterTable('directus_roles', (table) => {
|
|
54
|
-
table.dropColumn('ip_access');
|
|
55
|
-
table.dropColumn('enforce_tfa');
|
|
56
|
-
table.dropColumn('admin_access');
|
|
57
|
-
table.dropColumn('app_access');
|
|
58
|
-
table.uuid('parent').references('directus_roles.id');
|
|
59
|
-
});
|
|
60
|
-
/////////////////////////////////////////////////////////////////////////////////////////////////
|
|
61
|
-
// Link permissions to policies instead of roles
|
|
62
|
-
await knex.schema.alterTable('directus_permissions', (table) => {
|
|
63
|
-
table.uuid('policy').references('directus_policies.id').onDelete('CASCADE');
|
|
64
|
-
// Drop the foreign key constraint here in order to update `null` role to public policy ID
|
|
65
|
-
table.dropForeign('role');
|
|
66
|
-
});
|
|
67
|
-
await knex('directus_permissions')
|
|
68
|
-
.update({
|
|
69
|
-
role: PUBLIC_POLICY_ID,
|
|
70
|
-
})
|
|
71
|
-
.whereNull('role');
|
|
72
|
-
await knex('directus_permissions').update({
|
|
73
|
-
policy: knex.ref('role'),
|
|
74
|
-
});
|
|
75
|
-
await knex.schema.alterTable('directus_permissions', (table) => {
|
|
76
|
-
table.dropColumns('role');
|
|
77
|
-
table.dropNullable('policy');
|
|
78
|
-
});
|
|
79
|
-
/////////////////////////////////////////////////////////////////////////////////////////////////
|
|
80
|
-
// Setup junction table between roles/users and policies
|
|
81
|
-
// This could be a A2O style setup with a collection/item field rather than individual foreign
|
|
82
|
-
// keys, but we want to be able to show the reverse-relationship on the individual policies as
|
|
83
|
-
// well, which would require the O2A type to exist in Directus which currently doesn't.
|
|
84
|
-
// Shouldn't be the end of the world here, as we know we're only attaching policies to two other
|
|
85
|
-
// collections.
|
|
86
|
-
await knex.schema.createTable('directus_access', (table) => {
|
|
87
|
-
table.uuid('id').primary();
|
|
88
|
-
table.uuid('role').references('directus_roles.id').nullable().onDelete('CASCADE');
|
|
89
|
-
table.uuid('user').references('directus_users.id').nullable().onDelete('CASCADE');
|
|
90
|
-
table.uuid('policy').references('directus_policies.id').notNullable().onDelete('CASCADE');
|
|
91
|
-
table.integer('sort');
|
|
92
|
-
});
|
|
93
|
-
/////////////////////////////////////////////////////////////////////////////////////////////////
|
|
94
|
-
// Attach policies to existing roles for backwards compatibility
|
|
95
|
-
const policyAttachments = roles.map((role) => ({
|
|
96
|
-
id: randomUUID(),
|
|
97
|
-
role: role.id,
|
|
98
|
-
user: null,
|
|
99
|
-
policy: role.id,
|
|
100
|
-
sort: 1,
|
|
101
|
-
}));
|
|
102
|
-
await processChunk(policyAttachments, 100, async (chunk) => {
|
|
103
|
-
await knex('directus_access').insert(chunk);
|
|
104
|
-
});
|
|
105
|
-
await knex('directus_access').insert({
|
|
106
|
-
id: randomUUID(),
|
|
107
|
-
role: null,
|
|
108
|
-
user: null,
|
|
109
|
-
policy: PUBLIC_POLICY_ID,
|
|
110
|
-
sort: 1,
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
export async function down(knex) {
|
|
114
|
-
/////////////////////////////////////////////////////////////////////////////////////////////////
|
|
115
|
-
// Reinstate access control fields on directus roles + remove nesting
|
|
116
|
-
await knex.schema.alterTable('directus_roles', (table) => {
|
|
117
|
-
table.text('ip_access');
|
|
118
|
-
table.boolean('enforce_tfa').defaultTo(false).notNullable();
|
|
119
|
-
table.boolean('admin_access').defaultTo(false).notNullable();
|
|
120
|
-
table.boolean('app_access').defaultTo(true).notNullable();
|
|
121
|
-
table.dropForeign('parent');
|
|
122
|
-
table.dropColumn('parent');
|
|
123
|
-
});
|
|
124
|
-
/////////////////////////////////////////////////////////////////////////////////////////////////
|
|
125
|
-
// Copy policy access control rules back to roles
|
|
126
|
-
const policies = await knex
|
|
127
|
-
.select('id', 'ip_access', 'enforce_tfa', 'admin_access', 'app_access')
|
|
128
|
-
.from('directus_policies')
|
|
129
|
-
.whereNot({ id: PUBLIC_POLICY_ID });
|
|
130
|
-
for (const policy of policies) {
|
|
131
|
-
await knex('directus_roles')
|
|
132
|
-
.update({
|
|
133
|
-
ip_access: policy.ip_access,
|
|
134
|
-
enforce_tfa: policy.enforce_tfa,
|
|
135
|
-
admin_access: policy.admin_access,
|
|
136
|
-
app_access: policy.app_access,
|
|
137
|
-
})
|
|
138
|
-
.where({ id: policy.id });
|
|
139
|
-
}
|
|
140
|
-
/////////////////////////////////////////////////////////////////////////////////////////////////
|
|
141
|
-
// Drop all permissions that are only attached to a user
|
|
142
|
-
// TODO query all policies that are attached to a user and delete their permissions,
|
|
143
|
-
// since we don't know were to put them now and it'll cause a foreign key problem
|
|
144
|
-
// as soon as we reference directus_roles in directus_permissions again
|
|
145
|
-
/////////////////////////////////////////////////////////////////////////////////////////////////
|
|
146
|
-
// Drop policy attachments
|
|
147
|
-
await knex.schema.dropTable('directus_access');
|
|
148
|
-
/////////////////////////////////////////////////////////////////////////////////////////////////
|
|
149
|
-
// Reattach permissions to roles instead of policies
|
|
150
|
-
await knex.schema.alterTable('directus_permissions', (table) => {
|
|
151
|
-
table.uuid('role').nullable();
|
|
152
|
-
});
|
|
153
|
-
await knex('directus_permissions').update({
|
|
154
|
-
role: knex.ref('policy'),
|
|
155
|
-
});
|
|
156
|
-
await knex('directus_permissions')
|
|
157
|
-
.update({
|
|
158
|
-
role: null,
|
|
159
|
-
})
|
|
160
|
-
.where({ role: PUBLIC_POLICY_ID });
|
|
161
|
-
await knex.schema.alterTable('directus_permissions', (table) => {
|
|
162
|
-
table.uuid('role').references('directus_roles.id').alter();
|
|
163
|
-
table.dropForeign('policy');
|
|
164
|
-
table.dropColumn('policy');
|
|
165
|
-
});
|
|
166
|
-
/////////////////////////////////////////////////////////////////////////////////////////////////
|
|
167
|
-
// Drop policies table
|
|
168
|
-
await knex.schema.dropTable('directus_policies');
|
|
169
|
-
}
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import type { Filter, Query, SchemaOverview } from '@directus/types';
|
|
2
|
-
import type { Knex } from 'knex';
|
|
3
|
-
import type { FieldNode, FunctionFieldNode, O2MNode } from '../../../types/ast.js';
|
|
4
|
-
export declare function getDBQuery(schema: SchemaOverview, knex: Knex, table: string, fieldNodes: (FieldNode | FunctionFieldNode)[], o2mNodes: O2MNode[], query: Query, cases: Filter[]): Knex.QueryBuilder;
|
|
@@ -1,208 +0,0 @@
|
|
|
1
|
-
import { useEnv } from '@directus/env';
|
|
2
|
-
import { cloneDeep } from 'lodash-es';
|
|
3
|
-
import applyQuery, { applyLimit, applySort, generateAlias } from '../../../utils/apply-query.js';
|
|
4
|
-
import { getCollectionFromAlias } from '../../../utils/get-collection-from-alias.js';
|
|
5
|
-
import { getColumn } from '../../../utils/get-column.js';
|
|
6
|
-
import { getHelpers } from '../../helpers/index.js';
|
|
7
|
-
import { applyCaseWhen } from '../utils/apply-case-when.js';
|
|
8
|
-
import { getColumnPreprocessor } from '../utils/get-column-pre-processor.js';
|
|
9
|
-
import { getNodeAlias } from '../utils/get-field-alias.js';
|
|
10
|
-
import { getInnerQueryColumnPreProcessor } from '../utils/get-inner-query-column-pre-processor.js';
|
|
11
|
-
export function getDBQuery(schema, knex, table, fieldNodes, o2mNodes, query, cases) {
|
|
12
|
-
const aliasMap = Object.create(null);
|
|
13
|
-
const env = useEnv();
|
|
14
|
-
const preProcess = getColumnPreprocessor(knex, schema, table, cases, aliasMap);
|
|
15
|
-
const queryCopy = cloneDeep(query);
|
|
16
|
-
const helpers = getHelpers(knex);
|
|
17
|
-
const hasCaseWhen = o2mNodes.some((node) => node.whenCase && node.whenCase.length > 0) ||
|
|
18
|
-
fieldNodes.some((node) => node.whenCase && node.whenCase.length > 0);
|
|
19
|
-
queryCopy.limit = typeof queryCopy.limit === 'number' ? queryCopy.limit : Number(env['QUERY_LIMIT_DEFAULT']);
|
|
20
|
-
// Queries with aggregates and groupBy will not have duplicate result
|
|
21
|
-
if (queryCopy.aggregate || queryCopy.group) {
|
|
22
|
-
const flatQuery = knex.from(table).select(fieldNodes.map((node) => preProcess(node)));
|
|
23
|
-
return applyQuery(knex, table, flatQuery, queryCopy, schema, cases).query;
|
|
24
|
-
}
|
|
25
|
-
const primaryKey = schema.collections[table].primary;
|
|
26
|
-
let dbQuery = knex.from(table);
|
|
27
|
-
let sortRecords;
|
|
28
|
-
const innerQuerySortRecords = [];
|
|
29
|
-
let hasMultiRelationalSort;
|
|
30
|
-
if (queryCopy.sort) {
|
|
31
|
-
const sortResult = applySort(knex, schema, dbQuery, queryCopy, table, aliasMap, true);
|
|
32
|
-
if (sortResult) {
|
|
33
|
-
sortRecords = sortResult.sortRecords;
|
|
34
|
-
hasMultiRelationalSort = sortResult.hasMultiRelationalSort;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
const { hasMultiRelationalFilter } = applyQuery(knex, table, dbQuery, queryCopy, schema, cases, {
|
|
38
|
-
aliasMap,
|
|
39
|
-
isInnerQuery: true,
|
|
40
|
-
hasMultiRelationalSort,
|
|
41
|
-
});
|
|
42
|
-
const needsInnerQuery = hasMultiRelationalSort || hasMultiRelationalFilter;
|
|
43
|
-
if (needsInnerQuery) {
|
|
44
|
-
dbQuery.select(`${table}.${primaryKey}`);
|
|
45
|
-
// Only add distinct if there are no case/when constructs, since otherwise we rely on group by
|
|
46
|
-
if (!hasCaseWhen)
|
|
47
|
-
dbQuery.distinct();
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
dbQuery.select(fieldNodes.map((node) => preProcess(node)));
|
|
51
|
-
// Add flags for o2m fields with case/when to the let the DB to the partial item permissions
|
|
52
|
-
dbQuery.select(o2mNodes
|
|
53
|
-
.filter((node) => node.whenCase && node.whenCase.length > 0)
|
|
54
|
-
.map((node) => {
|
|
55
|
-
const columnCases = node.whenCase.map((index) => cases[index]);
|
|
56
|
-
return applyCaseWhen({
|
|
57
|
-
column: knex.raw(1),
|
|
58
|
-
columnCases,
|
|
59
|
-
aliasMap,
|
|
60
|
-
cases,
|
|
61
|
-
table,
|
|
62
|
-
alias: node.fieldKey,
|
|
63
|
-
}, { knex, schema });
|
|
64
|
-
}));
|
|
65
|
-
}
|
|
66
|
-
if (sortRecords) {
|
|
67
|
-
// Clears the order if any, eg: from MSSQL offset
|
|
68
|
-
dbQuery.clear('order');
|
|
69
|
-
if (needsInnerQuery) {
|
|
70
|
-
let orderByString = '';
|
|
71
|
-
const orderByFields = [];
|
|
72
|
-
sortRecords.map((sortRecord) => {
|
|
73
|
-
if (orderByString.length !== 0) {
|
|
74
|
-
orderByString += ', ';
|
|
75
|
-
}
|
|
76
|
-
const sortAlias = `sort_${generateAlias()}`;
|
|
77
|
-
if (sortRecord.column.includes('.')) {
|
|
78
|
-
const [alias, field] = sortRecord.column.split('.');
|
|
79
|
-
const originalCollectionName = getCollectionFromAlias(alias, aliasMap);
|
|
80
|
-
dbQuery.select(getColumn(knex, alias, field, sortAlias, schema, { originalCollectionName }));
|
|
81
|
-
orderByString += `?? ${sortRecord.order}`;
|
|
82
|
-
orderByFields.push(getColumn(knex, alias, field, false, schema, { originalCollectionName }));
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
dbQuery.select(getColumn(knex, table, sortRecord.column, sortAlias, schema));
|
|
86
|
-
orderByString += `?? ${sortRecord.order}`;
|
|
87
|
-
orderByFields.push(getColumn(knex, table, sortRecord.column, false, schema));
|
|
88
|
-
}
|
|
89
|
-
innerQuerySortRecords.push({ alias: sortAlias, order: sortRecord.order });
|
|
90
|
-
});
|
|
91
|
-
if (hasMultiRelationalSort) {
|
|
92
|
-
dbQuery = helpers.schema.applyMultiRelationalSort(knex, dbQuery, table, primaryKey, orderByString, orderByFields);
|
|
93
|
-
// Start order by with directus_row_number. The directus_row_number is derived from a window function that
|
|
94
|
-
// is ordered by the sort fields within every primary key partition. That ensures that the result with the
|
|
95
|
-
// row number = 1 is the top-most row of every partition, according to the selected sort fields.
|
|
96
|
-
// Since the only relevant result is the first row of this partition, adding the directus_row_number to the
|
|
97
|
-
// order by here ensures that all rows with a directus_row_number = 1 show up first in the inner query result,
|
|
98
|
-
// and are correctly truncated by the limit, but not earlier.
|
|
99
|
-
orderByString = `?? asc, ${orderByString}`;
|
|
100
|
-
orderByFields.unshift(knex.ref('directus_row_number'));
|
|
101
|
-
}
|
|
102
|
-
dbQuery.orderByRaw(orderByString, orderByFields);
|
|
103
|
-
}
|
|
104
|
-
else {
|
|
105
|
-
sortRecords.map((sortRecord) => {
|
|
106
|
-
if (sortRecord.column.includes('.')) {
|
|
107
|
-
const [alias, field] = sortRecord.column.split('.');
|
|
108
|
-
sortRecord.column = getColumn(knex, alias, field, false, schema, {
|
|
109
|
-
originalCollectionName: getCollectionFromAlias(alias, aliasMap),
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
else {
|
|
113
|
-
sortRecord.column = getColumn(knex, table, sortRecord.column, false, schema);
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
dbQuery.orderBy(sortRecords);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
if (!needsInnerQuery)
|
|
120
|
-
return dbQuery;
|
|
121
|
-
const innerCaseWhenAliasPrefix = generateAlias();
|
|
122
|
-
if (hasCaseWhen) {
|
|
123
|
-
/* If there are cases, we need to employ a trick in order to evaluate the case/when structure in the inner query,
|
|
124
|
-
while passing the result of the evaluation to the outer query. The case/when needs to be evaluated in the inner
|
|
125
|
-
query since only there all joined in tables, that might be required for the case/when, are available.
|
|
126
|
-
|
|
127
|
-
The problem is, that the resulting columns can not be directly selected in the inner query,
|
|
128
|
-
as a `SELECT DISTINCT` does not work for all datatypes in all vendors.
|
|
129
|
-
|
|
130
|
-
So instead of having an inner query which might look like this:
|
|
131
|
-
|
|
132
|
-
SELECT DISTINCT ...,
|
|
133
|
-
CASE WHEN <condition> THEN <actual-column> END AS <alias>
|
|
134
|
-
|
|
135
|
-
Another problem is that all not all rows with the same primary key are guaranteed to have the same value for
|
|
136
|
-
the columns with the case/when, so we to `or` those together, but counting the number of flags in a group by
|
|
137
|
-
operation. This way the flag is set to > 0 if any of the rows in the group allows access to the column.
|
|
138
|
-
|
|
139
|
-
The inner query only evaluates the condition and passes up or-ed flag, that is used in the wrapper query to select
|
|
140
|
-
the actual column:
|
|
141
|
-
|
|
142
|
-
SELECT ...,
|
|
143
|
-
COUNT (CASE WHEN <condition> THEN 1 END) AS <random-prefix>_<alias>
|
|
144
|
-
...
|
|
145
|
-
GROUP BY <primary-key>
|
|
146
|
-
|
|
147
|
-
Then, in the wrapper query there is no need to evaluate the condition again, but instead rely on the flag:
|
|
148
|
-
|
|
149
|
-
SELECT ...,
|
|
150
|
-
CASE WHEN `inner`.<random-prefix>_<alias> > 0 THEN <actual-column> END AS <alias>
|
|
151
|
-
*/
|
|
152
|
-
const innerPreprocess = getInnerQueryColumnPreProcessor(knex, schema, table, cases, aliasMap, innerCaseWhenAliasPrefix);
|
|
153
|
-
// To optimize the query we avoid having unnecessary columns in the inner query, that don't have a caseWhen, since
|
|
154
|
-
// they are selected in the outer query directly
|
|
155
|
-
dbQuery.select(fieldNodes.map(innerPreprocess).filter((x) => x !== null));
|
|
156
|
-
// In addition to the regular columns select a flag that indicates if a user has access to o2m related field
|
|
157
|
-
// based on the case/when of that field.
|
|
158
|
-
dbQuery.select(o2mNodes.map(innerPreprocess).filter((x) => x !== null));
|
|
159
|
-
const groupByFields = [knex.raw('??.??', [table, primaryKey])];
|
|
160
|
-
if (hasMultiRelationalSort) {
|
|
161
|
-
// Sort fields that are not directly in the table the primary key is from need to be included in the group
|
|
162
|
-
// by clause, otherwise this causes problems on some DBs
|
|
163
|
-
groupByFields.push(...innerQuerySortRecords.map(({ alias }) => knex.raw('??', alias)));
|
|
164
|
-
}
|
|
165
|
-
dbQuery.groupBy(groupByFields);
|
|
166
|
-
}
|
|
167
|
-
const wrapperQuery = knex
|
|
168
|
-
.from(table)
|
|
169
|
-
.innerJoin(knex.raw('??', dbQuery.as('inner')), `${table}.${primaryKey}`, `inner.${primaryKey}`);
|
|
170
|
-
if (!hasCaseWhen) {
|
|
171
|
-
// No need for case/when in the wrapper query, just select the preprocessed columns
|
|
172
|
-
wrapperQuery.select(fieldNodes.map((node) => preProcess(node)));
|
|
173
|
-
}
|
|
174
|
-
else {
|
|
175
|
-
// This applies a simplified case/when construct in the wrapper query, that only looks at flag > 1
|
|
176
|
-
// Distinguish between column with and without case/when and handle them differently
|
|
177
|
-
const plainColumns = fieldNodes.filter((fieldNode) => !fieldNode.whenCase || fieldNode.whenCase.length === 0);
|
|
178
|
-
const whenCaseColumns = fieldNodes.filter((fieldNode) => fieldNode.whenCase && fieldNode.whenCase.length > 0);
|
|
179
|
-
// Select the plain columns
|
|
180
|
-
wrapperQuery.select(plainColumns.map((node) => preProcess(node)));
|
|
181
|
-
// Select the case/when columns based on the flag from the inner query
|
|
182
|
-
wrapperQuery.select(whenCaseColumns.map((fieldNode) => {
|
|
183
|
-
const alias = getNodeAlias(fieldNode);
|
|
184
|
-
const innerAlias = `${innerCaseWhenAliasPrefix}_${alias}`;
|
|
185
|
-
// Preprocess the column without the case/when, since that is applied in a simpler fashion in the select
|
|
186
|
-
const column = preProcess({ ...fieldNode, whenCase: [] }, { noAlias: true });
|
|
187
|
-
return knex.raw(`CASE WHEN ??.?? > 0 THEN ?? END as ??`, ['inner', innerAlias, column, alias]);
|
|
188
|
-
}));
|
|
189
|
-
// Pass the flags of o2m fields up through the wrapper query
|
|
190
|
-
wrapperQuery.select(o2mNodes
|
|
191
|
-
.filter((node) => node.whenCase && node.whenCase.length > 0)
|
|
192
|
-
.map((node) => {
|
|
193
|
-
const alias = node.fieldKey;
|
|
194
|
-
const innerAlias = `${innerCaseWhenAliasPrefix}_${alias}`;
|
|
195
|
-
return knex.raw(`CASE WHEN ??.?? > 0 THEN 1 END as ??`, ['inner', innerAlias, alias]);
|
|
196
|
-
}));
|
|
197
|
-
}
|
|
198
|
-
if (sortRecords) {
|
|
199
|
-
innerQuerySortRecords.map((innerQuerySortRecord) => {
|
|
200
|
-
wrapperQuery.orderBy(`inner.${innerQuerySortRecord.alias}`, innerQuerySortRecord.order);
|
|
201
|
-
});
|
|
202
|
-
if (hasMultiRelationalSort) {
|
|
203
|
-
wrapperQuery.where('inner.directus_row_number', '=', 1);
|
|
204
|
-
applyLimit(knex, wrapperQuery, queryCopy.limit);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
return wrapperQuery;
|
|
208
|
-
}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { Query, SchemaOverview } from '@directus/types';
|
|
2
|
-
import type { FieldNode, FunctionFieldNode, NestedCollectionNode } from '../../../types/ast.js';
|
|
3
|
-
export declare function parseCurrentLevel(schema: SchemaOverview, collection: string, children: (NestedCollectionNode | FieldNode | FunctionFieldNode)[], query: Query): Promise<{
|
|
4
|
-
fieldNodes: FieldNode[];
|
|
5
|
-
nestedCollectionNodes: NestedCollectionNode[];
|
|
6
|
-
primaryKeyField: string;
|
|
7
|
-
}>;
|