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
+13-10Lines changed: 13 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -6,16 +6,17 @@ enable_shortcodes = true
6
6
---
7
7
8
8
-[About the Fermyon Wasm Functions Key Value Store](#about-the-fermyon-wasm-functions-key-value-store)
9
+
-[Key Value Limitations and Considerations](#key-value-limitations-and-considerations)
9
10
-[Creating a New Spin Application](#creating-a-new-spin-application)
10
11
-[Grant Key Value Store Permission](#grant-key-value-store-permission)
11
12
-[Implementing the Spin Application](#implementing-the-spin-application)
12
13
-[Testing the Spin Application](#testing-the-spin-application)
13
14
-[Deploying to Fermyon Wasm Functions](#deploying-to-fermyon-wasm-functions)
14
15
-[Next Steps](#next-steps)
15
16
16
-
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).
17
18
18
-
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.
19
20
20
21
## About the Fermyon Wasm Functions Key Value Store
21
22
@@ -45,7 +46,9 @@ $ npm install
45
46
46
47
## Grant Key Value Store Permission
47
48
48
-
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 key-value store label. This both grants the component access to the key value store and signals _Fermyon Wasm Functions_ (FWF) to provision one for the application. In this tutorial, we’ll use the label "default", but you can choose any label that works best for you.
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:
60
63
61
-
-`GET /get/:key` for retrieving a value from Key Value Store using the key provided as `:key`
62
-
-`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
63
66
64
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'`):
88
91
89
92
```JavaScript
90
93
functionhandleGetValue(key) {
@@ -97,19 +100,19 @@ function handleGetValue(key) {
97
100
returnnewResponse(null, { status:404 });
98
101
}
99
102
100
-
// load JSON data at key from Key Value Store
103
+
// load JSON data at key from key value store
101
104
let found =store.getJson(key);
102
105
103
106
// Return an HTTP 200 with corresponding HTTP header and payload
104
-
// 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.
113
116
114
117
```JavaScript
115
118
functionhandleSetValue(key, requestBody) {
@@ -209,7 +212,7 @@ Move back to the first terminal instance and terminate your Spin application by
209
212
210
213
## Deploying to Fermyon Wasm Functions
211
214
212
-
_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