Skip to content

Commit 03dbd0d

Browse files
committed
add documentation and examples
1 parent 914c5b4 commit 03dbd0d

File tree

4 files changed

+188
-5
lines changed

4 files changed

+188
-5
lines changed

docs/development/core/server/kibana-plugin-server.savedobjectsservicesetup.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,25 @@ When plugins access the Saved Objects client, a new client is created using the
2020

2121
## Example
2222

23+
24+
```ts
2325
import { SavedObjectsClient, CoreSetup } from 'src/core/server';
2426

25-
export class Plugin() { setup: (core: CoreSetup) =<!-- -->&gt; { core.savedObjects.setClientFactory((<!-- -->{ request: KibanaRequest }<!-- -->) =<!-- -->&gt; { return new SavedObjectsClient(core.savedObjects.scopedRepository(request)); }<!-- -->) } }
27+
export class Plugin() {
28+
setup: (core: CoreSetup) => {
29+
core.savedObjects.setClientFactory(({ request: KibanaRequest }) => {
30+
return new SavedObjectsClient(core.savedObjects.scopedRepository(request));
31+
})
32+
}
33+
}
34+
35+
```
2636

2737
## Properties
2838

2939
| Property | Type | Description |
3040
| --- | --- | --- |
3141
| [addClientWrapper](./kibana-plugin-server.savedobjectsservicesetup.addclientwrapper.md) | <code>(priority: number, id: string, factory: SavedObjectsClientWrapperFactory) =&gt; void</code> | Add a [client wrapper factory](./kibana-plugin-server.savedobjectsclientwrapperfactory.md) with the given priority. |
32-
| [registerMappings](./kibana-plugin-server.savedobjectsservicesetup.registermappings.md) | <code>(mappings: SavedObjectsTypeMappingDefinitions) =&gt; void</code> | TODO: doc + exemple |
42+
| [registerMappings](./kibana-plugin-server.savedobjectsservicesetup.registermappings.md) | <code>(mappings: SavedObjectsTypeMappingDefinitions) =&gt; void</code> | Register [saved object type mappings](./kibana-plugin-server.savedobjectstypemappingdefinitions.md) |
3343
| [setClientFactoryProvider](./kibana-plugin-server.savedobjectsservicesetup.setclientfactoryprovider.md) | <code>(clientFactoryProvider: SavedObjectsClientFactoryProvider) =&gt; void</code> | Set the default [factory provider](./kibana-plugin-server.savedobjectsclientfactoryprovider.md) for creating Saved Objects clients. Only one provider can be set, subsequent calls to this method will fail. |
3444

docs/development/core/server/kibana-plugin-server.savedobjectsservicesetup.registermappings.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,46 @@
44

55
## SavedObjectsServiceSetup.registerMappings property
66

7-
TODO: doc + exemple
7+
Register [saved object type mappings](./kibana-plugin-server.savedobjectstypemappingdefinitions.md)
88

99
<b>Signature:</b>
1010

1111
```typescript
1212
registerMappings: (mappings: SavedObjectsTypeMappingDefinitions) => void;
1313
```
14+
15+
## Remarks
16+
17+
It's possible to directly use an imported json mappings file to call this API. However, as typescript ensure type validation of the mappings, it's strongly encouraged to use a typescript definition instead.
18+
19+
## Example
20+
21+
22+
```ts
23+
// my-plugin/server/mappings.ts
24+
import { SavedObjectsTypeMappingDefinitions } from 'src/core/server';
25+
26+
export const mappings: SavedObjectsTypeMappingDefinitions = {
27+
'my-type': {
28+
properties: {
29+
afield: {
30+
type: "text"
31+
}
32+
}
33+
}
34+
}
35+
36+
```
37+
38+
```ts
39+
// my-plugin/server/plugin.ts
40+
import { mappings } from './mappings';
41+
42+
export class MyPlugin implements Plugin {
43+
setup({ savedObjects }) {
44+
savedObjects.registerMappings(mappings);
45+
}
46+
}
47+
48+
```
49+

