Class: Cloudflare::D1Statement

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudflare_workers.rb

Instance Method Summary collapse

Constructor Details

#initialize(js) ⇒ D1Statement

Returns a new instance of D1Statement.



677
678
679
# File 'lib/cloudflare_workers.rb', line 677

def initialize(js)
  @js = js
end

Instance Method Details

#allObject

Returns a JS Promise that resolves to a Ruby Array of Ruby Hashes.



689
690
691
692
693
694
# File 'lib/cloudflare_workers.rb', line 689

def all
  js_stmt = @js
  cf = Cloudflare
  err_cls = Cloudflare::D1Error
  `#{js_stmt}.all().then(function(res) { return #{cf}.$js_rows_to_ruby(res.results); }).catch(function(e) { #{Kernel}.$raise(#{err_cls}.$new(e.message || String(e), Opal.hash({binding_type: 'D1', operation: 'all'}))); })`
end

#bind(*args) ⇒ Object



681
682
683
684
685
686
# File 'lib/cloudflare_workers.rb', line 681

def bind(*args)
  js_args = `[]`
  args.each { |a| `#{js_args}.push(#{a})` }
  js = @js
  D1Statement.new(`#{js}.bind.apply(#{js}, #{js_args})`)
end

#firstObject

Returns a JS Promise that resolves to a single Ruby Hash (or nil).



697
698
699
700
701
702
# File 'lib/cloudflare_workers.rb', line 697

def first
  js_stmt = @js
  cf = Cloudflare
  err_cls = Cloudflare::D1Error
  `#{js_stmt}.first().then(function(res) { return res == null ? nil : #{cf}.$js_object_to_hash(res); }).catch(function(e) { #{Kernel}.$raise(#{err_cls}.$new(e.message || String(e), Opal.hash({binding_type: 'D1', operation: 'first'}))); })`
end

#runObject

Returns a JS Promise that resolves to a Ruby Hash with the D1 meta.



705
706
707
708
709
710
# File 'lib/cloudflare_workers.rb', line 705

def run
  js_stmt = @js
  cf = Cloudflare
  err_cls = Cloudflare::D1Error
  `#{js_stmt}.run().then(function(res) { return #{cf}.$js_object_to_hash(res); }).catch(function(e) { #{Kernel}.$raise(#{err_cls}.$new(e.message || String(e), Opal.hash({binding_type: 'D1', operation: 'run'}))); })`
end