@decaf-ts/for-fabric 0.1.2 → 0.1.3

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.
Files changed (155) hide show
  1. package/dist/for-fabric.cjs +1 -1
  2. package/dist/for-fabric.cjs.map +1 -1
  3. package/dist/for-fabric.js +1 -1
  4. package/dist/for-fabric.js.map +1 -1
  5. package/lib/client/FabricClientAdapter.cjs +94 -81
  6. package/lib/client/FabricClientAdapter.d.ts +24 -33
  7. package/lib/client/FabricClientAdapter.js.map +1 -1
  8. package/lib/client/FabricClientDispatch.cjs +14 -5
  9. package/lib/client/FabricClientDispatch.d.ts +7 -5
  10. package/lib/client/FabricClientDispatch.js.map +1 -1
  11. package/lib/client/FabricClientRepository.cjs +4 -137
  12. package/lib/client/FabricClientRepository.d.ts +6 -78
  13. package/lib/client/FabricClientRepository.js.map +1 -1
  14. package/lib/client/erc20/FabricERC20ClientRepository.cjs +17 -17
  15. package/lib/client/erc20/FabricERC20ClientRepository.d.ts +5 -5
  16. package/lib/client/erc20/FabricERC20ClientRepository.js.map +1 -1
  17. package/lib/client/logging.cjs +1 -3
  18. package/lib/client/logging.js.map +1 -1
  19. package/lib/client/services/FabricEnrollmentService.cjs +3 -3
  20. package/lib/client/services/FabricEnrollmentService.d.ts +2 -2
  21. package/lib/client/services/FabricEnrollmentService.js.map +1 -1
  22. package/lib/contracts/ContractAdapter.cjs +204 -115
  23. package/lib/contracts/ContractAdapter.d.ts +26 -34
  24. package/lib/contracts/ContractAdapter.js.map +1 -1
  25. package/lib/contracts/ContractContext.cjs +1 -9
  26. package/lib/contracts/ContractContext.d.ts +0 -6
  27. package/lib/contracts/ContractContext.js.map +1 -1
  28. package/lib/contracts/ContractPrivateDataAdapter.cjs +429 -288
  29. package/lib/contracts/ContractPrivateDataAdapter.d.ts +0 -76
  30. package/lib/contracts/ContractPrivateDataAdapter.js.map +1 -1
  31. package/lib/contracts/FabricConstruction.cjs +3 -3
  32. package/lib/contracts/FabricConstruction.d.ts +9 -8
  33. package/lib/contracts/FabricConstruction.js.map +1 -1
  34. package/lib/contracts/FabricContractRepository.cjs +7 -202
  35. package/lib/contracts/FabricContractRepository.d.ts +5 -68
  36. package/lib/contracts/FabricContractRepository.js.map +1 -1
  37. package/lib/contracts/FabricContractRepositoryObservableHandler.cjs +4 -1
  38. package/lib/contracts/FabricContractRepositoryObservableHandler.d.ts +4 -4
  39. package/lib/contracts/FabricContractRepositoryObservableHandler.js.map +1 -1
  40. package/lib/contracts/FabricContractSequence.cjs +39 -60
  41. package/lib/contracts/FabricContractSequence.d.ts +29 -41
  42. package/lib/contracts/FabricContractSequence.js.map +1 -1
  43. package/lib/contracts/FabricContractStatement.cjs +6 -6
  44. package/lib/contracts/FabricContractStatement.d.ts +4 -6
  45. package/lib/contracts/FabricContractStatement.js.map +1 -1
  46. package/lib/contracts/crud/crud-contract.cjs +74 -44
  47. package/lib/contracts/crud/crud-contract.d.ts +7 -11
  48. package/lib/contracts/crud/crud-contract.js.map +1 -1
  49. package/lib/contracts/crud/serialized-crud-contract.cjs +12 -21
  50. package/lib/contracts/crud/serialized-crud-contract.d.ts +3 -11
  51. package/lib/contracts/crud/serialized-crud-contract.js.map +1 -1
  52. package/lib/contracts/erc20/erc20contract.cjs +73 -67
  53. package/lib/contracts/erc20/erc20contract.d.ts +13 -12
  54. package/lib/contracts/erc20/erc20contract.js.map +1 -1
  55. package/lib/contracts/logging.cjs +9 -15
  56. package/lib/contracts/logging.d.ts +1 -2
  57. package/lib/contracts/logging.js.map +1 -1
  58. package/lib/contracts/private-data.cjs +202 -118
  59. package/lib/contracts/private-data.d.ts +0 -16
  60. package/lib/contracts/private-data.js.map +1 -1
  61. package/lib/contracts/types.d.ts +1 -1
  62. package/lib/esm/client/FabricClientAdapter.d.ts +24 -33
  63. package/lib/esm/client/FabricClientAdapter.js +90 -78
  64. package/lib/esm/client/FabricClientAdapter.js.map +1 -1
  65. package/lib/esm/client/FabricClientDispatch.d.ts +7 -5
  66. package/lib/esm/client/FabricClientDispatch.js +17 -8
  67. package/lib/esm/client/FabricClientDispatch.js.map +1 -1
  68. package/lib/esm/client/FabricClientRepository.d.ts +6 -78
  69. package/lib/esm/client/FabricClientRepository.js +4 -137
  70. package/lib/esm/client/FabricClientRepository.js.map +1 -1
  71. package/lib/esm/client/erc20/FabricERC20ClientRepository.d.ts +5 -5
  72. package/lib/esm/client/erc20/FabricERC20ClientRepository.js +17 -17
  73. package/lib/esm/client/erc20/FabricERC20ClientRepository.js.map +1 -1
  74. package/lib/esm/client/logging.js +1 -3
  75. package/lib/esm/client/logging.js.map +1 -1
  76. package/lib/esm/client/services/FabricEnrollmentService.d.ts +2 -2
  77. package/lib/esm/client/services/FabricEnrollmentService.js +3 -3
  78. package/lib/esm/client/services/FabricEnrollmentService.js.map +1 -1
  79. package/lib/esm/contracts/ContractAdapter.d.ts +26 -34
  80. package/lib/esm/contracts/ContractAdapter.js +205 -116
  81. package/lib/esm/contracts/ContractAdapter.js.map +1 -1
  82. package/lib/esm/contracts/ContractContext.d.ts +0 -6
  83. package/lib/esm/contracts/ContractContext.js +1 -9
  84. package/lib/esm/contracts/ContractContext.js.map +1 -1
  85. package/lib/esm/contracts/ContractPrivateDataAdapter.d.ts +0 -76
  86. package/lib/esm/contracts/ContractPrivateDataAdapter.js +430 -285
  87. package/lib/esm/contracts/ContractPrivateDataAdapter.js.map +1 -1
  88. package/lib/esm/contracts/FabricConstruction.d.ts +9 -8
  89. package/lib/esm/contracts/FabricConstruction.js +3 -3
  90. package/lib/esm/contracts/FabricConstruction.js.map +1 -1
  91. package/lib/esm/contracts/FabricContractRepository.d.ts +5 -68
  92. package/lib/esm/contracts/FabricContractRepository.js +7 -202
  93. package/lib/esm/contracts/FabricContractRepository.js.map +1 -1
  94. package/lib/esm/contracts/FabricContractRepositoryObservableHandler.d.ts +4 -4
  95. package/lib/esm/contracts/FabricContractRepositoryObservableHandler.js +5 -2
  96. package/lib/esm/contracts/FabricContractRepositoryObservableHandler.js.map +1 -1
  97. package/lib/esm/contracts/FabricContractSequence.d.ts +29 -41
  98. package/lib/esm/contracts/FabricContractSequence.js +40 -61
  99. package/lib/esm/contracts/FabricContractSequence.js.map +1 -1
  100. package/lib/esm/contracts/FabricContractStatement.d.ts +4 -6
  101. package/lib/esm/contracts/FabricContractStatement.js +7 -7
  102. package/lib/esm/contracts/FabricContractStatement.js.map +1 -1
  103. package/lib/esm/contracts/crud/crud-contract.d.ts +7 -11
  104. package/lib/esm/contracts/crud/crud-contract.js +75 -45
  105. package/lib/esm/contracts/crud/crud-contract.js.map +1 -1
  106. package/lib/esm/contracts/crud/serialized-crud-contract.d.ts +3 -11
  107. package/lib/esm/contracts/crud/serialized-crud-contract.js +12 -21
  108. package/lib/esm/contracts/crud/serialized-crud-contract.js.map +1 -1
  109. package/lib/esm/contracts/erc20/erc20contract.d.ts +13 -12
  110. package/lib/esm/contracts/erc20/erc20contract.js +73 -67
  111. package/lib/esm/contracts/erc20/erc20contract.js.map +1 -1
  112. package/lib/esm/contracts/logging.d.ts +1 -2
  113. package/lib/esm/contracts/logging.js +9 -15
  114. package/lib/esm/contracts/logging.js.map +1 -1
  115. package/lib/esm/contracts/private-data.d.ts +0 -16
  116. package/lib/esm/contracts/private-data.js +203 -111
  117. package/lib/esm/contracts/private-data.js.map +1 -1
  118. package/lib/esm/contracts/types.d.ts +1 -1
  119. package/lib/esm/shared/decorators.d.ts +14 -4
  120. package/lib/esm/shared/decorators.js +142 -42
  121. package/lib/esm/shared/decorators.js.map +1 -1
  122. package/lib/esm/shared/errors.js +2 -2
  123. package/lib/esm/shared/errors.js.map +1 -1
  124. package/lib/esm/shared/events.d.ts +1 -5
  125. package/lib/esm/shared/events.js.map +1 -1
  126. package/lib/esm/shared/model/Sequence.d.ts +14 -0
  127. package/lib/esm/shared/model/Sequence.js +33 -0
  128. package/lib/esm/shared/model/Sequence.js.map +1 -0
  129. package/lib/esm/shared/model/index.d.ts +2 -0
  130. package/lib/esm/shared/model/index.js +2 -0
  131. package/lib/esm/shared/model/index.js.map +1 -1
  132. package/lib/esm/version.d.ts +1 -1
  133. package/lib/esm/version.js +1 -1
  134. package/lib/shared/decorators.cjs +148 -41
  135. package/lib/shared/decorators.d.ts +14 -4
  136. package/lib/shared/decorators.js.map +1 -1
  137. package/lib/shared/errors.cjs +2 -2
  138. package/lib/shared/errors.js.map +1 -1
  139. package/lib/shared/events.d.ts +1 -5
  140. package/lib/shared/events.js.map +1 -1
  141. package/lib/shared/model/Sequence.cjs +36 -0
  142. package/lib/shared/model/Sequence.d.ts +14 -0
  143. package/lib/shared/model/Sequence.js.map +1 -0
  144. package/lib/shared/model/index.cjs +2 -0
  145. package/lib/shared/model/index.d.ts +2 -0
  146. package/lib/shared/model/index.js.map +1 -1
  147. package/lib/version.cjs +1 -1
  148. package/lib/version.d.ts +1 -1
  149. package/package.json +2 -3
  150. package/lib/client/services/LoggedService.cjs +0 -47
  151. package/lib/client/services/LoggedService.d.ts +0 -42
  152. package/lib/client/services/LoggedService.js.map +0 -1
  153. package/lib/esm/client/services/LoggedService.d.ts +0 -42
  154. package/lib/esm/client/services/LoggedService.js +0 -43
  155. package/lib/esm/client/services/LoggedService.js.map +0 -1
