Skip to content

Commit ad080c3

Browse files
committed
Key-Value Databases in Taubyte
1 parent f9daa79 commit ad080c3

File tree

1 file changed

+4
-68
lines changed

1 file changed

+4
-68
lines changed

content/posts/taubyte-databases.md

Lines changed: 4 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ image:
1313
src: /blog/images/taubyte-databases.png
1414
alt: Key-Value Databases in Taubyte
1515
summary: Add structured data storage to your Taubyte applications with key-value databases. Like storage, databases are created on-the-fly when first used—enabling dynamic, multi-tenant data isolation without extra configuration.
16-
date: 2025-01-14T12:00:00Z
16+
date: 2026-01-14T14:00:00Z
1717
categories: [Hand-on Learning]
1818
---
1919

@@ -46,12 +46,12 @@ Just like storage, the matcher can be any string or regular expression. Using a
4646
Taubyte databases are **instantiated on-the-fly** the first time you use them. This enables powerful patterns:
4747

4848
**Static matcher**:
49-
```
49+
```bash
5050
/example/kv
5151
```
5252

5353
**Dynamic matcher (regex)**:
54-
```
54+
```bash
5555
profile/history/[a-zA-Z0-9]+
5656
```
5757

@@ -230,74 +230,10 @@ curl http://your-domain.blackhole.localtau:14529/api/kv?key=message
230230

231231
Output:
232232

233-
```
233+
```bash
234234
hello world!
235235
```
236236

237-
## Advanced: Per-User Databases
238-
239-
Here's how to implement isolated databases per user:
240-
241-
### Database Configuration
242-
243-
Create a database with a regex matcher:
244-
```
245-
user/data/.*
246-
```
247-
248-
### User-Scoped Functions
249-
250-
```go
251-
package lib
252-
253-
import (
254-
"encoding/json"
255-
"github.com/taubyte/go-sdk/database"
256-
"github.com/taubyte/go-sdk/event"
257-
)
258-
259-
type UserDataRequest struct {
260-
UserID string `json:"user_id"`
261-
Key string `json:"key"`
262-
Value string `json:"value"`
263-
}
264-
265-
//export userSet
266-
func userSet(e event.Event) uint32 {
267-
h, err := e.HTTP()
268-
if err != nil {
269-
return 1
270-
}
271-
272-
body := h.Body()
273-
defer body.Close()
274-
275-
var req UserDataRequest
276-
if err := json.NewDecoder(body).Decode(&req); err != nil {
277-
h.Write([]byte(`{"error": "invalid request"}`))
278-
return 1
279-
}
280-
281-
// Dynamic database path per user
282-
dbPath := "user/data/" + req.UserID
283-
284-
db, err := database.Open(dbPath)
285-
if err != nil {
286-
h.Write([]byte(`{"error": "failed to open database"}`))
287-
return 1
288-
}
289-
defer db.Close()
290-
291-
db.Put(req.Key, []byte(req.Value))
292-
h.Write([]byte(`{"status": "stored for user ` + req.UserID + `"}`))
293-
return 0
294-
}
295-
```
296-
297-
This creates **isolated databases per user**:
298-
- `user/data/alice` → Alice's database (created on first use)
299-
- `user/data/bob` → Bob's database (created on first use)
300-
301237
## Database Operations
302238

303239
| Operation | Method | Description |

0 commit comments

Comments
 (0)