Skip to content

Commit eb96327

Browse files
committed
fix indexes being removed on update
1 parent 2de1a1c commit eb96327

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

__tests__/entity.update.unit.test.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ const TestTable = new Table({
55
name: 'test-table',
66
partitionKey: 'pk',
77
sortKey: 'sk',
8-
DocumentClient
8+
DocumentClient,
9+
indexes: {
10+
GSI1: { partitionKey: 'GSI1pk' }
11+
}
912
})
1013

1114
const TestEntity = new Entity({
@@ -78,6 +81,20 @@ const TestEntity3 = new Entity({
7881
table: TestTable3
7982
})
8083

84+
const TestEntityGSI = new Entity({
85+
name: 'TestEntityGSI',
86+
autoExecute: false,
87+
attributes: {
88+
email: { type: 'string', partitionKey: true },
89+
sk: { type: 'string', sortKey: true },
90+
test: { type: 'string' },
91+
test2: { type: 'string' },
92+
GSI1pk: { partitionKey: 'GSI1'}
93+
},
94+
timestamps: false,
95+
table: TestTable
96+
})
97+
8198

8299
describe('update',()=>{
83100

@@ -96,6 +113,11 @@ describe('update',()=>{
96113
expect(TableName).toBe('test-table')
97114
})
98115

116+
it('creates update with GSI', () => {
117+
let { TableName, Key, UpdateExpression, ExpressionAttributeNames, ExpressionAttributeValues } = TestEntityGSI.updateParams({ pk: 'test-pk', sk: 'test-sk', GSI1pk: 'test' })
118+
expect(UpdateExpression).toBe('SET #_et = if_not_exists(#_et,:_et), #GSI1pk = :GSI1pk')
119+
})
120+
99121

100122
it('creates update with multiple fields (default types)', () => {
101123
let { TableName, Key, UpdateExpression, ExpressionAttributeNames, ExpressionAttributeValues } = TestEntity.updateParams({

classes/Entity.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,6 @@ class Entity {
488488
const names = {}
489489
const values = {}
490490

491-
492491
// Loop through valid fields and add appropriate action
493492
Object.keys(data).forEach((field) => {
494493
const mapping = schema.attributes[field]
@@ -512,8 +511,10 @@ class Entity {
512511
REMOVE.push(`#${field}`)
513512
names[`#${field}`] = field
514513
} else if (
515-
!mapping.partitionKey
516-
&& !mapping.sortKey
514+
// !mapping.partitionKey
515+
// && !mapping.sortKey
516+
mapping.partitionKey !== true
517+
&& mapping.sortKey !== true
517518
&& (mapping.save === undefined || mapping.save === true)
518519
&& (!mapping.link || (mapping.link && mapping.save === true))
519520
) {

0 commit comments

Comments
 (0)