Skip to content

Commit 03af4d1

Browse files
authored
RAI-13623 add filtering/sorting options to listTransactions (#101)
* RAI-13623 add filtering/sorting options to listTransactions * add next param * address comments
1 parent 5989e8d commit 03af4d1

File tree

7 files changed

+98
-14
lines changed

7 files changed

+98
-14
lines changed

CHANGELOG.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
# Change Log
22

3+
## v0.7.4-alpha
4+
5+
- Added filtering, sorting and pagination options to `listTransactions` API. [\#101](https://github.com/RelationalAI/rai-sdk-javascript/pull/101)
6+
### Breaking Changes
7+
- `listTransactions` API now returns an object that contains `transactions` and `next` properties.
8+
- `transactions` is the result of the API call.
9+
- `next` is a continuation token that can be used to fetch the next page of results. `next` is `undefined` if there are no more results to fetch.
10+
11+
312
## v0.7.1-alpha
413
- Storing `AccessToken.createdOn` in seconds to be consistent with other RAI SDKs.
514

6-
## v0.7.0
7-
8-
## [v0.7.0](https://github.com/relationalai/rai-sdk-javascript/tree/v0.7.0) (2022-XX-XX)
15+
## [v0.7.0](https://github.com/relationalai/rai-sdk-javascript/tree/v0.7.0)
916

1017
[Full Changelog](https://github.com/relationalai/rai-sdk-javascript/compare/v0.6.3...v0.7.0)
1118

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@relationalai/rai-sdk-javascript",
33
"description": "RelationalAI SDK for JavaScript",
4-
"version": "0.7.3-alpha",
4+
"version": "0.7.4-alpha",
55
"author": {
66
"name": "RelationalAI",
77
"url": "https://relational.ai"

src/api/transaction/transactionAsyncApi.test.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import nock from 'nock';
1919

2020
import { baseUrl, getMockConfig } from '../../testUtils';
2121
import { TransactionAsyncApi } from './transactionAsyncApi';
22-
import { TransactionAsyncState } from './types';
22+
import { TransactionAsyncState, TransactionListOptions } from './types';
2323

2424
const path = '/transactions';
2525

@@ -280,23 +280,41 @@ describe('TransactionAsyncApi', () => {
280280

281281
scope.done();
282282

283-
expect(result).toEqual(mockTransactions);
283+
expect(result).toEqual(response);
284284
});
285285

286286
it('should list transactions with params', async () => {
287287
const response = {
288288
transactions: mockTransactions,
289+
next: 'nextToken',
289290
};
291+
const date = new Date();
292+
const options: TransactionListOptions = {
293+
engine_name: 'test_engine',
294+
tags: ['tag1', 'tag2'],
295+
'created_on.lt': date,
296+
'duration.gt': 1000,
297+
sortBy: {
298+
field: 'created_on',
299+
order: 'desc',
300+
},
301+
next: 'nextToken',
302+
};
303+
290304
const query = {
291305
engine_name: 'test_engine',
292306
tags: ['tag1', 'tag2'],
307+
'created_on.lt': date.getTime(),
308+
'duration.gt': 1000,
309+
$sortby: '-created_on',
310+
next: 'nextToken',
293311
};
294312
const scope = nock(baseUrl).get(path).query(query).reply(200, response);
295-
const result = await api.listTransactions(query);
313+
const result = await api.listTransactions(options);
296314

297315
scope.done();
298316

299-
expect(result).toEqual(mockTransactions);
317+
expect(result).toEqual(response);
300318
});
301319

302320
it('should get transaction', async () => {

src/api/transaction/transactionAsyncApi.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ import {
3131

3232
const ENDPOINT = 'transactions';
3333

34-
type ListResponse = { transactions: TransactionAsync[] };
34+
/**
35+
* @param next Is a continuation token that can be used to fetch the next page.
36+
* `next` value is undefined if there are no more results to fetch.
37+
*/
38+
type ListResponse = { transactions: TransactionAsync[]; next?: string };
39+
3540
type SingleResponse = { transaction: TransactionAsync };
3641
type CancelResponse = {
3742
message?: string;
@@ -54,10 +59,22 @@ export class TransactionAsyncApi extends Base {
5459
};
5560
}
5661

57-
async listTransactions(options?: TransactionListOptions) {
58-
const result = await this.get<ListResponse>(ENDPOINT, options);
62+
async listTransactions(
63+
options?: TransactionListOptions,
64+
): Promise<ListResponse> {
65+
const { sortBy, ...opts } = options ?? ({} as any);
66+
67+
Object.keys(opts).forEach(key => {
68+
if (opts[key] instanceof Date) {
69+
opts[key] = opts[key].getTime();
70+
}
71+
});
72+
73+
if (sortBy) {
74+
opts.$sortby = `${sortBy.order === 'desc' ? '-' : ''}${sortBy.field}`;
75+
}
5976

60-
return result.transactions;
77+
return this.get<ListResponse>(ENDPOINT, opts);
6178
}
6279

6380
async getTransaction(transactionId: string) {

src/api/transaction/types.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
import { Table } from 'apache-arrow';
1818

1919
import { RelationId } from '../../proto/generated/schema';
20+
import {
21+
BooleanOperator,
22+
NumericOperator,
23+
SortBy,
24+
StringOperator,
25+
} from '../../rest';
2026

2127
export type RelValue = string | number | boolean | null | number[];
2228

@@ -239,9 +245,36 @@ export type TransactionAsyncResult = {
239245
results: ArrowRelation[];
240246
};
241247

248+
export type TransactionListSortBy = SortBy<'created_on' | 'duration'>;
249+
250+
/**
251+
* TransactionListOptions represents the options that can be used to list
252+
* transactions which includes filtering, sorting and pagination options. Each
253+
* filter option has a set of operators that can be used to filter.
254+
*
255+
* @param sortBy Is used to perform sorting on a single field of `created_on`
256+
* and `duration` in ascending or descending orders.
257+
* @param next Is used as a continuation token for cursor based pagination.
258+
*/
242259
export type TransactionListOptions = {
260+
id?: string | string[];
261+
created_by?: string | string[];
243262
database_name?: string | string[];
244263
engine_name?: string | string[];
245264
state?: TransactionAsyncState | TransactionAsyncState[];
246265
tags?: string | string[];
247-
};
266+
} & {
267+
[key in
268+
| `id.${StringOperator}`
269+
| `created_by.${StringOperator}`
270+
| `state.${StringOperator}`
271+
| `database_name.${StringOperator}`
272+
| `engine_name.${StringOperator}`
273+
| `tags.${StringOperator}`]?: string | string[];
274+
} &
275+
{ [key in `created_on.${NumericOperator}`]?: number | Date } &
276+
{ [key in `duration.${NumericOperator}`]?: number } &
277+
{ [key in `read_only.${BooleanOperator}`]?: boolean } & {
278+
sortBy?: TransactionListSortBy;
279+
next?: string;
280+
};

src/rest.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ export type RequestOptions = {
3333
onResponse?: (r: ApiResponse) => void;
3434
};
3535

36+
export type SortBy<T extends string> = {
37+
field: T;
38+
order: 'asc' | 'desc';
39+
};
40+
41+
export type BooleanOperator = 'equals';
42+
export type StringOperator = 'equals' | 'contains';
43+
export type NumericOperator = 'lt' | 'lte' | 'gt' | 'gte';
44+
3645
export type PollOptions = {
3746
overheadRate?: number;
3847
maxInterval?: number;

0 commit comments

Comments
 (0)