cknight / kv-utils

Collection of utilities for working with Deno KV
MIT License
15 stars 0 forks source link

Util functions for working with multiple databases #1

Open ooker777 opened 1 month ago

ooker777 commented 1 month ago

Here is my current and proposed util function to work with both local and cloud KV databases:

const env = await load();
Deno.env.set("DENO_KV_ACCESS_TOKEN", env["DENO_KV_ACCESS_TOKEN"]);
const mapKV = new Map();
mapKV.set("Cloud", await Deno.openKv(env["LOCATION"]));
mapKV.set("Local", await Deno.openKv());

type KvName = "Cloud" | "Local"
type MapKV = Map<KvName, Deno.Kv>;
type TableName ="Table 1" | "Table 2" | "Table 3";
type CountMap = Map<TableName, number>;

export async function kvGet(key: Deno.KvKey, mapKV: MapKV) {
  const result = [] 
  for (const [_kvName, kv] of mapKV) {
    result.push(await kv.get(key));
  }
  return result 
}

export async function kvSet(key: Deno.KvKey, value: any, mapKV: MapKV) {
  for (const [_kvName, kv] of mapKV) {
    await kv.set(key, value);
  }
}

export async function kvDelete(key: Deno.KvKey, mapKV: MapKV) {
  for (const [_kvName, kv] of mapKV) {
    await kv.delete(key);
  }
}

export async function kvGetCount(tableName: TableName, kv: Deno.Kv) {
  const countMap = (await kv.get(["Counts of all tables"])).value as CountMap;
  return countMap.get(tableName) || 0;
}

export async function kvSetValueAndCount(key: Deno.KvKey, value: any, tableName: TableName, mapKV: MapKV) {
  for (const [_kvName, kv] of mapKV) {
    await kv.set(key, value);
    const countMap = (await kv.get(["Counts of all tables"])).value as CountMap;
    const currentCount = countMap.get(tableName) || 0;
    countMap.set(tableName, currentCount + 1);
    await kv.set(["Counts of all tables"], countMap);
  }
}

Do you think it's suitable for this repo?

cknight commented 1 month ago

I'm unclear on the purpose of setting values in both local and cloud at the same time?

Other points:

ooker777 commented 1 month ago

Maybe it's because I'm a noob and haven't figured out a better way for this. It's for testing purpose. Sometimes my data has a significant change, and I want to test in local before updating to the cloud. By making the mapKv map, I can turn on and off which database I want to push by simply commenting or uncommenting these lines:

mapKV.set("Cloud", await Deno.openKv(env["LOCATION"]));
mapKV.set("Local", await Deno.openKv());

Is that clearer?

Thanks for your other feedback. I've fixed the kvGet(). As for the kvSetValueAndCount, you are talking about a table of a database has duplicated updates from both database setting, right? I'm not sure why that is the case.