Skip to content

Commit

Permalink
refactor: rename Deno.openKv() to Deno.kv() (#18349)
Browse files Browse the repository at this point in the history
  • Loading branch information
ry authored Mar 22, 2023
1 parent e73e841 commit 50b793c
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 22 deletions.
14 changes: 6 additions & 8 deletions cli/tests/unit/kv_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ import {
} from "./test_util.ts";

Deno.test({
name: "openKv :memory: no permissions",
name: "kv :memory: no permissions",
permissions: {},
async fn() {
const db = await Deno.openKv(":memory:");
const db = await Deno.kv(":memory:");
await db.close();
},
});

Deno.test({
name: "openKv invalid filenames",
name: "kv invalid filenames",
permissions: {},
async fn() {
await assertRejects(
async () => await Deno.openKv(""),
async () => await Deno.kv(""),
TypeError,
"Filename cannot be empty",
);
await assertRejects(
async () => await Deno.openKv(":foo"),
async () => await Deno.kv(":foo"),
TypeError,
"Filename cannot start with ':' unless prefixed with './'",
);
Expand All @@ -37,9 +37,7 @@ function dbTest(name: string, fn: (db: Deno.Kv) => Promise<void>) {
Deno.test({
name,
async fn() {
const db: Deno.Kv = await Deno.openKv(
":memory:",
);
const db: Deno.Kv = await Deno.kv(":memory:");
try {
await fn(db);
} finally {
Expand Down
12 changes: 6 additions & 6 deletions cli/tsc/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1535,7 +1535,7 @@ declare namespace Deno {
* @tags allow-read, allow-write
* @category KV
*/
export function openKv(path?: string): Promise<Deno.Kv>;
export function kv(path?: string): Promise<Deno.Kv>;

/** **UNSTABLE**: New API, yet to be vetted.
*
Expand Down Expand Up @@ -1876,7 +1876,7 @@ declare namespace Deno {
* the returned entry will have a `null` value and versionstamp.
*
* ```ts
* const db = await Deno.openKv();
* const db = await Deno.kv();
* const result = await db.get(["foo"]);
* result.key; // ["foo"]
* result.value; // "bar"
Expand All @@ -1902,7 +1902,7 @@ declare namespace Deno {
* entry will have a `null` value and versionstamp.
*
* ```ts
* const db = await Deno.openKv();
* const db = await Deno.kv();
* const result = await db.getMany([["foo"], ["baz"]]);
* result[0].key; // ["foo"]
* result[0].value; // "bar"
Expand All @@ -1928,7 +1928,7 @@ declare namespace Deno {
* exists for the key, it will be overwritten.
*
* ```ts
* const db = await Deno.openKv();
* const db = await Deno.kv();
* await db.set(["foo"], "bar");
* ```
*/
Expand All @@ -1939,7 +1939,7 @@ declare namespace Deno {
* for the key, this operation is a no-op.
*
* ```ts
* const db = await Deno.openKv();
* const db = await Deno.kv();
* await db.delete(["foo"]);
* ```
*/
Expand Down Expand Up @@ -1971,7 +1971,7 @@ declare namespace Deno {
* not `["users", "noa"]` or `["users", "zoe"]`.
*
* ```ts
* const db = await Deno.openKv();
* const db = await Deno.kv();
* const entries = db.list({ prefix: ["users"] });
* for await (const entry of entries) {
* entry.key; // ["users", "alice"]
Expand Down
4 changes: 2 additions & 2 deletions ext/kv/01_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const encodeCursor: (
) => string = (selector, boundaryKey) =>
ops.op_kv_encode_cursor(selector, boundaryKey);

async function openKv(path: string) {
async function kv(path: string) {
const rid = await core.opAsync("op_kv_database_open", path);
return new Kv(rid);
}
Expand Down Expand Up @@ -466,4 +466,4 @@ class KvListIterator extends AsyncIterator
}
}

export { Kv, KvListIterator, KvU64, openKv };
export { Kv, kv, KvListIterator, KvU64 };
4 changes: 1 addition & 3 deletions ext/kv/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ where
{
let handler = {
let state = state.borrow();
state
.borrow::<UnstableChecker>()
.check_unstable("Deno.openKv");
state.borrow::<UnstableChecker>().check_unstable("Deno.kv");
state.borrow::<Rc<DBH>>().clone()
};
let db = handler.open(state.clone(), path).await?;
Expand Down
4 changes: 2 additions & 2 deletions ext/kv/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ impl<P: SqliteDbHandlerPermissions> DatabaseHandler for SqliteDbHandler<P> {
{
let mut state = state.borrow_mut();
let permissions = state.borrow_mut::<P>();
permissions.check_read(path, "Deno.openKv")?;
permissions.check_write(path, "Deno.openKv")?;
permissions.check_read(path, "Deno.kv")?;
permissions.check_write(path, "Deno.kv")?;
}
let flags = OpenFlags::default().difference(OpenFlags::SQLITE_OPEN_URI);
rusqlite::Connection::open_with_flags(path, flags)?
Expand Down
2 changes: 1 addition & 1 deletion runtime/js/90_deno_ns.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ const denoNsUnstable = {
funlockSync: fs.funlockSync,
upgradeHttp: http.upgradeHttp,
upgradeHttpRaw: flash.upgradeHttpRaw,
openKv: kv.openKv,
kv: kv.kv,
Kv: kv.Kv,
KvU64: kv.KvU64,
KvListIterator: kv.KvListIterator,
Expand Down

0 comments on commit 50b793c

Please sign in to comment.