Skip to content

Commit

Permalink
feat: able to use custom procedure (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
MicBun authored Dec 18, 2024
1 parent 106aea3 commit 1b565dd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/contracts-api/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,4 +515,35 @@ export class Stream {
)
.throw();
}

/**
* A custom method that accepts the procedure name and the input of GetRecordInput
* Returns the result of the procedure in the same format as StreamRecord
* I.e. a custom procedure named "get_price" that returns a list of date_value and value
* can be called with customGetProcedure("get_price", { dateFrom: "2021-01-01", dateTo: "2021-01-31" })
*/
public async customGetProcedure(
procedure: string,
input: GetRecordInput,
): Promise<StreamRecord[]> {
const result = await this.call<{ date_value: string; value: string }[]>(
procedure,
[
ActionInput.fromObject({
$date_from: input.dateFrom,
$date_to: input.dateTo,
$frozen_at: input.frozenAt,
$base_date: input.baseDate,
}),
],
);
return result
.mapRight((result) =>
result.map((row) => ({
dateValue: row.date_value,
value: row.value,
})),
)
.throw();
}
}
14 changes: 14 additions & 0 deletions tests/integration/primitiveStream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ describe.sequential(
expect(records[0].value).toBe("1.000000000000000000");
expect(records[0].dateValue).toBe("2020-01-01");

// Use Custom Procedure with the same name "get_record"
const customRecords = await primitiveStream.customGetProcedure(
"get_record",
{
dateFrom: "2020-01-01",
dateTo: "2021-01-01",
},
);

// Verify record content from the custom procedure
expect(customRecords.length).toBe(1);
expect(customRecords[0].value).toBe("1.000000000000000000");
expect(customRecords[0].dateValue).toBe("2020-01-01");

// Query index
const index = await primitiveStream.getIndex({
dateFrom: "2020-01-01",
Expand Down

0 comments on commit 1b565dd

Please sign in to comment.