Skip to content

Commit 9e67b7a

Browse files
committed
patch dynamodb-toolbox#74 - more investigation is needed
1 parent 64f0418 commit 9e67b7a

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

__tests__/entity.delete.unit.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,39 @@ describe('delete',()=>{
224224
expect(() => TestEntity.deleteBatch()).toThrow(`'pk' or 'email' is required`)
225225
})
226226

227+
228+
// Adding this for regression testing
229+
it('Non-Key Index Generated on Delete #74', async () => {
230+
const FoosTable = new Table({
231+
name: 'test-table',
232+
partitionKey: 'pk',
233+
sortKey: 'sk',
234+
indexes: {
235+
'GSI-1': { partitionKey: 'gsi1pk', sortKey: 'gsi1sk' },
236+
},
237+
DocumentClient,
238+
});
239+
const Foos = new Entity({
240+
name: 'Foo',
241+
table: FoosTable,
242+
timestamps: true,
243+
attributes: {
244+
pk: { hidden: true, partitionKey: true, default: (data) => (`FOO#${data.id}`) },
245+
sk: { hidden: true, sortKey: true, default: (data) => (`FOO#${data.id}`) },
246+
247+
// This next `default` gets executed on delete() and fails with "Cannot read property 'tenant' of undefined"
248+
gsi1pk: { hidden: true, default: (data) => (`TENANT#${data.meta.tenant}`) },
249+
250+
gsi1sk: { hidden: true, default: (data) => (`FOO#${data.id}`) },
251+
id: { required: 'always' },
252+
meta: { type: 'map', required: 'always' },
253+
__context__: { hidden: true },
254+
},
255+
});
256+
257+
const key = { id: 'xyz' }
258+
let result = Foos.deleteParams(key) // Fails with v0.2.0-beta. Fine with v0.2.0-alpha
259+
expect(result).toEqual({ TableName: 'test-table', Key: { pk: 'FOO#xyz', sk: 'FOO#xyz' } })
260+
})
261+
227262
})

lib/normalizeData.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,13 @@ module.exports = (DocumentClient) => (schema,linked,data,filter=false) => {
3535
map[attr] = map[attr](map)
3636
return map
3737
} else {
38-
map[attr] = map[attr](map)
39-
if (schema[attr].alias) map[schema[attr].alias] = map[attr]
40-
if (schema[attr].map) map[schema[attr].map] = map[attr]
38+
try {
39+
map[attr] = map[attr](map)
40+
if (schema[attr].alias) map[schema[attr].alias] = map[attr]
41+
if (schema[attr].map) map[schema[attr].map] = map[attr]
42+
} catch(e) {
43+
// TODO: Find a better way to prevent this for missing fields
44+
}
4145
return map
4246
}
4347
} // end dependsOn

0 commit comments

Comments
 (0)