-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
collections.ts
530 lines (433 loc) · 15.9 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
import SchemaInspector from '@directus/schema';
import { Knex } from 'knex';
import { getCache } from '../cache';
import { ALIAS_TYPES } from '../constants';
import getDatabase, { getSchemaInspector } from '../database';
import { systemCollectionRows } from '../database/system-data/collections';
import env from '../env';
import { ForbiddenException, InvalidPayloadException } from '../exceptions';
import logger from '../logger';
import { FieldsService, RawField } from '../services/fields';
import { ItemsService, MutationOptions } from '../services/items';
import Keyv from 'keyv';
import {
AbstractServiceOptions,
Accountability,
Collection,
CollectionMeta,
FieldMeta,
SchemaOverview,
} from '../types';
export type RawCollection = {
collection: string;
fields?: RawField[];
meta?: Partial<CollectionMeta>;
};
export class CollectionsService {
knex: Knex;
accountability: Accountability | null;
schemaInspector: ReturnType<typeof SchemaInspector>;
schema: SchemaOverview;
cache: Keyv<any> | null;
schemaCache: Keyv<any> | null;
constructor(options: AbstractServiceOptions) {
this.knex = options.knex || getDatabase();
this.accountability = options.accountability || null;
this.schemaInspector = options.knex ? SchemaInspector(options.knex) : getSchemaInspector();
this.schema = options.schema;
const { cache, schemaCache } = getCache();
this.cache = cache;
this.schemaCache = schemaCache;
}
/**
* Create a single new collection
*/
async createOne(payload: RawCollection, opts?: MutationOptions): Promise<string> {
if (this.accountability && this.accountability.admin !== true) {
throw new ForbiddenException();
}
if (!payload.collection) throw new InvalidPayloadException(`"collection" is required`);
// 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!,
};
}
return field;
});
// 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) => {
const fieldsService = new FieldsService({ knex: trx, schema: this.schema });
// This operation is locked to admin users only, so we don't have to worry about the order
// of operations here with regards to permissions checks
const collectionItemsService = new ItemsService('directus_collections', {
knex: trx,
accountability: this.accountability,
schema: this.schema,
});
const fieldItemsService = new ItemsService('directus_fields', {
knex: trx,
accountability: this.accountability,
schema: this.schema,
});
if (payload.collection.startsWith('directus_')) {
throw new InvalidPayloadException(`Collections can't start with "directus_"`);
}
if (payload.collection in this.schema.collections) {
throw new InvalidPayloadException(`Collection "${payload.collection}" already exists.`);
}
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);
}
}
});
await collectionItemsService.createOne({
...(payload.meta || {}),
collection: payload.collection,
});
const fieldPayloads = payload.fields!.filter((field) => field.meta).map((field) => field.meta) as FieldMeta[];
await fieldItemsService.createMany(fieldPayloads);
return payload.collection;
});
if (this.cache && env.CACHE_AUTO_PURGE && opts?.autoPurgeCache !== false) {
await this.cache.clear();
}
if (this.schemaCache) {
await this.schemaCache.clear();
}
return payload.collection;
}
/**
* Create multiple new collections
*/
async createMany(payloads: RawCollection[], opts?: MutationOptions): Promise<string[]> {
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 });
collectionNames.push(name);
}
return collectionNames;
});
if (this.cache && env.CACHE_AUTO_PURGE && opts?.autoPurgeCache !== false) {
await this.cache.clear();
}
if (this.schemaCache) {
await this.schemaCache.clear();
}
return collections;
}
/**
* Read all collections. Currently doesn't support any query.
*/
async readByQuery(): Promise<Collection[]> {
const collectionItemsService = new ItemsService('directus_collections', {
knex: this.knex,
schema: this.schema,
accountability: this.accountability,
});
let tablesInDatabase = await this.schemaInspector.tableInfo();
if (this.accountability && this.accountability.admin !== true) {
const collectionsYouHavePermissionToRead: string[] = this.schema.permissions
.filter((permission) => {
return permission.action === 'read';
})
.map(({ collection }) => collection);
tablesInDatabase = tablesInDatabase.filter((table) => {
return collectionsYouHavePermissionToRead.includes(table.name);
});
}
const tablesToFetchInfoFor = tablesInDatabase.map((table) => table.name);
const meta = (await collectionItemsService.readByQuery({
filter: { collection: { _in: tablesToFetchInfoFor } },
limit: -1,
})) as CollectionMeta[];
meta.push(...systemCollectionRows);
const collections: Collection[] = [];
for (const table of tablesInDatabase) {
const collection: Collection = {
collection: table.name,
meta: meta.find((systemInfo) => systemInfo?.collection === table.name) || null,
schema: table,
};
collections.push(collection);
}
return collections;
}
/**
* Get a single collection by name
*/
async readOne(collectionKey: string): Promise<Collection> {
const result = await this.readMany([collectionKey]);
return result[0];
}
/**
* Read many collections by name
*/
async readMany(collectionKeys: string[]): Promise<Collection[]> {
const collectionItemsService = new ItemsService('directus_collections', {
knex: this.knex,
accountability: this.accountability,
schema: this.schema,
});
if (this.accountability && this.accountability.admin !== true) {
const permissions = this.schema.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 ForbiddenException();
}
}
}
}
const tablesInDatabase = await this.schemaInspector.tableInfo();
const tables = tablesInDatabase.filter((table) => collectionKeys.includes(table.name));
const meta = (await collectionItemsService.readByQuery({
filter: { collection: { _in: collectionKeys } },
limit: -1,
})) as CollectionMeta[];
meta.push(...systemCollectionRows);
const collections: Collection[] = [];
for (const table of tables) {
const collection: Collection = {
collection: table.name,
meta: meta.find((systemInfo) => systemInfo?.collection === table.name) || null,
schema: table,
};
collections.push(collection);
}
return collections;
}
/**
* Update a single collection by name
*
* Note: only supports updating `meta`
*/
async updateOne(collectionKey: string, data: Partial<Collection>, opts?: MutationOptions): Promise<string> {
if (this.accountability && this.accountability.admin !== true) {
throw new ForbiddenException();
}
const collectionItemsService = new ItemsService('directus_collections', {
knex: this.knex,
accountability: this.accountability,
schema: this.schema,
});
const payload = data as Partial<Collection>;
if (!payload.meta) {
throw new InvalidPayloadException(`"meta" key is required`);
}
const exists = !!(await this.knex
.select('collection')
.from('directus_collections')
.where({ collection: collectionKey })
.first());
if (exists) {
await collectionItemsService.updateOne(collectionKey, payload.meta, opts);
} else {
await collectionItemsService.createOne({ ...payload.meta, collection: collectionKey }, opts);
}
return collectionKey;
}
/**
* Update multiple collections by name
*/
async updateMany(collectionKeys: string[], data: Partial<Collection>): Promise<string[]> {
if (this.accountability && this.accountability.admin !== true) {
throw new ForbiddenException();
}
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 });
}
});
return collectionKeys;
}
/**
* 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 ForbiddenException();
}
const tablesInDatabase = Object.keys(this.schema.collections);
if (tablesInDatabase.includes(collectionKey) === false) {
throw new ForbiddenException();
}
await this.knex.transaction(async (trx) => {
const collectionItemsService = new ItemsService('directus_collections', {
knex: trx,
accountability: this.accountability,
schema: this.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 keys = revisionsToDelete.map((record) => record.id);
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);
}
// Delete related m2o fields that point to current collection
if (relation.related_collection === collectionKey) {
await fieldsService.deleteField(relation.collection, relation.field);
}
const isM2O = relation.collection === collectionKey;
// Delete any fields that have a relationship to/from the current collection
if (isM2O && relation.related_collection && relation.meta?.one_field) {
await fieldsService.deleteField(relation.related_collection!, relation.meta.one_field);
} else {
await fieldsService.deleteField(relation.collection, relation.field);
}
}
const m2aRelationsThatIncludeThisCollection = this.schema.relations.filter((relation) => {
return relation.meta?.one_allowed_collections?.includes(collectionKey);
});
for (const relation of m2aRelationsThatIncludeThisCollection) {
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 });
}
await collectionItemsService.deleteOne(collectionKey);
await trx.schema.dropTable(collectionKey);
});
if (this.cache && env.CACHE_AUTO_PURGE && opts?.autoPurgeCache !== false) {
await this.cache.clear();
}
if (this.schemaCache) {
await this.schemaCache.clear();
}
return collectionKey;
}
/**
* Delete multiple collections by key
*/
async deleteMany(collectionKeys: string[], opts?: MutationOptions): Promise<string[]> {
if (this.accountability && this.accountability.admin !== true) {
throw new ForbiddenException();
}
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 });
}
});
if (this.cache && env.CACHE_AUTO_PURGE && opts?.autoPurgeCache !== false) {
await this.cache.clear();
}
if (this.schemaCache) {
await this.schemaCache.clear();
}
return collectionKeys;
}
/**
* @deprecated Use `createOne` or `createMany` instead
*/
create(data: RawCollection[]): Promise<string[]>;
create(data: RawCollection): Promise<string>;
async create(data: RawCollection | RawCollection[]): Promise<string | string[]> {
logger.warn(
'CollectionsService.create is deprecated and will be removed before v9.0.0. Use createOne or createMany instead.'
);
if (Array.isArray(data)) {
return await this.createMany(data);
} else {
return await this.createOne(data);
}
}
/**
* @deprecated Use `readOne` or `readMany` instead
*/
readByKey(collection: string[]): Promise<Collection[]>;
readByKey(collection: string): Promise<Collection>;
async readByKey(collection: string | string[]): Promise<Collection | Collection[]> {
logger.warn(
'CollectionsService.readByKey is deprecated and will be removed before v9.0.0. Use readOne or readMany instead.'
);
if (Array.isArray(collection)) return await this.readMany(collection);
return await this.readOne(collection);
}
/**
* @deprecated Use `updateOne` or `updateMany` instead
*/
update(data: Partial<Collection>, keys: string[]): Promise<string[]>;
update(data: Partial<Collection>, key: string): Promise<string>;
async update(data: Partial<Collection>, key: string | string[]): Promise<string | string[]> {
if (Array.isArray(key)) return await this.updateMany(key, data);
return await this.updateOne(key, data);
}
/**
* @deprecated Use `deleteOne` or `deleteMany` instead
*/
delete(collections: string[]): Promise<string[]>;
delete(collection: string): Promise<string>;
async delete(collection: string[] | string): Promise<string[] | string> {
logger.warn(
'CollectionsService.delete is deprecated and will be removed before v9.0.0. Use deleteOne or deleteMany instead.'
);
if (Array.isArray(collection)) return await this.deleteMany(collection);
return await this.deleteOne(collection);
}
}