* Update a single collection by name
(collectionKey: string, payload: DeepPartial<Collection>, opts?: MutationOptions)
| 450 | * Update a single collection by name |
| 451 | */ |
| 452 | async updateOne(collectionKey: string, payload: DeepPartial<Collection>, opts?: MutationOptions): Promise<string> { |
| 453 | if (this.accountability && this.accountability.admin !== true) { |
| 454 | throw new ForbiddenError(); |
| 455 | } |
| 456 | |
| 457 | const nestedActionEvents: ActionEventParams[] = []; |
| 458 | |
| 459 | try { |
| 460 | const collectionsItemsService = new ItemsService('directus_collections', { |
| 461 | knex: this.knex, |
| 462 | accountability: this.accountability, |
| 463 | schema: this.schema, |
| 464 | }); |
| 465 | |
| 466 | if (!payload.meta) { |
| 467 | return collectionKey; |
| 468 | } |
| 469 | |
| 470 | if (payload.meta?.status === 'active') { |
| 471 | await getEntitlementManager().assert('collections', { adding: 1, knex: this.knex }); |
| 472 | } |
| 473 | |
| 474 | const exists = !!(await this.knex |
| 475 | .select('collection') |
| 476 | .from('directus_collections') |
| 477 | .where({ collection: collectionKey }) |
| 478 | .first()); |
| 479 | |
| 480 | if (exists) { |
| 481 | await collectionsItemsService.updateOne(collectionKey, payload.meta, { |
| 482 | ...opts, |
| 483 | bypassEmitAction: (params) => |
| 484 | opts?.bypassEmitAction ? opts.bypassEmitAction(params) : nestedActionEvents.push(params), |
| 485 | }); |
| 486 | } else { |
| 487 | await collectionsItemsService.createOne( |
| 488 | { ...payload.meta, collection: collectionKey }, |
| 489 | { |
| 490 | ...opts, |
| 491 | bypassEmitAction: (params) => |
| 492 | opts?.bypassEmitAction ? opts.bypassEmitAction(params) : nestedActionEvents.push(params), |
| 493 | }, |
| 494 | ); |
| 495 | } |
| 496 | |
| 497 | return collectionKey; |
| 498 | } finally { |
| 499 | if (shouldClearCache(this.cache, opts)) { |
| 500 | await this.cache.clear(); |
| 501 | } |
| 502 | |
| 503 | if (opts?.autoPurgeSystemCache !== false) { |
| 504 | await clearSystemCache({ autoPurgeCache: opts?.autoPurgeCache }); |
| 505 | await getEntitlementManager().clearCache('collections'); |
| 506 | } |
| 507 | |
| 508 | if (opts?.emitEvents !== false && nestedActionEvents.length > 0) { |
| 509 | const updatedSchema = await getSchema(); |
no test coverage detected