@decaf-ts/for-fabric 0.1.15 → 0.1.17
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.
- package/README.md +5 -0
- package/dist/for-fabric.cjs +1 -1
- package/dist/for-fabric.cjs.map +1 -1
- package/dist/for-fabric.js +1 -1
- package/dist/for-fabric.js.map +1 -1
- package/lib/contracts/ContractAdapter.cjs +0 -4
- package/lib/contracts/ContractAdapter.d.ts +1 -2
- package/lib/contracts/ContractAdapter.js.map +1 -1
- package/lib/contracts/ContractPrivateDataAdapter.cjs +0 -5
- package/lib/contracts/ContractPrivateDataAdapter.js.map +1 -1
- package/lib/contracts/index.cjs +1 -2
- package/lib/contracts/index.d.ts +1 -2
- package/lib/contracts/index.js.map +1 -1
- package/lib/esm/contracts/ContractAdapter.d.ts +1 -2
- package/lib/esm/contracts/ContractAdapter.js +0 -4
- package/lib/esm/contracts/ContractAdapter.js.map +1 -1
- package/lib/esm/contracts/ContractPrivateDataAdapter.js +0 -5
- package/lib/esm/contracts/ContractPrivateDataAdapter.js.map +1 -1
- package/lib/esm/contracts/index.d.ts +1 -2
- package/lib/esm/contracts/index.js +1 -2
- package/lib/esm/contracts/index.js.map +1 -1
- package/lib/esm/version.d.ts +1 -1
- package/lib/esm/version.js +1 -1
- package/lib/version.cjs +1 -1
- package/lib/version.d.ts +1 -1
- package/package.json +1 -1
- package/lib/contracts/FabricContractSequence.cjs +0 -119
- package/lib/contracts/FabricContractSequence.d.ts +0 -50
- package/lib/contracts/FabricContractSequence.js.map +0 -1
- package/lib/esm/contracts/FabricContractSequence.d.ts +0 -50
- package/lib/esm/contracts/FabricContractSequence.js +0 -115
- package/lib/esm/contracts/FabricContractSequence.js.map +0 -1
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import { Context, InternalError, NotFoundError, OperationKeys, } from "@decaf-ts/db-decorators";
|
|
2
|
-
import { Repository } from "@decaf-ts/core";
|
|
3
|
-
import { Sequence } from "@decaf-ts/core";
|
|
4
|
-
import { SequenceModel } from "./../shared/model/Sequence.js";
|
|
5
|
-
/**
|
|
6
|
-
* @summary Abstract implementation of a Sequence
|
|
7
|
-
* @description provides the basic functionality for {@link Sequence}s
|
|
8
|
-
*
|
|
9
|
-
* @param {SequenceOptions} options
|
|
10
|
-
*
|
|
11
|
-
* @class CouchDBSequence
|
|
12
|
-
* @implements Sequence
|
|
13
|
-
*/
|
|
14
|
-
export class FabricContractSequence extends Sequence {
|
|
15
|
-
constructor(options, adapter) {
|
|
16
|
-
super(options, adapter);
|
|
17
|
-
this.repo = Repository.forModel(SequenceModel, adapter.alias);
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* @summary Retrieves the current value for the sequence
|
|
21
|
-
* @protected
|
|
22
|
-
*/
|
|
23
|
-
async current(...args) {
|
|
24
|
-
const contextArgs = await Context.args(OperationKeys.READ, SequenceModel, args, this.adapter);
|
|
25
|
-
const ctx = contextArgs.context;
|
|
26
|
-
const { name, startWith } = this.options;
|
|
27
|
-
try {
|
|
28
|
-
const sequence = await this.repo.read(name, ctx);
|
|
29
|
-
return this.parse(sequence.current);
|
|
30
|
-
}
|
|
31
|
-
catch (e) {
|
|
32
|
-
if (e instanceof NotFoundError) {
|
|
33
|
-
if (typeof startWith === "undefined")
|
|
34
|
-
throw new InternalError("Starting value is not defined for a non existing sequence");
|
|
35
|
-
try {
|
|
36
|
-
return this.parse(startWith);
|
|
37
|
-
}
|
|
38
|
-
catch (e) {
|
|
39
|
-
throw new InternalError(`Failed to parse initial value for sequence ${startWith}: ${e}`);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
throw new InternalError(`Failed to retrieve current value for sequence ${name}: ${e}`);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* @summary increments the sequence
|
|
47
|
-
* @description Sequence specific implementation
|
|
48
|
-
*
|
|
49
|
-
* @param {string | number | bigint} current
|
|
50
|
-
* @param count
|
|
51
|
-
* @protected
|
|
52
|
-
*/
|
|
53
|
-
async increment(current, count, ctx) {
|
|
54
|
-
const { type, incrementBy, name } = this.options;
|
|
55
|
-
let next;
|
|
56
|
-
const toIncrementBy = count || incrementBy;
|
|
57
|
-
if (toIncrementBy % incrementBy !== 0)
|
|
58
|
-
throw new InternalError(`Value to increment does not consider the incrementBy setting: ${incrementBy}`);
|
|
59
|
-
switch (type) {
|
|
60
|
-
case "Number":
|
|
61
|
-
next = this.parse(current) + toIncrementBy;
|
|
62
|
-
break;
|
|
63
|
-
case "BigInt":
|
|
64
|
-
next = this.parse(current) + BigInt(toIncrementBy);
|
|
65
|
-
break;
|
|
66
|
-
default:
|
|
67
|
-
throw new InternalError("Should never happen");
|
|
68
|
-
}
|
|
69
|
-
let seq;
|
|
70
|
-
try {
|
|
71
|
-
seq = await this.repo.update(new SequenceModel({ id: name, current: next }), ctx);
|
|
72
|
-
}
|
|
73
|
-
catch (e) {
|
|
74
|
-
if (!(e instanceof NotFoundError))
|
|
75
|
-
throw e;
|
|
76
|
-
seq = await this.repo.create(new SequenceModel({ id: name, current: next }), ctx);
|
|
77
|
-
}
|
|
78
|
-
return seq.current;
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* @description Gets the next value in the sequence
|
|
82
|
-
* @summary Retrieves the current value of the sequence and increments it by the
|
|
83
|
-
* configured increment amount. This is the main method used to get a new sequential value.
|
|
84
|
-
* @return A promise that resolves to the next value in the sequence
|
|
85
|
-
*/
|
|
86
|
-
async next(...argz) {
|
|
87
|
-
const contextArgs = await Context.args(OperationKeys.UPDATE, SequenceModel, argz, this.adapter);
|
|
88
|
-
const { context, args } = contextArgs;
|
|
89
|
-
const current = await this.current(...args);
|
|
90
|
-
return this.increment(current, undefined, context);
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* @description Generates a range of sequential values
|
|
94
|
-
* @summary Retrieves a specified number of sequential values from the sequence.
|
|
95
|
-
* This is useful when you need to allocate multiple IDs at once.
|
|
96
|
-
* The method increments the sequence by the total amount needed and returns all values in the range.
|
|
97
|
-
* @param {number} count - The number of sequential values to generate
|
|
98
|
-
* @return A promise that resolves to an array of sequential values
|
|
99
|
-
*/
|
|
100
|
-
async range(count, ...argz) {
|
|
101
|
-
const contextArgs = await Context.args(OperationKeys.UPDATE, SequenceModel, argz, this.adapter);
|
|
102
|
-
const { context, args } = contextArgs;
|
|
103
|
-
const current = (await this.current(...args));
|
|
104
|
-
const incrementBy = this.parse(this.options.incrementBy);
|
|
105
|
-
const next = await this.increment(current, this.parse(count) * incrementBy, context);
|
|
106
|
-
const range = [];
|
|
107
|
-
for (let i = 1; i <= count; i++) {
|
|
108
|
-
range.push(current + incrementBy * this.parse(i));
|
|
109
|
-
}
|
|
110
|
-
if (range[range.length - 1] !== next && this.options.type !== "String")
|
|
111
|
-
throw new InternalError("Miscalculation of range");
|
|
112
|
-
return range;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
//# sourceMappingURL=FabricContractSequence.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"FabricContractSequence.js","sourceRoot":"","sources":["../../../src/contracts/FabricContractSequence.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,aAAa,EACb,aAAa,EACb,aAAa,GACd,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,UAAU,EAAmB,MAAM,gBAAgB,CAAC;AAG7D,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,sCAAiC;AAGzD;;;;;;;;GAQG;AACH,MAAM,OAAO,sBAAuB,SAAQ,QAAQ;IAGlD,YACE,OAAwB,EACxB,OAA6D;QAE7D,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC,QAAQ,CAAC,aAAa,EAAE,OAAO,CAAC,KAAK,CAAQ,CAAC;IACvE,CAAC;IAED;;;OAGG;IACH,KA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