Skip to content
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

Create a sort key of a global index when it is a part of a primary key #179

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 43 additions & 8 deletions src/__tests__/normalizeData.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ let DefaultTable = new Table({
name: 'test-table',
partitionKey: 'pk',
sortKey: 'sk',
indexes: {
gs1: {partitionKey: 'gs1pk', sortKey: 'gs1sk'}
},
DocumentClient
})

// Create basic entity
DefaultTable.entities = new Entity({
name: 'User',
attributes: {
pk: { type: 'string', partitionKey: true },
id: ['gs1sk', 0, { type: 'string', partitionKey: true }],
sk: { type: 'string', sortKey: true },
set: { type: 'set', setType: 'string', alias: 'set_alias' },
set_alias2: { type: 'set', setType: 'string', map: 'set2' },
Expand All @@ -28,7 +31,10 @@ DefaultTable.entities = new Entity({
list_alias2: { type: 'list', map: 'list2' },
test: 'map',
linked1: ['sk',0, { save:true }],
linked2: ['sk',1]
linked2: ['sk',1],
gs1pk: { type: 'string', partitionKey: 'gs1'},
gs1sk: {type: 'string', sortKey: 'gs1'},
gs1sk2ndPart: ['gs1sk', 1, 'string']
}
})

Expand All @@ -40,11 +46,11 @@ const linked = DefaultTable.User.linked
describe('normalizeData', () => {

it('converts entity input to table attributes', async () => {
let result = normalizeData(DocumentClient)(attributes,linked,{
pk: 'test',
let result = normalizeData(DocumentClient)(attributes, linked, {
id: 'test',
set_alias: ['1','2','3'],
number: 1,
test: { test: true },
test: {test: true},
linked1: 'test1',
linked2: 'test2',
$remove: 'testx'
Expand All @@ -64,7 +70,7 @@ describe('normalizeData', () => {

it('filter out non-mapped fields', async () => {
let result = normalizeData(DocumentClient)(attributes,linked,{
pk: 'test',
id: 'test',
$remove: 'testx',
notAField: 'test123'
},true)
Expand All @@ -76,13 +82,42 @@ describe('normalizeData', () => {

it('fails on non-mapped fields', async () => {
expect(() => {
normalizeData(DocumentClient)(attributes,linked,{
pk: 'test',
normalizeData(DocumentClient)(attributes, linked, {
id: 'test',
$remove: 'testx',
notAField: 'test123'
})
}).toThrow(`Field 'notAField' does not have a mapping or alias`)

})

it('converts entity input to table attributes when primary key is also a part of a sort key of a global index', async () => {
let result = normalizeData(DocumentClient)(attributes, linked, {
id: 'test',
gs1pk: "test-gs1-pk",
set_alias: ['1', '2', '3'],
number: 1,
test: {test: true},
linked1: 'test1',
linked2: 'test2',
gs1sk2ndPart: "part",
$remove: 'testx'
})

expect(result).toEqual({
sk: 'test1#test2',
gs1pk: "test-gs1-pk",
gs1sk: "test#part",
gs1sk2ndPart: "part",
pk: 'test',
set: ['1', '2', '3'],
number: 1,
test: {test: true},
linked1: 'test1',
linked2: 'test2',
$remove: 'testx'
})
})


})
2 changes: 1 addition & 1 deletion src/lib/normalizeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default (DocumentClient: DocumentClient) => (schema:any,linked:any,data:a
const field = (schema[attr] && schema[attr].map) || attr

if (_data[field] !== undefined) return acc // if value exists, let override
let values = linked[attr].map((f:any) => {
let values = linked[attr].map((unmappedF: any) => schema[unmappedF].map ?? unmappedF).map((f:any) => {
if (_data[f] === undefined) { return null }
return transformAttr(schema[f],validateType(schema[f],f,_data[f]),_data)
}).filter((x:any) => x !== null)
Expand Down