@distrohelena/canton-typescript-sdk 0.1.8 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -44,7 +44,7 @@ export class GrpcContractQueryClient {
44
44
  args.where?.contractId?.in !== undefined ||
45
45
  args.where?.contractId?.is !== undefined ||
46
46
  args.where?.contractId?.isNot !== undefined ||
47
- args.where?.templateId !== undefined) {
47
+ (args.where?.templateId !== undefined && typeof args.where.templateId.equals !== "string")) {
48
48
  throw new QueryCapabilityError(QuerySource.grpc, "contracts.findMany");
49
49
  }
50
50
  const findArgs = args;
@@ -55,6 +55,11 @@ export class GrpcContractQueryClient {
55
55
  let rows = snapshot.filter((row) => args.where?.contractId?.equals === undefined
56
56
  ? true
57
57
  : row.contractId === args.where.contractId.equals);
58
+ if (args.where?.payload !== undefined)
59
+ rows = rows.filter((row) => matchesPayload(row.payload, args.where.payload));
60
+ const legacyTemplate = args.where?.templateId;
61
+ if (legacyTemplate?.equals !== undefined)
62
+ rows = rows.filter((row) => `${row.templateId.packageId}:${row.templateId.moduleName}:${row.templateId.entityName}` === legacyTemplate.equals);
58
63
  return rows;
59
64
  }
60
65
  unsupported(operation) {
@@ -99,7 +104,7 @@ export class GrpcContractQueryClient {
99
104
  function hasUnsupportedFilter(where) {
100
105
  if (where === undefined)
101
106
  return false;
102
- if ("and" in where || "or" in where || "not" in where || "payload" in where || "createdEventOffset" in where || "createdAt" in where || "archivedEventOffset" in where || "archivedAt" in where)
107
+ if ("and" in where || "or" in where || "not" in where || "createdEventOffset" in where || "createdAt" in where || "archivedEventOffset" in where || "archivedAt" in where)
103
108
  return true;
104
109
  for (const field of ["contractId", "templateId"]) {
105
110
  const filter = where[field];
@@ -115,7 +120,7 @@ function mapGrpcContract(value) {
115
120
  contractId: row.contractId ?? "",
116
121
  templateId: { packageId: template?.packageId ?? "", moduleName: template?.moduleName ?? "", entityName: template?.entityName ?? "" },
117
122
  packageId: null,
118
- payload: undefined,
123
+ payload: row.payload,
119
124
  witnesses: [],
120
125
  createdEventOffset: "",
121
126
  createdAt: null,
@@ -124,3 +129,24 @@ function mapGrpcContract(value) {
124
129
  active: true,
125
130
  };
126
131
  }
132
+ function matchesPayload(value, filter) {
133
+ const match = filter.match;
134
+ if (match === undefined)
135
+ return false;
136
+ const visit = (current, node) => Object.entries(node).every(([key, child]) => {
137
+ const next = current !== null && typeof current === "object" ? current[key] : undefined;
138
+ const predicate = child;
139
+ if (Object.keys(predicate).some((name) => ["equals", "lt", "lte", "gt", "gte", "like", "ilike"].includes(name)))
140
+ return compare(String(next ?? ""), predicate);
141
+ return visit(next, predicate);
142
+ });
143
+ return visit(value, match);
144
+ }
145
+ function compare(value, filter) { if (filter.equals !== undefined)
146
+ return value === filter.equals; if (filter.like !== undefined || filter.ilike !== undefined) {
147
+ const pattern = String(filter.like ?? filter.ilike).replace(/[.*+?^${}()|[\]\\]/g, "\\$&").replaceAll("%", ".*").replaceAll("_", ".");
148
+ return new RegExp(`^${pattern}$`, filter.ilike === undefined ? "" : "i").test(value);
149
+ } if (filter.lt !== undefined)
150
+ return value < String(filter.lt); if (filter.lte !== undefined)
151
+ return value <= String(filter.lte); if (filter.gt !== undefined)
152
+ return value > String(filter.gt); return value >= String(filter.gte); }
@@ -106,9 +106,14 @@ type PayloadValueFilter = {
106
106
  readonly gte?: never;
107
107
  readonly like?: never;
108
108
  };
109
- export type ContractPayloadFilter = {
109
+ export type PayloadMatch = {
110
+ readonly [field: string]: PayloadMatch | PayloadValueFilter;
111
+ };
112
+ export type ContractPayloadFilter = ({
110
113
  readonly path: string;
111
- } & PayloadValueFilter;
114
+ } & PayloadValueFilter) | {
115
+ readonly match: PayloadMatch;
116
+ };
112
117
  type ContractWhereFields = {
113
118
  readonly contractId?: StringFilter;
114
119
  readonly templateId?: Partial<{
@@ -72,18 +72,30 @@ function compileWhere(where, addValue) {
72
72
  }
73
73
  if (key === "payload") {
74
74
  const payload = value;
75
- const path = payload.path;
76
- if (typeof path !== "string" || path.split(".").some((x) => x.length === 0))
77
- throw new Error("payload path must contain non-empty segments");
78
- const ops = ["equals", "lt", "lte", "gt", "gte", "like", "ilike"].filter((op) => payload[op] !== undefined);
79
- if (ops.length !== 1 || typeof payload[ops[0]] !== "string")
80
- throw new Error("payload requires exactly one string predicate");
81
- const op = { equals: "=", lt: "<", lte: "<=", gt: ">", gte: ">=", like: "like", ilike: "ilike" }[ops[0]];
82
- parts.push(`contract_row.payload #>> ${addValue(path.split("."))}::text[] ${op} ${addValue(payload[ops[0]])}`);
75
+ const compilePayload = (path, filter) => { const ops = ["equals", "lt", "lte", "gt", "gte", "like", "ilike"].filter((op) => filter[op] !== undefined); if (ops.length === 1) {
76
+ const op = { equals: "=", lt: "<", lte: "<=", gt: ">", gte: ">=", like: "like", ilike: "ilike" }[ops[0]];
77
+ parts.push(`contract_row.payload #>> ${addValue(path)}::text[] ${op} ${addValue(filter[ops[0]])}`);
78
+ return;
79
+ } for (const [name, child] of Object.entries(filter))
80
+ compilePayload([...path, name], child); };
81
+ if (payload.match !== undefined) {
82
+ compilePayload([], payload.match);
83
+ }
84
+ else {
85
+ const path = payload.path;
86
+ if (typeof path !== "string" || path.split(".").some((x) => x.length === 0))
87
+ throw new Error("payload path must contain non-empty segments");
88
+ compilePayload(path.split("."), payload);
89
+ }
83
90
  continue;
84
91
  }
85
92
  if (key === "templateId") {
86
93
  const fields = { packageId: "contract_row.creation_package_id", moduleName: "contract_tpe_row.module_name", entityName: "contract_tpe_row.entity_name" };
94
+ const legacy = value;
95
+ if (typeof legacy.equals === "string") {
96
+ parts.push(`(contract_row.creation_package_id || ':' || contract_tpe_row.module_name || ':' || contract_tpe_row.entity_name) = ${addValue(legacy.equals)}`);
97
+ continue;
98
+ }
87
99
  for (const [name, filter] of Object.entries(value))
88
100
  for (const [op, operand] of Object.entries(filter)) {
89
101
  const sql = { equals: "=", lt: "<", lte: "<=", gt: ">", gte: ">=", like: "like", ilike: "ilike" }[op];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@distrohelena/canton-typescript-sdk",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",