You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/wasm-functions/using-key-value-store.md
+12-10Lines changed: 12 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -14,9 +14,9 @@ enable_shortcodes = true
14
14
-[Deploying to Fermyon Wasm Functions](#deploying-to-fermyon-wasm-functions)
15
15
-[Next Steps](#next-steps)
16
16
17
-
With Spin Key Value Store support in _Fermyon Wasm Functions_, you can persist non-relational data generated by your [Spin](https://spinframework.dev) application across application restarts and updates. _Fermyon Wasm Functions_ will provision and manage the key value store on your behalf, handling the heavy lifting for you. Spin Key Value Stores are isolated to a single application, and components cannot access the store unless explicitly granted permission. This capabilities-based security model ensures that only authorized components can interact with the data. To learn more about the Spin Key Value Store SDK, please visit the [API guide](https://spinframework.dev/v3/kv-store-api-guide).
17
+
With Spin key value store support in _Fermyon Wasm Functions_, you can persist non-relational data generated by your [Spin](https://spinframework.dev) application across application restarts and updates. _Fermyon Wasm Functions_ will provision and manage the key value store on your behalf, handling the heavy lifting for you. Spin key value stores are isolated to a single application, and components cannot access the store unless explicitly granted permission. This capabilities-based security model ensures that only authorized components can interact with the data. To learn more about the Spin key value store SDK, please visit the [API guide](https://spinframework.dev/v3/kv-store-api-guide).
18
18
19
-
As part of this tutorial, we will build a Spin application which leverages the Key Value Store provided by _Fermyon Wasm Functions_ for persistence. Once we have finished implementing the Spin app, we will run it locally using `spin up` for testing purposes, before we deploy it to _Fermyon Wasm Functions_ using the `spin aka deploy` command.
19
+
As part of this tutorial, we will build a Spin application which leverages the key value store provided by _Fermyon Wasm Functions_ for persistence. Once we have finished implementing the Spin app, we will run it locally using `spin up` for testing purposes, before we deploy it to _Fermyon Wasm Functions_ using the `spin aka deploy` command.
20
20
21
21
## About the Fermyon Wasm Functions Key Value Store
22
22
@@ -46,7 +46,9 @@ $ npm install
46
46
47
47
## Grant Key Value Store Permission
48
48
49
-
To enable a component in Spin to use a Key value Store, you need to [grant it the necessary permissions in the application's manifest](https://spinframework.dev/kv-store-api-guide#granting-key-value-store-permissions-to-components) (`spin.toml`) by adding a label. A label is a developer-defined string that links your component to an external resource. In this case, the resource is a Key Value Store. When deploying to _Fermyon Wasm Functions_, the label must be set to "default" to signal the platform to automatically provision a Key Value Store for your Spin application. While other labels besides "default" can be used for linking to existing resources in a local developer workflow, "default" is required for automatic key value provisioning on _Fermyon Wasm Functions_.
49
+
To enable a component in Spin to use a key-value store, you need to [grant it the necessary permissions in the application's manifest](https://spinframework.dev/kv-store-api-guide#granting-key-value-store-permissions-to-components) (`spin.toml`). To do this, add the label of the store to the component's `key_value_stores` in `spin.toml`.
50
+
51
+
In _Fermyon Wasm Functions_, the only label allowed is `"default"` which signals to the platform to automatically provision a Key Value store for your Spin application.
The `http-js` template generates a simple _Hello World_ example in `src/index.js`. As part of this section, we'll replace the existing code and use the HTTP router, provided by the template, to make our Spin application respond to the following HTTP request patterns:
61
63
62
-
-`GET /get/:key` for retrieving a value from Key Value Store using the key provided as `:key`
63
-
-`POST /set/:key` for storing a value provided as request payload using `:key` in Key Value Store
64
+
-`GET /get/:key` for retrieving a value from key value store using the key provided as `:key`
65
+
-`POST /set/:key` for storing a value provided as request payload using `:key` in key value store
64
66
65
67
Let's start by bringing necessary capabilities from the `@fermyon/spin-sdk` package into scope and laying out the routes:
Incoming `GET` requests at `/get/:key` will be handled by the `handleGetValue` function. As part of the function, we use the Key Value Store APIs provided by the Spin SDK for JavaScript (`import Kv from '@fermyon/spin-sdk'`):
90
+
Incoming `GET` requests at `/get/:key` will be handled by the `handleGetValue` function. As part of the function, we use the key value store APIs provided by the Spin SDK for JavaScript (`import Kv from '@fermyon/spin-sdk'`):
89
91
90
92
```JavaScript
91
93
functionhandleGetValue(key) {
@@ -98,19 +100,19 @@ function handleGetValue(key) {
98
100
returnnewResponse(null, { status:404 });
99
101
}
100
102
101
-
// load JSON data at key from Key Value Store
103
+
// load JSON data at key from key value store
102
104
let found =store.getJson(key);
103
105
104
106
// Return an HTTP 200 with corresponding HTTP header and payload
105
-
// if something was found at position key in the Key Value Store
107
+
// if something was found at position key in the key value store
Incoming `POST` requests at `/set/:key` will be handled by the `handleSetValue` function. After the request payload is validated, we use the Key Value Store APIs for persisting the data.
115
+
Incoming `POST` requests at `/set/:key` will be handled by the `handleSetValue` function. After the request payload is validated, we use the key value store APIs for persisting the data.
114
116
115
117
```JavaScript
116
118
functionhandleSetValue(key, requestBody) {
@@ -210,7 +212,7 @@ Move back to the first terminal instance and terminate your Spin application by
210
212
211
213
## Deploying to Fermyon Wasm Functions
212
214
213
-
_Fermyon Wasm Functions_ takes care of provisioning a Key Value Store for you. That said, all we have to do is deploying our Spin application using the `spin aka deploy` command:
215
+
_Fermyon Wasm Functions_ takes care of provisioning a key value store for you. That said, all we have to do is deploying our Spin application using the `spin aka deploy` command:
0 commit comments