Olli
Olli
DDeno
Created by Cheat-Code Sam on 5/25/2024 in #help
Indexing Items by Last Updated Time in Deno KV
Since you knwo it will be only 1 entry I think that should be fine
13 replies
DDeno
Created by Cheat-Code Sam on 5/25/2024 in #help
Indexing Items by Last Updated Time in Deno KV
yeah
13 replies
DDeno
Created by Cheat-Code Sam on 5/25/2024 in #help
Indexing Items by Last Updated Time in Deno KV
I dont think my proposal is a valid solution after all
13 replies
DDeno
Created by Cheat-Code Sam on 5/25/2024 in #help
Indexing Items by Last Updated Time in Deno KV
yeah, actually that part makes sense. But when deleting them, I think there will be an issue since it might delete other thread entries with the same lastUpdated value?
13 replies
DDeno
Created by Cheat-Code Sam on 5/25/2024 in #help
Indexing Items by Last Updated Time in Deno KV
Just a heads up, the id and lastUpdated parts of the secondary index key should maybe be flipped?
13 replies
DDeno
Created by Cheat-Code Sam on 5/25/2024 in #help
Indexing Items by Last Updated Time in Deno KV
I see the issue. One way to go about it might be to delete the existing index entries and then setting the new one on every update, so that there is always only 1 copy of any given thread:
type Thread = {
lastUpdated: string
posts: unknown[]
}

const kv = await Deno.openKv()

async function createThread(id: string, thread: Thread) {
await kv.atomic()
.check({
key: ["threads", id],
versionstamp: null
})
.set(["threads", id], thread)
.set(["threads", "last_updated", thread.lastUpdated, id], thread)
.commit()
}

async function updateThread(id: string, thread: Thread) {
// Get the current thread value
const currentThread = await kv.get<Thread>(["threads", id])
if (!currentThread.value) {
// ...do something
return
}

// Set the new thread entry
const atomic = kv.atomic()
atomic.set(["threads", id], thread)

// Delete any existing index entries
const iter = kv.list({ prefix: ["threads", "last_updated", currentThread.value.lastUpdated] })
for await (const { key } of iter) {
atomic.delete(key)
}

// Set the new index entry
await atomic
.set(["threads", "last_updated", thread.lastUpdated, id], thread)
.commit()
}
type Thread = {
lastUpdated: string
posts: unknown[]
}

const kv = await Deno.openKv()

async function createThread(id: string, thread: Thread) {
await kv.atomic()
.check({
key: ["threads", id],
versionstamp: null
})
.set(["threads", id], thread)
.set(["threads", "last_updated", thread.lastUpdated, id], thread)
.commit()
}

async function updateThread(id: string, thread: Thread) {
// Get the current thread value
const currentThread = await kv.get<Thread>(["threads", id])
if (!currentThread.value) {
// ...do something
return
}

// Set the new thread entry
const atomic = kv.atomic()
atomic.set(["threads", id], thread)

// Delete any existing index entries
const iter = kv.list({ prefix: ["threads", "last_updated", currentThread.value.lastUpdated] })
for await (const { key } of iter) {
atomic.delete(key)
}

// Set the new index entry
await atomic
.set(["threads", "last_updated", thread.lastUpdated, id], thread)
.commit()
}
This might be a bit excessive, but I dont really see any way to use unique indexing when there is a timestamp and id involved.
13 replies