-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
collections.ts
750 lines (621 loc) · 23.1 KB
/
collections.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
import { useEnv } from '@directus/env';
import { ForbiddenError, InvalidPayloadError } from '@directus/errors';
import type { SchemaInspector, Table } from '@directus/schema';
import { createInspector } from '@directus/schema';
import type { Accountability, FieldMeta, RawField, SchemaOverview } from '@directus/types';
import { addFieldFlag } from '@directus/utils';
import type Keyv from 'keyv';
import type { Knex } from 'knex';
import { chunk, groupBy, merge, omit } from 'lodash-es';
import { clearSystemCache, getCache } from '../cache.js';
import { ALIAS_TYPES } from '../constants.js';
import type { Helpers } from '../database/helpers/index.js';
import { getHelpers } from '../database/helpers/index.js';
import getDatabase, { getSchemaInspector } from '../database/index.js';
import { systemCollectionRows } from '../database/system-data/collections/index.js';
import emitter from '../emitter.js';
import type {
AbstractServiceOptions,
ActionEventParams,
Collection,
CollectionMeta,
MutationOptions,
} from '../types/index.js';
import { getSchema } from '../utils/get-schema.js';
import { shouldClearCache } from '../utils/should-clear-cache.js';
import { FieldsService } from './fields.js';
import { ItemsService } from './items.js';
export type RawCollection = {
collection: string;
fields?: RawField[];
schema?: Partial<Table> | null;
meta?: Partial<CollectionMeta> | null;
};
export class CollectionsService {
knex: Knex;
helpers: Helpers;
accountability: Accountability | null;
schemaInspector: SchemaInspector;
schema: SchemaOverview;
cache: Keyv<any> | null;
systemCache: Keyv<any>;
constructor(options: AbstractServiceOptions) {
this.knex = options.knex || getDatabase();
this.helpers = getHelpers(this.knex);
this.accountability = options.accountability || null;
this.schemaInspector = options.knex ? createInspector(options.knex) : getSchemaInspector();
this.schema = options.schema;
const { cache, systemCache } = getCache();
this.cache = cache;
this.systemCache = systemCache;
}
/**
* Create a single new collection
*/
async createOne(payload: RawCollection, opts?: MutationOptions): Promise<string> {
if (this.accountability && this.accountability.admin !== true) {
throw new ForbiddenError();
}
if (!payload.collection) throw new InvalidPayloadError({ reason: `"collection" is required` });
if (payload.collection.startsWith('directus_')) {
throw new InvalidPayloadError({ reason: `Collections can't start with "directus_"` });
}
const nestedActionEvents: ActionEventParams[] = [];
try {
const existingCollections: string[] = [
...((await this.knex.select('collection').from('directus_collections'))?.map(({ collection }) => collection) ??
[]),
...Object.keys(this.schema.collections),
];
if (existingCollections.includes(payload.collection)) {
throw new InvalidPayloadError({ reason: `Collection "${payload.collection}" already exists` });
}
// Create the collection/fields in a transaction so it'll be reverted in case of errors or
// permission problems. This might not work reliably in MySQL, as it doesn't support DDL in
// transactions.
await this.knex.transaction(async (trx) => {
if (payload.schema) {
// Directus heavily relies on the primary key of a collection, so we have to make sure that
// every collection that is created has a primary key. If no primary key field is created
// while making the collection, we default to an auto incremented id named `id`
if (!payload.fields) {
payload.fields = [
{
field: 'id',
type: 'integer',
meta: {
hidden: true,
interface: 'numeric',
readonly: true,
},
schema: {
is_primary_key: true,
has_auto_increment: true,
},
},
];
}
// Ensure that every field meta has the field/collection fields filled correctly
payload.fields = payload.fields.map((field) => {
if (field.meta) {
field.meta = {
...field.meta,
field: field.field,
collection: payload.collection!,
};
}
// Add flag for specific database type overrides
const flagToAdd = this.helpers.date.fieldFlagForField(field.type);
if (flagToAdd) {
addFieldFlag(field, flagToAdd);
}
return field;
});
const fieldsService = new FieldsService({ knex: trx, schema: this.schema });
await trx.schema.createTable(payload.collection, (table) => {
for (const field of payload.fields!) {
if (field.type && ALIAS_TYPES.includes(field.type) === false) {
fieldsService.addColumnToTable(table, field);
}
}
});
const fieldItemsService = new ItemsService('directus_fields', {
knex: trx,
accountability: this.accountability,
schema: this.schema,
});
const fieldPayloads = payload.fields!.filter((field) => field.meta).map((field) => field.meta) as FieldMeta[];
// Sort new fields that does not have any group defined, in ascending order.
// Lodash merge is used so that the "sort" can be overridden if defined.
let sortedFieldPayloads = fieldPayloads
.filter((field) => field?.group === undefined || field?.group === null)
.map((field, index) => merge({ sort: index + 1 }, field));
// Sort remaining new fields with group defined, if any, in ascending order.
// sortedFieldPayloads will be less than fieldPayloads if it filtered out any fields with group defined.
if (sortedFieldPayloads.length < fieldPayloads.length) {
const fieldsWithGroups = groupBy(
fieldPayloads.filter((field) => field?.group),
(field) => field?.group,
);
// The sort order is restarted from 1 for fields in each group and appended to sortedFieldPayloads.
// Lodash merge is used so that the "sort" can be overridden if defined.
for (const [_group, fields] of Object.entries(fieldsWithGroups)) {
sortedFieldPayloads = sortedFieldPayloads.concat(
fields.map((field, index) => merge({ sort: index + 1 }, field)),
);
}
}
await fieldItemsService.createMany(sortedFieldPayloads, {
bypassEmitAction: (params) =>
opts?.bypassEmitAction ? opts.bypassEmitAction(params) : nestedActionEvents.push(params),
bypassLimits: true,
});
}
if (payload.meta) {
const collectionItemsService = new ItemsService('directus_collections', {
knex: trx,
accountability: this.accountability,
schema: this.schema,
});
await collectionItemsService.createOne(
{
...payload.meta,
collection: payload.collection,
},
{
bypassEmitAction: (params) =>
opts?.bypassEmitAction ? opts.bypassEmitAction(params) : nestedActionEvents.push(params),
},
);
}
return payload.collection;
});
return payload.collection;
} finally {
if (shouldClearCache(this.cache, opts)) {
await this.cache.clear();
}
if (opts?.autoPurgeSystemCache !== false) {
await clearSystemCache({ autoPurgeCache: opts?.autoPurgeCache });
}
if (opts?.emitEvents !== false && nestedActionEvents.length > 0) {
const updatedSchema = await getSchema();
for (const nestedActionEvent of nestedActionEvents) {
nestedActionEvent.context.schema = updatedSchema;
emitter.emitAction(nestedActionEvent.event, nestedActionEvent.meta, nestedActionEvent.context);
}
}
}
}
/**
* Create multiple new collections
*/
async createMany(payloads: RawCollection[], opts?: MutationOptions): Promise<string[]> {
const nestedActionEvents: ActionEventParams[] = [];
try {
const collections = await this.knex.transaction(async (trx) => {
const service = new CollectionsService({
schema: this.schema,
accountability: this.accountability,
knex: trx,
});
const collectionNames: string[] = [];
for (const payload of payloads) {
const name = await service.createOne(payload, {
autoPurgeCache: false,
autoPurgeSystemCache: false,
bypassEmitAction: (params) => nestedActionEvents.push(params),
});
collectionNames.push(name);
}
return collectionNames;
});
return collections;
} finally {
if (shouldClearCache(this.cache, opts)) {
await this.cache.clear();
}
if (opts?.autoPurgeSystemCache !== false) {
await clearSystemCache({ autoPurgeCache: opts?.autoPurgeCache });
}
if (opts?.emitEvents !== false && nestedActionEvents.length > 0) {
const updatedSchema = await getSchema();
for (const nestedActionEvent of nestedActionEvents) {
nestedActionEvent.context.schema = updatedSchema;
emitter.emitAction(nestedActionEvent.event, nestedActionEvent.meta, nestedActionEvent.context);
}
}
}
}
/**
* Read all collections. Currently doesn't support any query.
*/
async readByQuery(): Promise<Collection[]> {
const env = useEnv();
const collectionItemsService = new ItemsService('directus_collections', {
knex: this.knex,
schema: this.schema,
accountability: this.accountability,
});
let tablesInDatabase = await this.schemaInspector.tableInfo();
let meta = (await collectionItemsService.readByQuery({
limit: -1,
})) as CollectionMeta[];
meta.push(...systemCollectionRows);
if (this.accountability && this.accountability.admin !== true) {
const collectionsGroups: { [key: string]: string } = meta.reduce(
(meta, item) => ({
...meta,
[item.collection]: item.group,
}),
{},
);
let collectionsYouHavePermissionToRead: string[] = this.accountability
.permissions!.filter((permission) => {
return permission.action === 'read';
})
.map(({ collection }) => collection);
for (const collection of collectionsYouHavePermissionToRead) {
const group = collectionsGroups[collection];
if (group) collectionsYouHavePermissionToRead.push(group);
delete collectionsGroups[collection];
}
collectionsYouHavePermissionToRead = [...new Set([...collectionsYouHavePermissionToRead])];
tablesInDatabase = tablesInDatabase.filter((table) => {
return collectionsYouHavePermissionToRead.includes(table.name);
});
meta = meta.filter((collectionMeta) => {
return collectionsYouHavePermissionToRead.includes(collectionMeta.collection);
});
}
const collections: Collection[] = [];
for (const collectionMeta of meta) {
const collection: Collection = {
collection: collectionMeta.collection,
meta: collectionMeta,
schema: tablesInDatabase.find((table) => table.name === collectionMeta.collection) ?? null,
};
collections.push(collection);
}
for (const table of tablesInDatabase) {
const exists = !!collections.find(({ collection }) => collection === table.name);
if (!exists) {
collections.push({
collection: table.name,
schema: table,
meta: null,
});
}
}
if (env['DB_EXCLUDE_TABLES']) {
return collections.filter(
(collection) => (env['DB_EXCLUDE_TABLES'] as string[]).includes(collection.collection) === false,
);
}
return collections;
}
/**
* Get a single collection by name
*/
async readOne(collectionKey: string): Promise<Collection> {
const result = await this.readMany([collectionKey]);
if (result.length === 0) throw new ForbiddenError();
return result[0]!;
}
/**
* Read many collections by name
*/
async readMany(collectionKeys: string[]): Promise<Collection[]> {
if (this.accountability && this.accountability.admin !== true) {
const permissions = this.accountability.permissions!.filter((permission) => {
return permission.action === 'read' && collectionKeys.includes(permission.collection);
});
if (collectionKeys.length !== permissions.length) {
const collectionsYouHavePermissionToRead = permissions.map(({ collection }) => collection);
for (const collectionKey of collectionKeys) {
if (collectionsYouHavePermissionToRead.includes(collectionKey) === false) {
throw new ForbiddenError();
}
}
}
}
const collections = await this.readByQuery();
return collections.filter(({ collection }) => collectionKeys.includes(collection));
}
/**
* Update a single collection by name
*/
async updateOne(collectionKey: string, data: Partial<Collection>, opts?: MutationOptions): Promise<string> {
if (this.accountability && this.accountability.admin !== true) {
throw new ForbiddenError();
}
const nestedActionEvents: ActionEventParams[] = [];
try {
const collectionItemsService = new ItemsService('directus_collections', {
knex: this.knex,
accountability: this.accountability,
schema: this.schema,
});
const payload = data as Partial<Collection>;
if (!payload.meta) {
return collectionKey;
}
const exists = !!(await this.knex
.select('collection')
.from('directus_collections')
.where({ collection: collectionKey })
.first());
if (exists) {
await collectionItemsService.updateOne(collectionKey, payload.meta, {
...opts,
bypassEmitAction: (params) =>
opts?.bypassEmitAction ? opts.bypassEmitAction(params) : nestedActionEvents.push(params),
});
} else {
await collectionItemsService.createOne(
{ ...payload.meta, collection: collectionKey },
{
...opts,
bypassEmitAction: (params) =>
opts?.bypassEmitAction ? opts.bypassEmitAction(params) : nestedActionEvents.push(params),
},
);
}
return collectionKey;
} finally {
if (shouldClearCache(this.cache, opts)) {
await this.cache.clear();
}
if (opts?.autoPurgeSystemCache !== false) {
await clearSystemCache({ autoPurgeCache: opts?.autoPurgeCache });
}
if (opts?.emitEvents !== false && nestedActionEvents.length > 0) {
const updatedSchema = await getSchema();
for (const nestedActionEvent of nestedActionEvents) {
nestedActionEvent.context.schema = updatedSchema;
emitter.emitAction(nestedActionEvent.event, nestedActionEvent.meta, nestedActionEvent.context);
}
}
}
}
/**
* Update multiple collections in a single transaction
*/
async updateBatch(data: Partial<Collection>[], opts?: MutationOptions): Promise<string[]> {
if (this.accountability && this.accountability.admin !== true) {
throw new ForbiddenError();
}
if (!Array.isArray(data)) {
throw new InvalidPayloadError({ reason: 'Input should be an array of collection changes' });
}
const collectionKey = 'collection';
const collectionKeys: string[] = [];
const nestedActionEvents: ActionEventParams[] = [];
try {
await this.knex.transaction(async (trx) => {
const collectionItemsService = new CollectionsService({
knex: trx,
accountability: this.accountability,
schema: this.schema,
});
for (const payload of data) {
if (!payload[collectionKey]) {
throw new InvalidPayloadError({ reason: `Collection in update misses collection key` });
}
await collectionItemsService.updateOne(payload[collectionKey], omit(payload, collectionKey), {
autoPurgeCache: false,
autoPurgeSystemCache: false,
bypassEmitAction: (params) =>
opts?.bypassEmitAction ? opts.bypassEmitAction(params) : nestedActionEvents.push(params),
});
collectionKeys.push(payload[collectionKey]);
}
});
} finally {
if (shouldClearCache(this.cache, opts)) {
await this.cache.clear();
}
if (opts?.autoPurgeSystemCache !== false) {
await clearSystemCache({ autoPurgeCache: opts?.autoPurgeCache });
}
if (opts?.emitEvents !== false && nestedActionEvents.length > 0) {
const updatedSchema = await getSchema();
for (const nestedActionEvent of nestedActionEvents) {
nestedActionEvent.context.schema = updatedSchema;
emitter.emitAction(nestedActionEvent.event, nestedActionEvent.meta, nestedActionEvent.context);
}
}
}
return collectionKeys;
}
/**
* Update multiple collections by name
*/
async updateMany(collectionKeys: string[], data: Partial<Collection>, opts?: MutationOptions): Promise<string[]> {
if (this.accountability && this.accountability.admin !== true) {
throw new ForbiddenError();
}
const nestedActionEvents: ActionEventParams[] = [];
try {
await this.knex.transaction(async (trx) => {
const service = new CollectionsService({
schema: this.schema,
accountability: this.accountability,
knex: trx,
});
for (const collectionKey of collectionKeys) {
await service.updateOne(collectionKey, data, {
autoPurgeCache: false,
autoPurgeSystemCache: false,
bypassEmitAction: (params) => nestedActionEvents.push(params),
});
}
});
return collectionKeys;
} finally {
if (shouldClearCache(this.cache, opts)) {
await this.cache.clear();
}
if (opts?.autoPurgeSystemCache !== false) {
await clearSystemCache({ autoPurgeCache: opts?.autoPurgeCache });
}
if (opts?.emitEvents !== false && nestedActionEvents.length > 0) {
const updatedSchema = await getSchema();
for (const nestedActionEvent of nestedActionEvents) {
nestedActionEvent.context.schema = updatedSchema;
emitter.emitAction(nestedActionEvent.event, nestedActionEvent.meta, nestedActionEvent.context);
}
}
}
}
/**
* Delete a single collection This will delete the table and all records within. It'll also
* delete any fields, presets, activity, revisions, and permissions relating to this collection
*/
async deleteOne(collectionKey: string, opts?: MutationOptions): Promise<string> {
if (this.accountability && this.accountability.admin !== true) {
throw new ForbiddenError();
}
const nestedActionEvents: ActionEventParams[] = [];
try {
const collections = await this.readByQuery();
const collectionToBeDeleted = collections.find((collection) => collection.collection === collectionKey);
if (!!collectionToBeDeleted === false) {
throw new ForbiddenError();
}
await this.knex.transaction(async (trx) => {
if (collectionToBeDeleted!.schema) {
await trx.schema.dropTable(collectionKey);
}
// Make sure this collection isn't used as a group in any other collections
await trx('directus_collections').update({ group: null }).where({ group: collectionKey });
if (collectionToBeDeleted!.meta) {
const collectionItemsService = new ItemsService('directus_collections', {
knex: trx,
accountability: this.accountability,
schema: this.schema,
});
await collectionItemsService.deleteOne(collectionKey, {
bypassEmitAction: (params) =>
opts?.bypassEmitAction ? opts.bypassEmitAction(params) : nestedActionEvents.push(params),
});
}
if (collectionToBeDeleted!.schema) {
const fieldsService = new FieldsService({
knex: trx,
accountability: this.accountability,
schema: this.schema,
});
await trx('directus_fields').delete().where('collection', '=', collectionKey);
await trx('directus_presets').delete().where('collection', '=', collectionKey);
const revisionsToDelete = await trx
.select('id')
.from('directus_revisions')
.where({ collection: collectionKey });
if (revisionsToDelete.length > 0) {
const chunks = chunk(
revisionsToDelete.map((record) => record.id),
10000,
);
for (const keys of chunks) {
await trx('directus_revisions').update({ parent: null }).whereIn('parent', keys);
}
}
await trx('directus_revisions').delete().where('collection', '=', collectionKey);
await trx('directus_activity').delete().where('collection', '=', collectionKey);
await trx('directus_permissions').delete().where('collection', '=', collectionKey);
await trx('directus_relations').delete().where({ many_collection: collectionKey });
const relations = this.schema.relations.filter((relation) => {
return relation.collection === collectionKey || relation.related_collection === collectionKey;
});
for (const relation of relations) {
// Delete related o2m fields that point to current collection
if (relation.related_collection && relation.meta?.one_field) {
await fieldsService.deleteField(relation.related_collection, relation.meta.one_field, {
autoPurgeCache: false,
autoPurgeSystemCache: false,
bypassEmitAction: (params) =>
opts?.bypassEmitAction ? opts.bypassEmitAction(params) : nestedActionEvents.push(params),
});
}
// Delete related m2o fields that point to current collection
if (relation.related_collection === collectionKey) {
await fieldsService.deleteField(relation.collection, relation.field, {
autoPurgeCache: false,
autoPurgeSystemCache: false,
bypassEmitAction: (params) =>
opts?.bypassEmitAction ? opts.bypassEmitAction(params) : nestedActionEvents.push(params),
});
}
}
const a2oRelationsThatIncludeThisCollection = this.schema.relations.filter((relation) => {
return relation.meta?.one_allowed_collections?.includes(collectionKey);
});
for (const relation of a2oRelationsThatIncludeThisCollection) {
const newAllowedCollections = relation
.meta!.one_allowed_collections!.filter((collection) => collectionKey !== collection)
.join(',');
await trx('directus_relations')
.update({ one_allowed_collections: newAllowedCollections })
.where({ id: relation.meta!.id });
}
}
});
return collectionKey;
} finally {
if (shouldClearCache(this.cache, opts)) {
await this.cache.clear();
}
if (opts?.autoPurgeSystemCache !== false) {
await clearSystemCache({ autoPurgeCache: opts?.autoPurgeCache });
}
if (opts?.emitEvents !== false && nestedActionEvents.length > 0) {
const updatedSchema = await getSchema();
for (const nestedActionEvent of nestedActionEvents) {
nestedActionEvent.context.schema = updatedSchema;
emitter.emitAction(nestedActionEvent.event, nestedActionEvent.meta, nestedActionEvent.context);
}
}
}
}
/**
* Delete multiple collections by key
*/
async deleteMany(collectionKeys: string[], opts?: MutationOptions): Promise<string[]> {
if (this.accountability && this.accountability.admin !== true) {
throw new ForbiddenError();
}
const nestedActionEvents: ActionEventParams[] = [];
try {
await this.knex.transaction(async (trx) => {
const service = new CollectionsService({
schema: this.schema,
accountability: this.accountability,
knex: trx,
});
for (const collectionKey of collectionKeys) {
await service.deleteOne(collectionKey, {
autoPurgeCache: false,
autoPurgeSystemCache: false,
bypassEmitAction: (params) => nestedActionEvents.push(params),
});
}
});
return collectionKeys;
} finally {
if (shouldClearCache(this.cache, opts)) {
await this.cache.clear();
}
if (opts?.autoPurgeSystemCache !== false) {
await clearSystemCache({ autoPurgeCache: opts?.autoPurgeCache });
}
if (opts?.emitEvents !== false && nestedActionEvents.length > 0) {
const updatedSchema = await getSchema();
for (const nestedActionEvent of nestedActionEvents) {
nestedActionEvent.context.schema = updatedSchema;
emitter.emitAction(nestedActionEvent.event, nestedActionEvent.meta, nestedActionEvent.context);
}
}
}
}
}