Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit 58520ee

Browse files
authored
don't use default DynamoDB marshaling; store items as JSON (#1)
1 parent 09a54fd commit 58520ee

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

dynamodb_feature_store.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ function dynamoDBFeatureStoreInternal(tableName, options) {
4141
}
4242
cb(null);
4343
} else {
44-
// strip namespace as it's just used for partitioning in the table
45-
delete data.Item['namespace'];
46-
cb(data.Item);
44+
cb(unmarshalItem(data.Item));
4745
}
4846
});
4947
};
@@ -58,11 +56,10 @@ function dynamoDBFeatureStoreInternal(tableName, options) {
5856
this.paginationHelper(params, function(params, cb) { return dynamoDBClient.query(params, cb); }).then(function (items) {
5957
var results = {};
6058
for (var i = 0; i < items.length; i++) {
61-
var item = items[i];
62-
// Remove the 'namespace' key from the item as it was only added to be
63-
// used as a partition key and is not part of the item itself.
64-
delete item['namespace'];
65-
results[item.key] = item;
59+
var item = unmarshalItem(items[i]);
60+
if (item) {
61+
results[item.key] = item;
62+
}
6663
}
6764
cb(results);
6865
}, function (err) {
@@ -204,14 +201,33 @@ function dynamoDBFeatureStoreInternal(tableName, options) {
204201
});
205202
};
206203

204+
function marshalItem(kind, item) {
205+
return {
206+
namespace: kind.namespace,
207+
key: item.key,
208+
version: item.version,
209+
item: JSON.stringify(item)
210+
};
211+
}
212+
213+
function unmarshalItem(dbItem) {
214+
var itemJson = dbItem.item;
215+
if (itemJson) {
216+
try {
217+
return JSON.parse(itemJson);
218+
} catch(e) {
219+
logger.error('database item did not contain a valid JSON object');
220+
}
221+
}
222+
return null;
223+
}
224+
207225
function makePutRequest(kind, item) {
208-
var storeItem = Object.assign({}, item);
209-
storeItem.namespace = kind.namespace;
210226
return {
211227
TableName: tableName,
212-
Item: storeItem,
228+
Item: marshalItem(kind, item),
213229
ConditionExpression: 'attribute_not_exists(version) OR version < :new_version',
214-
ExpressionAttributeValues: {':new_version': storeItem.version }
230+
ExpressionAttributeValues: {':new_version': item.version }
215231
};
216232
}
217233

@@ -223,4 +239,3 @@ function dynamoDBFeatureStoreInternal(tableName, options) {
223239
}
224240

225241
module.exports = DynamoDBFeatureStore;
226-

0 commit comments

Comments
 (0)