npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

d1-api

v0.1.0

Published

Use the Cloudflare D1 API outside Workers

Downloads

22

Readme

d1-api

npm package Downloads Issues

d1-api is a package for interacting with Cloudflare D1 outside of Cloudflare Workers, and tries to match the D1 client API used in Workers.

It uses the D1 HTTP API to run queries on your SQLite database. The D1 HTTP API has significate overhead which results in about half a second of latency on every query. This is intended and is not planned to be fixed by Cloudflare.

fetch is required (available in Node 18+ and other runtimes). This is an ESM-only module.

Install

npm install d1-api
pnpm install d1-api
yarn add d1-api

TypeScript typings are included with the package.

Usage

Remove type annotations (<{ id: number}>) to translate to JavaScript.

import D1API from 'd1-api';

const d1api = new D1API({
  accountId: 'account-id',
  apiKey: 'api-key',
  databaseId: 'database-id',
});

// TypeScript is fully supported.
type User = { id: number };

// Return results of all rows. Tagged template is used to prepare statements.
const response = await d1api.all<User>`SELECT * FROM users WHERE id = ${1}`;
// {
//   success: true,
//   // Row data here.
//   results: [
//     {
//       id: 1
//     }
//   ],
//   // Metadata such as duration, rows read/written, changed, etc.
//   meta: { ... }
// }

// Return first row. Returns null if no result.
// Tagged template is used to prepare statements.
const response = await d1api.first<User>`SELECT * FROM users WHERE id = ${1}`;
// {
//   id: 1
// }

// Execute a query. Tagged template is used to prepare statements.
const response =
  await d1api.exec<User>`UPDATE users SET id = ${1} WHERE id = ${2} RETURNING *`;
// {
//   success: true,
//   result: [
//     {
//       success: true,
//       // Row data here. Can be empty if non-SELECT statement
//       results: [
//         {
//           id: 1
//         }
//       ],
//       // Metadata such as duration, rows read/written, changed, etc.
//       meta: { ... }
//     }
//   ],
//   // Query errors. A D1Error is thrown if any errors are present.
//   errors: [],
//   // Messages/warnings are in the format of: { code: 1000, message: "Some message" }
//   messages: [],
// }

API

new D1API(options)

The options object requires the following fields:

  • accountId: Your Cloudflare account ID. This can be found as the ID in the URL on the dashboard after dash.cloudflare.com/, or in the sidebar of a zone.
  • apiKey: Your Cloudflare API key. This can be created under the user icon in the top-right under "My Profile", then "API Tokens" in the sidebar. Make sure to have D1 write access (even if you are just reading from the database).
  • databaseId: Your Cloudflare D1 database ID. This can be found on the D1 page.

Response

Same as D1API.exec.

D1API.all`SELECT * FROM user WHERE id = ${id}`

Returns result of all rows with tagged template.

You can reuse parameters inside the query by manually mapping them to ?N. Example: SELECT * FROM user WHERE id = ${id} AND age = ?1. This will filter where both id and age are equal to 1. This is not recommended as it can be error prone.

Response

{
  success: true,
  // Row data here.
  results: [
    {
      id: 1
    }
  ],
  // Metadata such as duration, rows read/written, changed, etc.
  meta: { ... }
}

D1API.allRaw("SELECT * FROM user WHERE id = ?1", [id])

Returns result of all rows with parameters binded using ?N, where N is the index of the paramter (starting at 1).

Prefer using all with tagged template.

Response

Same as D1API.all.

D1API.first`SELECT * FROM user WHERE id = ${id}`

Returns first row with tagged template.

You can reuse parameters inside the query by manually mapping them to ?N. Example: SELECT * FROM user WHERE id = ${id} AND age = ?1. This will filter where both id and age are equal to 1. This is not recommended as it can be error prone.

Response

{
  id: 1,
}

D1API.firstRaw("SELECT * FROM user WHERE id = ?1", [id])

Returns first row with parameters binded using ?N, where N is the index of the paramter (starting at 1).

Prefer using first with tagged template.

Response

Same as D1API.first.

D1API.exec`SELECT * FROM user WHERE id = ${id}`

Executes a query on the D1 API with tagged template.

You can reuse parameters inside the query by manually mapping them to ?N. Example: SELECT * FROM user WHERE id = ${id} AND age = ?1. This will filter where both id and age are equal to 1. This is not recommended as it can be error prone.

Response

{
  success: true,
  result: [
    {
      success: true,
      // Row data here. Can be empty if non-SELECT statement
      results: [
        {
          id: 1
        }
      ],
      // Metadata such as duration, rows read/written, changed, etc.
      meta: { ... }
    }
  ],
  // Query errors. A D1Error is thrown if any errors are present.
  errors: [],
  // Messages/warnings are in the format of: { code: 1000, message: "Some message" }
  messages: [],
}

D1API.execRaw("SELECT * FROM user WHERE id = ?1", [id])

Executes a query on the D1 API with parameters binded using ?N, where N is the index of the paramter (starting at 1).

Prefer using exec with tagged template.

D1Error

Error throw when the errors field is returned non-empty from the D1 API. It contains the errors field set to the errors, in the format of:

{
  code: 1000,
  message: 'Some error',
}

You can use the body field to get the original response.