src/core/MIGRATION.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
- [On the server side](#on-the-server-side)
5757
- [On the client side](#on-the-client-side)
5858
- [Updates an application navlink at runtime](#updates-an-app-navlink-at-runtime)
59+
- [Migrate my plugin's savedObjects definitions](#migrate-saved-object-definitions)
60+
- [Mappings](#migrate-saved-object-mappings)
5961

6062
Make no mistake, it is going to take a lot of work to move certain plugins to the new platform. Our target is to migrate the entire repo over to the new platform throughout 7.x and to remove the legacy plugin system no later than 8.0, and this is only possible if teams start on the effort now.
6163

@@ -1205,6 +1207,7 @@ In server code, `core` can be accessed from either `server.newPlatform` or `kbnS
12051207
| `request.getSavedObjectsClient` | [`context.core.savedObjects.client`](/docs/development/core/server/kibana-plugin-server.requesthandlercontext.core.md) | |
12061208
| `request.getUiSettingsService` | [`context.uiSettings.client`](/docs/development/core/server/kibana-plugin-server.iuisettingsclient.md) | |
12071209
| `kibana.Plugin.deprecations` | [Handle plugin configuration deprecations](#handle-plugin-config-deprecations) and [`PluginConfigDescriptor.deprecations`](docs/development/core/server/kibana-plugin-server.pluginconfigdescriptor.md) | Deprecations from New Platform are not applied to legacy configuration |
1210+
| `kibana.Plugin.mappings` | [Migrate my plugin's savedObjects definitions](#migrate-saved-object-definitions) and [`SavedObjectServices.registerMappings`](docs/development/core/server/kibana-plugin-server.savedobjectsservicesetup.registermappings.md) | Deprecations from New Platform are not applied to legacy configuration |
12081211

12091212
_See also: [Server's CoreSetup API Docs](/docs/development/core/server/kibana-plugin-server.coresetup.md)_
12101213
@@ -1654,4 +1657,105 @@ export class MyPlugin implements Plugin {
16541657
tooltip: 'Application disabled',
16551658
})
16561659
}
1660+
```
1661+
1662+
### Migrate my plugin's savedObjects definitions
1663+
1664+
Legacy plugins were using the `uiExports` in their plugin definition to define their saved object mappings, schema and migrations.
1665+
1666+
```js
1667+
import mappings from './mappings.json';
1668+
import { migrations } from './migrations';
1669+
1670+
new kibana.Plugin({
1671+
init(server){
1672+
// [...]
1673+
},
1674+
uiExports: {
1675+
mappings,
1676+
migrations,
1677+
savedObjectSchemas: {
1678+
'sample-data-telemetry': {
1679+
isNamespaceAgnostic: true,
1680+
},
1681+
'kql-telemetry': {
1682+
isNamespaceAgnostic: true,
1683+
},
1684+
},
1685+
},
1686+
})
1687+
```
1688+
1689+
In the new platform, all these registration are to be performed programmatically during your plugin's `setup` phase,
1690+
using the core `savedObjects` service APIs.
1691+
1692+
#### Mappings
1693+
1694+
For mapping, `savedObjects.registerMappings` should be used. It expects a mappings definition in the exact same
1695+
format they were in legacy.
1696+
1697+
```js
1698+
import mappings from './mappings.json';
1699+
1700+
new kibana.Plugin({
1701+
uiExports: {
1702+
mappings,
1703+
},
1704+
})
1705+
```
1706+
1707+
Would become:
1708+
1709+
```typescript
1710+
import mappings from './mappings.json';
1711+
1712+
export class MyPlugin implements Plugin {
1713+
setup({ savedObjects }) {
1714+
savedObjects.registerMappings(mappings as SavedObjectsTypeMappingDefinitions);
1715+
}
1716+
```
1717+
1718+
Note: in new platform, there are now proper typescript types for the saved objects mappings. It's strongly
1719+
advised to convert your json mapping file to typescript to ensure correct typings:
1720+
1721+
```json
1722+
// my-plugin/server/mappings.json
1723+
{
1724+
"my-type": {
1725+
"properties": {
1726+
"afield": {
1727+
"type": "text"
1728+
}
1729+
}
1730+
}
1731+
}
1732+
```
1733+
1734+
should be converted to a ts file
1735+
1736+
```typescript
1737+
// my-plugin/server/mappings.ts
1738+
import { SavedObjectsTypeMappingDefinitions } from 'src/core/server';
1739+
1740+
export const mappings: SavedObjectsTypeMappingDefinitions = {
1741+
'my-type': {
1742+
properties: {
1743+
afield: {
1744+
type: "text"
1745+
}
1746+
}
1747+
}
1748+
}
1749+
```
1750+
1751+
The usage would then become:
1752+
1753+
```typescript
1754+
import { mappings } from './mappings';
1755+
1756+
export class MyPlugin implements Plugin {
1757+
setup({ savedObjects }) {
1758+
savedObjects.registerMappings(mappings);
1759+
}
1760+
}
16571761
```

src/core/server/saved_objects/saved_objects_service.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ import { convertLegacyMappings } from './utils';
7272
* constructor.
7373
*
7474
* @example
75+
* ```ts
7576
* import { SavedObjectsClient, CoreSetup } from 'src/core/server';
7677
*
7778
* export class Plugin() {
@@ -81,6 +82,7 @@ import { convertLegacyMappings } from './utils';
8182
* })
8283
* }
8384
* }
85+
* ```
8486
*
8587
* @public
8688
*/
@@ -100,9 +102,40 @@ export interface SavedObjectsServiceSetup {
100102
factory: SavedObjectsClientWrapperFactory
101103
) => void;
102104

103-
// registerMapping: (type: string, mapping: SavedObjectsMapping) => void;
104105
/**
105-
* TODO: doc + exemple
106+
* Register {@link SavedObjectsTypeMappingDefinitions | saved object type mappings}
107+
*
108+
* @example
109+
*
110+
* ```ts
111+
* // my-plugin/server/mappings.ts
112+
* import { SavedObjectsTypeMappingDefinitions } from 'src/core/server';
113+
*
114+
* export const mappings: SavedObjectsTypeMappingDefinitions = {
115+
* 'my-type': {
116+
* properties: {
117+
* afield: {
118+
* type: "text"
119+
* }
120+
* }
121+
* }
122+
* }
123+
* ```
124+
*
125+
* ```ts
126+
* // my-plugin/server/plugin.ts
127+
* import { mappings } from './mappings';
128+
*
129+
* export class MyPlugin implements Plugin {
130+
* setup({ savedObjects }) {
131+
* savedObjects.registerMappings(mappings);
132+
* }
133+
* }
134+
* ```
135+
*
136+
* @remarks
137+
* It's possible to directly use an imported json mappings file to call this API. However, as typescript
138+
* ensure type validation of the mappings, it's strongly encouraged to use a typescript definition instead.
106139
*/
107140
registerMappings: (mappings: SavedObjectsTypeMappingDefinitions) => void;
108141
}

0 commit comments

Comments
 (0)