Class: AxHub::Data::DataTableClient

Inherits:
Object
  • Object
show all
Defined in:
lib/axhub_sdk/data/client.rb

Overview

Client bound to one tenant/app/table with CRUD + pagination.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, tenant_slug, app_slug, table_name, schema = nil) ⇒ DataTableClient

Returns a new instance of DataTableClient.



83
84
85
86
87
88
89
# File 'lib/axhub_sdk/data/client.rb', line 83

def initialize(client, tenant_slug, app_slug, table_name, schema = nil)
  @client = client
  @tenant_slug = tenant_slug
  @app_slug = app_slug
  @table_name = table_name
  @schema = schema
end

Instance Attribute Details

#schemaObject (readonly)

Returns the value of attribute schema.



81
82
83
# File 'lib/axhub_sdk/data/client.rb', line 81

def schema
  @schema
end

Instance Method Details

#count(where: nil) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/axhub_sdk/data/client.rb', line 142

def count(where: nil)
  # Same mass-scan guard as list() — the backend 400s an unfiltered count.
  if where.nil?
    raise ValidationError.new(
      'AxHub data count requires at least one WHERE filter (the backend rejects unfiltered scans). Pass `where:`.',
      'where_required'
    )
  end
  raw = @client.request_raw('GET', "#{_path}/_count", query: Data.serialize_where(where)) || {}
  raw['count']
end

#delete(row_id) ⇒ Object



180
181
182
183
# File 'lib/axhub_sdk/data/client.rb', line 180

def delete(row_id)
  @client.request_raw('DELETE', _path(row_id))
  nil
end

#get(row_id, select: nil) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/axhub_sdk/data/client.rb', line 154

def get(row_id, select: nil)
  Data.validate_select_columns(@schema, select)
  serialized_select = Data.serialize_select(select)
  query = serialized_select.nil? ? {} : { '_select' => serialized_select }
  row = @client.request_raw('GET', _path(row_id), query: query) || {}
  Data.project_row(row, select)
end

#insert(row) ⇒ Object



162
163
164
165
# File 'lib/axhub_sdk/data/client.rb', line 162

def insert(row)
  Data.run_schema_validation(@schema, row, 'insert')
  @client.request_raw('POST', _path, body: row)
end

#insert_many(rows) ⇒ Object



167
168
169
170
171
172
173
# File 'lib/axhub_sdk/data/client.rb', line 167

def insert_many(rows)
  rows.each { |row| Data.run_schema_validation(@schema, row, 'insert') }
  # mirrors node: no bulk endpoint exists, so insertMany loops single
  # inserts and returns { items, count }.
  items = rows.map { |row| insert(row) }
  { 'items' => items, 'count' => items.length }
end

#list(where: nil, order_by: nil, select: nil, page: nil, page_size: nil, limit: nil, cursor: nil, after: nil, before: nil, direction: nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/axhub_sdk/data/client.rb', line 91

def list(where: nil, order_by: nil, select: nil, page: nil, page_size: nil, limit: nil, cursor: nil, after: nil, before: nil, direction: nil)
  Data.validate_select_columns(@schema, select)
  Data._reject_legacy_page_options(after, before, direction, @table_name)
  resolved_page = Data._resolve_offset_page(cursor, page, @table_name)
  per_page = Data._clamp_per_page(page_size.nil? ? limit : page_size)
  # The AxHub data ring rejects an unfiltered list with HTTP 400
  # ("최소 1개의 WHERE 필터가 필요해요") as a deliberate mass-scan guard —
  # confirmed live 2026-06, mirrored by the `axhub data` CLI. Checked after
  # cursor/page validation so a malformed cursor still surfaces first.
  if where.nil?
    raise ValidationError.new(
      'AxHub data list requires at least one WHERE filter (the backend rejects unfiltered scans). Pass `where:`.',
      'where_required'
    )
  end
  query = Data.serialize_where(where).dup
  query['per_page'] = per_page unless per_page.nil?
  query['page'] = resolved_page if resolved_page != 1
  sort = Data.serialize_order_by(order_by)
  query['sort'] = sort if sort && sort != ''
  serialized_select = Data.serialize_select(select)
  query['_select'] = serialized_select unless serialized_select.nil?
  raw = @client.request_raw('GET', _path, query: query) || {}
  items = Data.project_rows(raw['items'] || [], select)
  # mirrors node: current_page falls back to the requested page, has_next
  # reads the backend `has_more` flag verbatim, has_prev derives client-side.
  current_page = raw['page'].nil? ? resolved_page : raw['page']
  has_next = !!(raw['has_more'] || false)
  has_prev = current_page > 1
  PaginatedList.new(
    items: items,
    next_cursor: has_next ? (current_page + 1).to_s : nil,
    first_cursor: has_prev ? (current_page - 1).to_s : nil,
    has_next: has_next,
    has_prev: has_prev,
    total_is_exact: false
  )
end

#list_all(where: nil, order_by: nil, select: nil, page_size: nil, limit: nil, &block) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/axhub_sdk/data/client.rb', line 130

def list_all(where: nil, order_by: nil, select: nil, page_size: nil, limit: nil, &block)
  base = { where: where, order_by: order_by, select: select, limit: limit }
  fetcher = lambda do |p|
    kwargs = base.reject { |_k, v| v.nil? }
    kwargs[:cursor] = p[:cursor] unless p[:cursor].nil?
    ps = p[:page_size].nil? ? page_size : p[:page_size]
    kwargs[:page_size] = ps unless ps.nil?
    list(**kwargs)
  end
  Data.list_all(fetcher, { page_size: page_size }, &block)
end

#update(row_id, patch) ⇒ Object



175
176
177
178
# File 'lib/axhub_sdk/data/client.rb', line 175

def update(row_id, patch)
  Data.run_schema_validation(@schema, patch, 'update')
  @client.request_raw('PATCH', _path(row_id), body: patch)
end