Skip to content

Commit 5dbedbb

Browse files
committed
Improve default detection for updateExpression
1 parent 0cc4720 commit 5dbedbb

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

__tests__/entity.update.unit.test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ describe('update',()=>{
101101
it('creates default update', () => {
102102
let { TableName, Key, UpdateExpression, ExpressionAttributeNames, ExpressionAttributeValues } = TestEntity.updateParams({ pk: 'test-pk', sk: 'test-sk' })
103103

104-
expect(UpdateExpression).toBe('SET #test_string = if_not_exists(#test_string,:test_string), #test_number_coerce = :test_number_coerce, #_ct = if_not_exists(#_ct,:_ct), #_md = :_md, #_et = if_not_exists(#_et,:_et)')
104+
expect(UpdateExpression).toBe('SET #test_string = if_not_exists(#test_string,:test_string), #test_number_coerce = if_not_exists(#test_number_coerce,:test_number_coerce), #_ct = if_not_exists(#_ct,:_ct), #_md = :_md, #_et = if_not_exists(#_et,:_et)')
105105
expect(ExpressionAttributeNames).toEqual({ '#_md': '_md', '#_ct': '_ct', '#test_string': 'test_string', '#test_number_coerce':'test_number_coerce', '#_et': '_et' })
106106
expect(ExpressionAttributeValues).toHaveProperty(':_ct')
107107
expect(ExpressionAttributeValues).toHaveProperty(':_md')
@@ -125,7 +125,7 @@ describe('update',()=>{
125125
sk: 'test-sk',
126126
test_string: 'test string'
127127
})
128-
expect(UpdateExpression).toBe('SET #test_string = :test_string, #test_number_coerce = :test_number_coerce, #_ct = if_not_exists(#_ct,:_ct), #_md = :_md, #_et = if_not_exists(#_et,:_et)')
128+
expect(UpdateExpression).toBe('SET #test_string = :test_string, #test_number_coerce = if_not_exists(#test_number_coerce,:test_number_coerce), #_ct = if_not_exists(#_ct,:_ct), #_md = :_md, #_et = if_not_exists(#_et,:_et)')
129129
expect(ExpressionAttributeNames).toEqual({ '#_md': '_md', '#_ct': '_ct', '#test_string': 'test_string', '#test_number_coerce':'test_number_coerce', '#_et': '_et' })
130130
expect(ExpressionAttributeValues).toHaveProperty(':_md')
131131
expect(ExpressionAttributeValues).toHaveProperty(':_ct')
@@ -289,7 +289,7 @@ describe('update',()=>{
289289
test_number: { $add: 10 },
290290
test_number_set_type: { $add: [1,2,3] }
291291
})
292-
expect(UpdateExpression).toBe('SET #test_string = if_not_exists(#test_string,:test_string), #test_number_coerce = :test_number_coerce, #_ct = if_not_exists(#_ct,:_ct), #_md = :_md, #_et = if_not_exists(#_et,:_et) ADD #test_number :test_number, #test_number_set_type :test_number_set_type')
292+
expect(UpdateExpression).toBe('SET #test_string = if_not_exists(#test_string,:test_string), #test_number_coerce = if_not_exists(#test_number_coerce,:test_number_coerce), #_ct = if_not_exists(#_ct,:_ct), #_md = :_md, #_et = if_not_exists(#_et,:_et) ADD #test_number :test_number, #test_number_set_type :test_number_set_type')
293293
expect(ExpressionAttributeNames).toEqual({ '#_md': '_md', '#_ct': '_ct', '#test_string': 'test_string', '#_et': '_et', '#test_number': 'test_number', '#test_number_set_type': 'test_number_set_type', '#test_number_coerce': 'test_number_coerce' })
294294
expect(ExpressionAttributeValues).toHaveProperty(':_md')
295295
expect(ExpressionAttributeValues).toHaveProperty(':_ct')
@@ -311,7 +311,7 @@ describe('update',()=>{
311311
test_string_set_type: { $delete: ['1','2','3'] },
312312
test_number_set_type: { $delete: [1,2,3] }
313313
})
314-
expect(UpdateExpression).toBe('SET #test_string = if_not_exists(#test_string,:test_string), #test_number_coerce = :test_number_coerce, #_ct = if_not_exists(#_ct,:_ct), #_md = :_md, #_et = if_not_exists(#_et,:_et) DELETE #test_string_set_type :test_string_set_type, #test_number_set_type :test_number_set_type')
314+
expect(UpdateExpression).toBe('SET #test_string = if_not_exists(#test_string,:test_string), #test_number_coerce = if_not_exists(#test_number_coerce,:test_number_coerce), #_ct = if_not_exists(#_ct,:_ct), #_md = :_md, #_et = if_not_exists(#_et,:_et) DELETE #test_string_set_type :test_string_set_type, #test_number_set_type :test_number_set_type')
315315
expect(ExpressionAttributeNames).toEqual({
316316
'#test_string': 'test_string',
317317
'#_et': '_et',
@@ -340,7 +340,7 @@ describe('update',()=>{
340340
sk: 'test-sk',
341341
test_list: { $remove: [2,3,8] }
342342
})
343-
expect(UpdateExpression).toBe('SET #test_string = if_not_exists(#test_string,:test_string), #test_number_coerce = :test_number_coerce, #_ct = if_not_exists(#_ct,:_ct), #_md = :_md, #_et = if_not_exists(#_et,:_et) REMOVE #test_list[2], #test_list[3], #test_list[8]')
343+
expect(UpdateExpression).toBe('SET #test_string = if_not_exists(#test_string,:test_string), #test_number_coerce = if_not_exists(#test_number_coerce,:test_number_coerce), #_ct = if_not_exists(#_ct,:_ct), #_md = :_md, #_et = if_not_exists(#_et,:_et) REMOVE #test_list[2], #test_list[3], #test_list[8]')
344344
expect(ExpressionAttributeNames).toEqual({
345345
'#test_string': 'test_string',
346346
'#_et': '_et',
@@ -365,7 +365,7 @@ describe('update',()=>{
365365
sk: 'test-sk',
366366
test_list: { 2: 'Test2', 5: 'Test5' }
367367
})
368-
expect(UpdateExpression).toBe('SET #test_string = if_not_exists(#test_string,:test_string), #test_number_coerce = :test_number_coerce, #_ct = if_not_exists(#_ct,:_ct), #_md = :_md, #_et = if_not_exists(#_et,:_et), #test_list[2] = :test_list_2, #test_list[5] = :test_list_5')
368+
expect(UpdateExpression).toBe('SET #test_string = if_not_exists(#test_string,:test_string), #test_number_coerce = if_not_exists(#test_number_coerce,:test_number_coerce), #_ct = if_not_exists(#_ct,:_ct), #_md = :_md, #_et = if_not_exists(#_et,:_et), #test_list[2] = :test_list_2, #test_list[5] = :test_list_5')
369369
expect(ExpressionAttributeNames).toEqual({
370370
'#test_string': 'test_string',
371371
'#_et': '_et',
@@ -394,7 +394,7 @@ describe('update',()=>{
394394
test_list: { $append: [1,2,3] },
395395
test_list_coerce: { $prepend: [1,2,3] }
396396
})
397-
expect(UpdateExpression).toBe('SET #test_string = if_not_exists(#test_string,:test_string), #test_number_coerce = :test_number_coerce, #_ct = if_not_exists(#_ct,:_ct), #_md = :_md, #_et = if_not_exists(#_et,:_et), #test_list = list_append(#test_list,:test_list), #test_list_coerce = list_append(:test_list_coerce,#test_list_coerce)')
397+
expect(UpdateExpression).toBe('SET #test_string = if_not_exists(#test_string,:test_string), #test_number_coerce = if_not_exists(#test_number_coerce,:test_number_coerce), #_ct = if_not_exists(#_ct,:_ct), #_md = :_md, #_et = if_not_exists(#_et,:_et), #test_list = list_append(#test_list,:test_list), #test_list_coerce = list_append(:test_list_coerce,#test_list_coerce)')
398398
expect(ExpressionAttributeNames).toEqual({
399399
'#test_string': 'test_string',
400400
'#_et': '_et',
@@ -429,7 +429,7 @@ describe('update',()=>{
429429
'prop5': [1,2,3]
430430
}}
431431
})
432-
expect(UpdateExpression).toBe('SET #test_string = if_not_exists(#test_string,:test_string), #test_number_coerce = :test_number_coerce, #_ct = if_not_exists(#_ct,:_ct), #_md = :_md, #_et = if_not_exists(#_et,:_et), #test_map.#test_map_prop1 = :test_map_prop1, #test_map.#test_map_prop2[1] = :test_map_prop2_1, #test_map.#test_map_prop2[4] = :test_map_prop2_4, #test_map.#test_map_prop3.#test_map_prop3_prop4 = :test_map_prop3_prop4, #test_map.#test_map_prop5 = :test_map_prop5')
432+
expect(UpdateExpression).toBe('SET #test_string = if_not_exists(#test_string,:test_string), #test_number_coerce = if_not_exists(#test_number_coerce,:test_number_coerce), #_ct = if_not_exists(#_ct,:_ct), #_md = :_md, #_et = if_not_exists(#_et,:_et), #test_map.#test_map_prop1 = :test_map_prop1, #test_map.#test_map_prop2[1] = :test_map_prop2_1, #test_map.#test_map_prop2[4] = :test_map_prop2_4, #test_map.#test_map_prop3.#test_map_prop3_prop4 = :test_map_prop3_prop4, #test_map.#test_map_prop5 = :test_map_prop5')
433433
expect(ExpressionAttributeNames).toEqual({
434434
'#_et': '_et',
435435
'#_ct': '_ct',
@@ -463,7 +463,7 @@ describe('update',()=>{
463463
count: { $add: 10 },
464464
contents: { a: 1, b: 2 }
465465
})
466-
expect(UpdateExpression).toBe('SET #test_string = if_not_exists(#test_string,:test_string), #test_number_coerce = :test_number_coerce, #_ct = if_not_exists(#_ct,:_ct), #_md = :_md, #_et = if_not_exists(#_et,:_et), #test_map = :test_map ADD #test_number :test_number')
466+
expect(UpdateExpression).toBe('SET #test_string = if_not_exists(#test_string,:test_string), #test_number_coerce = if_not_exists(#test_number_coerce,:test_number_coerce), #_ct = if_not_exists(#_ct,:_ct), #_md = :_md, #_et = if_not_exists(#_et,:_et), #test_map = :test_map ADD #test_number :test_number')
467467
expect(ExpressionAttributeNames).toEqual({
468468
'#_et': '_et',
469469
'#_ct': '_ct',

classes/Entity.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ class Entity {
625625
// if (hasValue(value)) {
626626
if (value !== undefined) {
627627
// Push the update to SET
628-
SET.push(mapping.default && !item[field] && !mapping.onUpdate ?
628+
SET.push(mapping.default !== undefined && item[field] === undefined && !mapping.onUpdate ?
629629
`#${field} = if_not_exists(#${field},:${field})`
630630
: `#${field} = :${field}`)
631631
// Add names and values
@@ -828,4 +828,4 @@ class Entity {
828828
} // end Entity
829829

830830
// Export the Entity class
831-
module.exports = Entity
831+
module.exports = Entity

0 commit comments

Comments
 (0)