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-batch

v1.0.3

Published

Improve the way you batch operations with Cloudflare D1.

Downloads

22

Readme

d1-batch

Improve the way you batch operations with Cloudflare D1.

The Batch class provides a way to assign keys to a batch of queries, run the queries, and subsequently fetch the results by referring to the keys. Queries are run in the same order as they are added to the Batch class.

For example, the following piece of code is a simplified invitiation flow. A user row is created if needed and a message is recorded when provided. Using Cloudflare D1's built-in batch() function:

var _insertUserResult, newUserResult, _insertMessageResult;
if (message) {
  [_insertUserResult, newUserResult, _insertMessageResult] = await d1.batch([
    d1.prepare("INSERT INTO users (name, email) VALUES (?, ?) ON CONFLICT (email) DO NOTHING").bind(name, email),
    d1.prepare("SELECT * FROM users WHERE email=?").bind(email),
    d1.prepare("INSERT INTO messages (sender, receiver, content) VALUES (?, LAST_INSERT_ROWID(), ?)").bind(from!.id, message),
  ]);
} else {
  [_insertUserResult, newUserResult, _insertMessageResult] = await d1.batch([
    d1.prepare("INSERT INTO users (name, email) VALUES (?, ?) ON CONFLICT (email) DO NOTHING").bind(name, email),
    d1.prepare("SELECT * FROM users WHERE email=?").bind(email),
  ]);
}
const newUser = (newUserResult as D1Result<UsersRow>).results[0];

The code is however cleaner if you use the Batch class. The code is more readable, and easier to change over time:

import { Batch } from "d1-batch";

const b = new Batch(d1);
b.enqueue("_insertUser", d1.prepare("INSERT INTO users (name, email) VALUES (?, ?) ON CONFLICT (email) DO NOTHING").bind(name, email));
b.enqueue("newUser", d1.prepare("SELECT * FROM users WHERE email=?").bind(email));
if (message) {
  b.enqueue("_insertMessage", d1.prepare("INSERT INTO messages (sender, receiver, content) VALUES (?, LAST_INSERT_ROWID(), ?)").bind(from!.id, message));
}
await b.query();
const newUser = b.first<UsersRow>("newUser")!;

Installation

npm install d1-batch

Some additional context

In general, it is preferrable to use a lightweight query builder or ORM to perform CRUD operations: it's quicker to implement and less brittle. However, while working on a latency sensitive Cloudflare Workers based project, we needed to carefuly control the batching of queries. Our endpoints where making anywhere from a coulpe to a dozen queries to D1, but with never more than 3 round-trips -- resulting in backend endpoints that would run in about 300ms. We initially used the default batch() function, but eventually implemented the Batch class for better ergonomics.

The concept of a Batch class makes it easier to hook up automatic error handling, query logging, or performance monitoring when needed.