Skip to content

Commit a6d6c9e

Browse files
committed
Fixes 362
This branch fixes issue #362. The root cause of the issue was that one of the composite attributes was hidden. Because the cursor was created using the "formatted" return item, it was the hidden composite was not available to the cursor formatting function. We now check for this unique case and perform some additional calculations. Ideally this reduces the surface area of performance implications to only entities that have a hidden composite attribute defined.
1 parent 320ae2c commit a6d6c9e

File tree

1 file changed

+46
-9
lines changed

1 file changed

+46
-9
lines changed

src/entity.js

+46-9
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,9 @@ class Entity {
733733
let count = 0;
734734
let hydratedUnprocessed = [];
735735
const shouldHydrate = config.hydrate && method === MethodTypes.query;
736+
let allRawItems = [];
737+
let indexHasHiddenComposites = (this.model.lookup.indexHasHiddenComposites[indexName] || this.model.lookup.indexHasHiddenComposites[TableIndex]);
738+
let hasCountOption = typeof config.count === "number";
736739
do {
737740
let limit = max === undefined ? parameters.Limit : max - count;
738741
let response = await this._exec(
@@ -747,6 +750,7 @@ class Entity {
747750
...config,
748751
includeKeys: shouldHydrate || config.includeKeys,
749752
ignoreOwnership: shouldHydrate || config.ignoreOwnership,
753+
includeRaw: hasCountOption && indexHasHiddenComposites,
750754
});
751755

752756
if (config.raw) {
@@ -774,13 +778,16 @@ class Entity {
774778
}
775779
} else if (Array.isArray(response.data)) {
776780
let prevCount = count
777-
if (!!max || !!config.count) {
781+
if (!!max || hasCountOption) {
778782
count += response.data.length;
779783
}
780784
let items = response.data;
781-
const moreItemsThanRequired = !!config.count && count > config.count;
785+
const moreItemsThanRequired = hasCountOption && count > config.count;
786+
let rawItems = response.raw || [];
782787
if (moreItemsThanRequired) {
783-
items = items.slice(0, config.count - prevCount);
788+
const endIndex = config.count - prevCount;
789+
items = items.slice(0, endIndex);
790+
rawItems = rawItems.slice(0, endIndex);
784791
}
785792
if (shouldHydrate) {
786793
const hydrated = await this.hydrate(
@@ -794,9 +801,10 @@ class Entity {
794801
);
795802
}
796803
results = [...results, ...items];
804+
allRawItems = [...allRawItems, ...rawItems];
797805
if (moreItemsThanRequired || count === config.count) {
798-
const lastItem = results[results.length - 1];
799-
ExclusiveStartKey = this._fromCompositeToKeysByIndex({ indexName, provided: lastItem });
806+
const lastRawItem = rawItems[rawItems.length - 1];
807+
ExclusiveStartKey = this._trimKeysToIndex({indexName, provided: lastRawItem});
800808
break;
801809
}
802810
} else {
@@ -955,6 +963,7 @@ class Entity {
955963
stackTrace = new e.ElectroError(e.ErrorCodes.AWSError);
956964
}
957965
try {
966+
let raw = {};
958967
let results = {};
959968
if (validations.isFunction(config.parse)) {
960969
results = config.parse(config, response);
@@ -980,18 +989,24 @@ class Entity {
980989
this.ownsKeys(response.Item)) ||
981990
this.ownsItem(response.Item)
982991
) {
992+
if (config.includeRaw) {
993+
raw = results.Item;
994+
}
983995
results = this.model.schema.formatItemForRetrieval(
984996
response.Item,
985997
config,
986998
);
987999
if (Object.keys(results).length === 0) {
1000+
raw = null;
9881001
results = null;
9891002
}
9901003
} else if (!config._objectOnEmpty) {
1004+
raw = null;
9911005
results = null;
9921006
}
9931007
} else if (response.Items) {
9941008
results = [];
1009+
raw = [];
9951010
for (let item of response.Items) {
9961011
if (
9971012
(config.ignoreOwnership &&
@@ -1008,6 +1023,9 @@ class Entity {
10081023
);
10091024
if (Object.keys(record).length > 0) {
10101025
results.push(record);
1026+
if (config.includeRaw) {
1027+
raw.push(item);
1028+
}
10111029
}
10121030
}
10131031
}
@@ -1017,6 +1035,7 @@ class Entity {
10171035
config,
10181036
);
10191037
if (Object.keys(results).length === 0) {
1038+
raw = null;
10201039
results = null;
10211040
}
10221041
} else if (config._objectOnEmpty) {
@@ -1027,18 +1046,28 @@ class Entity {
10271046
};
10281047
} else {
10291048
results = null;
1049+
raw = null;
10301050
}
10311051
}
10321052

1053+
let formatted = {
1054+
data: results,
1055+
}
1056+
1057+
if (config.includeRaw) {
1058+
formatted.raw = raw;
1059+
}
1060+
10331061
if (config._isPagination || response.LastEvaluatedKey) {
10341062
const nextPage = this._formatReturnPager(
10351063
config,
10361064
response.LastEvaluatedKey,
10371065
);
1038-
return { cursor: nextPage || null, data: results };
1066+
1067+
formatted.cursor = nextPage || null;
10391068
}
10401069

1041-
return { data: results };
1070+
return formatted;
10421071
} catch (err) {
10431072
if (
10441073
config.originalErr ||
@@ -4044,7 +4073,7 @@ class Entity {
40444073
}
40454074
}
40464075

4047-
let definition= {
4076+
let definition = {
40484077
pk,
40494078
sk,
40504079
hasSk,
@@ -4498,7 +4527,14 @@ class Entity {
44984527
getClient: () => this.client,
44994528
isRoot: true,
45004529
});
4501-
4530+
const indexHasHiddenComposites = {};
4531+
for (let hiddenAttr of schema.hiddenAttributes.values()) {
4532+
if (facets.byAttr[hiddenAttr]) {
4533+
for (const facet of facets.byAttr[hiddenAttr]) {
4534+
indexHasHiddenComposites[facet.index] = true;
4535+
}
4536+
}
4537+
}
45024538
let filters = this._normalizeFilters(model.filters);
45034539
// todo: consider a rename
45044540
let prefixes = this._normalizeKeyFixings({
@@ -4562,6 +4598,7 @@ class Entity {
45624598
clusteredIndexes,
45634599
indexHasSortKeys,
45644600
indexHasSubCollections,
4601+
indexHasHiddenComposites,
45654602
},
45664603
translations: {
45674604
keys: indexField,

0 commit comments

Comments
 (0)