Class: AxHub::Data::DataTableClient
- Inherits:
-
Object
- Object
- AxHub::Data::DataTableClient
- Defined in:
- lib/axhub_sdk/data/client.rb
Overview
Client bound to one tenant/app/table with CRUD + pagination.
Instance Attribute Summary collapse
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Instance Method Summary collapse
- #count(where: nil) ⇒ Object
- #delete(row_id) ⇒ Object
- #get(row_id, select: nil) ⇒ Object
-
#initialize(client, tenant_slug, app_slug, table_name, schema = nil) ⇒ DataTableClient
constructor
A new instance of DataTableClient.
- #insert(row) ⇒ Object
- #insert_many(rows) ⇒ Object
- #list(where: nil, order_by: nil, select: nil, page: nil, page_size: nil, limit: nil, cursor: nil, after: nil, before: nil, direction: nil) ⇒ Object
- #list_all(where: nil, order_by: nil, select: nil, page_size: nil, limit: nil, &block) ⇒ Object
- #update(row_id, patch) ⇒ Object
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
#schema ⇒ Object (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
132 133 134 135 |
# File 'lib/axhub_sdk/data/client.rb', line 132 def count(where: nil) raw = Data.map_where_required('count') { @client.request_raw('GET', "#{_path}/_count", query: Data.serialize_where(where)) } || {} raw['count'] end |
#delete(row_id) ⇒ Object
163 164 165 166 |
# File 'lib/axhub_sdk/data/client.rb', line 163 def delete(row_id) @client.request_raw('DELETE', _path(row_id)) nil end |
#get(row_id, select: nil) ⇒ Object
137 138 139 140 141 142 143 |
# File 'lib/axhub_sdk/data/client.rb', line 137 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
145 146 147 148 |
# File 'lib/axhub_sdk/data/client.rb', line 145 def insert(row) Data.run_schema_validation(@schema, row, 'insert') @client.request_raw('POST', _path, body: row) end |
#insert_many(rows) ⇒ Object
150 151 152 153 154 155 156 |
# File 'lib/axhub_sdk/data/client.rb', line 150 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 |
# 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.(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) 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 = Data.map_where_required('list') { @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
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/axhub_sdk/data/client.rb', line 120 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
158 159 160 161 |
# File 'lib/axhub_sdk/data/client.rb', line 158 def update(row_id, patch) Data.run_schema_validation(@schema, patch, 'update') @client.request_raw('PATCH', _path(row_id), body: patch) end |