sequel-d1
Sequel :d1 adapter and Opal/Cloudflare Workers glue for Cloudflare D1.
Quick start (Sinatra on Workers)
require 'sequel'
DB = nil
get '/users' do
DB ||= Sequel.connect(adapter: :d1, d1: env['cloudflare.env'].DB)
content_type 'application/json'
# On Opal/Workers, Dataset#all returns a Promise — resolve before JSON.
DB[:users].order(:id).all.__await__.to_json
end
d1: must respond to prepare(sql) returning a statement that supports bind(*args), all, and run (same contract as the JavaScript D1 API). The Ruby wrapper from homura-runtime (env['cloudflare.DB']) is the usual choice.
Opal build paths
cloudflare-workers-build --standalone --with-db now wires this up for you.
If you invoke opal manually, add the gem's packaged vendor/ before its
lib/ so require 'sequel' resolves to the bundled Opal-compatible entrypoint:
-I gems/sequel-d1/vendor -I gems/sequel-d1/lib -I lib -I vendor
Migrations (cloudflare-workers-migrate)
Compile Ruby migration files to wrangler-compatible .sql:
bundle exec cloudflare-workers-migrate compile db/migrations
Apply (uses WRANGLER_BIN or wrangler; database from --database or CLOUDFLARE_D1_DATABASE):
bundle exec cloudflare-workers-migrate apply --database homura-db
bundle exec cloudflare-workers-migrate apply --remote --database homura-db
License
MIT. See the repository LICENSE.