Skip to content

Commit

Permalink
FIX: warning with unique items
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael O'Brien committed Jun 28, 2024
1 parent 217b90a commit e20250a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 28 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dynamodb-onetable",
"version": "2.7.3",
"version": "2.7.4",
"description": "DynamoDB access library for single-table designs",
"main": "dist/cjs/index.js",
"module": "dist/mjs/index.js",
Expand Down
17 changes: 10 additions & 7 deletions src/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ export class Model {
}

/*
Update an item with unique attributes and actually updating a unique property.
Update an item with unique attributes.
Use a transaction to update a unique item for each unique attribute.
*/
async updateUnique(properties, params) {
Expand Down Expand Up @@ -1136,12 +1136,15 @@ export class Model {
}
} else if (value === undefined) {
if (field.required) {
this.table.log.error(`Required field "${name}" in model "${this.name}" not defined in table item`, {
model: this.name,
raw,
params,
field,
})
/*
Transactions transform the properties to return something, but
does not have all the properties and required fields may be missing)
*/
if (!params.transaction) {
this.table.log.error(`Required field "${name}" in model "${this.name}" not defined in table item`, {
model: this.name, raw, params, field,
})
}
}
} else if (field.schema && value !== null && typeof value == 'object') {
if (field.items && Array.isArray(value)) {
Expand Down
32 changes: 14 additions & 18 deletions test/unique.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,18 @@ test('Create user 1', async () => {
user = await User.create(props)
expect(user).toMatchObject(props)

let items = await table.scanItems()
let items = await table.scanItems({}, {parse: true})
expect(items.length).toBe(3)

let pk = (() => {
if (isV3()) {
let unique = items.filter((item) => item.pk.S.indexOf('interpolated') >= 0)
return unique[0].pk.S
}
if (isV2()) {
let unique = items.filter((item) => item.pk.indexOf('interpolated') >= 0)
return unique[0].pk
}
let upk = (() => {
let unique = items.filter((item) =>
item.pk?.indexOf('interpolated') >= 0
)
return unique[0].pk
})()
expect(pk.indexOf('Peter Smith') >= 0).toBe(true)
expect(pk.indexOf('[email protected]') >= 0).toBe(true)
expect(pk.indexOf('User') >= 0).toBe(true)
expect(upk.indexOf('Peter Smith') >= 0).toBe(true)
expect(upk.indexOf('[email protected]') >= 0).toBe(true)
expect(upk.indexOf('User') >= 0).toBe(true)
})

test('Create user 2', async () => {
Expand All @@ -66,7 +62,7 @@ test('Create user 2', async () => {
user = await User.create(props)
expect(user).toMatchObject(props)

let items = await table.scanItems()
let items = await table.scanItems({}, {parse: true})
expect(items.length).toBe(7)
})

Expand All @@ -78,7 +74,7 @@ test('Update user 2 with the same email', async () => {
user = await User.update(props, {return: 'get'})
expect(user).toMatchObject(props)

let items = await table.scanItems()
let items = await table.scanItems({}, {parse: true})
expect(items.length).toBe(7)
})

Expand All @@ -90,7 +86,7 @@ test('Update user 2 with unique email', async () => {
user = await User.update(props, {return: 'get'})
expect(user).toMatchObject(props)

let items = await table.scanItems()
let items = await table.scanItems({}, {parse: true})
expect(items.length).toBe(7)
})

Expand All @@ -116,7 +112,7 @@ test('Update with unknown property', async () => {
const {unknown, ...expectedProps} = props
expect(user).toMatchObject(expectedProps)

let items = await table.scanItems()
let items = await table.scanItems({}, {parse: true})
expect(items.length).toBe(7)
})

Expand All @@ -125,7 +121,7 @@ test('Update to remove optional unique property', async () => {
name: 'Judy Smith',
phone: null,
}
user = await User.update(props, {return: 'get', log: false})
user = await User.update(props, {return: 'get'})
const {phone, ...expectedProps} = props
expect(user).toMatchObject(expectedProps)
expect(user.phone).toBeUndefined()
Expand Down

0 comments on commit e20250a

Please sign in to comment.