@directus/api 23.0.0 → 23.1.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 +2 -0
- package/dist/controllers/activity.js +30 -27
- package/dist/controllers/assets.js +1 -1
- package/dist/controllers/comments.d.ts +2 -0
- package/dist/controllers/comments.js +153 -0
- package/dist/controllers/versions.js +10 -5
- package/dist/database/index.js +3 -0
- package/dist/database/migrations/20240909A-separate-comments.d.ts +3 -0
- package/dist/database/migrations/20240909A-separate-comments.js +65 -0
- package/dist/database/migrations/20240909B-consolidate-content-versioning.d.ts +3 -0
- package/dist/database/migrations/20240909B-consolidate-content-versioning.js +10 -0
- package/dist/database/run-ast/lib/get-db-query.d.ts +12 -2
- package/dist/database/run-ast/lib/get-db-query.js +2 -2
- package/dist/database/run-ast/modules/fetch-permitted-ast-root-fields.d.ts +15 -0
- package/dist/database/run-ast/modules/fetch-permitted-ast-root-fields.js +29 -0
- package/dist/database/run-ast/run-ast.js +8 -1
- package/dist/database/run-ast/utils/get-column-pre-processor.d.ts +1 -1
- package/dist/database/run-ast/utils/get-column-pre-processor.js +10 -2
- package/dist/permissions/modules/validate-access/lib/validate-item-access.d.ts +2 -1
- package/dist/permissions/modules/validate-access/lib/validate-item-access.js +18 -13
- package/dist/permissions/modules/validate-access/validate-access.d.ts +1 -0
- package/dist/permissions/modules/validate-access/validate-access.js +14 -1
- package/dist/services/activity.d.ts +1 -7
- package/dist/services/activity.js +0 -103
- package/dist/services/assets.js +5 -4
- package/dist/services/collections.js +6 -4
- package/dist/services/comments.d.ts +31 -0
- package/dist/services/comments.js +374 -0
- package/dist/services/graphql/index.js +17 -16
- package/dist/services/index.d.ts +1 -0
- package/dist/services/index.js +1 -0
- package/dist/services/items.js +3 -1
- package/dist/services/mail/index.d.ts +2 -1
- package/dist/services/mail/index.js +4 -1
- package/dist/services/payload.js +15 -14
- package/dist/services/users.js +1 -0
- package/dist/services/versions.js +59 -44
- package/dist/utils/apply-diff.js +5 -6
- package/dist/utils/get-service.js +3 -1
- package/dist/utils/sanitize-schema.d.ts +1 -1
- package/dist/utils/sanitize-schema.js +2 -0
- package/package.json +51 -51
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { FUNCTIONS } from '@directus/constants';
|
|
2
2
|
import { useEnv } from '@directus/env';
|
|
3
3
|
import { ErrorCode, ForbiddenError, InvalidPayloadError, isDirectusError } from '@directus/errors';
|
|
4
4
|
import { isSystemCollection } from '@directus/system-data';
|
|
@@ -11,6 +11,8 @@ import { clearSystemCache, getCache } from '../../cache.js';
|
|
|
11
11
|
import { DEFAULT_AUTH_PROVIDER, GENERATE_SPECIAL, REFRESH_COOKIE_OPTIONS, SESSION_COOKIE_OPTIONS, } from '../../constants.js';
|
|
12
12
|
import getDatabase from '../../database/index.js';
|
|
13
13
|
import { rateLimiter } from '../../middleware/rate-limiter-registration.js';
|
|
14
|
+
import { fetchAccountabilityCollectionAccess } from '../../permissions/modules/fetch-accountability-collection-access/fetch-accountability-collection-access.js';
|
|
15
|
+
import { fetchAccountabilityPolicyGlobals } from '../../permissions/modules/fetch-accountability-policy-globals/fetch-accountability-policy-globals.js';
|
|
14
16
|
import { fetchAllowedFieldMap } from '../../permissions/modules/fetch-allowed-field-map/fetch-allowed-field-map.js';
|
|
15
17
|
import { fetchInconsistentFieldMap } from '../../permissions/modules/fetch-inconsistent-field-map/fetch-inconsistent-field-map.js';
|
|
16
18
|
import { createDefaultAccountability } from '../../permissions/utils/create-default-accountability.js';
|
|
@@ -25,14 +27,15 @@ import { mergeVersionsRaw, mergeVersionsRecursive } from '../../utils/merge-vers
|
|
|
25
27
|
import { reduceSchema } from '../../utils/reduce-schema.js';
|
|
26
28
|
import { sanitizeQuery } from '../../utils/sanitize-query.js';
|
|
27
29
|
import { validateQuery } from '../../utils/validate-query.js';
|
|
28
|
-
import { ActivityService } from '../activity.js';
|
|
29
30
|
import { AuthenticationService } from '../authentication.js';
|
|
30
31
|
import { CollectionsService } from '../collections.js';
|
|
32
|
+
import { CommentsService } from '../comments.js';
|
|
31
33
|
import { ExtensionsService } from '../extensions.js';
|
|
32
34
|
import { FieldsService } from '../fields.js';
|
|
33
35
|
import { FilesService } from '../files.js';
|
|
34
36
|
import { RelationsService } from '../relations.js';
|
|
35
37
|
import { RevisionsService } from '../revisions.js';
|
|
38
|
+
import { RolesService } from '../roles.js';
|
|
36
39
|
import { ServerService } from '../server.js';
|
|
37
40
|
import { SpecificationService } from '../specifications.js';
|
|
38
41
|
import { TFAService } from '../tfa.js';
|
|
@@ -51,9 +54,6 @@ import { GraphQLVoid } from './types/void.js';
|
|
|
51
54
|
import { addPathToValidationError } from './utils/add-path-to-validation-error.js';
|
|
52
55
|
import processError from './utils/process-error.js';
|
|
53
56
|
import { sanitizeGraphqlSchema } from './utils/sanitize-gql-schema.js';
|
|
54
|
-
import { fetchAccountabilityCollectionAccess } from '../../permissions/modules/fetch-accountability-collection-access/fetch-accountability-collection-access.js';
|
|
55
|
-
import { fetchAccountabilityPolicyGlobals } from '../../permissions/modules/fetch-accountability-policy-globals/fetch-accountability-policy-globals.js';
|
|
56
|
-
import { RolesService } from '../roles.js';
|
|
57
57
|
const env = useEnv();
|
|
58
58
|
const validationRules = Array.from(specifiedRules);
|
|
59
59
|
if (env['GRAPHQL_INTROSPECTION'] === false) {
|
|
@@ -2313,6 +2313,9 @@ export class GraphQLService {
|
|
|
2313
2313
|
max_length: GraphQLInt,
|
|
2314
2314
|
numeric_precision: GraphQLInt,
|
|
2315
2315
|
numeric_scale: GraphQLInt,
|
|
2316
|
+
is_generated: GraphQLBoolean,
|
|
2317
|
+
generation_expression: GraphQLString,
|
|
2318
|
+
is_indexed: GraphQLBoolean,
|
|
2316
2319
|
is_nullable: GraphQLBoolean,
|
|
2317
2320
|
is_unique: GraphQLBoolean,
|
|
2318
2321
|
is_primary_key: GraphQLBoolean,
|
|
@@ -2771,17 +2774,13 @@ export class GraphQLService {
|
|
|
2771
2774
|
comment: new GraphQLNonNull(GraphQLString),
|
|
2772
2775
|
},
|
|
2773
2776
|
resolve: async (_, args, __, info) => {
|
|
2774
|
-
const service = new
|
|
2777
|
+
const service = new CommentsService({
|
|
2775
2778
|
accountability: this.accountability,
|
|
2776
2779
|
schema: this.schema,
|
|
2780
|
+
serviceOrigin: 'activity',
|
|
2777
2781
|
});
|
|
2778
2782
|
const primaryKey = await service.createOne({
|
|
2779
2783
|
...args,
|
|
2780
|
-
action: Action.COMMENT,
|
|
2781
|
-
user: this.accountability?.user,
|
|
2782
|
-
ip: this.accountability?.ip,
|
|
2783
|
-
user_agent: this.accountability?.userAgent,
|
|
2784
|
-
origin: this.accountability?.origin,
|
|
2785
2784
|
});
|
|
2786
2785
|
if ('directus_activity' in ReadCollectionTypes) {
|
|
2787
2786
|
const selections = this.replaceFragmentsInSelections(info.fieldNodes[0]?.selectionSet?.selections, info.fragments);
|
|
@@ -2802,15 +2801,16 @@ export class GraphQLService {
|
|
|
2802
2801
|
comment: new GraphQLNonNull(GraphQLString),
|
|
2803
2802
|
},
|
|
2804
2803
|
resolve: async (_, args, __, info) => {
|
|
2805
|
-
const
|
|
2804
|
+
const commentsService = new CommentsService({
|
|
2806
2805
|
accountability: this.accountability,
|
|
2807
2806
|
schema: this.schema,
|
|
2807
|
+
serviceOrigin: 'activity',
|
|
2808
2808
|
});
|
|
2809
|
-
const primaryKey = await
|
|
2809
|
+
const primaryKey = await commentsService.updateOne(args['id'], { comment: args['comment'] });
|
|
2810
2810
|
if ('directus_activity' in ReadCollectionTypes) {
|
|
2811
2811
|
const selections = this.replaceFragmentsInSelections(info.fieldNodes[0]?.selectionSet?.selections, info.fragments);
|
|
2812
2812
|
const query = this.getQuery(args, selections || [], info.variableValues);
|
|
2813
|
-
return await
|
|
2813
|
+
return { ...(await commentsService.readOne(primaryKey, query)), id: args['id'] };
|
|
2814
2814
|
}
|
|
2815
2815
|
return true;
|
|
2816
2816
|
},
|
|
@@ -2825,11 +2825,12 @@ export class GraphQLService {
|
|
|
2825
2825
|
id: new GraphQLNonNull(GraphQLID),
|
|
2826
2826
|
},
|
|
2827
2827
|
resolve: async (_, args) => {
|
|
2828
|
-
const
|
|
2828
|
+
const commentsService = new CommentsService({
|
|
2829
2829
|
accountability: this.accountability,
|
|
2830
2830
|
schema: this.schema,
|
|
2831
|
+
serviceOrigin: 'activity',
|
|
2831
2832
|
});
|
|
2832
|
-
await
|
|
2833
|
+
await commentsService.deleteOne(args['id']);
|
|
2833
2834
|
return { id: args['id'] };
|
|
2834
2835
|
},
|
|
2835
2836
|
},
|
package/dist/services/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export * from './activity.js';
|
|
|
3
3
|
export * from './assets.js';
|
|
4
4
|
export * from './authentication.js';
|
|
5
5
|
export * from './collections.js';
|
|
6
|
+
export * from './comments.js';
|
|
6
7
|
export * from './dashboards.js';
|
|
7
8
|
export * from './extensions.js';
|
|
8
9
|
export * from './fields.js';
|
package/dist/services/index.js
CHANGED
|
@@ -3,6 +3,7 @@ export * from './activity.js';
|
|
|
3
3
|
export * from './assets.js';
|
|
4
4
|
export * from './authentication.js';
|
|
5
5
|
export * from './collections.js';
|
|
6
|
+
export * from './comments.js';
|
|
6
7
|
export * from './dashboards.js';
|
|
7
8
|
export * from './extensions.js';
|
|
8
9
|
export * from './fields.js';
|
package/dist/services/items.js
CHANGED
|
@@ -543,6 +543,7 @@ export class ItemsService {
|
|
|
543
543
|
action: 'update',
|
|
544
544
|
collection: this.collection,
|
|
545
545
|
primaryKeys: keys,
|
|
546
|
+
fields: Object.keys(payloadAfterHooks),
|
|
546
547
|
}, {
|
|
547
548
|
schema: this.schema,
|
|
548
549
|
knex: this.knex,
|
|
@@ -706,7 +707,8 @@ export class ItemsService {
|
|
|
706
707
|
.where({ [primaryKeyField]: primaryKey })
|
|
707
708
|
.first());
|
|
708
709
|
if (exists) {
|
|
709
|
-
|
|
710
|
+
const { [primaryKeyField]: _, ...data } = payload;
|
|
711
|
+
return await this.updateOne(primaryKey, data, opts);
|
|
710
712
|
}
|
|
711
713
|
else {
|
|
712
714
|
return await this.createOne(payload, opts);
|
|
@@ -2,7 +2,8 @@ import type { Accountability, SchemaOverview } from '@directus/types';
|
|
|
2
2
|
import type { Knex } from 'knex';
|
|
3
3
|
import type { SendMailOptions, Transporter } from 'nodemailer';
|
|
4
4
|
import type { AbstractServiceOptions } from '../../types/index.js';
|
|
5
|
-
export type EmailOptions = SendMailOptions & {
|
|
5
|
+
export type EmailOptions = Omit<SendMailOptions, 'from'> & {
|
|
6
|
+
from?: string;
|
|
6
7
|
template?: {
|
|
7
8
|
name: string;
|
|
8
9
|
data: Record<string, any>;
|
|
@@ -42,7 +42,10 @@ export class MailService {
|
|
|
42
42
|
const { template, ...emailOptions } = payload;
|
|
43
43
|
let { html } = options;
|
|
44
44
|
const defaultTemplateData = await this.getDefaultTemplateData();
|
|
45
|
-
const from =
|
|
45
|
+
const from = {
|
|
46
|
+
name: defaultTemplateData.projectName,
|
|
47
|
+
address: options.from || env['EMAIL_FROM'],
|
|
48
|
+
};
|
|
46
49
|
if (template) {
|
|
47
50
|
let templateData = template.data;
|
|
48
51
|
templateData = {
|
package/dist/services/payload.js
CHANGED
|
@@ -3,7 +3,7 @@ import { parseJSON, toArray } from '@directus/utils';
|
|
|
3
3
|
import { format, isValid, parseISO } from 'date-fns';
|
|
4
4
|
import { unflatten } from 'flat';
|
|
5
5
|
import Joi from 'joi';
|
|
6
|
-
import { clone, cloneDeep, isNil, isObject, isPlainObject,
|
|
6
|
+
import { clone, cloneDeep, isNil, isObject, isPlainObject, pick } from 'lodash-es';
|
|
7
7
|
import { randomUUID } from 'node:crypto';
|
|
8
8
|
import { parse as wktToGeoJSON } from 'wellknown';
|
|
9
9
|
import { getHelpers } from '../database/helpers/index.js';
|
|
@@ -347,22 +347,22 @@ export class PayloadService {
|
|
|
347
347
|
knex: this.knex,
|
|
348
348
|
schema: this.schema,
|
|
349
349
|
});
|
|
350
|
-
const
|
|
350
|
+
const relatedPrimaryKeyField = this.schema.collections[relatedCollection].primary;
|
|
351
351
|
const relatedRecord = payload[relation.field];
|
|
352
352
|
if (['string', 'number'].includes(typeof relatedRecord))
|
|
353
353
|
continue;
|
|
354
|
-
const hasPrimaryKey =
|
|
355
|
-
let relatedPrimaryKey = relatedRecord[
|
|
354
|
+
const hasPrimaryKey = relatedPrimaryKeyField in relatedRecord;
|
|
355
|
+
let relatedPrimaryKey = relatedRecord[relatedPrimaryKeyField];
|
|
356
356
|
const exists = hasPrimaryKey &&
|
|
357
357
|
!!(await this.knex
|
|
358
|
-
.select(
|
|
358
|
+
.select(relatedPrimaryKeyField)
|
|
359
359
|
.from(relatedCollection)
|
|
360
|
-
.where({ [
|
|
360
|
+
.where({ [relatedPrimaryKeyField]: relatedPrimaryKey })
|
|
361
361
|
.first());
|
|
362
362
|
if (exists) {
|
|
363
|
-
const
|
|
364
|
-
if (Object.keys(
|
|
365
|
-
await service.updateOne(relatedPrimaryKey,
|
|
363
|
+
const { [relatedPrimaryKeyField]: _, ...record } = relatedRecord;
|
|
364
|
+
if (Object.keys(record).length > 0) {
|
|
365
|
+
await service.updateOne(relatedPrimaryKey, record, {
|
|
366
366
|
onRevisionCreate: (pk) => revisions.push(pk),
|
|
367
367
|
onRequireUserIntegrityCheck: (flags) => (userIntegrityCheckFlags |= flags),
|
|
368
368
|
bypassEmitAction: (params) => opts?.bypassEmitAction ? opts.bypassEmitAction(params) : nestedActionEvents.push(params),
|
|
@@ -425,9 +425,9 @@ export class PayloadService {
|
|
|
425
425
|
.where({ [relatedPrimaryKeyField]: relatedPrimaryKey })
|
|
426
426
|
.first());
|
|
427
427
|
if (exists) {
|
|
428
|
-
const
|
|
429
|
-
if (Object.keys(
|
|
430
|
-
await service.updateOne(relatedPrimaryKey,
|
|
428
|
+
const { [relatedPrimaryKeyField]: _, ...record } = relatedRecord;
|
|
429
|
+
if (Object.keys(record).length > 0) {
|
|
430
|
+
await service.updateOne(relatedPrimaryKey, record, {
|
|
431
431
|
onRevisionCreate: (pk) => revisions.push(pk),
|
|
432
432
|
onRequireUserIntegrityCheck: (flags) => (userIntegrityCheckFlags |= flags),
|
|
433
433
|
bypassEmitAction: (params) => opts?.bypassEmitAction ? opts.bypassEmitAction(params) : nestedActionEvents.push(params),
|
|
@@ -610,8 +610,9 @@ export class PayloadService {
|
|
|
610
610
|
if (alterations.update) {
|
|
611
611
|
const primaryKeyField = this.schema.collections[relation.collection].primary;
|
|
612
612
|
for (const item of alterations.update) {
|
|
613
|
-
|
|
614
|
-
|
|
613
|
+
const { [primaryKeyField]: key, ...record } = item;
|
|
614
|
+
await service.updateOne(key, {
|
|
615
|
+
...record,
|
|
615
616
|
[relation.field]: parent || payload[currentPrimaryKeyField],
|
|
616
617
|
}, {
|
|
617
618
|
onRevisionCreate: (pk) => revisions.push(pk),
|
package/dist/services/users.js
CHANGED
|
@@ -260,6 +260,7 @@ export class UsersService extends ItemsService {
|
|
|
260
260
|
}
|
|
261
261
|
}
|
|
262
262
|
// Manual constraint, see https://github.com/directus/directus/pull/19912
|
|
263
|
+
await this.knex('directus_comments').update({ user_updated: null }).whereIn('user_updated', keys);
|
|
263
264
|
await this.knex('directus_notifications').update({ sender: null }).whereIn('sender', keys);
|
|
264
265
|
await this.knex('directus_versions').update({ user_updated: null }).whereIn('user_updated', keys);
|
|
265
266
|
await super.deleteMany(keys, opts);
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { Action } from '@directus/constants';
|
|
2
|
-
import { InvalidPayloadError, UnprocessableContentError } from '@directus/errors';
|
|
2
|
+
import { ForbiddenError, InvalidPayloadError, UnprocessableContentError } from '@directus/errors';
|
|
3
3
|
import Joi from 'joi';
|
|
4
4
|
import { assign, pick } from 'lodash-es';
|
|
5
5
|
import objectHash from 'object-hash';
|
|
6
6
|
import { getCache } from '../cache.js';
|
|
7
|
-
import getDatabase from '../database/index.js';
|
|
8
7
|
import emitter from '../emitter.js';
|
|
9
8
|
import { validateAccess } from '../permissions/modules/validate-access/validate-access.js';
|
|
10
9
|
import { shouldClearCache } from '../utils/should-clear-cache.js';
|
|
@@ -17,19 +16,36 @@ export class VersionsService extends ItemsService {
|
|
|
17
16
|
super('directus_versions', options);
|
|
18
17
|
}
|
|
19
18
|
async validateCreateData(data) {
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
const versionCreateSchema = Joi.object({
|
|
20
|
+
key: Joi.string().required(),
|
|
21
|
+
name: Joi.string().allow(null),
|
|
22
|
+
collection: Joi.string().required(),
|
|
23
|
+
item: Joi.string().required(),
|
|
24
|
+
});
|
|
25
|
+
const { error } = versionCreateSchema.validate(data);
|
|
26
|
+
if (error)
|
|
27
|
+
throw new InvalidPayloadError({ reason: error.message });
|
|
22
28
|
// Reserves the "main" version key for the version query parameter
|
|
23
29
|
if (data['key'] === 'main')
|
|
24
30
|
throw new InvalidPayloadError({ reason: `"main" is a reserved version key` });
|
|
25
|
-
if (
|
|
26
|
-
|
|
31
|
+
if (this.accountability) {
|
|
32
|
+
try {
|
|
33
|
+
await validateAccess({
|
|
34
|
+
accountability: this.accountability,
|
|
35
|
+
action: 'read',
|
|
36
|
+
collection: data['collection'],
|
|
37
|
+
primaryKeys: [data['item']],
|
|
38
|
+
}, {
|
|
39
|
+
schema: this.schema,
|
|
40
|
+
knex: this.knex,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
throw new ForbiddenError();
|
|
45
|
+
}
|
|
27
46
|
}
|
|
28
|
-
if (!data['item'])
|
|
29
|
-
throw new InvalidPayloadError({ reason: `"item" is required` });
|
|
30
47
|
const { CollectionsService } = await import('./collections.js');
|
|
31
48
|
const collectionsService = new CollectionsService({
|
|
32
|
-
accountability: null,
|
|
33
49
|
knex: this.knex,
|
|
34
50
|
schema: this.schema,
|
|
35
51
|
});
|
|
@@ -39,7 +55,11 @@ export class VersionsService extends ItemsService {
|
|
|
39
55
|
reason: `Content Versioning is not enabled for collection "${data['collection']}"`,
|
|
40
56
|
});
|
|
41
57
|
}
|
|
42
|
-
const
|
|
58
|
+
const sudoService = new VersionsService({
|
|
59
|
+
knex: this.knex,
|
|
60
|
+
schema: this.schema,
|
|
61
|
+
});
|
|
62
|
+
const existingVersions = await sudoService.readByQuery({
|
|
43
63
|
aggregate: { count: ['*'] },
|
|
44
64
|
filter: { key: { _eq: data['key'] }, collection: { _eq: data['collection'] }, item: { _eq: data['item'] } },
|
|
45
65
|
});
|
|
@@ -48,32 +68,8 @@ export class VersionsService extends ItemsService {
|
|
|
48
68
|
reason: `Version "${data['key']}" already exists for item "${data['item']}" in collection "${data['collection']}"`,
|
|
49
69
|
});
|
|
50
70
|
}
|
|
51
|
-
// will throw an error if the accountability does not have permission to read the item
|
|
52
|
-
if (this.accountability) {
|
|
53
|
-
await validateAccess({
|
|
54
|
-
accountability: this.accountability,
|
|
55
|
-
action: 'read',
|
|
56
|
-
collection: data['collection'],
|
|
57
|
-
primaryKeys: [data['item']],
|
|
58
|
-
}, {
|
|
59
|
-
schema: this.schema,
|
|
60
|
-
knex: this.knex,
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
71
|
}
|
|
64
72
|
async getMainItem(collection, item, query) {
|
|
65
|
-
// will throw an error if the accountability does not have permission to read the item
|
|
66
|
-
if (this.accountability) {
|
|
67
|
-
await validateAccess({
|
|
68
|
-
accountability: this.accountability,
|
|
69
|
-
action: 'read',
|
|
70
|
-
collection,
|
|
71
|
-
primaryKeys: [item],
|
|
72
|
-
}, {
|
|
73
|
-
schema: this.schema,
|
|
74
|
-
knex: this.knex,
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
73
|
const itemsService = new ItemsService(collection, {
|
|
78
74
|
knex: this.knex,
|
|
79
75
|
accountability: this.accountability,
|
|
@@ -96,6 +92,7 @@ export class VersionsService extends ItemsService {
|
|
|
96
92
|
});
|
|
97
93
|
return result.map((revision) => revision['delta']);
|
|
98
94
|
}
|
|
95
|
+
// TODO: Remove legacy need to return a version array in subsequent release
|
|
99
96
|
async getVersionSaves(key, collection, item) {
|
|
100
97
|
const filter = {
|
|
101
98
|
key: { _eq: key },
|
|
@@ -107,6 +104,9 @@ export class VersionsService extends ItemsService {
|
|
|
107
104
|
const versions = await this.readByQuery({ filter });
|
|
108
105
|
if (!versions?.[0])
|
|
109
106
|
return null;
|
|
107
|
+
if (versions[0]['delta']) {
|
|
108
|
+
return [versions[0]['delta']];
|
|
109
|
+
}
|
|
110
110
|
const saves = await this.getVersionSavesById(versions[0]['id']);
|
|
111
111
|
return saves;
|
|
112
112
|
}
|
|
@@ -122,7 +122,6 @@ export class VersionsService extends ItemsService {
|
|
|
122
122
|
}
|
|
123
123
|
const keyCombos = new Set();
|
|
124
124
|
for (const item of data) {
|
|
125
|
-
await this.validateCreateData(item);
|
|
126
125
|
const keyCombo = `${item['key']}-${item['collection']}-${item['item']}`;
|
|
127
126
|
if (keyCombos.has(keyCombo)) {
|
|
128
127
|
throw new UnprocessableContentError({
|
|
@@ -130,8 +129,6 @@ export class VersionsService extends ItemsService {
|
|
|
130
129
|
});
|
|
131
130
|
}
|
|
132
131
|
keyCombos.add(keyCombo);
|
|
133
|
-
const mainItem = await this.getMainItem(item['collection'], item['item']);
|
|
134
|
-
item['hash'] = objectHash(mainItem);
|
|
135
132
|
}
|
|
136
133
|
return super.createMany(data, opts);
|
|
137
134
|
}
|
|
@@ -139,7 +136,7 @@ export class VersionsService extends ItemsService {
|
|
|
139
136
|
// Only allow updates on "key" and "name" fields
|
|
140
137
|
const versionUpdateSchema = Joi.object({
|
|
141
138
|
key: Joi.string(),
|
|
142
|
-
name: Joi.string().allow(null)
|
|
139
|
+
name: Joi.string().allow(null),
|
|
143
140
|
});
|
|
144
141
|
const { error } = versionUpdateSchema.validate(data);
|
|
145
142
|
if (error)
|
|
@@ -205,14 +202,25 @@ export class VersionsService extends ItemsService {
|
|
|
205
202
|
data: revisionDelta,
|
|
206
203
|
delta: revisionDelta,
|
|
207
204
|
});
|
|
205
|
+
let existingDelta = version['delta'];
|
|
206
|
+
if (!existingDelta) {
|
|
207
|
+
const saves = await this.getVersionSavesById(key);
|
|
208
|
+
existingDelta = assign({}, ...saves);
|
|
209
|
+
}
|
|
210
|
+
const finalVersionDelta = assign({}, existingDelta, revisionDelta ? JSON.parse(revisionDelta) : null);
|
|
211
|
+
const sudoService = new ItemsService(this.collection, {
|
|
212
|
+
knex: this.knex,
|
|
213
|
+
schema: this.schema,
|
|
214
|
+
});
|
|
215
|
+
await sudoService.updateOne(key, { delta: finalVersionDelta });
|
|
208
216
|
const { cache } = getCache();
|
|
209
217
|
if (shouldClearCache(cache, undefined, collection)) {
|
|
210
218
|
cache.clear();
|
|
211
219
|
}
|
|
212
|
-
return
|
|
220
|
+
return finalVersionDelta;
|
|
213
221
|
}
|
|
214
222
|
async promote(version, mainHash, fields) {
|
|
215
|
-
const { id, collection, item } = (await this.readOne(version));
|
|
223
|
+
const { id, collection, item, delta } = (await this.readOne(version));
|
|
216
224
|
// will throw an error if the accountability does not have permission to update the item
|
|
217
225
|
if (this.accountability) {
|
|
218
226
|
await validateAccess({
|
|
@@ -231,11 +239,18 @@ export class VersionsService extends ItemsService {
|
|
|
231
239
|
reason: `Main item has changed since this version was last updated`,
|
|
232
240
|
});
|
|
233
241
|
}
|
|
234
|
-
|
|
235
|
-
|
|
242
|
+
let versionResult;
|
|
243
|
+
if (delta) {
|
|
244
|
+
versionResult = delta;
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
const saves = await this.getVersionSavesById(id);
|
|
248
|
+
versionResult = assign({}, ...saves);
|
|
249
|
+
}
|
|
236
250
|
const payloadToUpdate = fields ? pick(versionResult, fields) : versionResult;
|
|
237
251
|
const itemsService = new ItemsService(collection, {
|
|
238
252
|
accountability: this.accountability,
|
|
253
|
+
knex: this.knex,
|
|
239
254
|
schema: this.schema,
|
|
240
255
|
});
|
|
241
256
|
const payloadAfterHooks = await emitter.emitFilter(['items.promote', `${collection}.items.promote`], payloadToUpdate, {
|
|
@@ -243,7 +258,7 @@ export class VersionsService extends ItemsService {
|
|
|
243
258
|
item,
|
|
244
259
|
version,
|
|
245
260
|
}, {
|
|
246
|
-
database:
|
|
261
|
+
database: this.knex,
|
|
247
262
|
schema: this.schema,
|
|
248
263
|
accountability: this.accountability,
|
|
249
264
|
});
|
|
@@ -254,7 +269,7 @@ export class VersionsService extends ItemsService {
|
|
|
254
269
|
item: updatedItemKey,
|
|
255
270
|
version,
|
|
256
271
|
}, {
|
|
257
|
-
database:
|
|
272
|
+
database: this.knex,
|
|
258
273
|
schema: this.schema,
|
|
259
274
|
accountability: this.accountability,
|
|
260
275
|
});
|
package/dist/utils/apply-diff.js
CHANGED
|
@@ -26,7 +26,6 @@ export async function applyDiff(currentSnapshot, snapshotDiff, options) {
|
|
|
26
26
|
await transaction(database, async (trx) => {
|
|
27
27
|
const collectionsService = new CollectionsService({ knex: trx, schema });
|
|
28
28
|
const getNestedCollectionsToCreate = (currentLevelCollection) => snapshotDiff.collections.filter(({ diff }) => diff[0].rhs?.meta?.group === currentLevelCollection);
|
|
29
|
-
const getNestedCollectionsToDelete = (currentLevelCollection) => snapshotDiff.collections.filter(({ diff }) => diff[0].lhs?.meta?.group === currentLevelCollection);
|
|
30
29
|
const createCollections = async (collections) => {
|
|
31
30
|
for (const { collection, diff } of collections) {
|
|
32
31
|
if (diff?.[0]?.kind === DiffKind.NEW && diff[0].rhs) {
|
|
@@ -82,7 +81,6 @@ export async function applyDiff(currentSnapshot, snapshotDiff, options) {
|
|
|
82
81
|
// clean up deleted relations from existing schema
|
|
83
82
|
schema.relations = schema.relations.filter((r) => r.related_collection !== collection && r.collection !== collection);
|
|
84
83
|
}
|
|
85
|
-
await deleteCollections(getNestedCollectionsToDelete(collection));
|
|
86
84
|
try {
|
|
87
85
|
await collectionsService.deleteOne(collection, mutationOptions);
|
|
88
86
|
}
|
|
@@ -122,13 +120,14 @@ export async function applyDiff(currentSnapshot, snapshotDiff, options) {
|
|
|
122
120
|
// Create top level collections (no group, or highest level in existing group) first,
|
|
123
121
|
// then continue with nested collections recursively
|
|
124
122
|
await createCollections(snapshotDiff.collections.filter(filterCollectionsForCreation));
|
|
125
|
-
|
|
126
|
-
await deleteCollections(snapshotDiff.collections.filter(({ diff }) => {
|
|
123
|
+
const collectionsToDelete = snapshotDiff.collections.filter(({ diff }) => {
|
|
127
124
|
if (diff.length === 0 || diff[0] === undefined)
|
|
128
125
|
return false;
|
|
129
126
|
const collectionDiff = diff[0];
|
|
130
|
-
return collectionDiff.kind === DiffKind.DELETE
|
|
131
|
-
})
|
|
127
|
+
return collectionDiff.kind === DiffKind.DELETE;
|
|
128
|
+
});
|
|
129
|
+
if (collectionsToDelete.length > 0)
|
|
130
|
+
await deleteCollections(collectionsToDelete);
|
|
132
131
|
for (const { collection, diff } of snapshotDiff.collections) {
|
|
133
132
|
if (diff?.[0]?.kind === DiffKind.EDIT || diff?.[0]?.kind === DiffKind.ARRAY) {
|
|
134
133
|
const currentCollection = currentSnapshot.collections.find((field) => {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ForbiddenError } from '@directus/errors';
|
|
2
|
-
import { AccessService, ActivityService, DashboardsService, FilesService, FlowsService, FoldersService, ItemsService, NotificationsService, OperationsService, PanelsService, PermissionsService, PoliciesService, PresetsService, RevisionsService, RolesService, SettingsService, SharesService, TranslationsService, UsersService, VersionsService, WebhooksService, } from '../services/index.js';
|
|
2
|
+
import { AccessService, ActivityService, CommentsService, DashboardsService, FilesService, FlowsService, FoldersService, ItemsService, NotificationsService, OperationsService, PanelsService, PermissionsService, PoliciesService, PresetsService, RevisionsService, RolesService, SettingsService, SharesService, TranslationsService, UsersService, VersionsService, WebhooksService, } from '../services/index.js';
|
|
3
3
|
/**
|
|
4
4
|
* Select the correct service for the given collection. This allows the individual services to run
|
|
5
5
|
* their custom checks (f.e. it allows `UsersService` to prevent updating TFA secret from outside).
|
|
@@ -10,6 +10,8 @@ export function getService(collection, opts) {
|
|
|
10
10
|
return new AccessService(opts);
|
|
11
11
|
case 'directus_activity':
|
|
12
12
|
return new ActivityService(opts);
|
|
13
|
+
case 'directus_comments':
|
|
14
|
+
return new CommentsService({ ...opts, serviceOrigin: 'comments' });
|
|
13
15
|
case 'directus_dashboards':
|
|
14
16
|
return new DashboardsService(opts);
|
|
15
17
|
case 'directus_files':
|
|
@@ -16,7 +16,7 @@ export declare function sanitizeCollection(collection: Collection | undefined):
|
|
|
16
16
|
* @returns sanitized field
|
|
17
17
|
*/
|
|
18
18
|
export declare function sanitizeField(field: Field | undefined, sanitizeAllSchema?: boolean): Partial<Field> | undefined;
|
|
19
|
-
export declare function sanitizeColumn(column: Column): Pick<Column, "table" | "name" | "numeric_precision" | "data_type" | "default_value" | "max_length" | "numeric_scale" | "is_nullable" | "is_unique" | "is_primary_key" | "is_generated" | "generation_expression" | "has_auto_increment" | "foreign_key_table" | "foreign_key_column">;
|
|
19
|
+
export declare function sanitizeColumn(column: Column): Pick<Column, "table" | "name" | "numeric_precision" | "data_type" | "default_value" | "max_length" | "numeric_scale" | "is_nullable" | "is_unique" | "is_indexed" | "is_primary_key" | "is_generated" | "generation_expression" | "has_auto_increment" | "foreign_key_table" | "foreign_key_column">;
|
|
20
20
|
/**
|
|
21
21
|
* Pick certain database vendor specific relation properties that should be compared when performing diff
|
|
22
22
|
*
|
|
@@ -34,6 +34,7 @@ export function sanitizeField(field, sanitizeAllSchema = false) {
|
|
|
34
34
|
'schema.numeric_scale',
|
|
35
35
|
'schema.is_nullable',
|
|
36
36
|
'schema.is_unique',
|
|
37
|
+
'schema.is_indexed',
|
|
37
38
|
'schema.is_primary_key',
|
|
38
39
|
'schema.is_generated',
|
|
39
40
|
'schema.generation_expression',
|
|
@@ -54,6 +55,7 @@ export function sanitizeColumn(column) {
|
|
|
54
55
|
'numeric_scale',
|
|
55
56
|
'is_nullable',
|
|
56
57
|
'is_unique',
|
|
58
|
+
'is_indexed',
|
|
57
59
|
'is_primary_key',
|
|
58
60
|
'is_generated',
|
|
59
61
|
'generation_expression',
|