Skip to content

Commit

Permalink
Update schema to support new field type: embedded_block
Browse files Browse the repository at this point in the history
  • Loading branch information
delphaber committed Nov 25, 2023
1 parent 86a69bc commit a4c710d
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
68 changes: 68 additions & 0 deletions packages/cma-client-node/__tests__/embeddedBlock.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { generateNewCmaClient } from '../../../jest-helpers/generateNewCmaClient';
import { buildBlockRecord, SchemaTypes } from '@datocms/cma-client';

describe('embedded block', () => {
it.concurrent('create, update', async () => {
const client = await generateNewCmaClient();

const articleItemType = await client.itemTypes.create({
name: 'Article',
api_key: 'article',
});

const contentItemType = await client.itemTypes.create({
name: 'Block',
api_key: 'block',
modular_block: true,
});

await client.fields.create(contentItemType.id, {
label: 'Text',
field_type: 'text',
api_key: 'text',
});

await client.fields.create(articleItemType.id, {
label: 'Content',
field_type: 'embedded_block',
api_key: 'content',
validators: {
embedded_block_block: { item_type: contentItemType.id },
required: {},
},
});

const item = await client.items.create({
item_type: articleItemType,
content: buildBlockRecord({
text: 'Foo',
item_type: contentItemType,
}),
});

expect(typeof item.content).toBe('string');

const itemWithNestedBlocks = await client.items.find(item.id, {
nested: true,
});

const block = itemWithNestedBlocks.content as SchemaTypes.Item;

await client.items.update(item.id, {
content: buildBlockRecord({
id: block.id,
text: `Updated ${block.attributes.text}`,
item_type: block.relationships.item_type.data,
}),
});

const updatedItemWithNestedBlocks = await client.items.find(item.id, {
nested: true,
});

const updatedBlock =
updatedItemWithNestedBlocks.content as SchemaTypes.Item;

expect(updatedBlock.attributes.text).toEqual('Updated Foo');
});
});
26 changes: 26 additions & 0 deletions packages/cma-client/src/generated/SchemaTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4387,6 +4387,17 @@ export type ItemTypeDestroyJobSchema = {
* </details>
*
* <details>
* <summary>Embedded Block (<code>embedded_block</code>)</summary>
*
* | Property | Value |
* | ------------------------------ | ---------------------------------- |
* | Code | `embedded_block` |
* | Built-in editors for the field | `embedded_block` |
* | Required validators | `embedded_block_block`, `required` |
*
* </details>
*
* <details>
* <summary>Color (<code>color</code>)</summary>
*
* | Property | Value |
Expand Down Expand Up @@ -4760,6 +4771,7 @@ export type ItemTypeDestroyJobSchema = {
* <summary><code>required</code></summary>
*
* Value must be specified or it won't be valid.
* This validator is required for fields of type `embedded_block`.
*
* </details>
*
Expand Down Expand Up @@ -4822,6 +4834,17 @@ export type ItemTypeDestroyJobSchema = {
* </details>
*
* <details>
* <summary><code>embedded_block_block</code></summary>
*
* Only accept reference to block record of the specified block model.
*
* | Parameter | Type | Required | Description |
* | ------------ | ----------------------- | -------- | ------------------------------ |
* | `item_type` | `Block Model ID` | ✅ | Allowed Block Model ID |
*
* </details>
*
* <details>
* <summary><code>rich_text_blocks</code></summary>
*
* Only accept references to block records of the specified block models.
Expand Down Expand Up @@ -5133,6 +5156,7 @@ export type FieldAttributes = {
| 'color'
| 'date'
| 'date_time'
| 'embedded_block'
| 'file'
| 'float'
| 'gallery'
Expand Down Expand Up @@ -5280,6 +5304,7 @@ export type FieldCreateSchema = {
| 'color'
| 'date'
| 'date_time'
| 'embedded_block'
| 'file'
| 'float'
| 'gallery'
Expand Down Expand Up @@ -5509,6 +5534,7 @@ export type FieldUpdateSchema = {
| 'color'
| 'date'
| 'date_time'
| 'embedded_block'
| 'file'
| 'float'
| 'gallery'
Expand Down
27 changes: 27 additions & 0 deletions packages/cma-client/src/generated/SimpleSchemaTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4912,6 +4912,17 @@ export type JobData = {
* </details>
*
* <details>
* <summary>Embedded Block (<code>embedded_block</code>)</summary>
*
* | Property | Value |
* | ------------------------------ | ---------------------------------- |
* | Code | `embedded_block` |
* | Built-in editors for the field | `embedded_block` |
* | Required validators | `embedded_block_block`, `required` |
*
* </details>
*
* <details>
* <summary>Color (<code>color</code>)</summary>
*
* | Property | Value |
Expand Down Expand Up @@ -5285,6 +5296,7 @@ export type JobData = {
* <summary><code>required</code></summary>
*
* Value must be specified or it won't be valid.
* This validator is required for fields of type `embedded_block`.
*
* </details>
*
Expand Down Expand Up @@ -5347,6 +5359,17 @@ export type JobData = {
* </details>
*
* <details>
* <summary><code>embedded_block_block</code></summary>
*
* Only accept reference to block record of the specified block model.
*
* | Parameter | Type | Required | Description |
* | ------------ | ----------------------- | -------- | ------------------------------ |
* | `item_type` | `Block Model ID` | ✅ | Allowed Block Model ID |
*
* </details>
*
* <details>
* <summary><code>rich_text_blocks</code></summary>
*
* Only accept references to block records of the specified block models.
Expand Down Expand Up @@ -5647,6 +5670,7 @@ export type Field = {
| 'color'
| 'date'
| 'date_time'
| 'embedded_block'
| 'file'
| 'float'
| 'gallery'
Expand Down Expand Up @@ -5774,6 +5798,7 @@ export type FieldAttributes = {
| 'color'
| 'date'
| 'date_time'
| 'embedded_block'
| 'file'
| 'float'
| 'gallery'
Expand Down Expand Up @@ -5906,6 +5931,7 @@ export type FieldCreateSchema = {
| 'color'
| 'date'
| 'date_time'
| 'embedded_block'
| 'file'
| 'float'
| 'gallery'
Expand Down Expand Up @@ -6104,6 +6130,7 @@ export type FieldUpdateSchema = {
| 'color'
| 'date'
| 'date_time'
| 'embedded_block'
| 'file'
| 'float'
| 'gallery'
Expand Down

0 comments on commit a4c710d

Please sign in to comment.