Skip to content

Commit

Permalink
chore: added a where method on cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Jul 6, 2023
1 parent f07d439 commit 783cbdf
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 17 deletions.
23 changes: 22 additions & 1 deletion src/contract/table-cursor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {API, isInstanceOf, UInt64} from '@wharfkit/session'
import {Name} from '@wharfkit/session'
import {Table} from './table'
import {wrapIndexValue} from '../utils'
import {QueryOptions, Table, WhereQuery} from './table'

interface TableCursorParams {
table: Table
Expand Down Expand Up @@ -154,4 +155,24 @@ export class TableCursor<TableRow> {
}
return rows
}

/**
* Returns a new cursor with updated parameters.
*
* @returns A new cursor with updated parameters.
*/
where(query: WhereQuery, queryOptions?: QueryOptions) {
return new TableCursor({
table: this.table,
tableParams: {
...this.tableParams,
lower_bound:
wrapIndexValue(query.from, queryOptions?.index_type) ||
this.tableParams.lower_bound,
upper_bound:
wrapIndexValue(query.to, queryOptions?.index_type) ||
this.tableParams.upper_bound,
},
})
}
}
22 changes: 6 additions & 16 deletions src/contract/table.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {ABI, ABISerializableConstructor, Name, NameType, UInt64} from '@wharfkit/session'
import {ABI, ABISerializableConstructor, API, Name, NameType, UInt64} from '@wharfkit/session'
import type {Contract} from '../contract'
import {indexPositionInWords} from '../utils'
import {indexPositionInWords, wrapIndexValue} from '../utils'
import {TableCursor} from './table-cursor'

export interface QueryOptions {
index?: string
scope?: NameType
index_type?: API.v1.TableIndexType
}

export interface WhereQueryOptions extends QueryOptions {
Expand Down Expand Up @@ -94,7 +95,7 @@ export class Table<TableRow extends ABISerializableConstructor = ABISerializable
*/
where(
query: WhereQuery,
{limit = 10, scope = this.contract.account, index}: WhereQueryOptions = {}
{limit = 10, scope = this.contract.account, index, index_type}: WhereQueryOptions = {}
): TableCursor<TableRow> {
if (!query) {
throw new Error('Index value range must be provided')
Expand All @@ -106,25 +107,14 @@ export class Table<TableRow extends ABISerializableConstructor = ABISerializable
throw new Error('Index value for "from" or "to" must be provided')
}

const lower_bound = from
? typeof from === 'string'
? Name.from(from)
: UInt64.from(from)
: undefined
const upper_bound = to
? typeof to === 'string'
? Name.from(to)
: UInt64.from(to)
: undefined

const tableRowsParams = {
table: this.name,
code: this.contract.account,
scope,
type: this.rowType,
limit,
lower_bound,
upper_bound,
lower_bound: wrapIndexValue(from, index_type),
upper_bound: wrapIndexValue(from, index_type),
}

return new TableCursor({
Expand Down
23 changes: 23 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import {
ABISerializable,
ABISerializableConstructor,
ABISerializableObject,
API,
Name,
} from '@wharfkit/session'

export function pascalCase(value: string): string {
return value
.split(/_| /)
Expand Down Expand Up @@ -41,3 +49,18 @@ export function indexPositionInWords(index: number): string {
'tenth',
][index]
}

export function wrapIndexValue(
value,
indexType?: API.v1.TableIndexType
): API.v1.TableIndexType | undefined {
if (!value) {
return
}

if (indexType) {
return indexType.from(value)
}

return Name.from(value)
}

0 comments on commit 783cbdf

Please sign in to comment.