denoland / deno_kv_oauth

High-level OAuth 2.0 powered by Deno KV.
https://jsr.io/@deno/kv-oauth
MIT License
261 stars 27 forks source link

Deno.openKv with Subdirectory issue on open #288

Closed mcgear closed 10 months ago

mcgear commented 10 months ago

My rust skills are garbage, otherwise i would just submit a pr... That said, the issue that I am seeing is that when opening a new Deno KV instance in a subdirectory, if the directory does not already exist, it fails to open:

const denokv = await Deno.openKv('./denoKv/deno.db');

I get the following error:

TypeError: unable to open database file: ./denoKv/deno.db: Error code 14: Unable to open the database file
const kv = await Deno.openKv(path);
           ^
    at async Object.openKv (ext:deno_kv/01_db.ts:10:15)
    at async https://deno.land/x/deno_kv_oauth@v0.10.0/lib/_kv.ts:10:12

If the directory already exists for ./denoKv then everything works fine.

Would it be possible to update the code so that it creates any directories if they do not already exist? Before attempting to create the DB files.

The workaround is to create the ./denoKv directory with a .gitkeep file in it, but would be nice if it handles directory creation as well.

iuioiua commented 10 months ago

Thank you for the suggestion. Though, the idea is outside of the scope of this module. If you'd like to pursue having this functionality, I suggest opening an issue in the main runtime repository (https://github.com/denoland/deno).

jollytoad commented 10 months ago

@mcgear there is a function in the standard lib to ensure a directory exists... ensureDir

and if you need to determine the directory from the full path... dirname

import { ensureDir } from "https://deno.land/std@0.210.0/fs/ensure_dir.ts";
import { dirname } from "https://deno.land/std@0.210.0/path/dirname.ts";

const DB = './denoKv/deno.db';

await ensureDir(dirname(DB));
const denokv = await Deno.openKv(DB);
iuioiua commented 10 months ago

Great suggestion, @jollytoad!