Class: Hecks::Adapters::D1::Connection
- Inherits:
-
Object
- Object
- Hecks::Adapters::D1::Connection
- Defined in:
- lib/hecks/adapters/driven/d1.rb
Overview
THE TRANSPORT — mirrors just the slice of SQLite3::Database's own interface (execute/get_first_row/get_first_value, rows as column-name-keyed hashes) that Sqlite::SchemaBuilder, Sqlite::Codec, and this file's own methods already assume. One stateless HTTP call per execute, not a persistent connection — D1 has no connection to hold open.
Constant Summary collapse
- ENDPOINT =
"https://api.cloudflare.com/client/v4"
Instance Method Summary collapse
-
#batch(statements) ⇒ Object
D1 batches are SQL transactions: statements execute in order and a failure rolls the entire sequence back.
- #execute(sql, binds = []) ⇒ Object
- #get_first_row(sql, binds = []) ⇒ Object
- #get_first_value(sql, binds = []) ⇒ Object
-
#initialize(account_id:, database_id:, api_token:) ⇒ Connection
constructor
A new instance of Connection.
Constructor Details
#initialize(account_id:, database_id:, api_token:) ⇒ Connection
Returns a new instance of Connection.
36 37 38 39 |
# File 'lib/hecks/adapters/driven/d1.rb', line 36 def initialize(account_id:, database_id:, api_token:) @uri = URI("#{ENDPOINT}/accounts/#{account_id}/d1/database/#{database_id}/query") @api_token = api_token end |
Instance Method Details
#batch(statements) ⇒ Object
D1 batches are SQL transactions: statements execute in order and a failure rolls the entire sequence back. Keep the tuple-shaped local seam small so adapter code and focused fakes do not need to know the REST request envelope. https://developers.cloudflare.com/d1/worker-api/d1-database/#batch
50 51 52 53 54 55 56 57 |
# File 'lib/hecks/adapters/driven/d1.rb', line 50 def batch(statements) payload = { batch: statements.map do |sql, binds| { sql: sql, params: binds || [] } end } response_results(payload).map { |result| result.fetch("results", []) } end |
#execute(sql, binds = []) ⇒ Object
41 42 43 |
# File 'lib/hecks/adapters/driven/d1.rb', line 41 def execute(sql, binds = []) response_results({ sql: sql, params: binds }).first.fetch("results", []) end |
#get_first_row(sql, binds = []) ⇒ Object
93 94 95 |
# File 'lib/hecks/adapters/driven/d1.rb', line 93 def get_first_row(sql, binds = []) execute(sql, binds).first end |
#get_first_value(sql, binds = []) ⇒ Object
97 98 99 |
# File 'lib/hecks/adapters/driven/d1.rb', line 97 def get_first_value(sql, binds = []) get_first_row(sql, binds)&.values&.first end |