From 40ac0a2999492d28aaeafa2db0f82b8ff8657686 Mon Sep 17 00:00:00 2001 From: suzhou Date: Thu, 29 Jun 2023 15:46:56 +0800 Subject: [PATCH] Add some logic check when overwrite a saved object (#32) * feat: add some logic check when overwrite a saved object Signed-off-by: SuZhoue-Joe * fix: type check Signed-off-by: SuZhoue-Joe * feat: update Signed-off-by: SuZhoue-Joe --------- Signed-off-by: SuZhoue-Joe --- .../server/saved_objects/service/lib/repository.ts | 14 +++++++++++++- src/core/types/saved_objects.ts | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/core/server/saved_objects/service/lib/repository.ts b/src/core/server/saved_objects/service/lib/repository.ts index 4a8ceb5e0b3b..b992adc0f24e 100644 --- a/src/core/server/saved_objects/service/lib/repository.ts +++ b/src/core/server/saved_objects/service/lib/repository.ts @@ -280,6 +280,18 @@ export class SavedObjectsRepository { } } + let savedObjectWorkspaces; + + if (id && overwrite) { + // do not overwrite workspaces + const currentItem = await this.get(type, id); + if (currentItem && currentItem.workspaces) { + savedObjectWorkspaces = currentItem.workspaces; + } + } else { + savedObjectWorkspaces = workspaces; + } + const migrated = this._migrator.migrateDocument({ id, type, @@ -290,7 +302,7 @@ export class SavedObjectsRepository { migrationVersion, updated_at: time, ...(Array.isArray(references) && { references }), - ...(Array.isArray(workspaces) && { workspaces }), + ...(Array.isArray(savedObjectWorkspaces) && { workspaces: savedObjectWorkspaces }), }); const raw = this._serializer.savedObjectToRaw(migrated as SavedObjectSanitizedDoc); diff --git a/src/core/types/saved_objects.ts b/src/core/types/saved_objects.ts index 81e1ed029ddc..47faffb0b922 100644 --- a/src/core/types/saved_objects.ts +++ b/src/core/types/saved_objects.ts @@ -113,6 +113,7 @@ export interface SavedObject { * space. */ originId?: string; + workspaces?: string[]; } export interface SavedObjectError {