d1-level
TypeScript icon, indicating that this package has built-in type declarations

2.3.1 • Public • Published

d1-level

An abstract-level database backed by Cloudflare D1.

✅ The implementation passes all 1000+ tests of abstract-level/test test suite.

Usage

This is how you would use it inside a Cloudflare worker:

import { D1Level } from "d1-level";

export interface Env {
  D1: D1Database;
}

export default {
  async fetch(request, env) {
    const level = new D1Level(env.D1);
    await level.open();
    const value = await level.get("value");

    return new Response(`Hello ${value}!`);
  },
} satisfies ExportedHandler<Env>;

Remote D1 support

The package also supports remote databases via the D1 REST API:

import { RemoteD1Level } from "d1-level/remote";

const level = new RemoteD1Level();

By default the D1_ACCOUNT_ID, D1_DATABASE_ID and D1_API_TOKEN environment variables are used. You can also pass these options to the constructor:

const level = new RemoteD1Level({
  accountId: "1234",
  databaseId: "5678",
  apiToken: "xxxx",
});

You can also leverage import conditions to write isomorphic code that uses the REST API when running in Node and the regular API otherwise:

import { D1Level } from "d1-level/conditional";

// D1Level will be RemoteD1Level when imported in Node and regular D1Level in all other environments.

License

MIT

Package Sidebar

Install

npm i d1-level

Weekly Downloads

4

Version

2.3.1

License

MIT

Unpacked Size

13.9 kB

Total Files

8

Last publish

Collaborators

  • fgnass