electrodb 2.10.2 → 2.10.4
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/index.d.ts +3 -2
- package/package.json +1 -1
- package/src/entity.js +36 -51
package/index.d.ts
CHANGED
|
@@ -2930,11 +2930,12 @@ export type ParamRecord<Options = ParamOptions> = <P = Record<string, any>>(
|
|
|
2930
2930
|
options?: Options,
|
|
2931
2931
|
) => P;
|
|
2932
2932
|
|
|
2933
|
-
export class ElectroError extends Error {
|
|
2933
|
+
export class ElectroError<E extends Error = Error> extends Error {
|
|
2934
2934
|
readonly name: "ElectroError";
|
|
2935
2935
|
readonly code: number;
|
|
2936
2936
|
readonly date: number;
|
|
2937
2937
|
readonly isElectroError: boolean;
|
|
2938
|
+
readonly cause: E | undefined;
|
|
2938
2939
|
ref: {
|
|
2939
2940
|
readonly code: number;
|
|
2940
2941
|
readonly section: string;
|
|
@@ -2967,7 +2968,7 @@ export interface ElectroValidationErrorFieldReference<T extends Error = Error> {
|
|
|
2967
2968
|
|
|
2968
2969
|
export class ElectroValidationError<
|
|
2969
2970
|
T extends Error = Error,
|
|
2970
|
-
> extends ElectroError {
|
|
2971
|
+
> extends ElectroError<T> {
|
|
2971
2972
|
readonly fields: ReadonlyArray<ElectroValidationErrorFieldReference<T>>;
|
|
2972
2973
|
}
|
|
2973
2974
|
|
package/package.json
CHANGED
package/src/entity.js
CHANGED
|
@@ -184,36 +184,6 @@ class Entity {
|
|
|
184
184
|
return this.model.version;
|
|
185
185
|
}
|
|
186
186
|
|
|
187
|
-
// ownsItem(item) {
|
|
188
|
-
// return (
|
|
189
|
-
// item &&
|
|
190
|
-
// this.getName() === item[this.identifiers.entity] &&
|
|
191
|
-
// this.getVersion() === item[this.identifiers.version] &&
|
|
192
|
-
// validations.isStringHasLength(item[this.identifiers.entity]) &&
|
|
193
|
-
// validations.isStringHasLength(item[this.identifiers.version])
|
|
194
|
-
// ) || !!this.ownsKeys(item)
|
|
195
|
-
// }
|
|
196
|
-
|
|
197
|
-
// ownsKeys({keys = {}}) {
|
|
198
|
-
// let {pk, sk} = this.model.prefixes[TableIndex];
|
|
199
|
-
// let hasSK = this.model.lookup.indexHasSortKeys[TableIndex];
|
|
200
|
-
// let pkMatch = typeof keys[pk.field] === "string" && keys[pk.field].startsWith(pk.prefix);
|
|
201
|
-
// let skMatch = pkMatch && !hasSK;
|
|
202
|
-
// if (pkMatch && hasSK) {
|
|
203
|
-
// skMatch = typeof keys[sk.field] === "string" && keys[sk.field].startsWith(sk.prefix);
|
|
204
|
-
// }
|
|
205
|
-
//
|
|
206
|
-
// return (pkMatch && skMatch &&
|
|
207
|
-
// this._formatKeysToItem(TableIndex, key) !== null);
|
|
208
|
-
// }
|
|
209
|
-
|
|
210
|
-
// ownsCursor({ cursor }) {
|
|
211
|
-
// if (typeof cursor === 'string') {
|
|
212
|
-
// cursor = u.cursorFormatter.deserialize(cursor);
|
|
213
|
-
// }
|
|
214
|
-
// return this.ownsKeys({ keys: cursor });
|
|
215
|
-
// }
|
|
216
|
-
|
|
217
187
|
ownsItem(item) {
|
|
218
188
|
return (
|
|
219
189
|
item &&
|
|
@@ -246,13 +216,18 @@ class Entity {
|
|
|
246
216
|
ownsKeys(key = {}) {
|
|
247
217
|
let { pk, sk } = this.model.prefixes[TableIndex];
|
|
248
218
|
let hasSK = this.model.lookup.indexHasSortKeys[TableIndex];
|
|
249
|
-
|
|
250
|
-
|
|
219
|
+
const typeofPkProvided = typeof key[pk.field];
|
|
220
|
+
const pkPrefixMatch =
|
|
221
|
+
typeofPkProvided === "string" && key[pk.field].startsWith(pk.prefix);
|
|
222
|
+
const isNumericPk = typeofPkProvided === "number" && pk.cast === "number";
|
|
223
|
+
let pkMatch = pkPrefixMatch || isNumericPk;
|
|
251
224
|
let skMatch = pkMatch && !hasSK;
|
|
252
225
|
if (pkMatch && hasSK) {
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
key[sk.field].startsWith(sk.prefix);
|
|
226
|
+
const typeofSkProvided = typeof key[sk.field];
|
|
227
|
+
const skPrefixMatch =
|
|
228
|
+
typeofSkProvided === "string" && key[sk.field].startsWith(sk.prefix);
|
|
229
|
+
const isNumericSk = typeofSkProvided === "number" && sk.cast === "number";
|
|
230
|
+
skMatch = skPrefixMatch || isNumericSk;
|
|
256
231
|
}
|
|
257
232
|
|
|
258
233
|
return (
|
|
@@ -519,7 +494,7 @@ class Entity {
|
|
|
519
494
|
async go(method, parameters = {}, config = {}) {
|
|
520
495
|
let stackTrace;
|
|
521
496
|
if (!config.originalErr) {
|
|
522
|
-
stackTrace = new e.ElectroError(e.ErrorCodes.AWSError);
|
|
497
|
+
stackTrace = new e.ElectroError(e.ErrorCodes.AWSError).stack;
|
|
523
498
|
}
|
|
524
499
|
try {
|
|
525
500
|
switch (method) {
|
|
@@ -538,12 +513,13 @@ class Entity {
|
|
|
538
513
|
return Promise.reject(err);
|
|
539
514
|
} else {
|
|
540
515
|
if (err.__isAWSError) {
|
|
541
|
-
|
|
516
|
+
const error = new e.ElectroError(
|
|
542
517
|
e.ErrorCodes.AWSError,
|
|
543
518
|
`Error thrown by DynamoDB client: "${err.message}"`,
|
|
544
519
|
err,
|
|
545
|
-
)
|
|
546
|
-
|
|
520
|
+
);
|
|
521
|
+
error.stack = stackTrace;
|
|
522
|
+
return Promise.reject(error);
|
|
547
523
|
} else if (err.isElectroError) {
|
|
548
524
|
return Promise.reject(err);
|
|
549
525
|
} else {
|
|
@@ -950,7 +926,7 @@ class Entity {
|
|
|
950
926
|
formatResponse(response, index, config = {}) {
|
|
951
927
|
let stackTrace;
|
|
952
928
|
if (!config.originalErr) {
|
|
953
|
-
stackTrace = new e.ElectroError(e.ErrorCodes.AWSError);
|
|
929
|
+
stackTrace = new e.ElectroError(e.ErrorCodes.AWSError).stack;
|
|
954
930
|
}
|
|
955
931
|
try {
|
|
956
932
|
let results = {};
|
|
@@ -1038,11 +1014,17 @@ class Entity {
|
|
|
1038
1014
|
|
|
1039
1015
|
return { data: results };
|
|
1040
1016
|
} catch (err) {
|
|
1041
|
-
if (config.originalErr || stackTrace === undefined) {
|
|
1017
|
+
if (config.originalErr || stackTrace === undefined || err.isElectroError) {
|
|
1042
1018
|
throw err;
|
|
1043
1019
|
} else {
|
|
1044
|
-
|
|
1045
|
-
|
|
1020
|
+
const error = new e.ElectroError(
|
|
1021
|
+
e.ErrorCodes.AWSError,
|
|
1022
|
+
err.message,
|
|
1023
|
+
err,
|
|
1024
|
+
);
|
|
1025
|
+
error.stack = stackTrace;
|
|
1026
|
+
|
|
1027
|
+
throw error;
|
|
1046
1028
|
}
|
|
1047
1029
|
}
|
|
1048
1030
|
}
|
|
@@ -1481,7 +1463,7 @@ class Entity {
|
|
|
1481
1463
|
}
|
|
1482
1464
|
|
|
1483
1465
|
_createKeyDeconstructor(prefixes = {}, labels = [], attributes = {}) {
|
|
1484
|
-
let { prefix, isCustom, postfix } = prefixes;
|
|
1466
|
+
let { prefix, isCustom, postfix, cast } = prefixes;
|
|
1485
1467
|
let names = [];
|
|
1486
1468
|
let types = [];
|
|
1487
1469
|
let pattern = `^${this._regexpEscape(prefix || "")}`;
|
|
@@ -1512,16 +1494,19 @@ class Entity {
|
|
|
1512
1494
|
let regex = new RegExp(pattern, "i");
|
|
1513
1495
|
|
|
1514
1496
|
return ({ key } = {}) => {
|
|
1515
|
-
|
|
1497
|
+
const typeofKey = typeof key;
|
|
1498
|
+
if (!["string", "number"].includes(typeofKey)) {
|
|
1516
1499
|
return null;
|
|
1517
1500
|
}
|
|
1518
1501
|
key = `${key}`;
|
|
1502
|
+
const isNumeric =
|
|
1503
|
+
cast === CastKeyOptions.number && typeofKey === "number";
|
|
1519
1504
|
let match = key.match(regex);
|
|
1520
1505
|
let results = {};
|
|
1521
|
-
if (match) {
|
|
1506
|
+
if (match || isNumeric) {
|
|
1522
1507
|
for (let i = 0; i < names.length; i++) {
|
|
1523
|
-
let
|
|
1524
|
-
let value = match[i + 1];
|
|
1508
|
+
let keyName = names[i];
|
|
1509
|
+
let value = isNumeric ? key : match[i + 1];
|
|
1525
1510
|
let type = types[i];
|
|
1526
1511
|
switch (type) {
|
|
1527
1512
|
case "number": {
|
|
@@ -1533,8 +1518,8 @@ class Entity {
|
|
|
1533
1518
|
break;
|
|
1534
1519
|
}
|
|
1535
1520
|
}
|
|
1536
|
-
if (
|
|
1537
|
-
results[
|
|
1521
|
+
if (keyName && value !== undefined) {
|
|
1522
|
+
results[keyName] = value;
|
|
1538
1523
|
}
|
|
1539
1524
|
}
|
|
1540
1525
|
} else {
|
|
@@ -1565,7 +1550,7 @@ class Entity {
|
|
|
1565
1550
|
let skComposites = {};
|
|
1566
1551
|
if (indexHasSortKey) {
|
|
1567
1552
|
const sk = keys[skName];
|
|
1568
|
-
if (
|
|
1553
|
+
if (sk === undefined) {
|
|
1569
1554
|
return null;
|
|
1570
1555
|
}
|
|
1571
1556
|
skComposites = deconstructors.sk({ key: sk });
|