Giter VIP home page Giter VIP logo

d1-sql-tag's Introduction

d1-sql-tag

npm version

A template literal for working with Cloudflare D1 database.

npm install d1-sql-tag

Usage with Cloudflare Workers

If you have created a D1 database and configured it with the binding name DB, in wrangler.toml, you can create a template literal tag with createD1SqlTag().

We also set up a callback to log stats for each query, like so:

D1 batch: 286ms · 1 queries
1: SELECT ?1 AS message
   ↳ 0.3053ms · 0 changed · 0 read · 0 written
import { createD1SqlTag, logQueryResults } from "d1-sql-tag";

export interface Env {
  DB: D1Database;
}

function createSqlTag(db: D1Database) {
  return createD1SqlTag(db, {
    afterQuery(batchId, queries, results, duration) {
      logQueryResults(queries, results, duration);
    },
  });
}

export default {
  async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
    const sql = createSqlTag(env.DB);
    const result = await sql`SELECT ${"hello world"} AS message`.all<{
      message: string;
    }>();
    return new Response(`Message: ${result.results[0].message}`);
  },
};

Usage with Hono on Cloudflare Workers

If you have created a D1 database and configured it with the binding name DB, in wrangler.toml, you can create a template literal tag with createD1SqlTag().

We also set up a callback to log stats for each query, like so:

D1 batch: 286ms · 1 queries
1: SELECT ?1 AS message
   ↳ 0.3053ms · 0 changed · 0 read · 0 written

Additionally, we use hono/timing to send Server-Timing headers for the total response time, how long we wait for each batch, and how long each query takes. Open the network tab in your browser's devtools, select the request and look at the "Timing" tab.

import { createD1SqlTag, logQueryResults } from "d1-sql-tag";
import { Hono, type Context } from "hono";
import { endTime, setMetric, startTime, timing } from "hono/timing";

type Bindings = {
  DB: D1Database;
};

const app = new Hono<{ Bindings: Bindings }>();

function createSqlTag(c: Context<{ Bindings: Bindings }>) {
  return createD1SqlTag(c.env.DB, {
    beforeQuery(batchId, queries) {
      startTime(c, `db-${batchId}`);
    },
    afterQuery(batchId, queries, results, duration) {
      endTime(c, `db-${batchId}`);
      results.forEach((result, i) => {
        setMetric(c, `db-${batchId}-query-${i + 1}`, result.meta.duration);
      });
      logQueryResults(queries, results, duration);
    },
  });
}

app.use("*", timing());

app.get("/", async (c) => {
  const sql = createSqlTag(c);
  const result = await sql`SELECT ${"hello world"} AS message`.all<{
    message: string;
  }>();
  return c.text(`Message: ${result.results[0].message}`);
});

export default app;

License

MIT

d1-sql-tag's People

Contributors

dependabot[bot] avatar github-actions[bot] avatar jonasb avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.