diff --git a/src/docs/src/KV/add.md b/src/docs/src/KV/add.md index f90e87fcf..f95414888 100644 --- a/src/docs/src/KV/add.md +++ b/src/docs/src/KV/add.md @@ -4,7 +4,7 @@ description: Add values to an existing key or nested path. platforms: [websites, apps, nodejs, workers] --- -Add values to an existing key. When you pass an object, each key is treated as a path and the value is added at that path. +Add values to an existing key. When you pass an array, its elements are appended to the array stored at the key. When you pass an object, each key is treated as a path and the value is added at that path. ## Syntax @@ -23,6 +23,8 @@ The key to add values to. The value to add to the key. Defaults to `1` when omitted. +An array is appended element by element, so wrap a single value in an array to append it as one element: `puter.kv.add('scores', [5])` appends `5`. + #### `pathAndValue` (Object) (optional) An object where each key is a dot-separated path (for example, `"profile.tags"`) and each value is the value (or values) to add at that path. @@ -35,6 +37,29 @@ Returns a `Promise` that resolves to the updated value stored at `key`. ## Examples +Append to an array stored at a key + +```html;kv-add-array + + + + + + +``` + Add values to an array inside an object ```html;kv-add diff --git a/src/docs/src/examples.js b/src/docs/src/examples.js index 7a86913ae..031a3b952 100644 --- a/src/docs/src/examples.js +++ b/src/docs/src/examples.js @@ -449,6 +449,12 @@ const examples = [ slug: 'kv-add', source: '/playground/examples/kv-add.html', }, + { + title: 'Add (Array value)', + description: 'Append values to an array stored at a key with Puter.js key-value API. Run and experiment with this example in the playground.', + slug: 'kv-add-array', + source: '/playground/examples/kv-add-array.html', + }, { title: 'Remove', description: 'Remove values by path with Puter.js key-value API. Run and modify this code example instantly in your browser.', diff --git a/src/docs/src/playground/examples/kv-add-array.html b/src/docs/src/playground/examples/kv-add-array.html new file mode 100644 index 000000000..0f5bc53d8 --- /dev/null +++ b/src/docs/src/playground/examples/kv-add-array.html @@ -0,0 +1,18 @@ + + + + + +