npm.io
0.3.1 • Published 2d ago

@thebookingkit/d1

Licence
MIT
Version
0.3.1
Deps
3
Size
151 kB
Vulns
0
Weekly
0

@thebookingkit/d1

Cloudflare D1/SQLite adapter for building booking systems on the edge.

npm version License: MIT TypeScript

Part of The Booking Kit — The Headless Booking Primitive.

Install

npm install @thebookingkit/d1

Quick Start

import { d1DayQuery, d1BookingRowsToInputs, encodeD1Date, D1BookingLock } from "@thebookingkit/d1";
import { getAvailableSlots } from "@thebookingkit/core";

// Query bookings for a day — returns aligned bounds + dateRange
const { bounds, dateRange } = d1DayQuery("2026-03-09");

// Convert D1 rows to core engine inputs
const slots = getAvailableSlots(rules, [], d1BookingRowsToInputs(rows), dateRange, tz);

// Encode dates for INSERT
await db.insert(bookings).values({ startsAt: encodeD1Date(slot.startTime) });

// Prevent double-bookings with advisory locks
const lock = new D1BookingLock(rawDb);
await lock.withLock(`${barberId}:${date}`, async () => { /* insert */ });

Key Features

  • Date CodecD1DateCodec for canonical UTC-Z encoding/decoding between D1 text columns and JS Date objects
  • Query Helpersd1DayQuery and d1LocalDayQuery produce aligned SQL bounds and DateRange in one call
  • Booking Bridged1BookingRowsToInputs converts D1 rows to @thebookingkit/core inputs
  • Schedule AdapterweeklyScheduleToRules and intersectSchedulesToRules convert WeeklySchedule JSON to availability rules
  • Advisory LocksD1BookingLock prevents double-bookings in SQLite (no EXCLUDE USING gist in D1)
  • Migration UtilitiesfindLegacyRows, migrateRowDates, buildMigrationSql for date format upgrades

Documentation

Full Documentation

License

MIT

Keywords