D
Deno

help

kv store key granuality

Ppihentagy10/26/2023
How cheap are the kv.get operations? Say I have a userprofile with 3 fields, should I create 3 keys for them, so that I can easily update th 3 fields, or should I keep number of keys low, and just have one value for the user (in that case when updating, one should first get the "complex" value, update it and then write back the updated value)
DAHDeno AI Helper10/26/2023
The documentation does not provide a specific answer to your question about the cost of kv.get operations or the granularity of keys in Deno's key-value store. However, it does provide some insights that might help you make a decision. Firstly, the documentation mentions that Deno KV is designed to be fast and easy to use, and it supports atomic operations that allow multiple modifications to take place at once. This could be beneficial if you decide to use separate keys for each field of a user profile, as you could update all fields in a single atomic operation. On the other hand, the documentation also mentions that keys in Deno KV are sequences of key parts, which allow modeling of hierarchical data. This could be useful if you decide to store all fields of a user profile under a single key, as you could structure the key in a way that represents the hierarchy of the data. In terms of performance, the documentation does not provide specific details. However, it does mention that all writes to the KV store are strongly consistent and immediately durably persisted, and reads are strongly consistent by default. Ultimately, the decision might depend on the specific requirements of your application, such as the frequency of updates to different fields, the complexity of the user profile data, and the need for atomicity of operations.
Eeric.uhd10/27/2023
You should use only one key per user like so
kv.set([“users”, “SEQ ID/ULID/UUID”], {
email,
password,
created_at,
});
kv.set([“users”, “SEQ ID/ULID/UUID”], {
email,
password,
created_at,
});
But you should also create some indexes for fast retrieval and search by value Like:
kv.atomic()
.set([“users”, “SEQ ID/ULID/UUID”], {
email,
password,
created_at,
})
.set([“users_by_email”], “SEQ ID/ULID/UUID”)
.commit()
kv.atomic()
.set([“users”, “SEQ ID/ULID/UUID”], {
email,
password,
created_at,
})
.set([“users_by_email”], “SEQ ID/ULID/UUID”)
.commit()
Ppihentagy10/27/2023
Ok, but in that case the upload logic would be a bit more complicated: gettint the whole user object, doing an updated user object, and setting it instead of just boom, addimg a new sub object I mean where there are multiple value to a user
Eeric.uhd10/28/2023
Your message is a bit confusing but I assume you want to be able to just send the minimum data possible over the wire (internet connection). Let’s say your user modified one of his attribute/field on his user account panel, you could just send this one field, yes obviously you can. Then on your backend, you GET the WHOLE user back, and modify the field before re-SET the WHOLE user in Deno KV. That’s really simple. It’s not so far from classic SQL logic. But yes in SQL you could do INSERT INTO users (the-single-attribute) VALUES ‘new value’ ___ Although you could envision to set each field of a user one by one, you would have to create a whole “system” hierarchy and DenoKv allows that
kv.atomic()
.set([“Users”,UserId,”email”], john.doe@gmail.com)
.set(…)
.set(…)
.commit()
kv.atomic()
.set([“Users”,UserId,”email”], john.doe@gmail.com)
.set(…)
.set(…)
.commit()
Then in case of retrieval you can get the whole user back like so:
let user = {};
const userFields = kv.list({ prefix: [“users”,UserId] })
for await (const field of userFields) {
user[field.key[2]] = field.value;
}
let user = {};
const userFields = kv.list({ prefix: [“users”,UserId] })
for await (const field of userFields) {
user[field.key[2]] = field.value;
}
Yes it works: https://dash.deno.com/playground/granular-kv

Looking for more? Join the community!

Recommended Posts
deno tap metadataHello. I am running deno tests with `deno test --reporter=tap`. I would like to add additional fieldUsing ESBuild with DenoHey, is anyone more familiar with ESBuild? I am currently trying to transpile my Deno TypeScript proMultiple subdomainsHow can I listen for incoming http request on multiple subdomains?Can't upgrade request to websocketI want to use a websocket in one of my middlewares. But I keep getting an error saying I can't upgrAccess `navigator.geolocation` in deno runtimeHello, is it possible to access `navigator.geolocation` in the deno runtime?Tracking Down Lost MemoryHi, I have a long running service that creates many `JsRuntime`s over time and I have found that I `deno run` ignores @deno-types?```ts Uncaught SyntaxError: The requested module 'pg' does not provide an export named 'Pool' importWhere is my Twitter access token with Deno KV Oauth?I signed in successfully and now the Deno KV database is just storing `true`. It's not enough to intHow do I import the stripe SDK into my fresh.js projectHi there! How do I import the stripe SDK into my fresh.js project? Im struggling with my deno.json fUnable to find NPM:Rollup 4.1.4 on M1 MacHi all, hooking up a new build process for UI project, trying to use Rollup on an M1 Mac. My build How to get custom folder structure with Fresh?Hello, I want to integrate Fresh into my existing project. I want to use the 4 directories: componenFresh error: Could not fetch latest versionTotal newbie here. I'm getting "Update check failed: Could not fetch latest version." when I make a VSCode Test Debugging```json { "name": "Test", "request": "launch", "type": "node", "cwd": "$Can you recommend a mysql driver for deno that isn't a complete dead end?mysql2 port: never updated. mysql driver: doesn't support strings mysql connector from Oracle: no on