Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Util functions for working with multiple databases #1

Open
ooker777 opened this issue May 25, 2024 · 2 comments
Open

Util functions for working with multiple databases #1

ooker777 opened this issue May 25, 2024 · 2 comments

Comments

@ooker777
Copy link

ooker777 commented May 25, 2024

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?

@ooker777 ooker777 changed the title An util function for setting values for both local and cloud KV, and increasing the total count number Util functions for working with multiple databases May 25, 2024
@cknight
Copy link
Owner

cknight commented May 25, 2024

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

Other points:

  • Your kvGet() function doesn't return anything.
  • The kvSetValueAndCount isn't atomic. Let's say the count is 0 for all tables. You could have a situation where two requests both get the CountMap for the same TableName before updating it, both set-value-and-update-count, but now your table count is 1, but the table has two values on it.

@ooker777
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants