D
Deno

help

Clear deno kv db

AAOC2/21/2024
How can I clear out the database? using list requires the keys to be known, but I just want to clear out the instances that were added by mistake
NNDH2/21/2024
// get a list of ALL keys in the KvDb
export async function getAllKeys() {
const allKeys = []
// the empty `prefix` below will return every key
const entries = db.list({ prefix: [] })
for await (const entry of entries) {
allKeys.push(entry.key)
}
return allKeys
}

// add known keys to this set
const knownSet = new Set()

// validate the key as known (wanted)
const known = (key) => knownSet.has(key);

/** delete all unknown rows from the db */
export async function clearUnknown() {
getAllKeys()
.then((keys) => {
keys.forEach( (key) => {
if(!known(key) db.delete(key);
})
})
}
// get a list of ALL keys in the KvDb
export async function getAllKeys() {
const allKeys = []
// the empty `prefix` below will return every key
const entries = db.list({ prefix: [] })
for await (const entry of entries) {
allKeys.push(entry.key)
}
return allKeys
}

// add known keys to this set
const knownSet = new Set()

// validate the key as known (wanted)
const known = (key) => knownSet.has(key);

/** delete all unknown rows from the db */
export async function clearUnknown() {
getAllKeys()
.then((keys) => {
keys.forEach( (key) => {
if(!known(key) db.delete(key);
})
})
}
You would first create an es6-Set of valid (known) keys.
You could then use the above clearUnknown method to filter your known keys to be kept.
This assumes you know the keys you want to keep.
You could retrieve the array of allKeys, and edit it to hold only known keys. Place that in knownSet
AAOC2/21/2024
i didn't know about db.list({ prefix: [] }). that's super useful. thanks!
NNDH2/21/2024
I use the above without the known test, to empty a kvdb. When this is used, its a good idea to close() the db to flush the write-ahead logs(WAL). another useful utility method Bulk-Transfer:
// utility to bulk transfer kv rows
export async function copyDB(from = 'data.db', to = 'copied.db') {

const fromDB = await Deno.openKv(from)
const destinationDB = (to.length > 0)
? await Deno.openKv(to)
: await Deno.openKv(); // if empty string was passed, use the default kvDb

const entries = fromDB.list({ prefix: [] })
for await (const entry of entries) {
await destinationDB.set(entry.key, entry.value);
}
fromDB.close()
destinationDB.close()
}
// utility to bulk transfer kv rows
export async function copyDB(from = 'data.db', to = 'copied.db') {

const fromDB = await Deno.openKv(from)
const destinationDB = (to.length > 0)
? await Deno.openKv(to)
: await Deno.openKv(); // if empty string was passed, use the default kvDb

const entries = fromDB.list({ prefix: [] })
for await (const entry of entries) {
await destinationDB.set(entry.key, entry.value);
}
fromDB.close()
destinationDB.close()
}

Looking for more? Join the community!

Recommended Posts
how to break console.log maximum characters limithi, im using usual `deno run file.ts` command and realized that there is a limit to number characternpm import : class is not found but it is well declared in node_moduleI try to use Tatum SDK in deno (https://github.com/tatumio/tatum-js). I pick up the easiest code exaInteractive Jupyter notebookHas anyone figured out how to do interactivity in a Jupyter notebook with Deno kernel in VSCode? LiRun all pending ticks synchronouslyCurrently it seems like `run_event_loop` is the only way to run all the pending ops that are schedulHow can I do server sent events on the client side in deno?I have a server api which is doing server sent events correctly, I can see it in curl doing what I wunable to connect to mongodb using deno inside containerFirst of all, I want to mention that when I run `deno task preview` on my local machine / laptop, itTC39 stage 3 decorators not working anymore in Deno DeployAs of Feb 15th, deployments to #deploy™ fail when using TC39 decorators. Despite the decision to suUse Deno permissions in your own CLIIf I create a CLI with Deno, can I somehow reuse or pass on the permissions? ```bash > mycli --alloHas someone worked on a win32/x11/mac native window creation using FFI?Using only operating system native libraries? No SDL, etc.How to unset white background of SVG in Jupyter notebook?I’ve got a Jupyter notebook with Deno kernel in VSCode that displays an SVG. The SVG has a forced whHow to get the Docker ARM image for DenoI keep getting this error when trying to deploy my code: ``` WARNING: The requested image's platfoDeno compiled bin not create fileHi, I have a script that create file with Deno.writeFileSync() on current location When run script