




















Since we’ve launched Deno KV, our globally-managed serverless database built right into the Deno runtime, developers love how simple it is to add state to their applications without configuration or provisioning a database:
const kv = await Deno.openKv();
In addition to
the ability to connect remotely to a managed Deno KV instance,
we’ve also published
a standalone open source denokv binary
for self-hosting - including
continuous backup to S3 or GCS with point-in-time recovery.
Today, we’re excited to bring Deno KV access to Node.js with our official Deno KV npm package. Now you can use Deno KV’s simple API to add a globally-distributed, ACID-supported database to your Node projects.
In this post, we’ll cover:
To install the package:
Once you’ve added the package to your Node project, you can import the openKv
function (supports both ESM import and CJS require-based usage):
import { openKv } from "@deno/kv"; const kv = await openKv(<KV Connect URL>); const key = [ "users", crypto.randomUUID() ]; const value = { name: "Alice" }; await kv.set(key, value); const result = await kv.get(key); console.log(result.value);
By default, the access token used for authentication comes from the
DENO_KV_ACCESS_TOKEN environment variable. You can also pass it explicitly via
options:
import { openKv } from "@deno/kv"; const kv = await openKv(<KV Connect URL>, { accessToken: myToken });
KV Connect URLs to Deno Deploy KV databases look like:
https://api.deno.com/databases/<database-id>/connectThe database-id for your project can be found in the
Deno Deploy dashboard, under the project’s KV
tab.
The Deno KV npm package allows you to connect to four kinds of databases using a single function:
const kv = await openKv( "https://api.deno.com/databases/<database-id>/connect", );
const kv = await openKv("http://localhost:4512");
const kv = await openKv("kv.db");
const kv = await openKv();
No matter which kind of database you choose, the returned kv instance has the
same methods for database operations — following the
KV API provided in Deno itself.
The get,
getMany,
set,
delete,
atomic,
enqueue, and
close methods
are supported, even the new watch method!
listenQueue
method is supported for in-memory and SQLite databases, but not for remote
databases (it’s not yet supported by the underlying protocol).Check out an annotated example of how to interact with the KV API.
Deno KV on Deno Deploy is a globally-distributed, highly-performant database (built on FoundationDB) that scales without any provisioning or orchestration.
If you don’t have an existing Deno Deploy project, you can create a new Deno KV database for free without needing to deploy a project:
openKv (see above)To remotely connect to your new Deno KV database, you’ll also need to create an
access token and set that as the DENO_KV_ACCESS_TOKEN environment variable:
Finally, set your new token as the environment variable DENO_KV_ACCESS_TOKEN
when running your Node project. Now you can connect remotely to a
globally-managed Deno KV instance!
@deno/kv is targeted to Node
18+, optimized for performance and compatibility with databases created by
Deno itself. It has no third-party npm package dependencies, and uses a single
platform-specific binary for SQLite access on the
four native architectures
supported by Deno itself. The architecture-appropriate native dependency is
selected and installed at npm install time using the standard
peer dependency mechanism.
Deno KV uses V8’s serialization format for storing values
in local and remote databases, available by default in Deno, and by default in
Node via the built-in v8 module. This
package will run on other Node-compatible runtimes, provided a compatible V8
serializer is available. See
Other runtimes for more
details.
The source code for this package is freely available, and utilizes:
denokv Rust
codebase via the excellent NAPI-RS frameworkThe release of the @deno/kv npm package means that you can now access Deno KV in your Node projects, whether that is the globally-managed Deno KV built on FoundationDB or a local one backed by SQLite. Building with Deno KV’s simple API makes it easy to add state to your projects. With Deno KV, along with other cloud primitives built into the Deno runtime such as Deno Queues and Deno Cron, creating a scalable, production-ready application is simpler than ever.
We just announced Deno Cron 🦕️🕒️, a zero config approach to adding and managing scheduled tasks, available in 1.38 or later and on Deno Deploy.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。