Skip to content

Commit

Permalink
enhancement: renamed where to query and find to get
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Jul 6, 2023
1 parent 783cbdf commit fbfd00e
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 67 deletions.
40 changes: 20 additions & 20 deletions contracts/decentiumorg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export namespace _Decentiumorg {
export namespace tables {
export class blogs {
static fieldToIndex = {author: {type: 'name', index_position: 'primary'}}
static where(
static query(
{limit = 10},
queryParams: _Decentiumorg.types.blogsQueryParams,
client: APIClient
Expand All @@ -183,9 +183,9 @@ export namespace _Decentiumorg {
rowType: _Decentiumorg.types.Blog_row,
fieldToIndex: blogs.fieldToIndex,
})
return blogsTable.where(queryParams)
return blogsTable.query(queryParams)
}
static find(
static get(
queryParams: _Decentiumorg.types.blogsQueryParams,
client: APIClient
): Promise<_Decentiumorg.types.Blog_row> {
Expand All @@ -196,7 +196,7 @@ export namespace _Decentiumorg {
rowType: _Decentiumorg.types.Blog_row,
fieldToIndex: blogs.fieldToIndex,
})
return blogsTable.find(queryParams)
return blogsTable.get(queryParams)
}
static all(
queryParams: _Decentiumorg.types.blogsQueryParams,
Expand All @@ -218,7 +218,7 @@ export namespace _Decentiumorg {
from: {type: 'uint128', index_position: 'secondary'},
to: {type: 'uint128', index_position: 'tertiary'},
}
static where(
static query(
{limit = 10},
queryParams: _Decentiumorg.types.linksQueryParams,
client: APIClient
Expand All @@ -230,9 +230,9 @@ export namespace _Decentiumorg {
rowType: _Decentiumorg.types.Link_row,
fieldToIndex: links.fieldToIndex,
})
return linksTable.where(queryParams)
return linksTable.query(queryParams)
}
static find(
static get(
queryParams: _Decentiumorg.types.linksQueryParams,
client: APIClient
): Promise<_Decentiumorg.types.Link_row> {
Expand All @@ -243,7 +243,7 @@ export namespace _Decentiumorg {
rowType: _Decentiumorg.types.Link_row,
fieldToIndex: links.fieldToIndex,
})
return linksTable.find(queryParams)
return linksTable.get(queryParams)
}
static all(
queryParams: _Decentiumorg.types.linksQueryParams,
Expand All @@ -264,7 +264,7 @@ export namespace _Decentiumorg {
slug: {type: 'name', index_position: 'primary'},
updated: {type: 'uint64', index_position: 'secondary'},
}
static where(
static query(
{limit = 10},
queryParams: _Decentiumorg.types.postsQueryParams,
client: APIClient
Expand All @@ -276,9 +276,9 @@ export namespace _Decentiumorg {
rowType: _Decentiumorg.types.Post_row,
fieldToIndex: posts.fieldToIndex,
})
return postsTable.where(queryParams)
return postsTable.query(queryParams)
}
static find(
static get(
queryParams: _Decentiumorg.types.postsQueryParams,
client: APIClient
): Promise<_Decentiumorg.types.Post_row> {
Expand All @@ -289,7 +289,7 @@ export namespace _Decentiumorg {
rowType: _Decentiumorg.types.Post_row,
fieldToIndex: posts.fieldToIndex,
})
return postsTable.find(queryParams)
return postsTable.get(queryParams)
}
static all(
queryParams: _Decentiumorg.types.postsQueryParams,
Expand All @@ -307,7 +307,7 @@ export namespace _Decentiumorg {
}
export class state {
static fieldToIndex = {}
static where(
static query(
{limit = 10},
queryParams: _Decentiumorg.types.stateQueryParams,
client: APIClient
Expand All @@ -319,9 +319,9 @@ export namespace _Decentiumorg {
rowType: _Decentiumorg.types.State,
fieldToIndex: state.fieldToIndex,
})
return stateTable.where(queryParams)
return stateTable.query(queryParams)
}
static find(
static get(
queryParams: _Decentiumorg.types.stateQueryParams,
client: APIClient
): Promise<_Decentiumorg.types.State> {
Expand All @@ -332,7 +332,7 @@ export namespace _Decentiumorg {
rowType: _Decentiumorg.types.State,
fieldToIndex: state.fieldToIndex,
})
return stateTable.find(queryParams)
return stateTable.get(queryParams)
}
static all(
queryParams: _Decentiumorg.types.stateQueryParams,
Expand All @@ -355,7 +355,7 @@ export namespace _Decentiumorg {
cscore: {type: 'uint128', index_position: 'tertiary'},
permlink: {type: 'uint128', index_position: 'fourth'},
}
static where(
static query(
{limit = 10},
queryParams: _Decentiumorg.types.trendingQueryParams,
client: APIClient
Expand All @@ -367,9 +367,9 @@ export namespace _Decentiumorg {
rowType: _Decentiumorg.types.Trending_row,
fieldToIndex: trending.fieldToIndex,
})
return trendingTable.where(queryParams)
return trendingTable.query(queryParams)
}
static find(
static get(
queryParams: _Decentiumorg.types.trendingQueryParams,
client: APIClient
): Promise<_Decentiumorg.types.Trending_row> {
Expand All @@ -380,7 +380,7 @@ export namespace _Decentiumorg {
rowType: _Decentiumorg.types.Trending_row,
fieldToIndex: trending.fieldToIndex,
})
return trendingTable.find(queryParams)
return trendingTable.get(queryParams)
}
static all(
queryParams: _Decentiumorg.types.trendingQueryParams,
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {capitalize} from '../utils'

export async function generateTableClass(contractName, namespaceName, table, abi) {
const tableName = table.name
const struct = abi.structs.find((struct) => struct.name === table.type)
const struct = abi.structs.get((struct) => struct.name === table.type)
const members: ts.ClassElement[] = []
const rowType = `${namespaceName}.types.${capitalize(struct.name)}`

Expand Down
2 changes: 1 addition & 1 deletion src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class Contract {
async getTable(name: NameType): Promise<Table> {
const tables = await this.getTables()

const table = tables.find((table) => table.name.equals(name))
const table = tables.get((table) => table.name.equals(name))

if (!table) {
throw new Error(`No table found with name ${name}`)
Expand Down
10 changes: 3 additions & 7 deletions src/contract/table-cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,13 @@ export class TableCursor<TableRow> {
*
* @returns A new cursor with updated parameters.
*/
where(query: WhereQuery, queryOptions?: QueryOptions) {
query(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,
lower_bound: wrapIndexValue(query.from) || this.tableParams.lower_bound,
upper_bound: wrapIndexValue(query.to) || this.tableParams.upper_bound,
},
})
}
Expand Down
8 changes: 4 additions & 4 deletions src/contract/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class Table<TableRow extends ABISerializableConstructor = ABISerializable
* - `limit`: Maximum number of rows to return.
* @returns {TableCursor<TableRow>} Promise resolving to a `TableCursor` of the filtered table rows.
*/
where(
query(
query: WhereQuery,
{limit = 10, scope = this.contract.account, index, index_type}: WhereQueryOptions = {}
): TableCursor<TableRow> {
Expand All @@ -113,8 +113,8 @@ export class Table<TableRow extends ABISerializableConstructor = ABISerializable
scope,
type: this.rowType,
limit,
lower_bound: wrapIndexValue(from, index_type),
upper_bound: wrapIndexValue(from, index_type),
lower_bound: wrapIndexValue(from),
upper_bound: wrapIndexValue(from),
}

return new TableCursor({
Expand All @@ -131,7 +131,7 @@ export class Table<TableRow extends ABISerializableConstructor = ABISerializable
* 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 find(query, queryOptions?: QueryOptions): Promise<TableRow> {
async get(query, queryOptions?: QueryOptions): Promise<TableRow> {
if (!query) {
throw new Error('Index value must be provided')
}
Expand Down
8 changes: 4 additions & 4 deletions src/example-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export namespace _Blog {
},
}

static where(
static query(
queryParams: _Blog.types.UsersWhereQueryParams,
{limit = 10} = {},
client: APIClient
Expand All @@ -60,10 +60,10 @@ export namespace _Blog {
fieldToIndex: Users.fieldToIndex,
})

return usersTable.where(queryParams, {limit})
return usersTable.query(queryParams, {limit})
}

static async find(
static async get(
queryParams: _Blog.types.UsersFindQueryParams,
client: APIClient
): Promise<_Blog.types.UsersRow> {
Expand All @@ -74,7 +74,7 @@ export namespace _Blog {
fieldToIndex: Users.fieldToIndex,
})

return usersTable.find(queryParams)
return usersTable.get(queryParams)
}

static first(limit, client): TableCursor<_Blog.types.UsersRow> {
Expand Down
26 changes: 20 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ import {
ABISerializableConstructor,
ABISerializableObject,
API,
Checksum160,
Checksum256,
Float64,
isInstanceOf,
Name,
UInt128,
UInt32,
UInt64,
} from '@wharfkit/session'

export function pascalCase(value: string): string {
Expand Down Expand Up @@ -50,16 +57,23 @@ export function indexPositionInWords(index: number): string {
][index]
}

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

if (indexType) {
return indexType.from(value)
if (
isInstanceOf(value, UInt128) ||
isInstanceOf(value, UInt64) ||
isInstanceOf(value, Float64) ||
isInstanceOf(value, Checksum256) ||
isInstanceOf(value, Checksum160)
) {
return value
}

if (typeof value === 'number') {
return UInt64.from(value)
}

return Name.from(value)
Expand Down
16 changes: 8 additions & 8 deletions test/codegen-samples/rewards.gm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export namespace _RewardsGm {
export namespace tables {
export class config {
static fieldToIndex = {}
static where(
static query(
queryParams: _RewardsGm.types.ConfigWhereQueryParams,
getTableRowsOptions: GetTableRowsOptions,
client: APIClient
Expand All @@ -76,9 +76,9 @@ export namespace _RewardsGm {
rowType: _RewardsGm.types.Config,
fieldToIndex: config.fieldToIndex,
})
return configTable.where(queryParams, getTableRowsOptions)
return configTable.query(queryParams, getTableRowsOptions)
}
static find(
static get(
queryParams: _RewardsGm.types.ConfigFindQueryParams,
client: APIClient
): Promise<_RewardsGm.types.Config> {
Expand All @@ -88,7 +88,7 @@ export namespace _RewardsGm {
rowType: _RewardsGm.types.Config,
fieldToIndex: config.fieldToIndex,
})
return configTable.find(queryParams)
return configTable.get(queryParams)
}
static first(limit: number, client: APIClient): TableCursor<_RewardsGm.types.Config> {
const configTable = Table.from({
Expand All @@ -102,7 +102,7 @@ export namespace _RewardsGm {
}
export class users {
static fieldToIndex = {}
static where(
static query(
queryParams: _RewardsGm.types.UsersWhereQueryParams,
getTableRowsOptions: GetTableRowsOptions,
client: APIClient
Expand All @@ -113,9 +113,9 @@ export namespace _RewardsGm {
rowType: _RewardsGm.types.User_row,
fieldToIndex: users.fieldToIndex,
})
return usersTable.where(queryParams, getTableRowsOptions)
return usersTable.query(queryParams, getTableRowsOptions)
}
static find(
static get(
queryParams: _RewardsGm.types.UsersFindQueryParams,
client: APIClient
): Promise<_RewardsGm.types.User_row> {
Expand All @@ -125,7 +125,7 @@ export namespace _RewardsGm {
rowType: _RewardsGm.types.User_row,
fieldToIndex: users.fieldToIndex,
})
return usersTable.find(queryParams)
return usersTable.get(queryParams)
}
static first(limit: number, client: APIClient): TableCursor<_RewardsGm.types.User_row> {
const usersTable = Table.from({
Expand Down
Loading

0 comments on commit fbfd00e

Please sign in to comment.