From c72ae56ae779b8b0c889b3af2297c22a4cf6825c Mon Sep 17 00:00:00 2001 From: Marcelo Luiz Onhate Date: Wed, 3 Jul 2024 11:35:14 -0300 Subject: [PATCH 1/2] fix: log error on missing required fields when finding with fields selection --- test/find.ts | 19 ++++++++++++++++++- test/schemas/defaultSchema.ts | 8 ++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/test/find.ts b/test/find.ts index 77445c2f..d6eb8010 100644 --- a/test/find.ts +++ b/test/find.ts @@ -1,16 +1,25 @@ /* find-and-scan.ts - Basic find and scan options */ -import {AWS, Client, Match, Table, print, dump, delay} from './utils/init' import {DefaultSchema} from './schemas' +import {Client, Table} from './utils/init' // jest.setTimeout(7200 * 1000) +const onErrorLog = jest.fn(); const table = new Table({ name: 'FindTable', client: Client, partial: false, schema: DefaultSchema, + logger: (level, message, ctx) => { + if (level === 'info') return console.log({message, ctx}) + if (level === 'warn') return console.warn({message, ctx}) + if (level === 'error') { + onErrorLog({message, ctx}) + return console.error({message, ctx}) + } + }, }) test('Create Table', async () => { @@ -21,6 +30,7 @@ test('Create Table', async () => { }) let User = table.getModel('User') +let Pet = table.getModel('Pet') let user: any let users: any @@ -99,6 +109,13 @@ test('List with begins_with', async () => { expect(items.length).toBe(1) }) +test('Find Pets names only (should not log error on missing required fields)', async () => { + await Pet.create({name: 'Guinness', race: 'dog', breed:'shih-tzu'}) + let items = await Pet.find({}, {fields: ['name']}) + expect(items).toHaveLength(1) + expect(onErrorLog).not.toHaveBeenCalled(); +}) + test('Destroy Table', async () => { await table.deleteTable('DeleteTableForever') expect(await table.exists()).toBe(false) diff --git a/test/schemas/defaultSchema.ts b/test/schemas/defaultSchema.ts index 2f33c5f4..dba241df 100644 --- a/test/schemas/defaultSchema.ts +++ b/test/schemas/defaultSchema.ts @@ -35,6 +35,14 @@ export default { gs3pk: {type: String, value: '${_type}#${status}'}, gs3sk: {type: String, value: '${_type}#${name}'}, }, + Pet: { + pk: {type: String, value: '${_type}', hidden: true}, + sk: {type: String, value: '${_type}#${id}', hidden: true}, + id: {type: String, generate: 'ulid'}, + name: {type: String}, + race: {type: String, enum: ['dog', 'cat', 'fish'], required: true}, + breed: {type: String, required: true}, + } }, params: { isoDates: true, From 16741d66c9e8ad6bd2af0c42bc6a6de5db799b78 Mon Sep 17 00:00:00 2001 From: Marcelo Luiz Onhate Date: Wed, 3 Jul 2024 11:39:10 -0300 Subject: [PATCH 2/2] fix: log error on missing required fields when finding with fields selection --- src/Model.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Model.js b/src/Model.js index a2d459aa..f514a862 100644 --- a/src/Model.js +++ b/src/Model.js @@ -1136,11 +1136,12 @@ export class Model { } } else if (value === undefined) { if (field.required) { - /* - Transactions transform the properties to return something, but - does not have all the properties and required fields may be missing) + /* + Transactions transform the properties to return something, but + does not have all the properties and required fields may be missing). + Also find operation with fields selections may not include required fields. */ - if (!params.transaction) { + if (!params.transaction && !params.fields) { this.table.log.error(`Required field "${name}" in model "${this.name}" not defined in table item`, { model: this.name, raw, params, field, }) @@ -1607,7 +1608,7 @@ export class Model { delete properties[name] if (this.getPartial(field, params) === false && pathname.match(/[[.]/)) { /* - Partial disabled for a nested object + Partial disabled for a nested object Don't create remove entry as the entire object is being created/updated */ continue @@ -2034,7 +2035,7 @@ export class Model { /* Return if a field supports partial updates of its children. - Only relevant for fields with nested schema + Only relevant for fields with nested schema */ getPartial(field, params) { let partial = params.partial