|
13 | 13 | src: /blog/images/taubyte-databases.png |
14 | 14 | alt: Key-Value Databases in Taubyte |
15 | 15 | 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 |
17 | 17 | categories: [Hand-on Learning] |
18 | 18 | --- |
19 | 19 |
|
@@ -46,12 +46,12 @@ Just like storage, the matcher can be any string or regular expression. Using a |
46 | 46 | Taubyte databases are **instantiated on-the-fly** the first time you use them. This enables powerful patterns: |
47 | 47 |
|
48 | 48 | **Static matcher**: |
49 | | -``` |
| 49 | +```bash |
50 | 50 | /example/kv |
51 | 51 | ``` |
52 | 52 |
|
53 | 53 | **Dynamic matcher (regex)**: |
54 | | -``` |
| 54 | +```bash |
55 | 55 | profile/history/[a-zA-Z0-9]+ |
56 | 56 | ``` |
57 | 57 |
|
@@ -230,74 +230,10 @@ curl http://your-domain.blackhole.localtau:14529/api/kv?key=message |
230 | 230 |
|
231 | 231 | Output: |
232 | 232 |
|
233 | | -``` |
| 233 | +```bash |
234 | 234 | hello world! |
235 | 235 | ``` |
236 | 236 |
|
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 | | - |
301 | 237 | ## Database Operations |
302 | 238 |
|
303 | 239 | | Operation | Method | Description | |
|
0 commit comments