Skip to content

Commit 54a74b0

Browse files
authored
docs: document JSON properties (medusajs#13099)
1 parent 1c1e1c6 commit 54a74b0

File tree

14 files changed

+1072
-17
lines changed

14 files changed

+1072
-17
lines changed

www/apps/api-reference/generated/generated-admin-sidebar.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ const generatedgeneratedAdminSidebarSidebar = {
2020
"path": "http-compression",
2121
"loaded": true
2222
},
23+
{
24+
"type": "link",
25+
"title": "Manage Metadata",
26+
"path": "manage-metadata",
27+
"loaded": true
28+
},
2329
{
2430
"type": "link",
2531
"title": "Select Fields and Relations",

www/apps/api-reference/generated/generated-store-sidebar.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ const generatedgeneratedStoreSidebarSidebar = {
2626
"path": "http-compression",
2727
"loaded": true
2828
},
29+
{
30+
"type": "link",
31+
"title": "Manage Metadata",
32+
"path": "manage-metadata",
33+
"loaded": true
34+
},
2935
{
3036
"type": "link",
3137
"title": "Select Fields and Relations",

www/apps/api-reference/markdown/admin.mdx

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,226 @@ x-no-compression: false
483483

484484
<DividedMarkdownContent>
485485

486+
## Manage Metadata
487+
488+
Many data models in Medusa, such as products and carts, have a `metadata` field that allows you to store custom information in key-value pairs.
489+
490+
When setting or updating the `metadata` field using the relevant API routes, Medusa will merge the new metadata with the existing metadata.
491+
492+
<Note>
493+
494+
The instructions in this section apply to any [JSON property in a data model](!docs!/learn/fundamentals/data-models/json-properties).
495+
496+
</Note>
497+
498+
</DividedMarkdownContent>
499+
500+
</DividedMarkdownLayout>
501+
502+
<DividedMarkdownLayout addYSpacing>
503+
504+
<DividedMarkdownContent>
505+
506+
### Accepted Values in Metadata
507+
508+
The `metadata` is an object of key-value pairs, where the keys are strings and the values can be one of the following types:
509+
510+
- String
511+
- An empty string deletes the property from the metadata.
512+
- Number
513+
- Boolean
514+
- Date
515+
- Object
516+
- Arrays of any of the above types
517+
518+
The `metadata` is not validated, so you can store any custom data in it.
519+
520+
</DividedMarkdownContent>
521+
522+
<DividedMarkdownCode>
523+
524+
```ts title="Metadata Example"
525+
{
526+
"metadata": {
527+
"category": "electronics",
528+
"views": 1500,
529+
"is_featured": true,
530+
"tags": ["new", "sale"],
531+
"details": {
532+
"warranty": "2 years",
533+
"origin": "USA"
534+
}
535+
}
536+
}
537+
```
538+
539+
</DividedMarkdownCode>
540+
541+
</DividedMarkdownLayout>
542+
543+
<DividedMarkdownLayout addYSpacing>
544+
545+
<DividedMarkdownContent>
546+
547+
### Add or Update New Property in Metadata
548+
549+
To add or update a property in the `metadata`, pass the property in the request body as a key-value pair. This won't affect existing properties in the metadata.
550+
551+
</DividedMarkdownContent>
552+
553+
<DividedMarkdownCode>
554+
555+
<CodeTabs group="request-with-result">
556+
557+
<CodeTab label="JS SDK" value="js-sdk">
558+
559+
```js title="Add new metadata property"
560+
sdk.admin.product.update("prod_123", {
561+
metadata: {
562+
new_property: "value"
563+
}
564+
})
565+
```
566+
567+
</CodeTab>
568+
569+
<CodeTab label="Result" value="result">
570+
571+
```json title="Result"
572+
{
573+
"id": "prod_123",
574+
"metadata": {
575+
"new_property": "value",
576+
"old_property": "value"
577+
}
578+
}
579+
```
580+
581+
</CodeTab>
582+
583+
</CodeTabs>
584+
585+
</DividedMarkdownCode>
586+
587+
</DividedMarkdownLayout>
588+
589+
<DividedMarkdownLayout addYSpacing>
590+
591+
<DividedMarkdownContent>
592+
593+
### Update Nested Objects in Metadata
594+
595+
When updating a nested object in the `metadata`, you must pass the entire object in the request body.
596+
597+
Medusa doesn't merge nested objects, so if you pass a partial object, the existing properties in the nested object will be removed.
598+
599+
</DividedMarkdownContent>
600+
601+
<DividedMarkdownCode>
602+
603+
<CodeTabs group="request-with-result">
604+
605+
<CodeTab label="JS SDK" value="js-sdk">
606+
607+
```js title="Update nested object in metadata"
608+
sdk.admin.product.update("prod_123", {
609+
metadata: {
610+
nested_object: {
611+
property1: "value1",
612+
property2: "value2"
613+
}
614+
}
615+
})
616+
```
617+
618+
</CodeTab>
619+
620+
<CodeTab label="Result" value="result">
621+
622+
```json title="Result"
623+
{
624+
"id": "prod_123",
625+
"metadata": {
626+
"nested_object": {
627+
"property1": "value1",
628+
"property2": "value2"
629+
}
630+
}
631+
}
632+
```
633+
634+
</CodeTab>
635+
636+
</CodeTabs>
637+
638+
</DividedMarkdownCode>
639+
640+
</DividedMarkdownLayout>
641+
642+
<DividedMarkdownLayout addYSpacing>
643+
644+
<DividedMarkdownContent>
645+
646+
### Remove Property from Metadata
647+
648+
To remove a property from the `metadata`, pass the property in the request body with an empty string value. This will remove the property from the `metadata` without affecting other properties.
649+
650+
<Feedback
651+
event="survey_api-ref"
652+
extraData={{
653+
area: "store",
654+
section: "metadata"
655+
}}
656+
pathName="/api/store"
657+
question="Was this section helpful?"
658+
vertical={true}
659+
/>
660+
661+
</DividedMarkdownContent>
662+
663+
<DividedMarkdownCode>
664+
665+
<CodeTabs group="request-with-result">
666+
667+
<CodeTab label="JS SDK" value="js-sdk">
668+
669+
```js title="Remove metadata property"
670+
sdk.admin.product.update("prod_123", {
671+
metadata: {
672+
property_to_remove: ""
673+
}
674+
})
675+
```
676+
677+
</CodeTab>
678+
679+
<CodeTab label="Result" value="result">
680+
681+
```json title="Result"
682+
{
683+
"id": "prod_123",
684+
"metadata": {
685+
"other_property": "value"
686+
}
687+
}
688+
```
689+
690+
</CodeTab>
691+
692+
</CodeTabs>
693+
694+
</DividedMarkdownCode>
695+
696+
</DividedMarkdownLayout>
697+
698+
</SectionContainer>
699+
700+
<SectionContainer noTopPadding={true}>
701+
702+
<DividedMarkdownLayout>
703+
704+
<DividedMarkdownContent>
705+
486706
## Select Fields and Relations
487707

488708
</DividedMarkdownContent>

0 commit comments

Comments
 (0)