Skip to content

Commit c5a5294

Browse files
committed
additional entity method updates
1 parent c5b3419 commit c5a5294

11 files changed

+271
-130
lines changed

__tests__/bootstrap-tests.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ module.exports.DynamoDB = new AWS.DynamoDB({
2323
module.exports.DocumentClient = new AWS.DynamoDB.DocumentClient({
2424
endpoint: 'http://localhost:4567',
2525
region: 'us-east-1',
26-
credentials: new AWS.Credentials({ accessKeyId: 'test', secretAccessKey: 'test' })
26+
credentials: new AWS.Credentials({ accessKeyId: 'test', secretAccessKey: 'test' }),
27+
// convertEmptyValues: true
2728
})
2829

2930
// Delay helper

__tests__/delete.unit.test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const TestEntity = new Entity({
2222
describe('delete',()=>{
2323

2424
it('gets the key from inputs (sync)', () => {
25-
let { TableName, Key } = TestEntity.deleteSync({ pk: 'test-pk', sk: 'test-sk' })
25+
let { TableName, Key } = TestEntity.generateDeleteParams({ pk: 'test-pk', sk: 'test-sk' })
2626
expect(TableName).toBe('test-table')
2727
expect(Key).toEqual({ pk: 'test-pk', sk: 'test-sk' })
2828
})
@@ -34,7 +34,7 @@ describe('delete',()=>{
3434
})
3535

3636
it('gets the key from input aliases (sync)', () => {
37-
let { TableName, Key } = TestEntity.deleteSync({ email: 'test-pk', sort: 'test-sk' })
37+
let { TableName, Key } = TestEntity.generateDeleteParams({ email: 'test-pk', sort: 'test-sk' })
3838
expect(TableName).toBe('test-table')
3939
expect(Key).toEqual({ pk: 'test-pk', sk: 'test-sk' })
4040
})
@@ -46,7 +46,7 @@ describe('delete',()=>{
4646
})
4747

4848
it('filters out extra data (sync)', () => {
49-
let { TableName, Key } = TestEntity.deleteSync({ pk: 'test-pk', sk: 'test-sk', test: 'test' })
49+
let { TableName, Key } = TestEntity.generateDeleteParams({ pk: 'test-pk', sk: 'test-sk', test: 'test' })
5050
expect(TableName).toBe('test-table')
5151
expect(Key).toEqual({ pk: 'test-pk', sk: 'test-sk' })
5252
})
@@ -58,7 +58,7 @@ describe('delete',()=>{
5858
})
5959

6060
it('coerces key values to correct types (sync)', () => {
61-
let { TableName, Key } = TestEntity.deleteSync({ pk: 1, sk: true })
61+
let { TableName, Key } = TestEntity.generateDeleteParams({ pk: 1, sk: true })
6262
expect(TableName).toBe('test-table')
6363
expect(Key).toEqual({ pk: '1', sk: 'true' })
6464
})
@@ -70,7 +70,7 @@ describe('delete',()=>{
7070
})
7171

7272
it('fails with undefined input (sync)', () => {
73-
expect(() => TestEntity.deleteSync()).toThrow(`'pk' or 'email' is required`)
73+
expect(() => TestEntity.generateDeleteParams()).toThrow(`'pk' or 'email' is required`)
7474
})
7575

7676
it('fails with undefined input (async)', () => {

__tests__/get.unit.test.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const TestEntity2 = new Entity({
4040
describe('get',()=>{
4141

4242
it('gets the key from inputs (sync)', async () => {
43-
const { TableName, Key } = TestEntity.getSync({ pk: 'test-pk', sk: 'test-sk' })
43+
const { TableName, Key } = TestEntity.generateGetParams({ pk: 'test-pk', sk: 'test-sk' })
4444
expect(TableName).toBe('test-table')
4545
expect(Key).toEqual({ pk: 'test-pk', sk: 'test-sk' })
4646
})
@@ -52,7 +52,7 @@ describe('get',()=>{
5252
})
5353

5454
it('gets the key from input aliases (sync)', async () => {
55-
let { TableName, Key } = TestEntity.getSync({ email: 'test-pk', sort: 'test-sk' })
55+
let { TableName, Key } = TestEntity.generateGetParams({ email: 'test-pk', sort: 'test-sk' })
5656
expect(TableName).toBe('test-table')
5757
expect(Key).toEqual({ pk: 'test-pk', sk: 'test-sk' })
5858
})
@@ -64,7 +64,7 @@ describe('get',()=>{
6464
})
6565

6666
it('filters out extra data (sync)', async () => {
67-
let { TableName, Key } = TestEntity.getSync({ pk: 'test-pk', sk: 'test-sk', test: 'test' })
67+
let { TableName, Key } = TestEntity.generateGetParams({ pk: 'test-pk', sk: 'test-sk', test: 'test' })
6868
expect(TableName).toBe('test-table')
6969
expect(Key).toEqual({ pk: 'test-pk', sk: 'test-sk' })
7070
})
@@ -76,7 +76,7 @@ describe('get',()=>{
7676
})
7777

7878
it('coerces key values to correct types (sync)', async () => {
79-
let { TableName, Key } = TestEntity.getSync({ pk: 1, sk: true })
79+
let { TableName, Key } = TestEntity.generateGetParams({ pk: 1, sk: true })
8080
expect(TableName).toBe('test-table')
8181
expect(Key).toEqual({ pk: '1', sk: 'true' })
8282
})
@@ -88,31 +88,31 @@ describe('get',()=>{
8888
})
8989

9090
it('fails with undefined input (sync)', () => {
91-
expect(() => TestEntity.getSync()).toThrow(`'pk' or 'email' is required`)
91+
expect(() => TestEntity.generateGetParams()).toThrow(`'pk' or 'email' is required`)
9292
})
9393

9494
it('fails with undefined input (async)', async () => {
9595
expect(TestEntity.get()).rejects.toBe(`'pk' or 'email' is required`)
9696
})
9797

9898
it('fails when missing the sortKey (sync)', () => {
99-
expect(() => TestEntity.getSync({ pk: 'test-pk' })).toThrow(`'sk' or 'sort' is required`)
99+
expect(() => TestEntity.generateGetParams({ pk: 'test-pk' })).toThrow(`'sk' or 'sort' is required`)
100100
})
101101

102102
it('fails when missing the sortKey (async)', () => {
103103
expect(TestEntity.get({ pk: 'test-pk' })).rejects.toBe(`'sk' or 'sort' is required`)
104104
})
105105

106106
it('fails when missing partitionKey (no alias) (sync)', () => {
107-
expect(() => TestEntity2.getSync()).toThrow(`'pk' is required`)
107+
expect(() => TestEntity2.generateGetParams()).toThrow(`'pk' is required`)
108108
})
109109

110110
it('fails when missing partitionKey (no alias) (async)', () => {
111111
expect(TestEntity2.get()).rejects.toBe(`'pk' is required`)
112112
})
113113

114114
it('fails when missing the sortKey (no alias) (sync)', () => {
115-
expect(() => TestEntity2.getSync({ pk: 'test-pk' })).toThrow(`'sk' is required`)
115+
expect(() => TestEntity2.generateGetParams({ pk: 'test-pk' })).toThrow(`'sk' is required`)
116116
})
117117

118118
it('fails when missing the sortKey (no alias) (async)', () => {

__tests__/put.unit.test.js

+22-21
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const TestEntity3 = new Entity({
7171
describe('put',()=>{
7272

7373
it('creates basic item',() => {
74-
let { TableName, Item } = TestEntity.putSync({ pk: 'test-pk', sk: 'test-sk' })
74+
let { TableName, Item } = TestEntity.generatePutParams({ pk: 'test-pk', sk: 'test-sk' })
7575

7676
expect(Item.pk).toBe('test-pk')
7777
expect(Item.sk).toBe('test-sk')
@@ -82,7 +82,7 @@ describe('put',()=>{
8282
})
8383

8484
it('creates item with aliases',() => {
85-
let { Item } = TestEntity.putSync({ email: 'test-pk', sort: 'test-sk', count: 5 })
85+
let { Item } = TestEntity.generatePutParams({ email: 'test-pk', sort: 'test-sk', count: 5 })
8686

8787
expect(Item.pk).toBe('test-pk')
8888
expect(Item.sk).toBe('test-sk')
@@ -94,7 +94,7 @@ describe('put',()=>{
9494
})
9595

9696
it('creates item with default override',() => {
97-
let { Item } = TestEntity.putSync({ pk: 'test-pk', sk: 'test-sk', test_string: 'different value' })
97+
let { Item } = TestEntity.generatePutParams({ pk: 'test-pk', sk: 'test-sk', test_string: 'different value' })
9898
expect(Item.pk).toBe('test-pk')
9999
expect(Item.sk).toBe('test-sk')
100100
expect(Item._tp).toBe('TestEntity')
@@ -104,7 +104,7 @@ describe('put',()=>{
104104
})
105105

106106
it('creates item with saved composite field',() => {
107-
let { Item } = TestEntity2.putSync({
107+
let { Item } = TestEntity2.generatePutParams({
108108
pk: 'test-pk',
109109
test_composite: 'test',
110110
})
@@ -113,17 +113,18 @@ describe('put',()=>{
113113
})
114114

115115
it('creates item that ignores field with no value',() => {
116-
let { Item } = TestEntity2.putSync({
116+
let { Item } = TestEntity2.generatePutParams({
117117
pk: 'test-pk',
118118
test_composite: undefined
119119
})
120+
120121
expect(Item.pk).toBe('test-pk')
121122
expect(Item).not.toHaveProperty('sk')
122123
expect(Item).not.toHaveProperty('test_composite')
123124
})
124125

125126
it('creates item that overrides composite key',() => {
126-
let { Item } = TestEntity2.putSync({
127+
let { Item } = TestEntity2.generatePutParams({
127128
pk: 'test-pk',
128129
sk: 'override',
129130
test_composite: 'test',
@@ -136,7 +137,7 @@ describe('put',()=>{
136137
})
137138

138139
it('creates item that generates composite key',() => {
139-
let { Item } = TestEntity2.putSync({
140+
let { Item } = TestEntity2.generatePutParams({
140141
pk: 'test-pk',
141142
test_composite: 'test',
142143
test_composite2: 'test2'
@@ -148,91 +149,91 @@ describe('put',()=>{
148149
})
149150

150151
it('fails with undefined input', () => {
151-
expect(() => TestEntity.putSync()).toThrow(`'pk' or 'email' is required`)
152+
expect(() => TestEntity.generatePutParams()).toThrow(`'pk' or 'email' is required`)
152153
})
153154

154155
it('fails when using an undefined schema field', () => {
155-
expect(() => TestEntity.putSync({
156+
expect(() => TestEntity.generatePutParams({
156157
'pk': 'test-pk',
157158
'sk': 'test-sk',
158159
'unknown': '?'
159160
})).toThrow(`Field 'unknown' does not have a mapping or alias`)
160161
})
161162

162163
it('fails when invalid string provided with no coercion', () => {
163-
expect(() => TestEntity.putSync({
164+
expect(() => TestEntity.generatePutParams({
164165
'pk': 'test-pk',
165166
'sk': 'test-sk',
166167
'test_string': 1
167168
})).toThrow(`'test_string' must be of type string`)
168169
})
169170

170171
it('fails when invalid boolean provided with no coercion', () => {
171-
expect(() => TestEntity.putSync({
172+
expect(() => TestEntity.generatePutParams({
172173
'pk': 'test-pk',
173174
'sk': 'test-sk',
174175
'test_boolean': 'x'
175176
})).toThrow(`'test_boolean' must be of type boolean`)
176177
})
177178

178179
it('fails when invalid number provided with no coercion', () => {
179-
expect(() => TestEntity.putSync({
180+
expect(() => TestEntity.generatePutParams({
180181
'pk': 'test-pk',
181182
'sk': 'test-sk',
182183
'test_number': 'x'
183184
})).toThrow(`'test_number' must be of type number`)
184185
})
185186

186187
it('fails when invalid number cannot be coerced', () => {
187-
expect(() => TestEntity.putSync({
188+
expect(() => TestEntity.generatePutParams({
188189
'pk': 'test-pk',
189190
'sk': 'test-sk',
190191
'test_number_coerce': 'x1'
191192
})).toThrow(`Could not convert 'x1' to a number for 'test_number_coerce'`)
192193
})
193194

194195
it('fails when invalid array provided with no coercion', () => {
195-
expect(() => TestEntity.putSync({
196+
expect(() => TestEntity.generatePutParams({
196197
'pk': 'test-pk',
197198
'sk': 'test-sk',
198199
'test_list': 'x'
199200
})).toThrow(`'test_list' must be a list (array)`)
200201
})
201202

202203
it('fails when invalid map provided', () => {
203-
expect(() => TestEntity.putSync({
204+
expect(() => TestEntity.generatePutParams({
204205
'pk': 'test-pk',
205206
'sk': 'test-sk',
206207
'test_map': 'x'
207208
})).toThrow(`'test_map' must be a map (object)`)
208209
})
209210

210211
it('fails when set contains different types', () => {
211-
expect(() => TestEntity.putSync({
212+
expect(() => TestEntity.generatePutParams({
212213
'pk': 'test-pk',
213214
'sk': 'test-sk',
214215
'test_string_set_type': [1,2,3]
215216
})).toThrow(`'test_string_set_type' must be a valid set (array) containing only string types`)
216217
})
217218

218219
it('fails when set contains multiple types', () => {
219-
expect(() => TestEntity.putSync({
220+
expect(() => TestEntity.generatePutParams({
220221
'pk': 'test-pk',
221222
'sk': 'test-sk',
222223
'test_string_set': ['test',1]
223224
})).toThrow(`String Set contains Number value`)
224225
})
225226

226227
it('fails when set coerces array and doesn\'t match type', () => {
227-
expect(() => TestEntity.putSync({
228+
expect(() => TestEntity.generatePutParams({
228229
'pk': 'test-pk',
229230
'sk': 'test-sk',
230231
'test_number_set_type_coerce': "1,2,3"
231232
})).toThrow(`'test_number_set_type_coerce' must be a valid set (array) of type number`)
232233
})
233234

234235
it('coerces array into set', () => {
235-
let { Item } = TestEntity.putSync({
236+
let { Item } = TestEntity.generatePutParams({
236237
'pk': 'test-pk',
237238
'sk': 'test-sk',
238239
'test_string_set_type_coerce': "1,2,3"
@@ -241,15 +242,15 @@ describe('put',()=>{
241242
})
242243

243244
it('fails when set doesn\'t contain array with no coercion', () => {
244-
expect(() => TestEntity.putSync({
245+
expect(() => TestEntity.generatePutParams({
245246
'pk': 'test-pk',
246247
'sk': 'test-sk',
247248
'test_string_set': 'test'
248249
})).toThrow(`'test_string_set' must be a valid set (array)`)
249250
})
250251

251252
it('fails when missing a required field', () => {
252-
expect(() => TestEntity3.putSync({
253+
expect(() => TestEntity3.generatePutParams({
253254
'pk': 'test-pk',
254255
'test2': 'test'
255256
})).toThrow(`'test' is a required field`)

0 commit comments

Comments
 (0)