Skip to content

Commit 4402652

Browse files
committed
remove type requirement from table attributes and fix mappings issue
1 parent 0c4ccef commit 4402652

File tree

5 files changed

+24
-24
lines changed

5 files changed

+24
-24
lines changed

__tests__/entity-creation.unit.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ describe('Entity creation', ()=> {
323323
expect(TestTable.Table.entityField).toBe('_et')
324324
expect(TestTable.Table.indexes).toEqual({})
325325
expect(TestTable.Table.attributes).toEqual({
326-
_et: { type: 'string' },
326+
_et: { type: 'string', mappings: {} },
327327
pk: { type: 'string', mappings: { TestEnt: { pk: 'string' } } },
328328
_ct: { mappings: { TestEnt: { created: 'string' } } },
329329
_md: { mappings: { TestEnt: { modified: 'string' } } }

__tests__/parseTable.unit.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ describe('parseTable', () => {
2424
let tbl = parseTable(table)
2525
expect(tbl.name).toBe('test-table')
2626
expect(tbl.alias).toBe('test-table-alias')
27-
expect(tbl.Table.attributes.entity).toEqual({ type: 'string' })
28-
expect(tbl.Table.attributes.pk).toEqual({ type: 'string' })
29-
expect(tbl.Table.attributes.sk).toEqual({ type: 'string' })
27+
expect(tbl.Table.attributes.entity).toEqual({ type: 'string', mappings: {} })
28+
expect(tbl.Table.attributes.pk).toEqual({ type: 'string', mappings: {} })
29+
expect(tbl.Table.attributes.sk).toEqual({ type: 'string', mappings: {} })
3030
expect(tbl.Table.entityField).toBe('entity')
3131
expect(tbl._entities).toEqual([])
3232
expect(tbl.autoExecute).toBe(false)

__tests__/parseTableAttributes.unit.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ let attrs = {
99
describe('parseTableAttributes', () => {
1010

1111
it('parse simple attributes', async () => {
12-
expect(parseTableAttributes(attrs,'pk','sk')).toEqual({ pk: { type: 'string' }, sk: { type: 'string' } })
12+
expect(parseTableAttributes(attrs,'pk','sk')).toEqual({ pk: { type: 'string', mappings: {} }, sk: { type: 'string', mappings: {} } })
1313
})
1414

15-
it('fails when attribute is missing type', async () => {
15+
it.skip('fails when attribute is missing type', async () => {
1616
expect(() => {
1717
parseTableAttributes(Object.assign({},attrs,{ test: {} }),'pk','sk')
1818
}).toThrow(`Invalid or missing type for 'test'. Valid types are 'string', 'boolean', 'number', 'list', 'map', 'binary', and 'set'.`)

__tests__/table-creation.unit.test.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('Table creation', ()=> {
2020
expect(TestTable.Table.sortKey).toBeNull()
2121
expect(TestTable.Table.entityField).toBe('_et')
2222
expect(TestTable.Table.indexes).toEqual({})
23-
expect(TestTable.Table.attributes).toEqual({ _et: { type: 'string' } })
23+
expect(TestTable.Table.attributes).toEqual({ _et: { type: 'string', mappings: {} } })
2424
expect(TestTable.autoExecute).toBe(true)
2525
expect(TestTable.autoParse).toBe(true)
2626
expect(TestTable.entities).toEqual([])
@@ -43,7 +43,7 @@ describe('Table creation', ()=> {
4343
expect(TestTable.Table.sortKey).toBe('sk')
4444
expect(TestTable.Table.entityField).toBe('entity')
4545
expect(TestTable.Table.indexes).toEqual({})
46-
expect(TestTable.Table.attributes).toEqual({ entity: { type: 'string' } })
46+
expect(TestTable.Table.attributes).toEqual({ entity: { type: 'string', mappings: {} } })
4747
expect(TestTable.autoExecute).toBe(false)
4848
expect(TestTable.autoParse).toBe(false)
4949
expect(TestTable.entities).toEqual([])
@@ -79,16 +79,16 @@ describe('Table creation', ()=> {
7979

8080
// Check attribute parsing
8181
expect(TestTable.Table.attributes).toEqual({
82-
stringAttr: { type: 'string' },
83-
numberAttr: { type: 'number' },
84-
binaryAttr: { type: 'binary' },
85-
booleanAttr: { type: 'boolean' },
86-
listAttr: { type: 'list' },
87-
mapAttr: { type: 'map' },
88-
stringSetAttr: { type: 'set' },
89-
numberSetAttr: { type: 'set', setType: 'number' },
90-
binarySetAttr: { type: 'set', setType: 'binary' },
91-
_et: { type: 'string' }
82+
stringAttr: { type: 'string', mappings: {} },
83+
numberAttr: { type: 'number', mappings: {} },
84+
binaryAttr: { type: 'binary', mappings: {} },
85+
booleanAttr: { type: 'boolean', mappings: {} },
86+
listAttr: { type: 'list', mappings: {} },
87+
mapAttr: { type: 'map', mappings: {} },
88+
stringSetAttr: { type: 'set', mappings: {} },
89+
numberSetAttr: { type: 'set', setType: 'number', mappings: {} },
90+
binarySetAttr: { type: 'set', setType: 'binary', mappings: {} },
91+
_et: { type: 'string', mappings: {} }
9292
})
9393
}) // end table w/ attributes
9494

@@ -114,7 +114,7 @@ describe('Table creation', ()=> {
114114
expect(TestTable.Table.partitionKey).toBe('pk')
115115
expect(TestTable.Table.sortKey).toBeNull()
116116
expect(TestTable.Table.entityField).toBe('_et')
117-
expect(TestTable.Table.attributes).toEqual({ _et: { type: 'string' } })
117+
expect(TestTable.Table.attributes).toEqual({ _et: { type: 'string', mappings: {} } })
118118
expect(TestTable.autoExecute).toBe(true)
119119
expect(TestTable.autoParse).toBe(true)
120120
expect(TestTable.entities).toEqual([])
@@ -143,7 +143,7 @@ describe('Table creation', ()=> {
143143
expect(TestTable.Table.sortKey).toBeNull()
144144
expect(TestTable.Table.entityField).toBe('_et')
145145
expect(TestTable.Table.indexes).toEqual({})
146-
expect(TestTable.Table.attributes).toEqual({ _et: { type: 'string' } })
146+
expect(TestTable.Table.attributes).toEqual({ _et: { type: 'string', mappings: {} } })
147147
expect(TestTable.autoExecute).toBe(true)
148148
expect(TestTable.autoParse).toBe(true)
149149
expect(TestTable.entities).toEqual([])
@@ -166,7 +166,7 @@ describe('Table creation', ()=> {
166166
expect(TestTable.Table.sortKey).toBeNull()
167167
expect(TestTable.Table.entityField).toBe('_et')
168168
expect(TestTable.Table.indexes).toEqual({})
169-
expect(TestTable.Table.attributes).toEqual({ _et: { type: 'string' } })
169+
expect(TestTable.Table.attributes).toEqual({ _et: { type: 'string', mappings: {} } })
170170
expect(TestTable.autoExecute).toBe(true)
171171
expect(TestTable.autoParse).toBe(true)
172172
expect(TestTable.entities).toEqual([])

lib/parseTableAttributes.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ module.exports = (attrs,partitionKey,sortKey) => Object.keys(attrs).reduce((acc,
1616
: validTypes.includes(attrs[field]) ?
1717
Object.assign(acc,parseAttributeConfig(field,{ type: attrs[field] }))
1818
: typeError(field)
19-
} else {
19+
} else {
2020
return [partitionKey,sortKey].includes(field) && !validKeyTypes.includes(attrs[field].type) ?
2121
keyTypeError(field)
22-
: validTypes.includes(attrs[field].type) ?
22+
: (validTypes.includes(attrs[field].type) || !attrs[field].type) ?
2323
Object.assign(acc,parseAttributeConfig(field,attrs[field]))
2424
: typeError(field)
2525
}
@@ -47,6 +47,6 @@ const parseAttributeConfig = (field,config) => {
4747
// if (!config.type) config.type = 'string'
4848

4949
return {
50-
[field]: config
50+
[field]: { ...config, mappings: {} }
5151
}
5252
}

0 commit comments

Comments
 (0)