@thebookingkit/d1
v0.1.4
Published
Cloudflare D1/SQLite adapter for The Booking Kit — date codec, row converters, and advisory locking
Maintainers
Readme
@thebookingkit/d1
Cloudflare D1/SQLite adapter for building booking systems on the edge.
Part of The Booking Kit — The Headless Booking Primitive.
Install
npm install @thebookingkit/d1Quick 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 Codec —
D1DateCodecfor canonical UTC-Z encoding/decoding between D1 text columns and JS Date objects - Query Helpers —
d1DayQueryandd1LocalDayQueryproduce aligned SQL bounds andDateRangein one call - Booking Bridge —
d1BookingRowsToInputsconverts D1 rows to@thebookingkit/coreinputs - Schedule Adapter —
weeklyScheduleToRulesandintersectSchedulesToRulesconvert WeeklySchedule JSON to availability rules - Advisory Locks —
D1BookingLockprevents double-bookings in SQLite (noEXCLUDE USING gistin D1) - Migration Utilities —
findLegacyRows,migrateRowDates,buildMigrationSqlfor date format upgrades
Documentation
License
MIT
