Skip to content

Commit 9ee608f

Browse files
authored
Merge pull request dynamodb-toolbox#53 from omichowdhury/alias-undefined-fix
Allow attributes not specified in entity to be passed through
2 parents a8fefa5 + 4670adf commit 9ee608f

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

__tests__/formatItem.unit.test.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ DefaultTable.entities = new Entity({
2828
list_alias2: { type: 'list', map: 'list2' },
2929
test: 'map',
3030
linked1: ['sk',0, { save: false }],
31-
linked2: ['sk',1, { save: false }]
31+
linked2: ['sk',1, { save: false }],
32+
composite1: { type: 'string', alias: 'composite1_alias' },
33+
linked3: ['composite1',0, { save: false }],
34+
linked4: ['composite1',1, { save: false, alias: 'linked4_alias' }],
35+
composite2_alias: { type: 'string', map: 'composite2' },
36+
linked5: ['composite2_alias',0, { save: false, }],
37+
linked6: ['composite2_alias',1, { save: false, alias: 'linked6_alias' }],
3238
}
3339
})
3440

@@ -83,4 +89,19 @@ describe('formatItem', () => {
8389
expect(result).toEqual({ linked1: 'test1' })
8490
})
8591

92+
it('formats item with linked aliased composite field', () => {
93+
let result = formatItem(DocumentClient)(DefaultTable.User.schema.attributes,DefaultTable.User.linked,{ composite1: 'test1#test2' })
94+
expect(result).toEqual({ composite1_alias: 'test1#test2', linked3: 'test1', linked4_alias: 'test2' })
95+
})
96+
97+
it('formats item with linked mapped composite field', () => {
98+
let result = formatItem(DocumentClient)(DefaultTable.User.schema.attributes,DefaultTable.User.linked,{ composite2: 'test1#test2' })
99+
expect(result).toEqual({ composite2_alias: 'test1#test2', linked5: 'test1', linked6_alias: 'test2' })
100+
})
101+
102+
it('passes through attribute not specified in entity', () => {
103+
let result = formatItem(DocumentClient)(DefaultTable.User.schema.attributes,DefaultTable.User.linked,{ unspecified: 'value' })
104+
expect(result).toEqual({ unspecified: 'value' })
105+
})
106+
86107
})

lib/formatItem.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ module.exports = (DocumentClient) => (attributes,linked,item,include=[]) => {
1818
const validateType = validateTypes(DocumentClient)
1919

2020
return Object.keys(item).reduce((acc,field) => {
21-
22-
if (linked[field] || linked[attributes[field].alias]) {
23-
Object.assign(acc, (linked[field] || linked[attributes[field].alias]).reduce((acc,f,i) => {
21+
const link = linked[field] || attributes[field] && attributes[field].alias && linked[attributes[field].alias]
22+
if (link) {
23+
Object.assign(acc, link.reduce((acc,f,i) => {
2424
if (attributes[f].save || attributes[f].hidden || (include.length > 0 && !include.includes(f))) return acc
2525
return Object.assign(acc,{
2626
[attributes[f].alias || f]: validateType(attributes[f],f,
@@ -38,7 +38,7 @@ module.exports = (DocumentClient) => (attributes,linked,item,include=[]) => {
3838
if (attributes[field] && attributes[field].type === 'set' && Array.isArray(item[field].values)) { item[field] = item[field].values }
3939
return Object.assign(acc,{
4040
[(attributes[field] && attributes[field].alias) || field]: (
41-
attributes[field].prefix || attributes[field].suffix
41+
attributes[field] && (attributes[field].prefix || attributes[field].suffix)
4242
? item[field]
4343
.replace(new RegExp(`^${escapeRegExp(attributes[field].prefix)}`),'')
4444
.replace(new RegExp(`${escapeRegExp(attributes[field].suffix)}$`),'')

0 commit comments

Comments
 (0)