Skip to content

Commit

Permalink
Auto auto incrementing id generator
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchheddles committed Jul 8, 2023
1 parent d4f37c3 commit 26273cf
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 204 deletions.
87 changes: 44 additions & 43 deletions src/__tests__/entity.update.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ const TestEntity4 = new Entity({
autoExecute: false,
attributes: {
email: { type: 'string', partitionKey: true },
test_number_default_with_map: { type: 'number', map: 'test_mapped_number', default: 0, onUpdate: false },
test_number_default_with_map: {
type: 'number',
map: 'test_mapped_number',
default: 0,
onUpdate: false
}
},
timestamps: false,
table: TestTable4
Expand All @@ -123,7 +128,7 @@ const TestEntityGSI = new Entity({
} as const)

describe('update', () => {
it('creates default update', () => {
it.only('creates default update', () => {
const {
TableName,
Key,
Expand All @@ -133,25 +138,25 @@ describe('update', () => {
} = TestEntity.updateParams({ email: 'test-pk', sort: 'test-sk' })

expect(UpdateExpression).toBe(
'SET #test_string = if_not_exists(#test_string,:test_string), #test_number_coerce = if_not_exists(#test_number_coerce,:test_number_coerce), #test_boolean_default = if_not_exists(#test_boolean_default,:test_boolean_default), #_ct = if_not_exists(#_ct,:_ct), #_md = :_md, #_et = if_not_exists(#_et,:_et)'
'SET #attr1 = if_not_exists(#attr1,:attr1), #attr2 = if_not_exists(#attr2,:attr2), #attr3 = if_not_exists(#attr3,:attr3), #attr4 = if_not_exists(#attr4,:attr4), #attr5 = :attr5, #attr6 = if_not_exists(#attr6,:attr6)'
)
expect(ExpressionAttributeNames).toEqual({
'#_md': '_md',
'#_ct': '_ct',
'#test_string': 'test_string',
'#test_boolean_default': 'test_boolean_default',
'#test_number_coerce': 'test_number_coerce',
'#_et': '_et'
'#attr1': 'test_string',
'#attr2': 'test_number_coerce',
'#attr3': 'test_boolean_default',
'#attr4': '_ct',
'#attr5': '_md',
'#attr6': '_et'
})

assert.ok(ExpressionAttributeValues !== undefined, 'ExpressionAttributeValues is undefined')
expect(ExpressionAttributeValues).toHaveProperty(':_ct')
expect(ExpressionAttributeValues).toHaveProperty(':_md')
expect(ExpressionAttributeValues).toHaveProperty(':test_string')
expect(ExpressionAttributeValues).toHaveProperty(':test_number_coerce')
expect(ExpressionAttributeValues).toHaveProperty(':test_boolean_default')
expect(ExpressionAttributeValues).toHaveProperty(':_et')
expect(ExpressionAttributeValues?.[':_et']).toBe('TestEntity')
expect(ExpressionAttributeValues).toHaveProperty(':attr1')
expect(ExpressionAttributeValues).toHaveProperty(':attr2')
expect(ExpressionAttributeValues).toHaveProperty(':attr3')
expect(ExpressionAttributeValues).toHaveProperty(':attr4')
expect(ExpressionAttributeValues).toHaveProperty(':attr5')
expect(ExpressionAttributeValues).toHaveProperty(':attr6')
expect(ExpressionAttributeValues?.[':attr6']).toBe('TestEntity')
expect(Key).toEqual({ pk: 'test-pk', sk: 'test-sk' })
expect(TableName).toBe('test-table')
})
Expand Down Expand Up @@ -235,13 +240,9 @@ describe('update', () => {
expect(ExpressionAttributeNames?.['#test_mapped_number']).toBe('test_mapped_number')
expect(ExpressionAttributeValues?.[':test_mapped_number']).toBe(111)

expect(UpdateExpression).toBe(
'SET #test_mapped_number = :test_mapped_number'
)

expect(UpdateExpression).toBe('SET #test_mapped_number = :test_mapped_number')
})


it('fails when removing fields with default values', () => {
expect(() =>
TestEntity.updateParams({ email: 'test-pk', sort: 'test-sk', $remove: 'test_string' })
Expand Down Expand Up @@ -390,18 +391,14 @@ describe('update', () => {

expect(ExpressionAttributeValues?.[':test_string_set']).toEqual(new Set(['1', '2', '3']))
expect(ExpressionAttributeValues?.[':test_number_set']).toEqual(new Set([1, 2, 3]))
expect(ExpressionAttributeValues?.[':test_binary_set']).toEqual(new Set([
Buffer.from('1'),
Buffer.from('2'),
Buffer.from('3')
]))
expect(ExpressionAttributeValues?.[':test_binary_set']).toEqual(
new Set([Buffer.from('1'), Buffer.from('2'), Buffer.from('3')])
)
expect(ExpressionAttributeValues?.[':test_string_set_type']).toEqual(new Set(['1', '2', '3']))
expect(ExpressionAttributeValues?.[':test_number_set_type']).toEqual(new Set([1, 2, 3]))
expect(ExpressionAttributeValues?.[':test_binary_set_type']).toEqual(new Set([
Buffer.from('1'),
Buffer.from('2'),
Buffer.from('3')
]))
expect(ExpressionAttributeValues?.[':test_binary_set_type']).toEqual(
new Set([Buffer.from('1'), Buffer.from('2'), Buffer.from('3')])
)
expect(Key).toEqual({ pk: 'test-pk', sk: 'test-sk' })
expect(TableName).toBe('test-table')
})
Expand Down Expand Up @@ -500,7 +497,11 @@ describe('update', () => {
expect(ExpressionAttributeValues).not.toHaveProperty(':sk')
expect(ExpressionAttributeValues).toHaveProperty(':_et')
expect(ExpressionAttributeValues?.[':_et']).toBe('TestEntity')
expect(Array.from(ExpressionAttributeValues?.[':test_string_set_type'])).toEqual(['1', '2', '3'])
expect(Array.from(ExpressionAttributeValues?.[':test_string_set_type'])).toEqual([
'1',
'2',
'3'
])
expect(Array.from(ExpressionAttributeValues?.[':test_number_set_type'])).toEqual([1, 2, 3])
expect(Key).toEqual({ pk: 'test-pk', sk: 'test-sk' })
expect(TableName).toBe('test-table')
Expand Down Expand Up @@ -638,7 +639,7 @@ describe('update', () => {
)
})

it('doesn\'t provide a default list value when not appending/prepending a value to a list', () => {
it("doesn't provide a default list value when not appending/prepending a value to a list", () => {
const { TableName, Key, ExpressionAttributeValues } = TestEntity.updateParams({
email: 'test-pk',
sort: 'test-sk'
Expand All @@ -650,7 +651,7 @@ describe('update', () => {
expect(ExpressionAttributeValues).not.toHaveProperty(ATTRIBUTE_VALUES_LIST_DEFAULT_KEY)
})

it('doesn\'t provide a default list value when not appending/prepending a value to a nested list within a map.', () => {
it("doesn't provide a default list value when not appending/prepending a value to a nested list within a map.", () => {
const {
TableName,
Key,
Expand Down Expand Up @@ -880,7 +881,7 @@ describe('update', () => {
expect(ExpressionAttributeValues?.[':test3']).toBe(0)
})

it('allows using list operations on a required list field', ()=> {
it('allows using list operations on a required list field', () => {
const { ExpressionAttributeValues } = TestEntity.updateParams({
email: 'test-pk',
sort: 'test-sk',
Expand Down Expand Up @@ -927,7 +928,7 @@ describe('update', () => {
).toThrow(`Field 'unknown' does not have a mapping or alias`)
})

it('fails when missing an \'always\' required field', () => {
it("fails when missing an 'always' required field", () => {
// @ts-expect-error
expect(() => TestEntity3.updateParams({ email: 'test-pk' })).toThrow(
`'test2' is a required field`
Expand Down Expand Up @@ -1184,7 +1185,7 @@ describe('update', () => {
{ email: 'x', sort: 'y', unknown: '?' },
{ strictSchemaCheck: true }
)
).toThrow('Field \'unknown\' does not have a mapping or alias')
).toThrow("Field 'unknown' does not have a mapping or alias")
})

it('allows unmapped attributes when strictSchemaCheck is false.', () => {
Expand All @@ -1211,26 +1212,26 @@ describe('update', () => {
})

it('should keep empty lists if removeNulls is true', () => {
const params = TestEntity.updateParams(
{ email: 'x', sort: 'y', test_list: [] },
)
const params = TestEntity.updateParams({ email: 'x', sort: 'y', test_list: [] })

expect(params.UpdateExpression).toBe('SET #test_string = if_not_exists(#test_string,:test_string), #test_number_coerce = if_not_exists(#test_number_coerce,:test_number_coerce), #test_boolean_default = if_not_exists(#test_boolean_default,:test_boolean_default), #_ct = if_not_exists(#_ct,:_ct), #_md = :_md, #_et = if_not_exists(#_et,:_et), #test_list = :test_list')
expect(params.UpdateExpression).toBe(
'SET #test_string = if_not_exists(#test_string,:test_string), #test_number_coerce = if_not_exists(#test_number_coerce,:test_number_coerce), #test_boolean_default = if_not_exists(#test_boolean_default,:test_boolean_default), #_ct = if_not_exists(#_ct,:_ct), #_md = :_md, #_et = if_not_exists(#_et,:_et), #test_list = :test_list'
)
expect(params.ExpressionAttributeNames).toEqual({
'#_ct': '_ct',
'#_et': '_et',
'#_md': '_md',
'#test_boolean_default': 'test_boolean_default',
'#test_list': 'test_list',
'#test_number_coerce': 'test_number_coerce',
'#test_string': 'test_string',
'#test_string': 'test_string'
})
expect(params.ExpressionAttributeValues).toEqual({
':_ct': expect.any(String),
':_et': 'TestEntity',
':_md': expect.any(String),
':test_boolean_default': false,
':test_list': [],
':test_list': [],
':test_number_coerce': 0,
':test_string': 'default string'
})
Expand Down

0 comments on commit 26273cf

Please sign in to comment.