Skip to content

Commit

Permalink
feat: add data migration API
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvxd committed Dec 11, 2023
1 parent bf4eadd commit f987324
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 17 deletions.
34 changes: 19 additions & 15 deletions apps/docs/pages/docs/api-reference/data.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,31 @@ An object produced by Puck describing the shape of content.
}
```

## `content`
This document describes the latest supported data. To learn more about automatic data migration, visit the [Data Migration docs](/docs/integrating-puck/data-migration).

## Params

#### `content`

An array containing an object for each component in the main content region.

### `content[*]`
#### `content[*]`

#### Params
##### Params

| Param | Example | Type | Status |
| ------------------------------ | ---------------------------------- | ------- | -------- |
| [`type`](#contenttype) | `type: "HeadingBlock"` | "array" | Required |
| [`props`](#contentprops) | `props: { title: "Hello, world" }` | Object | Required |
| [`readOnly`](#contentreadonly) | `readOnly: { title: true }` | Object | - |

#### Required params
##### Required params

##### `content[*].type`
###### `content[*].type`

The type of the component, which tells Puck to run the [`render()`](/docs/api-reference/configuration/component-config#renderprops) method for the component of the [same key](/docs/api-reference/config#components).

##### `content[*].props`
###### `content[*].props`

The props stored based on the [`component config`](/docs/api-reference/configuration/component-config) that Puck will pass to the [`render()`](/docs/api-reference/configuration/component-config#renderprops) method for the component of the [same key](/docs/api-reference/config#components).

Expand All @@ -58,9 +62,9 @@ The props stored based on the [`component config`](/docs/api-reference/configura
}
```

#### Optional params
##### Optional params

##### `content[*].readOnly`
###### `content[*].readOnly`

An object describing which fields are set to [read-only](/docs/api-reference/configuration/component-config#datareadonly-1).

Expand All @@ -83,20 +87,20 @@ An object describing which fields are set to [read-only](/docs/api-reference/con
}
```

## `root`
### `root`

An object describing data for the [`root` config](/docs/api-reference/config#root).

### Params
#### Params

| Param | Example | Type | Status |
| --------------------------- | ---------------------------------- | ------ | ------ |
| [`props`](#rootprops) | `props: { title: "Hello, world" }` | Object | - |
| [`readOnly`](#rootreadonly) | `readOnly: { title: true }` | Object | - |

### Optional params
#### Optional params

#### `root.props`
##### `root.props`

The props stored based on the [`component config`](/docs/api-reference/configuration/component-config) that Puck will pass to the [`render()`](/docs/api-reference/configuration/component-config#renderprops) method for the [`root` config](/docs/api-reference/config#root).

Expand All @@ -108,7 +112,7 @@ The props stored based on the [`component config`](/docs/api-reference/configura
}
```

#### `root.readOnly`
##### `root.readOnly`

An object describing which fields are set to [read-only](/docs/api-reference/configuration/component-config#datareadonly-1).

Expand All @@ -124,11 +128,11 @@ An object describing which fields are set to [read-only](/docs/api-reference/con
}
```

## `zones`
### `zones`

An object describing nested content regions for each [DropZone](/docs/api-reference/components/drop-zone).

### `zones[zoneKey]`
#### `zones[zoneKey]`

An array describing the content for a particular region. Shares a shape with [`content`](#content).

Expand Down
1 change: 1 addition & 0 deletions apps/docs/pages/docs/api-reference/functions.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Functions

- [migrate](functions/migrate) - Migrate a legacy [data payload](/docs/api-reference/data) to the latest shape.
- [resolveAllData](functions/resolve-all-data) - Utility function to execute all [`resolveData` methods](/docs/api-reference/configuration/component-config#resolvedatadata-params) on a data payload.
- [transformProps](functions/transform-props) - Transform component props stored in the [data payload](/docs/api-reference/data). Use this for migrations, like prop renames.
21 changes: 21 additions & 0 deletions apps/docs/pages/docs/api-reference/functions/migrate.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: migrate
---

# migrate

Migrate the [Data payload](/docs/api-reference/data) to the latest shape, automatically transforming deprecated data.

```tsx copy showLineNumbers {7-10}
import { migrate } from "@measured/puck";

migrate(legacyData);
```

## Returns

The updated [Data](/docs/api-reference/data) object.

## Notes

- Called internally whenever you pass data to [`<Puck>`](/docs/api-reference/components/puck) or [`<Render>`](/docs/api-reference/components/render), or calling [`transformProps`](/docs/api-reference/functions/transform-props).
4 changes: 3 additions & 1 deletion apps/docs/pages/docs/integrating-puck/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
"categories": {},
"dynamic-props": {},
"external-data-sources": {},
"server-components": {}
"server-components": {},
"prop-migration": {},
"data-migration": {}
}
36 changes: 36 additions & 0 deletions apps/docs/pages/docs/integrating-puck/data-migration.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Data Migration

Puck automatically migrates legacy data payloads on render, transforming any deprecated properties to their latest counterparts as described by [Data](/docs/api-reference/data).

This data migration strategy reduces the number of breaking changes required.

Specifically, Puck will migrate the data when rendering [`<Puck>`](/docs/api-reference/components/puck) or [`<Render>`](/docs/api-reference/components/render), or calling [`transformProps`](/docs/api-reference/functions/transform-props).

## Types

Puck exports two types:

- `Data`, a backwards compatible type that supports all deprecated data shapes still supported.
- `CurrentData`, the latest data shape used by Puck internally.

You'll generally want to use `Data` to avoid breaking changes during minor version upgrades, but can use `CurrentData` to stay on the bleeding edge.

## Manually migrating data

If you manually want to update your data payload to the latest `CurrentData`, you can run the `migrate` method:

```tsx
import { migrate } from "@measured/puck";

migrate(legacyData);
```

## Opting out

There is currently no way to opt-out of automatic data migrations.

## Further reading

- [`Data` API reference](/docs/api-reference/data)
- [`migrate` API reference](/docs/api-reference/functions/migrate)
- [Prop migration](/docs/integrating-puck/prop-migration)
2 changes: 1 addition & 1 deletion packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ export * from "./components/Puck";
export * from "./components/Render";

export * from "./lib/resolve-all-data";
export { transformProps } from "./transforms";
export { migrate, transformProps } from "./transforms";

export { FieldLabel } from "./components/InputOrGroup";

0 comments on commit f987324

Please sign in to comment.