dynamo-bao 0.1.5 → 0.1.7
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/package.json +3 -2
- package/src/exceptions.js +97 -0
- package/src/fields.js +61 -16
- package/src/filter-expression.js +35 -22
- package/src/index.js +6 -2
- package/src/key-condition.js +34 -20
- package/src/mixins/mutation-mixin.js +226 -120
- package/src/mixins/query-mixin.js +8 -2
- package/src/mixins/unique-constraint-mixin.js +12 -3
- package/src/mixins/validation-mixin.js +27 -10
- package/src/model-manager.js +5 -4
- package/src/model.js +78 -38
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dynamo-bao",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "Simple powerful data modeling primitives for DynamoDB in JavaScript.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -45,7 +45,8 @@
|
|
|
45
45
|
"jest": "^29.7.0",
|
|
46
46
|
"jsdoc": "^4.0.4",
|
|
47
47
|
"prettier": "3.4.2",
|
|
48
|
-
"serverless": "^3.38.0"
|
|
48
|
+
"serverless": "^3.38.0",
|
|
49
|
+
"formidable": ">=2.1.3"
|
|
49
50
|
},
|
|
50
51
|
"bin": {
|
|
51
52
|
"bao-init": "bin/dynamo-bao-create-table.js",
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview
|
|
3
|
+
* Custom exception classes for dynamo-bao library.
|
|
4
|
+
* These provide more specific error types than generic Error objects.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Base class for all dynamo-bao exceptions
|
|
9
|
+
*/
|
|
10
|
+
class BaoError extends Error {
|
|
11
|
+
constructor(message, code = null) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = this.constructor.name;
|
|
14
|
+
this.code = code;
|
|
15
|
+
Error.captureStackTrace(this, this.constructor);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Thrown when model configuration is invalid
|
|
21
|
+
* Examples: missing modelPrefix, invalid field names, invalid index configurations
|
|
22
|
+
*/
|
|
23
|
+
class ConfigurationError extends BaoError {
|
|
24
|
+
constructor(message, modelName = null) {
|
|
25
|
+
super(message);
|
|
26
|
+
this.modelName = modelName;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Thrown when field validation fails
|
|
32
|
+
* Examples: required field missing, invalid ULID format, type validation failures
|
|
33
|
+
*/
|
|
34
|
+
class ValidationError extends BaoError {
|
|
35
|
+
constructor(message, fieldName = null, value = null) {
|
|
36
|
+
super(message);
|
|
37
|
+
this.fieldName = fieldName;
|
|
38
|
+
this.value = value;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Thrown when query construction or execution fails
|
|
44
|
+
* Examples: invalid operators, field not found, index not found, malformed conditions
|
|
45
|
+
*/
|
|
46
|
+
class QueryError extends BaoError {
|
|
47
|
+
constructor(message, indexName = null, fieldName = null) {
|
|
48
|
+
super(message);
|
|
49
|
+
this.indexName = indexName;
|
|
50
|
+
this.fieldName = fieldName;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Thrown when a database item is not found
|
|
56
|
+
* This is distinct from the existing ObjectNotFound class which is a result wrapper
|
|
57
|
+
*/
|
|
58
|
+
class ItemNotFoundError extends BaoError {
|
|
59
|
+
constructor(message = "Item not found", primaryId = null) {
|
|
60
|
+
super(message);
|
|
61
|
+
this.primaryId = primaryId;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Thrown when DynamoDB operations fail due to conditions
|
|
67
|
+
* Examples: conditional check failures, unique constraint violations
|
|
68
|
+
*/
|
|
69
|
+
class ConditionalError extends BaoError {
|
|
70
|
+
constructor(message, operation = null, condition = null) {
|
|
71
|
+
super(message);
|
|
72
|
+
this.operation = operation;
|
|
73
|
+
this.condition = condition;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Thrown when data parsing or conversion fails
|
|
79
|
+
* Examples: invalid primary ID format, JSON parsing errors, type conversion failures
|
|
80
|
+
*/
|
|
81
|
+
class DataFormatError extends BaoError {
|
|
82
|
+
constructor(message, data = null, expectedFormat = null) {
|
|
83
|
+
super(message);
|
|
84
|
+
this.data = data;
|
|
85
|
+
this.expectedFormat = expectedFormat;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
module.exports = {
|
|
90
|
+
BaoError,
|
|
91
|
+
ConfigurationError,
|
|
92
|
+
ValidationError,
|
|
93
|
+
QueryError,
|
|
94
|
+
ItemNotFoundError,
|
|
95
|
+
ConditionalError,
|
|
96
|
+
DataFormatError,
|
|
97
|
+
};
|
package/src/fields.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
const { ulid, decodeTime } = require("ulid");
|
|
7
7
|
const { defaultLogger: logger } = require("./utils/logger");
|
|
8
|
+
const { ValidationError } = require("./exceptions");
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* @class BaoBaseField
|
|
@@ -43,7 +44,7 @@ class BaoBaseField {
|
|
|
43
44
|
*/
|
|
44
45
|
validate(value) {
|
|
45
46
|
if (this.required && (value === null || value === undefined)) {
|
|
46
|
-
throw new
|
|
47
|
+
throw new ValidationError("Field is required", null, value);
|
|
47
48
|
}
|
|
48
49
|
return true;
|
|
49
50
|
}
|
|
@@ -155,7 +156,19 @@ class BaoBaseField {
|
|
|
155
156
|
* A field that stores a string value.
|
|
156
157
|
*/
|
|
157
158
|
class StringField extends BaoBaseField {
|
|
158
|
-
|
|
159
|
+
validate(value) {
|
|
160
|
+
if (this.required && !value) {
|
|
161
|
+
throw new ValidationError("Field is required", null, value);
|
|
162
|
+
}
|
|
163
|
+
if (value && this.maxLength && value.length > this.maxLength) {
|
|
164
|
+
throw new ValidationError(
|
|
165
|
+
`Value exceeds maximum length of ${this.maxLength}`,
|
|
166
|
+
null,
|
|
167
|
+
value,
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
159
172
|
}
|
|
160
173
|
|
|
161
174
|
/**
|
|
@@ -167,7 +180,7 @@ class StringField extends BaoBaseField {
|
|
|
167
180
|
class DateTimeField extends BaoBaseField {
|
|
168
181
|
validate(value) {
|
|
169
182
|
if (this.required && value === undefined) {
|
|
170
|
-
throw new
|
|
183
|
+
throw new ValidationError("Field is required", null, value);
|
|
171
184
|
}
|
|
172
185
|
if (
|
|
173
186
|
value !== undefined &&
|
|
@@ -175,8 +188,10 @@ class DateTimeField extends BaoBaseField {
|
|
|
175
188
|
typeof value !== "number" &&
|
|
176
189
|
typeof value !== "string"
|
|
177
190
|
) {
|
|
178
|
-
throw new
|
|
191
|
+
throw new ValidationError(
|
|
179
192
|
"DateTimeField value must be a Date object, timestamp number, or ISO string",
|
|
193
|
+
null,
|
|
194
|
+
value,
|
|
180
195
|
);
|
|
181
196
|
}
|
|
182
197
|
}
|
|
@@ -376,21 +391,25 @@ class UlidField extends BaoBaseField {
|
|
|
376
391
|
|
|
377
392
|
validate(value) {
|
|
378
393
|
if (!value && !this.autoAssign) {
|
|
379
|
-
throw new
|
|
394
|
+
throw new ValidationError("ULID is required", null, value);
|
|
380
395
|
}
|
|
381
396
|
|
|
382
397
|
if (value) {
|
|
383
398
|
// Check if it's a valid ULID format
|
|
384
399
|
// ULIDs are 26 characters, uppercase alphanumeric
|
|
385
400
|
if (!/^[0-9A-Z]{26}$/.test(value)) {
|
|
386
|
-
throw new
|
|
401
|
+
throw new ValidationError("Invalid ULID format", null, value);
|
|
387
402
|
}
|
|
388
403
|
|
|
389
404
|
try {
|
|
390
405
|
// Attempt to decode the timestamp to verify it's valid
|
|
391
406
|
decodeTime(value);
|
|
392
407
|
} catch (error) {
|
|
393
|
-
throw new
|
|
408
|
+
throw new ValidationError(
|
|
409
|
+
"Invalid ULID: could not decode timestamp",
|
|
410
|
+
null,
|
|
411
|
+
value,
|
|
412
|
+
);
|
|
394
413
|
}
|
|
395
414
|
}
|
|
396
415
|
return true;
|
|
@@ -421,7 +440,7 @@ class RelatedField extends BaoBaseField {
|
|
|
421
440
|
|
|
422
441
|
validate(value) {
|
|
423
442
|
if (this.required && !value) {
|
|
424
|
-
throw new
|
|
443
|
+
throw new ValidationError("Field is required", null, value);
|
|
425
444
|
}
|
|
426
445
|
// Allow both string IDs and model instances
|
|
427
446
|
if (
|
|
@@ -429,8 +448,10 @@ class RelatedField extends BaoBaseField {
|
|
|
429
448
|
typeof value !== "string" &&
|
|
430
449
|
(!value.getPrimaryId || typeof value.getPrimaryId !== "function")
|
|
431
450
|
) {
|
|
432
|
-
throw new
|
|
451
|
+
throw new ValidationError(
|
|
433
452
|
"Related field value must be a string ID or model instance",
|
|
453
|
+
null,
|
|
454
|
+
value,
|
|
434
455
|
);
|
|
435
456
|
}
|
|
436
457
|
return true;
|
|
@@ -475,7 +496,11 @@ class CounterField extends BaoBaseField {
|
|
|
475
496
|
}
|
|
476
497
|
// Accept regular integer values
|
|
477
498
|
if (!Number.isInteger(value)) {
|
|
478
|
-
throw new
|
|
499
|
+
throw new ValidationError(
|
|
500
|
+
"CounterField value must be an integer",
|
|
501
|
+
null,
|
|
502
|
+
value,
|
|
503
|
+
);
|
|
479
504
|
}
|
|
480
505
|
}
|
|
481
506
|
return true;
|
|
@@ -576,7 +601,11 @@ class BinaryField extends BaoBaseField {
|
|
|
576
601
|
!(value instanceof Buffer) &&
|
|
577
602
|
!(value instanceof Uint8Array)
|
|
578
603
|
) {
|
|
579
|
-
throw new
|
|
604
|
+
throw new ValidationError(
|
|
605
|
+
"BinaryField value must be a Buffer or Uint8Array",
|
|
606
|
+
null,
|
|
607
|
+
value,
|
|
608
|
+
);
|
|
580
609
|
}
|
|
581
610
|
return true;
|
|
582
611
|
}
|
|
@@ -593,7 +622,11 @@ class BinaryField extends BaoBaseField {
|
|
|
593
622
|
}
|
|
594
623
|
|
|
595
624
|
toGsi(value) {
|
|
596
|
-
throw new
|
|
625
|
+
throw new ValidationError(
|
|
626
|
+
"BinaryField does not support GSI conversion",
|
|
627
|
+
null,
|
|
628
|
+
value,
|
|
629
|
+
);
|
|
597
630
|
}
|
|
598
631
|
}
|
|
599
632
|
|
|
@@ -619,7 +652,11 @@ class VersionField extends BaoBaseField {
|
|
|
619
652
|
if (value !== undefined) {
|
|
620
653
|
// Verify it's a valid ULID string
|
|
621
654
|
if (typeof value !== "string" || value.length !== 26) {
|
|
622
|
-
throw new
|
|
655
|
+
throw new ValidationError(
|
|
656
|
+
"VersionField value must be a valid ULID",
|
|
657
|
+
null,
|
|
658
|
+
value,
|
|
659
|
+
);
|
|
623
660
|
}
|
|
624
661
|
}
|
|
625
662
|
return true;
|
|
@@ -654,7 +691,11 @@ class BooleanField extends BaoBaseField {
|
|
|
654
691
|
validate(value) {
|
|
655
692
|
super.validate(value);
|
|
656
693
|
if (value !== undefined && value !== null && typeof value !== "boolean") {
|
|
657
|
-
throw new
|
|
694
|
+
throw new ValidationError(
|
|
695
|
+
"BooleanField value must be a boolean",
|
|
696
|
+
null,
|
|
697
|
+
value,
|
|
698
|
+
);
|
|
658
699
|
}
|
|
659
700
|
return true;
|
|
660
701
|
}
|
|
@@ -699,7 +740,7 @@ class TtlField extends DateTimeField {
|
|
|
699
740
|
if (value === null || value === undefined) {
|
|
700
741
|
return true; // Allow null/undefined for field removal
|
|
701
742
|
}
|
|
702
|
-
|
|
743
|
+
super.validate(value); // Use parent validation for dates
|
|
703
744
|
}
|
|
704
745
|
|
|
705
746
|
toDy(value) {
|
|
@@ -714,7 +755,11 @@ class TtlField extends DateTimeField {
|
|
|
714
755
|
} else {
|
|
715
756
|
date = new Date(value);
|
|
716
757
|
if (isNaN(date.getTime())) {
|
|
717
|
-
throw new
|
|
758
|
+
throw new ValidationError(
|
|
759
|
+
"Invalid date value provided for TTL field",
|
|
760
|
+
null,
|
|
761
|
+
value,
|
|
762
|
+
);
|
|
718
763
|
}
|
|
719
764
|
}
|
|
720
765
|
|
package/src/filter-expression.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
const { defaultLogger: logger } = require("./utils/logger");
|
|
2
|
+
const { QueryError, ValidationError } = require("./exceptions");
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* Supporting class for building DynamoDB filter expressions.
|
|
3
6
|
* @class
|
|
@@ -59,13 +62,22 @@ class FilterExpressionBuilder {
|
|
|
59
62
|
case "$beginsWith":
|
|
60
63
|
return `begins_with(${nameKey}, ${this.generateValue(convertedValue)})`;
|
|
61
64
|
case "$in":
|
|
62
|
-
if (!Array.isArray(
|
|
63
|
-
throw new
|
|
65
|
+
if (!Array.isArray(value)) {
|
|
66
|
+
throw new ValidationError("$in operator requires an array value");
|
|
64
67
|
}
|
|
65
|
-
const valueKeys =
|
|
68
|
+
const valueKeys = value.map((v) => this.generateValue(v));
|
|
66
69
|
return `${nameKey} IN (${valueKeys.join(", ")})`;
|
|
70
|
+
case "$exists":
|
|
71
|
+
if (typeof value !== "boolean") {
|
|
72
|
+
throw new ValidationError(
|
|
73
|
+
"$exists operator requires a boolean value",
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
return value
|
|
77
|
+
? `attribute_exists(${nameKey})`
|
|
78
|
+
: `attribute_not_exists(${nameKey})`;
|
|
67
79
|
default:
|
|
68
|
-
throw new
|
|
80
|
+
throw new QueryError(`Unsupported operator: ${operator}`);
|
|
69
81
|
}
|
|
70
82
|
}
|
|
71
83
|
|
|
@@ -82,7 +94,7 @@ class FilterExpressionBuilder {
|
|
|
82
94
|
|
|
83
95
|
const operators = Object.keys(condition);
|
|
84
96
|
if (operators.length === 0) {
|
|
85
|
-
throw new
|
|
97
|
+
throw new QueryError(`Empty condition object for field: ${fieldName}`);
|
|
86
98
|
}
|
|
87
99
|
|
|
88
100
|
const expressions = operators.map((operator) => {
|
|
@@ -100,7 +112,7 @@ class FilterExpressionBuilder {
|
|
|
100
112
|
// Build expression for logical operators
|
|
101
113
|
buildLogicalExpression(operator, conditions, model) {
|
|
102
114
|
if (!Array.isArray(conditions)) {
|
|
103
|
-
throw new
|
|
115
|
+
throw new QueryError(`${operator} requires an array of conditions`);
|
|
104
116
|
}
|
|
105
117
|
|
|
106
118
|
const expressions = conditions.map((condition) => {
|
|
@@ -201,8 +213,9 @@ class FilterExpressionBuilder {
|
|
|
201
213
|
}
|
|
202
214
|
|
|
203
215
|
// Validate field exists in model
|
|
204
|
-
|
|
205
|
-
|
|
216
|
+
const field = model.fields[key];
|
|
217
|
+
if (!field) {
|
|
218
|
+
throw new QueryError(`Unknown field in filter: ${key}`);
|
|
206
219
|
}
|
|
207
220
|
|
|
208
221
|
// Recursively validate nested conditions
|
|
@@ -214,20 +227,20 @@ class FilterExpressionBuilder {
|
|
|
214
227
|
) {
|
|
215
228
|
const operators = Object.keys(value);
|
|
216
229
|
operators.forEach((op) => {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
) {
|
|
230
|
-
throw new
|
|
230
|
+
const validOperators = [
|
|
231
|
+
"$eq",
|
|
232
|
+
"$ne",
|
|
233
|
+
"$gt",
|
|
234
|
+
"$gte",
|
|
235
|
+
"$lt",
|
|
236
|
+
"$lte",
|
|
237
|
+
"$in",
|
|
238
|
+
"$contains",
|
|
239
|
+
"$beginsWith",
|
|
240
|
+
"$exists",
|
|
241
|
+
];
|
|
242
|
+
if (!validOperators.includes(op)) {
|
|
243
|
+
throw new QueryError(`Invalid operator ${op} for field ${key}`);
|
|
231
244
|
}
|
|
232
245
|
});
|
|
233
246
|
}
|
package/src/index.js
CHANGED
|
@@ -6,6 +6,8 @@ const { ModelManager } = require("./model-manager");
|
|
|
6
6
|
const { defaultLogger: logger } = require("./utils/logger");
|
|
7
7
|
const fields = require("./fields");
|
|
8
8
|
const constants = require("./constants");
|
|
9
|
+
const exceptions = require("./exceptions");
|
|
10
|
+
const { ConfigurationError } = require("./exceptions");
|
|
9
11
|
|
|
10
12
|
const {
|
|
11
13
|
BaoModel,
|
|
@@ -35,8 +37,7 @@ function findModelFiles(dir) {
|
|
|
35
37
|
|
|
36
38
|
function _registerModels(manager = null, modelsDir = null) {
|
|
37
39
|
if (!modelsDir) {
|
|
38
|
-
throw new
|
|
39
|
-
return;
|
|
40
|
+
throw new ConfigurationError("modelsDir is required");
|
|
40
41
|
}
|
|
41
42
|
|
|
42
43
|
const modelFiles = findModelFiles(modelsDir);
|
|
@@ -116,6 +117,9 @@ const firstExport = {
|
|
|
116
117
|
// Export the constants module directly
|
|
117
118
|
constants,
|
|
118
119
|
|
|
120
|
+
// Export the exceptions module
|
|
121
|
+
exceptions,
|
|
122
|
+
|
|
119
123
|
models: {},
|
|
120
124
|
};
|
|
121
125
|
|
package/src/key-condition.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
const { defaultLogger: logger } = require("./utils/logger");
|
|
2
|
+
const { QueryError } = require("./exceptions");
|
|
3
|
+
|
|
1
4
|
class KeyConditionBuilder {
|
|
2
5
|
constructor() {
|
|
3
6
|
this.names = {};
|
|
@@ -29,20 +32,27 @@ class KeyConditionBuilder {
|
|
|
29
32
|
validateSortKeyField(model, indexName, fieldName) {
|
|
30
33
|
const index = model.indexes[indexName];
|
|
31
34
|
if (!index) {
|
|
32
|
-
throw new
|
|
35
|
+
throw new QueryError(
|
|
36
|
+
`Index "${indexName}" not found in ${model.name}`,
|
|
37
|
+
indexName,
|
|
38
|
+
);
|
|
33
39
|
}
|
|
34
40
|
|
|
35
41
|
if (index.sk === "modelPrefix") {
|
|
36
|
-
throw new
|
|
42
|
+
throw new QueryError(
|
|
37
43
|
`Cannot query by sort key on index "${indexName}" as it uses modelPrefix`,
|
|
44
|
+
indexName,
|
|
45
|
+
fieldName,
|
|
38
46
|
);
|
|
39
47
|
}
|
|
40
48
|
|
|
41
49
|
const skField = index.sk;
|
|
42
50
|
if (skField !== fieldName) {
|
|
43
|
-
throw new
|
|
51
|
+
throw new QueryError(
|
|
44
52
|
`Field "${fieldName}" is not the sort key for index "${indexName}". ` +
|
|
45
53
|
`Expected "${skField}"`,
|
|
54
|
+
indexName,
|
|
55
|
+
fieldName,
|
|
46
56
|
);
|
|
47
57
|
}
|
|
48
58
|
}
|
|
@@ -86,7 +96,9 @@ class KeyConditionBuilder {
|
|
|
86
96
|
|
|
87
97
|
case "$between":
|
|
88
98
|
if (!Array.isArray(value) || value.length !== 2) {
|
|
89
|
-
throw new
|
|
99
|
+
throw new QueryError(
|
|
100
|
+
"$between requires an array with exactly 2 elements",
|
|
101
|
+
);
|
|
90
102
|
}
|
|
91
103
|
return {
|
|
92
104
|
condition: `${nameKey} BETWEEN ${this.generateValue(value[0])} AND ${this.generateValue(value[1])}`,
|
|
@@ -111,7 +123,7 @@ class KeyConditionBuilder {
|
|
|
111
123
|
};
|
|
112
124
|
|
|
113
125
|
default:
|
|
114
|
-
throw new
|
|
126
|
+
throw new QueryError(`Unsupported sort key operator: ${operator}`);
|
|
115
127
|
}
|
|
116
128
|
}
|
|
117
129
|
|
|
@@ -167,7 +179,7 @@ class KeyConditionBuilder {
|
|
|
167
179
|
buildKeyCondition(model, indexName, condition, gsiSortKeyName) {
|
|
168
180
|
// Validate the condition format
|
|
169
181
|
if (!condition || typeof condition !== "object") {
|
|
170
|
-
throw new
|
|
182
|
+
throw new QueryError("Invalid condition format");
|
|
171
183
|
}
|
|
172
184
|
|
|
173
185
|
const [[fieldName, fieldCondition]] = Object.entries(condition);
|
|
@@ -175,8 +187,10 @@ class KeyConditionBuilder {
|
|
|
175
187
|
// Validate that this field is the sort key for the index
|
|
176
188
|
const index = model.indexes[indexName];
|
|
177
189
|
if (!index || index.sk !== fieldName) {
|
|
178
|
-
throw new
|
|
190
|
+
throw new QueryError(
|
|
179
191
|
`Field "${fieldName}" is not the sort key for index "${indexName}"`,
|
|
192
|
+
indexName,
|
|
193
|
+
fieldName,
|
|
180
194
|
);
|
|
181
195
|
}
|
|
182
196
|
|
|
@@ -201,13 +215,13 @@ class KeyConditionBuilder {
|
|
|
201
215
|
"$lte",
|
|
202
216
|
];
|
|
203
217
|
if (!validOperators.includes(operator)) {
|
|
204
|
-
throw new
|
|
218
|
+
throw new QueryError(`Unsupported sort key operator: ${operator}`);
|
|
205
219
|
}
|
|
206
220
|
|
|
207
221
|
switch (operator) {
|
|
208
222
|
case "$between":
|
|
209
223
|
if (!Array.isArray(value) || value.length !== 2) {
|
|
210
|
-
throw new
|
|
224
|
+
throw new QueryError(
|
|
211
225
|
"$between requires an array with exactly 2 elements",
|
|
212
226
|
);
|
|
213
227
|
}
|
|
@@ -238,20 +252,20 @@ class KeyConditionBuilder {
|
|
|
238
252
|
};
|
|
239
253
|
|
|
240
254
|
default:
|
|
241
|
-
throw new
|
|
255
|
+
throw new QueryError(`Unsupported sort key operator: ${operator}`);
|
|
256
|
+
}
|
|
257
|
+
} else {
|
|
258
|
+
// Handle direct value (treat as equality)
|
|
259
|
+
if (fieldCondition === undefined) {
|
|
260
|
+
throw new QueryError("Sort key condition value cannot be undefined");
|
|
242
261
|
}
|
|
243
|
-
}
|
|
244
262
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
263
|
+
return {
|
|
264
|
+
condition: `${nameKey} = :sortKeyValue`,
|
|
265
|
+
names,
|
|
266
|
+
values: { ":sortKeyValue": field.toGsi(fieldCondition) },
|
|
267
|
+
};
|
|
248
268
|
}
|
|
249
|
-
|
|
250
|
-
return {
|
|
251
|
-
condition: `${nameKey} = :sortKeyValue`,
|
|
252
|
-
names,
|
|
253
|
-
values: { ":sortKeyValue": field.toGsi(fieldCondition) },
|
|
254
|
-
};
|
|
255
269
|
}
|
|
256
270
|
}
|
|
257
271
|
|