@@ -1,286 +1,431 @@
1
- import { FabricContractAdapter } from "./ContractAdapter.js";
2
- import { NotFoundError, SerializationError, } from "@decaf-ts/db-decorators";
3
- import { MISSING_PRIVATE_DATA_REGEX, modelToPrivate, processModel, } from "./private-data.js";
4
- import { UnauthorizedPrivateDataAccess } from "./../shared/errors.js";
5
- import { FabricContractSequence } from "./FabricContractSequence.js";
6
- import { CouchDBKeys } from "@decaf-ts/for-couchdb";
7
- export class FabricContractPrivateDataAdapter extends FabricContractAdapter {
8
- /**
9
- * @description Creates a new FabricContractAdapter instance
10
- * @summary Initializes an adapter for interacting with the Fabric state database
11
- * @param {void} scope - Not used in this adapter
12
- * @param {string} [alias] - Optional alias for the adapter instance
13
- */
14
- constructor(scope, alias, collections) {
15
- super(scope, alias);
16
- this.collections = collections;
17
- this.collections = collections || [];
18
- }
19
- async Sequence(options) {
20
- return new FabricContractSequence(options, this, this.collections);
21
- }
22
- /**
23
- * @description Reads a record from the state database
24
- * @summary Retrieves and deserializes a record from the Fabric state database
25
- * @param {string} tableName - The name of the table/collection
26
- * @param {string | number} id - The record identifier
27
- * @param {...any[]} args - Additional arguments, including the chaincode stub and logger
28
- * @return {Promise<Record<string, any>>} Promise resolving to the retrieved record
29
- */
30
- async read(tableName, id, instance, ...args) {
31
- const { stub, logger } = args.pop();
32
- const log = logger.for(this.read);
33
- let model;
34
- try {
35
- const results = await this.readState(stub, tableName, id.toString(), instance);
36
- if (results.length < 1) {
37
- log.debug(`No record found for id ${id} in ${tableName} table`);
38
- throw new NotFoundError(`No record found for id ${id} in ${tableName} table`);
39
- }
40
- else if (results.length < 2) {
41
- log.debug(`No record found for id ${id} in ${tableName} table`);
42
- model = results.pop();
43
- }
44
- else {
45
- model = this.mergeModels(results);
46
- }
47
- }
48
- catch (e) {
49
- throw this.parseError(e);
50
- }
51
- return model;
52
- }
53
- /**
54
- * @description Deletes a record from the state database
55
- * @summary Retrieves a record and then removes it from the Fabric state database
56
- * @param {string} tableName - The name of the table/collection
57
- * @param {string | number} id - The record identifier to delete
58
- * @param {...any[]} args - Additional arguments, including the chaincode stub and logger
59
- * @return {Promise<Record<string, any>>} Promise resolving to the deleted record
60
- */
61
- async delete(tableName, id, instance, ...args) {
62
- const ctx = args.pop();
63
- const { stub, logger } = ctx;
64
- const log = logger.for(this.delete);
65
- args.push(ctx);
66
- let model;
67
- try {
68
- model = await this.read(tableName, id, instance, ...args);
69
- log.verbose(`deleting entry with pk ${id} from ${tableName} table`);
70
- this.deleteState(stub, tableName, id.toString(), instance);
71
- }
72
- catch (e) {
73
- throw this.parseError(e);
74
- }
75
- return model;
76
- }
77
- prepare(model, pk, ...args) {
78
- const { stub, logger } = args.pop();
79
- const tableName = args.shift();
80
- const log = logger.for(this.prepare);
81
- const split = processModel(this, model);
82
- // if ((model as any)[PersistenceKeys.METADATA]) {
83
- // log.silly(
84
- // `Passing along persistence metadata for ${(model as any)[PersistenceKeys.METADATA]}`
85
- // );
86
- // Object.defineProperty(split.result, PersistenceKeys.METADATA, {
87
- // enumerable: false,
88
- // writable: false,
89
- // configurable: true,
90
- // value: (model as any)[PersistenceKeys.METADATA],
91
- // });
92
- // }
93
- log.info(`Preparing record for ${tableName} table with pk ${model[pk]}`);
94
- return {
95
- record: split.privateData,
96
- id: stub.createCompositeKey(tableName, [String(model[pk])]),
97
- transient: split.transient,
98
- };
99
- }
100
- createPrefix(tableName, id, model, ...args) {
101
- const ctx = args.pop();
102
- const collections = Object.keys(model);
103
- for (const collection of collections) {
104
- model[collection][CouchDBKeys.TABLE] = tableName;
105
- }
106
- const record = model;
107
- return [tableName, id, record, ctx];
108
- }
109
- updatePrefix(tableName, id, model, ...args) {
110
- const ctx = args.pop();
111
- const collections = Object.keys(model);
112
- for (const collection of collections) {
113
- model[collection][CouchDBKeys.TABLE] = tableName;
114
- }
115
- const record = model;
116
- return [tableName, id, record, ctx];
117
- }
118
- async putState(stub, id, model,
119
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
120
- ...args) {
121
- const collections = Object.keys(model);
122
- let res = {};
123
- let data;
124
- for (const collection of collections) {
125
- res = model[collection];
126
- try {
127
- data = Buffer.from(FabricContractAdapter.serializer.serialize(model[collection]));
128
- }
129
- catch (e) {
130
- throw new SerializationError(`Failed to serialize record with id ${id}: ${e}`);
131
- }
132
- await stub.putPrivateData(collection, id.toString(), data);
133
- }
134
- return res;
135
- }
136
- async readState(stub, tableName, id, instance,
137
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
138
- ...args) {
139
- const composedKey = stub.createCompositeKey(tableName, [String(id)]);
140
- const model = modelToPrivate(instance);
141
- const collections = Object.keys(model.private);
142
- const results = [];
143
- for (const collection of collections) {
144
- try {
145
- let res = await stub.getPrivateData(collection, composedKey);
146
- if (res.toString() === "") {
147
- throw new NotFoundError(`Entry with id ${id} doesn't exist...`);
148
- }
149
- try {
150
- res = FabricContractAdapter.serializer.deserialize(res.toString()
151
- // model.constructor.name
152
- );
153
- }
154
- catch (e) {
155
- throw new SerializationError(`Failed to parse private data: ${e}`);
156
- }
157
- results.push(res);
158
- }
159
- catch (e) {
160
- if (MISSING_PRIVATE_DATA_REGEX.test(e.message))
161
- throw new UnauthorizedPrivateDataAccess(e);
162
- throw e;
163
- }
164
- }
165
- return results;
166
- }
167
- async deleteState(stub, tableName, id, instance,
168
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
169
- ...args) {
170
- const composedKey = stub.createCompositeKey(tableName, [String(id)]);
171
- const model = modelToPrivate(instance);
172
- const collections = Object.keys(model.private);
173
- for (const collection of collections) {
174
- await stub.deletePrivateData(collection, composedKey);
175
- }
176
- }
177
- async queryResult(stub, rawInput, instance) {
178
- const privateData = modelToPrivate(instance).private;
179
- const collection = Object.keys(privateData)[0] || "";
180
- const result = (await stub.getPrivateDataQueryResult(collection, JSON.stringify(rawInput)));
181
- const iterator = result.iterator;
182
- return iterator;
183
- }
184
- async queryResultPaginated(stub, rawInput, limit = 250, skip = undefined, instance) {
185
- const privateData = modelToPrivate(instance).private;
186
- const collection = Object.keys(privateData)[0] || "";
187
- const iterator = await stub.getPrivateDataQueryResult(collection, JSON.stringify(rawInput));
188
- const results = [];
189
- let count = 0;
190
- let reachedBookmark = skip ? false : true;
191
- let lastKey = null;
192
- while (true) {
193
- const res = await iterator.next();
194
- if (res.value && res.value.value.toString()) {
195
- const recordKey = res.value.key;
196
- const recordValue = res.value.value.toString("utf8");
197
- // If we have a skip, skip until we reach it
198
- if (!reachedBookmark) {
199
- if (recordKey === skip?.toString()) {
200
- reachedBookmark = true;
201
- }
202
- continue;
203
- }
204
- results.push({ Key: recordKey, Record: JSON.parse(recordValue) });
205
- lastKey = recordKey;
206
- count++;
207
- if (count >= limit) {
208
- await iterator.close();
209
- return {
210
- iterator: results,
211
- metadata: {
212
- fetchedRecordsCount: results.length,
213
- bookmark: lastKey,
214
- },
215
- };
216
- }
217
- }
218
- if (res.done) {
219
- await iterator.close();
220
- return {
221
- iterator: results,
222
- metadata: {
223
- fetchedRecordsCount: results.length,
224
- bookmark: "",
225
- },
226
- };
227
- }
228
- }
229
- // return (await stub.getQueryResultWithPagination(
230
- // JSON.stringify(rawInput),
231
- // limit,
232
- // skip?.toString()
233
- // )) as StateQueryResponse<Iterators.StateQueryIterator>;
234
- }
235
- /**
236
- * @description Executes a raw query against the state database
237
- * @summary Performs a rich query using CouchDB syntax against the Fabric state database
238
- * @template R - The return type
239
- * @param {MangoQuery} rawInput - The Mango Query to execute
240
- * @param {boolean} docsOnly - Whether to return only documents (not used in this implementation)
241
- * @param {...any[]} args - Additional arguments, including the chaincode stub and logger
242
- * @return {Promise<R>} Promise resolving to the query results
243
- * @mermaid
244
- * sequenceDiagram
245
- * participant Caller
246
- * participant FabricContractAdapter
247
- * participant Stub
248
- * participant StateDB
249
- *
250
- * Caller->>FabricContractAdapter: raw(rawInput, docsOnly, ctx)
251
- * FabricContractAdapter->>FabricContractAdapter: Extract limit and skip
252
- * alt With pagination
253
- * FabricContractAdapter->>Stub: getQueryResultWithPagination(query, limit, skip)
254
- * else Without pagination
255
- * FabricContractAdapter->>Stub: getQueryResult(query)
256
- * end
257
- * Stub->>StateDB: Execute query
258
- * StateDB-->>Stub: Iterator
259
- * Stub-->>FabricContractAdapter: Iterator
260
- * FabricContractAdapter->>FabricContractAdapter: resultIterator(log, iterator)
261
- * FabricContractAdapter-->>Caller: results
262
- */
263
- async raw(rawInput, docsOnly, ...args) {
264
- const { stub, logger } = args.pop();
265
- const log = logger.for(this.raw);
266
- const { skip, limit } = rawInput;
267
- const instance = args.shift();
268
- let iterator;
269
- if (limit || skip) {
270
- delete rawInput["limit"];
271
- delete rawInput["skip"];
272
- log.debug(`Retrieving paginated iterator: limit: ${limit}/ skip: ${skip}`);
273
- const response = (await this.queryResultPaginated(stub, rawInput, limit || 250, skip?.toString(), instance));
274
- iterator = response.iterator;
275
- }
276
- else {
277
- log.debug("Retrieving iterator");
278
- iterator = (await this.queryResult(stub, rawInput, instance));
279
- }
280
- log.debug("Iterator acquired");
281
- const results = (await this.resultIterator(log, iterator));
282
- log.debug(`returning {0} results`, `${Array.isArray(results) ? results.length : 1}`);
283
- return results;
284
- }
285
- }
1
+ "use strict";
2
+ // import { ChaincodeStub, Iterators, StateQueryResponse } from "fabric-shim";
3
+ // import { FabricContractAdapter } from "./ContractAdapter";
4
+ // import {
5
+ // BaseError,
6
+ // NotFoundError,
7
+ // SerializationError,
8
+ // } from "@decaf-ts/db-decorators";
9
+ // import { Model } from "@decaf-ts/decorator-validation";
10
+ // import {
11
+ // MISSING_PRIVATE_DATA_REGEX,
12
+ // modelToPrivate,
13
+ // processModel,
14
+ // } from "./private-data";
15
+ // import { UnauthorizedPrivateDataAccess } from "../shared/errors";
16
+ // import { FabricContractSequence } from "./FabricContractSequence";
17
+ // import { Sequence, SequenceOptions } from "@decaf-ts/core";
18
+ // import { FabricContractContext } from "./ContractContext";
19
+ // import { CouchDBKeys, MangoQuery } from "@decaf-ts/for-couchdb";
20
+ //
21
+ // export class FabricContractPrivateDataAdapter extends FabricContractAdapter {
22
+ // /**
23
+ // * @description Creates a new FabricContractAdapter instance
24
+ // * @summary Initializes an adapter for interacting with the Fabric state database
25
+ // * @param {void} scope - Not used in this adapter
26
+ // * @param {string} [alias] - Optional alias for the adapter instance
27
+ // */
28
+ // constructor(
29
+ // scope: void,
30
+ // alias?: string,
31
+ // private readonly collections?: string[]
32
+ // ) {
33
+ // super(scope, alias);
34
+ // this.collections = collections || [];
35
+ // }
36
+ //
37
+ // override async Sequence(options: SequenceOptions): Promise<Sequence> {
38
+ // return new FabricContractSequence(options, this, this.collections);
39
+ // }
40
+ //
41
+ // /**
42
+ // * @description Reads a record from the state database
43
+ // * @summary Retrieves and deserializes a record from the Fabric state database
44
+ // * @param {string} tableName - The name of the table/collection
45
+ // * @param {string | number} id - The record identifier
46
+ // * @param {...any[]} args - Additional arguments, including the chaincode stub and logger
47
+ // * @return {Promise<Record<string, any>>} Promise resolving to the retrieved record
48
+ // */
49
+ // override async read(
50
+ // tableName: string,
51
+ // id: string | number,
52
+ // instance: any,
53
+ // ...args: any[]
54
+ // ): Promise<Record<string, any>> {
55
+ // const { stub, logger } = args.pop();
56
+ // const log = logger.for(this.read);
57
+ //
58
+ // let model: Record<string, any>;
59
+ // try {
60
+ // const results = await this.readState(
61
+ // stub,
62
+ // tableName,
63
+ // id.toString(),
64
+ // instance
65
+ // );
66
+ //
67
+ // if (results.length < 1) {
68
+ // log.debug(`No record found for id ${id} in ${tableName} table`);
69
+ // throw new NotFoundError(
70
+ // `No record found for id ${id} in ${tableName} table`
71
+ // );
72
+ // } else if (results.length < 2) {
73
+ // log.debug(`No record found for id ${id} in ${tableName} table`);
74
+ // model = results.pop() as Record<string, any>;
75
+ // } else {
76
+ // model = this.mergeModels(results);
77
+ // }
78
+ // } catch (e: unknown) {
79
+ // throw this.parseError(e as Error);
80
+ // }
81
+ //
82
+ // return model;
83
+ // }
84
+ //
85
+ // /**
86
+ // * @description Deletes a record from the state database
87
+ // * @summary Retrieves a record and then removes it from the Fabric state database
88
+ // * @param {string} tableName - The name of the table/collection
89
+ // * @param {string | number} id - The record identifier to delete
90
+ // * @param {...any[]} args - Additional arguments, including the chaincode stub and logger
91
+ // * @return {Promise<Record<string, any>>} Promise resolving to the deleted record
92
+ // */
93
+ // override async delete(
94
+ // tableName: string,
95
+ // id: string | number,
96
+ // instance: any,
97
+ // ...args: any[]
98
+ // ): Promise<Record<string, any>> {
99
+ // const ctx = args.pop();
100
+ // const { stub, logger } = ctx;
101
+ // const log = logger.for(this.delete);
102
+ //
103
+ // args.push(ctx);
104
+ //
105
+ // let model: Record<string, any>;
106
+ // try {
107
+ // model = await this.read(tableName, id, instance, ...args);
108
+ // log.verbose(`deleting entry with pk ${id} from ${tableName} table`);
109
+ // this.deleteState(stub, tableName, id.toString(), instance);
110
+ // } catch (e: unknown) {
111
+ // throw this.parseError(e as Error);
112
+ // }
113
+ //
114
+ // return model;
115
+ // }
116
+ // override prepare<M extends Model>(
117
+ // model: M,
118
+ // pk: keyof M,
119
+ // ...args: any[]
120
+ // ): {
121
+ // record: Record<string, any>;
122
+ // id: string;
123
+ // transient?: Record<string, any>;
124
+ // } {
125
+ // const { stub, logger } = args.pop();
126
+ // const tableName = args.shift();
127
+ // const log = logger.for(this.prepare);
128
+ //
129
+ // const split = processModel(this, model);
130
+ // // if ((model as any)[PersistenceKeys.METADATA]) {
131
+ // // log.silly(
132
+ // // `Passing along persistence metadata for ${(model as any)[PersistenceKeys.METADATA]}`
133
+ // // );
134
+ // // Object.defineProperty(split.result, PersistenceKeys.METADATA, {
135
+ // // enumerable: false,
136
+ // // writable: false,
137
+ // // configurable: true,
138
+ // // value: (model as any)[PersistenceKeys.METADATA],
139
+ // // });
140
+ // // }
141
+ //
142
+ // log.info(`Preparing record for ${tableName} table with pk ${model[pk]}`);
143
+ //
144
+ // return {
145
+ // record: split.privateData as Record<string, any>,
146
+ // id: stub.createCompositeKey(tableName, [String(model[pk])]),
147
+ // transient: split.transient,
148
+ // };
149
+ // }
150
+ //
151
+ // override createPrefix(
152
+ // tableName: string,
153
+ // id: string | number,
154
+ // model: Record<string, any>,
155
+ // ...args: any[]
156
+ // ) {
157
+ // const ctx: FabricContractContext = args.pop();
158
+ // const collections = Object.keys(model);
159
+ // for (const collection of collections) {
160
+ // model[collection][CouchDBKeys.TABLE] = tableName;
161
+ // }
162
+ //
163
+ // const record: Record<string, any> = model;
164
+ //
165
+ // return [tableName, id, record, ctx];
166
+ // }
167
+ //
168
+ // override updatePrefix(
169
+ // tableName: string,
170
+ // id: string | number,
171
+ // model: Record<string, any>,
172
+ // ...args: any[]
173
+ // ): (string | number | Record<string, any>)[] {
174
+ // const ctx: FabricContractContext = args.pop();
175
+ // const collections = Object.keys(model);
176
+ //
177
+ // for (const collection of collections) {
178
+ // model[collection][CouchDBKeys.TABLE] = tableName;
179
+ // }
180
+ //
181
+ // const record: Record<string, any> = model;
182
+ //
183
+ // return [tableName, id, record, ctx];
184
+ // }
185
+ // override async putState(
186
+ // stub: ChaincodeStub,
187
+ // id: string,
188
+ // model: Record<string, any>,
189
+ // // eslint-disable-next-line @typescript-eslint/no-unused-vars
190
+ // ...args: any[]
191
+ // ) {
192
+ // const collections = Object.keys(model);
193
+ // let res: Record<string, any> = {};
194
+ // let data: Buffer;
195
+ //
196
+ // for (const collection of collections) {
197
+ // res = model![collection];
198
+ // try {
199
+ // data = Buffer.from(
200
+ // FabricContractAdapter.serializer.serialize(
201
+ // model![collection] as Model
202
+ // )
203
+ // );
204
+ // } catch (e: unknown) {
205
+ // throw new SerializationError(
206
+ // `Failed to serialize record with id ${id}: ${e}`
207
+ // );
208
+ // }
209
+ // await stub.putPrivateData(collection, id.toString(), data);
210
+ // }
211
+ //
212
+ // return res;
213
+ // }
214
+ //
215
+ // override async readState(
216
+ // stub: ChaincodeStub,
217
+ // tableName: string,
218
+ // id: string,
219
+ // instance: any,
220
+ // // eslint-disable-next-line @typescript-eslint/no-unused-vars
221
+ // ...args: any[]
222
+ // ) {
223
+ // const composedKey = stub.createCompositeKey(tableName, [String(id)]);
224
+ // const model = modelToPrivate(instance);
225
+ // const collections = Object.keys(model.private!);
226
+ // const results: any[] = [];
227
+ //
228
+ // for (const collection of collections) {
229
+ // try {
230
+ // let res: Buffer | Record<string, any> = await stub.getPrivateData(
231
+ // collection,
232
+ // composedKey
233
+ // );
234
+ // if (res.toString() === "") {
235
+ // throw new NotFoundError(`Entry with id ${id} doesn't exist...`);
236
+ // }
237
+ // try {
238
+ // res = FabricContractAdapter.serializer.deserialize(
239
+ // res.toString()
240
+ // // model.constructor.name
241
+ // );
242
+ // } catch (e: unknown) {
243
+ // throw new SerializationError(`Failed to parse private data: ${e}`);
244
+ // }
245
+ // results.push(res);
246
+ // } catch (e: unknown) {
247
+ // if (MISSING_PRIVATE_DATA_REGEX.test((e as BaseError).message))
248
+ // throw new UnauthorizedPrivateDataAccess(e as BaseError);
249
+ // throw e;
250
+ // }
251
+ // }
252
+ //
253
+ // return results;
254
+ // }
255
+ //
256
+ // override async deleteState(
257
+ // stub: ChaincodeStub,
258
+ // tableName: string,
259
+ // id: string,
260
+ // instance: any,
261
+ // // eslint-disable-next-line @typescript-eslint/no-unused-vars
262
+ // ...args: any[]
263
+ // ) {
264
+ // const composedKey = stub.createCompositeKey(tableName, [String(id)]);
265
+ // const model = modelToPrivate(instance);
266
+ // const collections = Object.keys(model.private!);
267
+ //
268
+ // for (const collection of collections) {
269
+ // await stub.deletePrivateData(collection, composedKey);
270
+ // }
271
+ // }
272
+ //
273
+ // override async queryResult(
274
+ // stub: ChaincodeStub,
275
+ // rawInput: any,
276
+ // instance: any
277
+ // ): Promise<Iterators.StateQueryIterator> {
278
+ // const privateData = modelToPrivate(instance).private!;
279
+ // const collection = Object.keys(privateData)[0] || "";
280
+ //
281
+ // const result = (await stub.getPrivateDataQueryResult(
282
+ // collection,
283
+ // JSON.stringify(rawInput)
284
+ // )) as any;
285
+ //
286
+ // const iterator = result.iterator as Iterators.StateQueryIterator;
287
+ //
288
+ // return iterator;
289
+ // }
290
+ //
291
+ // override async queryResultPaginated(
292
+ // stub: ChaincodeStub,
293
+ // rawInput: any,
294
+ // limit: number = 250,
295
+ // skip: number | undefined = undefined,
296
+ // instance: any
297
+ // ): Promise<StateQueryResponse<Iterators.StateQueryIterator>> {
298
+ // const privateData = modelToPrivate(instance).private!;
299
+ // const collection = Object.keys(privateData)[0] || "";
300
+ //
301
+ // const iterator = await stub.getPrivateDataQueryResult(
302
+ // collection,
303
+ // JSON.stringify(rawInput)
304
+ // );
305
+ //
306
+ // const results: any[] = [];
307
+ // let count = 0;
308
+ // let reachedBookmark = skip ? false : true;
309
+ // let lastKey: string | null = null;
310
+ //
311
+ // while (true) {
312
+ // const res = await iterator.next();
313
+ //
314
+ // if (res.value && res.value.value.toString()) {
315
+ // const recordKey = res.value.key;
316
+ // const recordValue = (res.value.value as any).toString("utf8");
317
+ //
318
+ // // If we have a skip, skip until we reach it
319
+ // if (!reachedBookmark) {
320
+ // if (recordKey === skip?.toString()) {
321
+ // reachedBookmark = true;
322
+ // }
323
+ // continue;
324
+ // }
325
+ //
326
+ // results.push({ Key: recordKey, Record: JSON.parse(recordValue) });
327
+ // lastKey = recordKey;
328
+ // count++;
329
+ //
330
+ // if (count >= limit) {
331
+ // await iterator.close();
332
+ // return {
333
+ // iterator: results as unknown as Iterators.StateQueryIterator,
334
+ // metadata: {
335
+ // fetchedRecordsCount: results.length,
336
+ // bookmark: lastKey,
337
+ // },
338
+ // };
339
+ // }
340
+ // }
341
+ //
342
+ // if (res.done) {
343
+ // await iterator.close();
344
+ // return {
345
+ // iterator: results as unknown as Iterators.StateQueryIterator,
346
+ // metadata: {
347
+ // fetchedRecordsCount: results.length,
348
+ // bookmark: "",
349
+ // },
350
+ // };
351
+ // }
352
+ // }
353
+ // // return (await stub.getQueryResultWithPagination(
354
+ // // JSON.stringify(rawInput),
355
+ // // limit,
356
+ // // skip?.toString()
357
+ // // )) as StateQueryResponse<Iterators.StateQueryIterator>;
358
+ // }
359
+ //
360
+ // /**
361
+ // * @description Executes a raw query against the state database
362
+ // * @summary Performs a rich query using CouchDB syntax against the Fabric state database
363
+ // * @template R - The return type
364
+ // * @param {MangoQuery} rawInput - The Mango Query to execute
365
+ // * @param {boolean} docsOnly - Whether to return only documents (not used in this implementation)
366
+ // * @param {...any[]} args - Additional arguments, including the chaincode stub and logger
367
+ // * @return {Promise<R>} Promise resolving to the query results
368
+ // * @mermaid
369
+ // * sequenceDiagram
370
+ // * participant Caller
371
+ // * participant FabricContractAdapter
372
+ // * participant Stub
373
+ // * participant StateDB
374
+ // *
375
+ // * Caller->>FabricContractAdapter: raw(rawInput, docsOnly, ctx)
376
+ // * FabricContractAdapter->>FabricContractAdapter: Extract limit and skip
377
+ // * alt With pagination
378
+ // * FabricContractAdapter->>Stub: getQueryResultWithPagination(query, limit, skip)
379
+ // * else Without pagination
380
+ // * FabricContractAdapter->>Stub: getQueryResult(query)
381
+ // * end
382
+ // * Stub->>StateDB: Execute query
383
+ // * StateDB-->>Stub: Iterator
384
+ // * Stub-->>FabricContractAdapter: Iterator
385
+ // * FabricContractAdapter->>FabricContractAdapter: resultIterator(log, iterator)
386
+ // * FabricContractAdapter-->>Caller: results
387
+ // */
388
+ // override async raw<R>(
389
+ // rawInput: MangoQuery,
390
+ // docsOnly: boolean,
391
+ // ...args: any[]
392
+ // ): Promise<R> {
393
+ // const { stub, logger } = args.pop();
394
+ // const log = logger.for(this.raw);
395
+ // const { skip, limit } = rawInput;
396
+ // const instance = args.shift();
397
+ // let iterator: Iterators.StateQueryIterator;
398
+ // if (limit || skip) {
399
+ // delete rawInput["limit"];
400
+ // delete rawInput["skip"];
401
+ // log.debug(
402
+ // `Retrieving paginated iterator: limit: ${limit}/ skip: ${skip}`
403
+ // );
404
+ // const response: StateQueryResponse<Iterators.StateQueryIterator> =
405
+ // (await this.queryResultPaginated(
406
+ // stub,
407
+ // rawInput,
408
+ // limit || 250,
409
+ // (skip as any)?.toString(),
410
+ // instance
411
+ // )) as StateQueryResponse<Iterators.StateQueryIterator>;
412
+ // iterator = response.iterator;
413
+ // } else {
414
+ // log.debug("Retrieving iterator");
415
+ // iterator = (await this.queryResult(
416
+ // stub,
417
+ // rawInput,
418
+ // instance
419
+ // )) as Iterators.StateQueryIterator;
420
+ // }
421
+ // log.debug("Iterator acquired");
422
+ //
423
+ // const results = (await this.resultIterator(log, iterator)) as R;
424
+ // log.debug(
425
+ // `returning {0} results`,
426
+ // `${Array.isArray(results) ? results.length : 1}`
427
+ // );
428
+ // return results;
429
+ // }
430
+ // }
286
431
  //# sourceMappingURL=ContractPrivateDataAdapter.js.map