Skip to content

Commit bf6672b

Browse files
authoredMay 15, 2023
fix: entity with sort fails to query without sort (#355)
1 parent c0e2501 commit bf6672b

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed
 

‎apps/tests/aws-runtime/test/test-service.ts

+11
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,11 @@ export const allCountersByN = counter.index("allCountersByN", {
569569
sort: ["n"],
570570
});
571571

572+
export const countersByNamespace = counter.index("countersOrderedByNamespace", {
573+
partition: ["id"],
574+
sort: ["namespace"],
575+
});
576+
572577
export const countersByN = counter.index("countersByN", {
573578
sort: ["n"],
574579
});
@@ -660,6 +665,12 @@ export const entityIndexTask = task(
660665
namespace: e.value.namespace,
661666
}))
662667
),
668+
countersByNamespace.query({ id }).then((q) =>
669+
q.entries?.map((e) => ({
670+
n: e.value.n,
671+
namespace: e.value.namespace,
672+
}))
673+
),
663674
countersByN.query({ namespace: "another", id }).then((q) =>
664675
q.entries?.map((e) => ({
665676
n: e.value.n,

‎apps/tests/aws-runtime/test/tester.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,20 @@ eventualRuntimeTestHarness(
149149
n: 1000,
150150
},
151151
],
152+
[
153+
{
154+
namespace: "another",
155+
n: 1000,
156+
},
157+
{
158+
namespace: "default",
159+
n: 6,
160+
},
161+
{
162+
namespace: "different",
163+
n: 1,
164+
},
165+
],
152166
[
153167
{
154168
namespace: "another",

‎packages/@eventual/aws-runtime/src/stores/entity-store.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ import {
1515
} from "@aws-sdk/client-dynamodb";
1616
import { marshall, unmarshall } from "@aws-sdk/util-dynamodb";
1717
import {
18-
Entity,
1918
Attributes,
19+
Entity,
2020
EntityConsistencyOptions,
21+
EntityIndex,
2122
EntityQueryOptions,
2223
EntityQueryResult,
24+
EntityReadOptions,
2325
EntitySetOptions,
2426
EntityWithMetadata,
2527
TransactionCancelled,
2628
TransactionConflict,
2729
UnexpectedVersion,
28-
EntityIndex,
29-
EntityReadOptions,
3030
} from "@eventual/core";
3131
import {
3232
EntityProvider,
@@ -204,7 +204,9 @@ export class AWSEntityStore extends EntityStore {
204204
IndexName: entity.kind === "EntityIndex" ? entity.name : undefined,
205205
ConsistentRead: options?.consistentRead,
206206
KeyConditionExpression:
207-
queryKey.sort && queryKey.sort.keyValue !== undefined
207+
queryKey.sort &&
208+
(queryKey.sort.keyValue !== undefined ||
209+
queryKey.sort.keyValue === "")
208210
? queryKey.sort.partialValue
209211
? `${formatAttributeNameMapKey(
210212
queryKey.partition.keyAttribute
@@ -224,7 +226,9 @@ export class AWSEntityStore extends EntityStore {
224226
typeof queryKey.partition.keyValue === "number"
225227
? { N: queryKey.partition.keyValue.toString() }
226228
: { S: queryKey.partition.keyValue },
227-
...(queryKey.sort && queryKey.sort.keyValue !== undefined
229+
...(queryKey.sort &&
230+
(queryKey.sort.keyValue !== undefined ||
231+
queryKey.sort.keyValue === "")
228232
? {
229233
":sk":
230234
typeof queryKey.sort.keyValue === "string"

‎packages/@eventual/core-runtime/src/stores/entity-store.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -351,12 +351,17 @@ function formatNormalizedPart(
351351
attributes: keyPart.attributes,
352352
parts,
353353
keyAttribute: keyPart.keyAttribute,
354-
keyValue: (keyPart.type === "number"
355-
? parts[0]?.value
356-
: (missingValueIndex === -1 ? parts : parts.slice(0, missingValueIndex))
357-
.map((p) => p.value)
358-
.join("#")) as any,
359-
partialValue: missingValueIndex !== -1,
354+
keyValue:
355+
// if there are no present values, return undefined
356+
missingValueIndex === 0
357+
? undefined
358+
: keyPart.type === "number"
359+
? parts[0]!.value
360+
: (missingValueIndex === -1 ? parts : parts.slice(0, missingValueIndex))
361+
.map((p) => p.value)
362+
.join("#"),
363+
// since true is more permissive (allows undefined), hack the types here to allow any value
364+
partialValue: (missingValueIndex !== -1) as true,
360365
};
361366
}
362367

0 commit comments

Comments
 (0)
Please sign in to comment.