Skip to content

Commit

Permalink
Query Merge (#36)
Browse files Browse the repository at this point in the history
undefined
  • Loading branch information
aaroncox authored Jul 27, 2023
1 parent 9b90b04 commit 3a0ced2
Show file tree
Hide file tree
Showing 73 changed files with 151 additions and 26,887 deletions.
110 changes: 42 additions & 68 deletions src/contract/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import {ABI, ABIDef, API, APIClient, Name, NameType, Serializer} from '@greymass
import {indexPositionInWords, wrapIndexValue} from '../utils'
import {TableCursor} from './table-cursor'

export interface QueryOptions {
export interface QueryParams {
index?: string
scope?: NameType
key_type?: keyof API.v1.TableIndexTypes
}

export interface Query extends QueryOptions {
json?: boolean
from?: API.v1.TableIndexType | string | number
to?: API.v1.TableIndexType | string | number
maxRows?: number
rowsPerAPIRequest?: number
}

Expand Down Expand Up @@ -104,33 +103,35 @@ export class Table<RowType = any> {
* - `limit`: Maximum number of rows to return.
* @returns {TableCursor<TableRow>} Promise resolving to a `TableCursor` of the filtered table rows.
*/
query(query: Query): TableCursor<RowType> {
const {from, to, rowsPerAPIRequest} = query

query(params: QueryParams = {}): TableCursor<RowType> {
const tableRowsParams: any = {
// Table query
table: this.name,
code: this.account,
scope: query.scope || this.account,
scope: params.scope || this.account,
// Response typing
type: this.rowType,
limit: rowsPerAPIRequest || this.defaultRowLimit,
lower_bound: wrapIndexValue(from),
upper_bound: wrapIndexValue(to),
key_type: query.key_type,
// Filtering
key_type: params.key_type,
lower_bound: wrapIndexValue(params.from),
upper_bound: wrapIndexValue(params.to),
limit: params.rowsPerAPIRequest || this.defaultRowLimit,
}

if (query.index) {
if (params.index) {
const fieldToIndexMapping = this.getFieldToIndex()

if (!fieldToIndexMapping[query.index]) {
throw new Error(`Field ${query.index} is not a valid index.`)
if (!fieldToIndexMapping[params.index]) {
throw new Error(`Field ${params.index} is not a valid index.`)
}

tableRowsParams.index_position = fieldToIndexMapping[query.index].index_position
tableRowsParams.index_position = fieldToIndexMapping[params.index].index_position
}

return new TableCursor<RowType>({
abi: this.abi,
client: this.client,
maxRows: params.maxRows,
params: tableRowsParams,
})
}
Expand All @@ -142,38 +143,40 @@ export class Table<RowType = any> {
* Each key-value pair in the queryParams object corresponds to a field and its expected value in the table.
* @returns {Promise<TableRow>} Promise resolving to a single table row.
*/
async get(
queryValue?: API.v1.TableIndexType | string,
{scope = this.account, index, key_type}: QueryOptions = {}
): Promise<RowType> {
async get(value?: API.v1.TableIndexType | string, params: QueryParams = {}): Promise<RowType> {
const fieldToIndexMapping = this.getFieldToIndex()

const tableRowsParams = {
table: this.name,
code: this.account,
scope,
scope: params.scope || this.account,
type: this.rowType!,
limit: 1,
lower_bound: wrapIndexValue(queryValue),
upper_bound: wrapIndexValue(queryValue),
index_position: index ? fieldToIndexMapping[index].index_position : 'primary',
key_type: key_type,
lower_bound: wrapIndexValue(value),
upper_bound: wrapIndexValue(value),
index_position: params.index
? fieldToIndexMapping[params.index].index_position
: 'primary',
key_type: params.key_type,
json: false,
}

let {rows} = await this.client!.v1.chain.get_table_rows(tableRowsParams)
const {rows} = await this.client!.v1.chain.get_table_rows(tableRowsParams)
let [row] = rows

if (!this.rowType) {
rows = [
Serializer.decode({
data: rows[0],
abi: this.abi,
type: this.tableABI.type,
}),
]
row = Serializer.decode({
data: row,
abi: this.abi,
type: this.tableABI.type,
})
}

if (params.json) {
row = Serializer.objectify(row)
}

return rows[0]
return row
}

/**
Expand All @@ -184,48 +187,19 @@ export class Table<RowType = any> {
* - `limit`: Maximum number of rows to return.
* @returns {TableCursor<TableRow>} Promise resolving to a `TableCursor` of the table rows.
*/
first(maxRows: number, options: QueryOptions = {}): TableCursor<RowType> {
const tableRowsParams = {
table: this.name,
limit: maxRows,
code: this.account,
type: this.rowType,
scope: options.scope,
}

return new TableCursor<RowType>({
abi: this.abi,
client: this.client,
first(maxRows: number, params: QueryParams = {}): TableCursor<RowType> {
return this.query({
...params,
maxRows,
params: tableRowsParams,
})
}

/**
* Returns a cursor to get every single rows on the table.
* @returns {TableCursor}
*/
cursor(): TableCursor<RowType> {
const tableRowsParams = {
table: this.name,
code: this.account,
type: this.rowType,
limit: this.defaultRowLimit,
}

return new TableCursor<RowType>({
abi: this.abi,
client: this.client,
params: tableRowsParams,
})
}

/**
* Returns all the rows from the table.
* @returns {Promise<TableRow[]>} Promise resolving to an array of table rows.
*/
async all(): Promise<RowType[]> {
return this.cursor().all()
async all(params: QueryParams = {}): Promise<RowType[]> {
return this.query(params).all()
}

getFieldToIndex() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"path": "https://eos.greymass.com/v1/chain/get_table_rows",
"params": {
"method": "POST",
"body": "{\"json\":false,\"limit\":1000,\"table\":\"namebids\",\"code\":\"eosio\",\"lower_bound\":\"nkr\",\"scope\":\"eosio\",\"key_type\":\"name\"}"
"body": "{\"json\":false,\"limit\":1000,\"table\":\"namebids\",\"code\":\"eosio\",\"scope\":\"eosio\",\"key_type\":\"name\",\"lower_bound\":\"nkr\"}"
}
},
"status": 200,
Expand Down
Loading

0 comments on commit 3a0ced2

Please sign in to comment.