Skip to content

Commit

Permalink
fix: resolve null attributes being wrapped when parsed (dynamodb-tool…
Browse files Browse the repository at this point in the history
…box#521)

Co-authored-by: "lucas.subli" <[email protected]>
  • Loading branch information
naorpeled and lucas-subli authored Apr 21, 2023
1 parent 8aca594 commit 00b8943
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
9 changes: 9 additions & 0 deletions src/__tests__/formatItem.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,13 @@ describe('formatItem', () => {
)
expect(result).toEqual({ unspecified: 'value' })
})

it('passes through null attribute', () => {
const result = formatItem()(
DefaultTable.User.schema.attributes,
DefaultTable.User.linked,
{ number: null }
)
expect(result).toEqual({ number: null })
})
})
32 changes: 19 additions & 13 deletions src/lib/formatItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,28 @@ import { Linked } from './parseEntity'

// Convert from DocumentClient values, which may be wrapped sets or numbers,
// into normal TS values.
const convertDynamoValues = (value: any, attr?: PureAttributeDefinition) => {
const convertDynamoValues = (value: unknown, attr?: PureAttributeDefinition): unknown => {
if (value === null) {
return value
}

// Unwrap bigint/number sets to regular numbers/bigints
if (attr && attr.type === 'set') {
if (attr.setType === 'bigint') {
value = Array.from(value).map((n: any) => BigInt(n))
value = Array.from(value as Set<bigint>).map((n: any) => BigInt(n))
} else if (attr.setType === 'number') {
value = Array.from(value).map((n: any) => Number(n))
value = Array.from(value as Set<number>).map((n: any) => Number(n))
} else {
value = Array.from(value)
value = Array.from(value as Set<unknown>)
}
}

// Convert wrapped number values to bigints
if (attr && attr.type === 'bigint') {
value = BigInt(value)
value = BigInt(value as number)
}
if (attr && attr.type === 'number') {
value = Number(value)
value = Number(value as number)
}

return value
Expand All @@ -38,7 +42,7 @@ export default () => (
attributes: { [key: string]: PureAttributeDefinition },
linked: Linked,
item: any,
include: string[] = []
include: string[] = [],
) => {
// TODO: Support nested maps?
// TODO: include alias support?
Expand All @@ -59,27 +63,29 @@ export default () => (
attributes[f].save ||
attributes[f].hidden ||
(include.length > 0 && !include.includes(f))
)
) {
return acc
}
return Object.assign(acc, {
[attributes[f].alias || f]: validateType(
attributes[f],
f,
item[field]
.replace(new RegExp(`^${escapeRegExp(attributes[field].prefix!)}`), '')
.replace(new RegExp(`${escapeRegExp(attributes[field].suffix!)}$`), '')
.split(attributes[field].delimiter || '#')[i]
)
.split(attributes[field].delimiter || '#')[i],
),
})
}, {})
}, {}),
)
}

if (
(attributes[field] && attributes[field].hidden) ||
(include.length > 0 && !include.includes(field))
)
) {
return acc
}

item[field] = convertDynamoValues(item[field], attributes[field])

Expand All @@ -96,7 +102,7 @@ export default () => (
: fieldValue

return Object.assign(acc, {
[(attributes[field] && attributes[field].alias) || field]: transformedValue
[(attributes[field] && attributes[field].alias) || field]: transformedValue,
})
}, {})
}
Expand Down

0 comments on commit 00b8943

Please sign in to comment.