Skip to content

Commit 4deae55

Browse files
committed
fix dynamodb-toolbox#69 by adding alias support to defaults data object
1 parent 5d904a5 commit 4deae55

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

__tests__/entity.put.unit.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ const TestEntity3 = new Entity({
7070
table: TestTable2
7171
})
7272

73+
const TestTable3 = new Table({
74+
name: 'TestTable3',
75+
partitionKey: 'pk',
76+
sortKey: 'sk',
77+
DocumentClient
78+
})
79+
80+
const TestEntity4 = new Entity({
81+
name: 'TestEntity4',
82+
attributes: {
83+
id: { partitionKey: true},
84+
sk: { hidden: true, sortKey: true, default: (data) => data.id },
85+
test: { alias: 'xyz' }
86+
},
87+
table: TestTable3
88+
});
89+
7390
describe('put',()=>{
7491

7592
it('creates basic item',() => {
@@ -366,4 +383,12 @@ describe('put',()=>{
366383
expect(TableName).toBe('test-table')
367384
})
368385

386+
387+
it('correctly aliases pks', () => {
388+
let { Item } = TestEntity4.putParams({ id: 3, xyz: '123' })
389+
expect(Item.sk).toBe('3')
390+
// expect(TableName).toBe('test-table')
391+
})
392+
393+
369394
})

classes/Entity.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,13 +701,16 @@ class Entity {
701701
putParams(item={},options={},params={}) {
702702
// Extract schema and defaults
703703
const { schema, defaults, required, linked, _table } = this
704-
704+
705705
// Initialize validateType with the DocumentClient
706706
const validateType = validateTypes(this.DocumentClient)
707707

708708
// Merge defaults
709709
const data = normalizeData(this.DocumentClient)(schema.attributes,linked,Object.assign({},defaults,item))
710710

711+
// console.log(data);
712+
713+
711714
// Extract valid options
712715
const {
713716
conditions, // ConditionExpression

lib/normalizeData.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,33 @@ module.exports = (DocumentClient) => (schema,linked,data,filter=false) => {
1515
// Intialize validate type
1616
const validateType = validateTypes(DocumentClient)
1717

18-
let _data = Object.keys(data).reduce((acc,field) => {
18+
// Generate normalized data object
19+
let dataMap = Object.keys(data).reduce((acc,field) => {
1920

21+
// Return a map with normalized data and alias references
2022
return Object.assign(acc,
21-
schema[field] ? { [schema[field].map || field] : data[field] }
23+
schema[field] ? {
24+
data: { ...acc.data, [schema[field].map || field] : data[field] },
25+
aliases: { ...acc.aliases, [schema[field].alias || field] : data[field] }
26+
}
2227
: filter ? {} // this will filter out non-mapped fields
23-
: field === '$remove' ? { $remove: data[field] } // support for removes
28+
: field === '$remove' ? { data: { ...acc.data, $remove: data[field] } } // support for removes
2429
: error(`Field '${field}' does not have a mapping or alias`)
2530
)
31+
32+
},{ data: {}, aliases: {} })
33+
34+
35+
// Generate final data and evaluate function expressions
36+
let _data = Object.keys(dataMap.data).reduce((acc,field) => {
37+
return Object.assign(acc, {
38+
[field]: typeof dataMap.data[field] === 'function' ? dataMap.data[field]({ ...dataMap.data, ...dataMap.aliases }) : dataMap.data[field]
39+
})
2640
},{})
2741

42+
// console.log(_data)
43+
44+
2845
// Process linked
2946
let composites = Object.keys(linked).reduce((acc,attr) => {
3047

lib/validateTypes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
const { toBool, hasValue, error } = require('./utils')
1010

1111
// Performs type validation/coercian
12-
module.exports = (DocumentClient) => (mapping,field,input,data={}) => {
12+
module.exports = (DocumentClient) => (mapping,field,value,data={}) => {
1313

1414
// Evaluate function expressions
1515
// TODO: should this happen here?
16-
let value = typeof input === 'function' ? input(data) : input
16+
// let value = typeof input === 'function' ? input(data) : input
1717

1818
// return if undefined or null
1919
if (!hasValue(value)) return value

0 commit comments

Comments
 (0)