Class: WOWSQL::Table

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

Overview

Table interface for direct PostgREST operations.

All mutations use PostgREST native query parameters (?id=eq.val) and Prefer headers (return=representation, resolution=merge-duplicates).

Examples:

user = client.table("users").get_by_id("uuid-here")
result = client.table("users").create({ email: "a@b.com", name: "Alice" })

Instance Method Summary collapse

Constructor Details

#initialize(client, table_name) ⇒ Table

Returns a new instance of Table.



13
14
15
16
# File 'lib/wowsql/table.rb', line 13

def initialize(client, table_name)
  @client     = client
  @table_name = table_name
end

Instance Method Details

#bulk_insert(records) ⇒ Array<Hash>

Insert multiple rows.

Parameters:

  • records (Array<Hash>)

    List of row hashes

Returns:

  • (Array<Hash>)

    Created rows



109
110
111
112
113
114
115
116
117
# File 'lib/wowsql/table.rb', line 109

def bulk_insert(records)
  return [] if records.nil? || records.empty?

  result = @client.request(
    'POST', "/#{@table_name}", nil, records,
    'Prefer' => 'return=representation'
  )
  normalise_data(result)
end

#countObject

── Aggregates / pagination ───────────────────────────────────



164
165
166
# File 'lib/wowsql/table.rb', line 164

def count
  QueryBuilder.new(@client, @table_name).count
end

#create(data) ⇒ Hash Also known as: insert

Insert a single row and return the created record.

Parameters:

  • data (Hash)

    Column→value map

Returns:

  • (Hash)

    Created row



95
96
97
98
99
100
101
# File 'lib/wowsql/table.rb', line 95

def create(data)
  result = @client.request(
    'POST', "/#{@table_name}", nil, data,
    'Prefer' => 'return=representation'
  )
  normalise_data(result).first || {}
end

#delete(record_id) ⇒ Hash

Delete a row by ID.

Parameters:

  • record_id (String)

    Row UUID

Returns:

  • (Hash)

    Deleted row



152
153
154
155
156
157
158
159
160
# File 'lib/wowsql/table.rb', line 152

def delete(record_id)
  result = @client.request(
    'DELETE', "/#{@table_name}",
    { 'id' => "eq.#{record_id}" },
    nil,
    'Prefer' => 'return=representation'
  )
  normalise_data(result).first || {}
end

#eq(column, value) ⇒ Object



28
29
30
# File 'lib/wowsql/table.rb', line 28

def eq(column, value)
  QueryBuilder.new(@client, @table_name).eq(column, value)
end

#filter(column, operator = nil, value = nil, logical_op: 'AND') ⇒ Object



24
25
26
# File 'lib/wowsql/table.rb', line 24

def filter(column, operator = nil, value = nil, logical_op: 'AND')
  QueryBuilder.new(@client, @table_name).filter(column, operator, value, logical_op: logical_op)
end

#get(options = nil) ⇒ Hash

Retrieve all rows (with optional filter options forwarded to QueryBuilder#get).

Parameters:

  • options (Hash, nil) (defaults to: nil)

    Reserved for compatibility; use chained methods instead

Returns:

  • (Hash)

    { data: Array, count: Integer, total: Integer, limit: Integer, offset: Integer }



70
71
72
# File 'lib/wowsql/table.rb', line 70

def get(options = nil)
  QueryBuilder.new(@client, @table_name).get(options)
end

#get_by_id(record_id) ⇒ Hash

Retrieve a single row by primary key.

Parameters:

  • record_id (String)

    Row UUID

Returns:

  • (Hash)

    Row data



78
79
80
81
82
83
84
85
86
87
# File 'lib/wowsql/table.rb', line 78

def get_by_id(record_id)
  result = @client.request(
    'GET', "/#{@table_name}",
    { 'id' => "eq.#{record_id}" },
    nil,
    'Prefer' => 'return=representation'
  )
  data = normalise_data(result)
  data.first
end

#gt(column, value) ⇒ Object



36
37
38
# File 'lib/wowsql/table.rb', line 36

def gt(column, value)
  QueryBuilder.new(@client, @table_name).gt(column, value)
end

#gte(column, value) ⇒ Object



40
41
42
# File 'lib/wowsql/table.rb', line 40

def gte(column, value)
  QueryBuilder.new(@client, @table_name).gte(column, value)
end

#limit(n) ⇒ Object



56
57
58
# File 'lib/wowsql/table.rb', line 56

def limit(n)
  QueryBuilder.new(@client, @table_name).limit(n)
end

#lt(column, value) ⇒ Object



44
45
46
# File 'lib/wowsql/table.rb', line 44

def lt(column, value)
  QueryBuilder.new(@client, @table_name).lt(column, value)
end

#lte(column, value) ⇒ Object



48
49
50
# File 'lib/wowsql/table.rb', line 48

def lte(column, value)
  QueryBuilder.new(@client, @table_name).lte(column, value)
end

#neq(column, value) ⇒ Object



32
33
34
# File 'lib/wowsql/table.rb', line 32

def neq(column, value)
  QueryBuilder.new(@client, @table_name).neq(column, value)
end

#offset(n) ⇒ Object



60
61
62
# File 'lib/wowsql/table.rb', line 60

def offset(n)
  QueryBuilder.new(@client, @table_name).offset(n)
end

#order_by(column, direction = 'asc') ⇒ Object



52
53
54
# File 'lib/wowsql/table.rb', line 52

def order_by(column, direction = 'asc')
  QueryBuilder.new(@client, @table_name).order_by(column, direction)
end

#paginate(page: 1, per_page: 20) ⇒ Object



168
169
170
# File 'lib/wowsql/table.rb', line 168

def paginate(page: 1, per_page: 20)
  QueryBuilder.new(@client, @table_name).paginate(page: page, per_page: per_page)
end

#select(*columns) ⇒ Object

── Query builder shortcuts ───────────────────────────────────



20
21
22
# File 'lib/wowsql/table.rb', line 20

def select(*columns)
  QueryBuilder.new(@client, @table_name).select(*columns)
end

#update(record_id, data) ⇒ Hash

Update a row by ID.

Parameters:

  • record_id (String)

    Row UUID

  • data (Hash)

    Columns to update

Returns:

  • (Hash)

    Updated row



138
139
140
141
142
143
144
145
146
# File 'lib/wowsql/table.rb', line 138

def update(record_id, data)
  result = @client.request(
    'PATCH', "/#{@table_name}",
    { 'id' => "eq.#{record_id}" },
    data,
    'Prefer' => 'return=representation'
  )
  normalise_data(result).first || {}
end

#upsert(data, on_conflict: 'id') ⇒ Hash

Insert or update based on conflict column.

Parameters:

  • data (Hash)

    Row data (must include the conflict column value)

  • on_conflict (String) (defaults to: 'id')

    Column to detect conflicts on (default: “id”)

Returns:

  • (Hash)

    Upserted row



124
125
126
127
128
129
130
131
# File 'lib/wowsql/table.rb', line 124

def upsert(data, on_conflict: 'id')
  result = @client.request(
    'POST', "/#{@table_name}", nil, data,
    'Prefer'             => 'return=representation,resolution=merge-duplicates',
    'on-conflict-column' => on_conflict
  )
  normalise_data(result).first || {}
end