@@ -42,6 +42,7 @@ import {
42
42
43
43
import { SimpleSavedObject } from './simple_saved_object' ;
44
44
import { HttpFetchOptions , HttpSetup } from '../http' ;
45
+ import { WorkspacesStart } from '../workspace' ;
45
46
46
47
type SavedObjectsFindOptions = Omit <
47
48
SavedObjectFindOptionsServer ,
@@ -61,6 +62,7 @@ export interface SavedObjectsCreateOptions {
61
62
/** {@inheritDoc SavedObjectsMigrationVersion } */
62
63
migrationVersion ?: SavedObjectsMigrationVersion ;
63
64
references ?: SavedObjectReference [ ] ;
65
+ workspaces ?: string [ ] ;
64
66
}
65
67
66
68
/**
@@ -183,6 +185,7 @@ const getObjectsToFetch = (queue: BatchQueueEntry[]): ObjectTypeAndId[] => {
183
185
export class SavedObjectsClient {
184
186
private http : HttpSetup ;
185
187
private batchQueue : BatchQueueEntry [ ] ;
188
+ private currentWorkspaceId ?: string ;
186
189
187
190
/**
188
191
* Throttled processing of get requests into bulk requests at 100ms interval
@@ -227,6 +230,15 @@ export class SavedObjectsClient {
227
230
this . batchQueue = [ ] ;
228
231
}
229
232
233
+ private async _getCurrentWorkspace ( ) : Promise < string | null > {
234
+ return this . currentWorkspaceId || null ;
235
+ }
236
+
237
+ public async setCurrentWorkspace ( workspaceId : string ) : Promise < boolean > {
238
+ this . currentWorkspaceId = workspaceId ;
239
+ return true ;
240
+ }
241
+
230
242
/**
231
243
* Persists an object
232
244
*
@@ -235,7 +247,7 @@ export class SavedObjectsClient {
235
247
* @param options
236
248
* @returns
237
249
*/
238
- public create = < T = unknown > (
250
+ public create = async < T = unknown > (
239
251
type : string ,
240
252
attributes : T ,
241
253
options : SavedObjectsCreateOptions = { }
@@ -248,6 +260,7 @@ export class SavedObjectsClient {
248
260
const query = {
249
261
overwrite : options . overwrite ,
250
262
} ;
263
+ const currentWorkspaceId = await this . _getCurrentWorkspace ( ) ;
251
264
252
265
const createRequest : Promise < SavedObject < T > > = this . savedObjectsFetch ( path , {
253
266
method : 'POST' ,
@@ -256,6 +269,11 @@ export class SavedObjectsClient {
256
269
attributes,
257
270
migrationVersion : options . migrationVersion ,
258
271
references : options . references ,
272
+ ...( options . workspaces || currentWorkspaceId
273
+ ? {
274
+ workspaces : options . workspaces || [ currentWorkspaceId ] ,
275
+ }
276
+ : { } ) ,
259
277
} ) ,
260
278
} ) ;
261
279
@@ -328,7 +346,7 @@ export class SavedObjectsClient {
328
346
* @property {object } [options.hasReference] - { type, id }
329
347
* @returns A find result with objects matching the specified search.
330
348
*/
331
- public find = < T = unknown > (
349
+ public find = async < T = unknown > (
332
350
options : SavedObjectsFindOptions
333
351
) : Promise < SavedObjectsFindResponsePublic < T > > => {
334
352
const path = this . getPath ( [ '_find' ] ) ;
@@ -345,9 +363,18 @@ export class SavedObjectsClient {
345
363
filter : 'filter' ,
346
364
namespaces : 'namespaces' ,
347
365
preference : 'preference' ,
366
+ workspaces : 'workspaces' ,
348
367
} ;
349
368
350
- const renamedQuery = renameKeys < SavedObjectsFindOptions , any > ( renameMap , options ) ;
369
+ const workspaces = [
370
+ ...( options . workspaces || [ await this . _getCurrentWorkspace ( ) ] ) ,
371
+ 'public' ,
372
+ ] . filter ( ( item ) => item ) ;
373
+
374
+ const renamedQuery = renameKeys < SavedObjectsFindOptions , any > ( renameMap , {
375
+ ...options ,
376
+ workspaces,
377
+ } ) ;
351
378
const query = pick . apply ( null , [ renamedQuery , ...Object . values < string > ( renameMap ) ] ) as Partial <
352
379
Record < string , any >
353
380
> ;
0 commit comments