Skip to content

fix(null-values): support null values #427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/decorator/impl/index/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ export function initOrUpdateIndex(indexType: IndexType, indexData: IndexData, ta
/**
* @hidden
*/
function initOrUpdateGSI(indexes: Record<string, DynamoDB.KeyType>, indexData: IndexData): Partial<PropertyMetadata<any>> {
function initOrUpdateGSI(
indexes: Record<string, DynamoDB.KeyType>,
indexData: IndexData,
): Partial<PropertyMetadata<any>> {
if (indexes[indexData.name]) {
// TODO INVESTIGATE when we throw an error we have a problem where multiple different classes extend one base class, this will be executed multiple times
// throw new Error(
Expand Down
12 changes: 10 additions & 2 deletions src/dynamo/expression/condition-expression-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,20 @@ describe('expressions', () => {
[undefined, undefined, {}],
{},
[],
{ blub: undefined, other: undefined },
{ blub: undefined, other: undefined, empty_arr: [] },
new Set(arr),
]

const filteredObj = deepFilter(obj, (item) => item !== undefined)
expect(filteredObj).toEqual([{ street: 'street', zip: 1524 }, [{ age: 25 }], new Set([arr[0], arr[1]])])
expect(filteredObj).toEqual([
{ street: 'street', zip: 1524 },
[{ age: 25 }],
[{}],
{},
[],
{ empty_arr: [] },
new Set([arr[0], arr[1]]),
])
})

it('use property metadata', () => {
Expand Down
14 changes: 7 additions & 7 deletions src/dynamo/expression/condition-expression-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,41 +51,41 @@ export function deepFilter(obj: any, filterFn: (value: any) => boolean): any {
const returnArr: any[] = []
obj.forEach((i) => {
const item = deepFilter(i, filterFn)
if (item !== null) {
if (item !== undefined) {
returnArr.push(item)
}
})

return returnArr.length ? returnArr : null
return returnArr
} else if (obj instanceof Set) {
const returnArr: any[] = []
Array.from(obj).forEach((i) => {
const item = deepFilter(i, filterFn)
if (item !== null) {
if (item !== undefined) {
returnArr.push(item)
}
})

return returnArr.length ? new Set(returnArr) : null
return new Set(returnArr)
} else if (isPlainObject(obj)) {
const returnObj: Record<string, any> = {}

for (const key in obj) {
if (obj.hasOwnProperty(key)) {
const value = obj[key]
const item = deepFilter(value, filterFn)
if (item !== null) {
if (item !== undefined) {
returnObj[key] = item
}
}
}

return Object.keys(returnObj).length ? returnObj : null
return returnObj
} else {
if (filterFn(obj)) {
return obj
} else {
return null
return undefined
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/helper/fetch-all.function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import { ScanRequest } from '../dynamo/request/scan/scan.request'
* available. This can be used with scan and query requests.
*/

export function fetchAll<T>(request: ScanRequest<T> | QueryRequest<T>, startKey?: Record<string, DynamoDB.AttributeValue>): Promise<T[]> {
export function fetchAll<T>(
request: ScanRequest<T> | QueryRequest<T>,
startKey?: Record<string, DynamoDB.AttributeValue>,
): Promise<T[]> {
request.limit(ReadManyRequest.INFINITE_LIMIT)
if (startKey) {
request.exclusiveStartKey(startKey)
Expand Down
11 changes: 9 additions & 2 deletions src/mapper/mapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,18 @@ describe('Mapper', () => {
const employee1 = <MapAttribute<Employee>>attrValue.L[0]
expect(employee1).toBeDefined()
expect(keyOf(employee1)).toBe('M')
expect(Object.keys(employee1.M).length).toBe(2)
expect(Object.keys(employee1.M).length).toBe(3)
expect(employee1.M.name).toBeDefined()
expect(keyOf(employee1.M.name)).toBe('S')
expect((<StringAttribute>employee1.M.name).S).toBe('max')

expect(employee1.M.age).toBeDefined()
expect(keyOf(employee1.M.age)).toBe('N')
expect((<NumberAttribute>employee1.M.age).N).toBe('25')

expect(employee1.M.age).toBeDefined()
expect(keyOf(employee1.M.sortedSet)).toBe('NULL')
expect((<NullAttribute>employee1.M.sortedSet).NULL).toBe(true)
})

it('heterogenous Set without decorator should throw', () => {
Expand Down Expand Up @@ -713,7 +717,10 @@ describe('Mapper', () => {
describe('model with non string/number/binary keys', () => {
it('should accept date as HASH or RANGE key', () => {
const now = new Date()
const toDbVal: Record<string, DynamoDB.AttributeValue> = toDb(new ModelWithDateAsHashKey(now), ModelWithDateAsHashKey)
const toDbVal: Record<string, DynamoDB.AttributeValue> = toDb(
new ModelWithDateAsHashKey(now),
ModelWithDateAsHashKey,
)
expect(toDbVal.startDate.S).toBeDefined()
expect(toDbVal.startDate.S).toEqual(now.toISOString())
})
Expand Down
4 changes: 2 additions & 2 deletions src/mapper/mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function toDb<T>(item: T, modelConstructor?: ModelConstructor<T>): Attrib

let attributeValue: Attribute | undefined | null

if (propertyValue === undefined || propertyValue === null) {
if (propertyValue === undefined) {
// noop ignore because we can't map it
} else {
/*
Expand Down Expand Up @@ -274,7 +274,7 @@ export function fromDb<T>(attributeMap: Attributes<T>, modelConstructor?: ModelC
modelValue = fromDbOne(attributeValue)
}

if (modelValue !== null && modelValue !== undefined) {
if (modelValue !== undefined) {
Reflect.set(<any>model, propertyMetadata ? propertyMetadata.name : attributeName, modelValue)
}
})
Expand Down