From 20b10cd6ae9f0b05582ec57160ff73307cf2dc69 Mon Sep 17 00:00:00 2001 From: Michael O'Brien Date: Tue, 3 Sep 2024 12:07:01 +1000 Subject: [PATCH] DEV: add warn mode in schema params --- src/Model.d.ts | 3 +++ src/Model.js | 26 +++++++++++++------------- src/Schema.js | 3 +++ src/Table.js | 2 ++ test/debug.ts | 12 ++++++------ 5 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/Model.d.ts b/src/Model.d.ts index 2ac5590..a59b9f8 100644 --- a/src/Model.d.ts +++ b/src/Model.d.ts @@ -95,6 +95,7 @@ export type OneSchemaParams = { separator?: string // Separator string uses in value templates typeField?: string // Name of model type attribute. Default "_type". updatedField?: string // Name of "updated" timestamp attribute. Default 'updated'. + warn?: boolean // Emit warnings for some conditions. Default false. } /* @@ -261,6 +262,7 @@ export type OneParams = { many?: boolean maxPages?: number next?: object + // DEPRECATED noerror?: boolean parse?: boolean partial?: boolean @@ -283,6 +285,7 @@ export type OneParams = { transaction?: object type?: string tunnel?: object + warn?: Boolean where?: string profile?: string } diff --git a/src/Model.js b/src/Model.js index f1b15b4..059e128 100644 --- a/src/Model.js +++ b/src/Model.js @@ -330,7 +330,7 @@ export class Model { params.expression = expression let items = (t.TransactItems = t.TransactItems || []) items.push({[top]: cmd}) - return this.transformReadItem(op, properties, properties, params) + return this.transformReadItem(op, properties, properties, params, expression) } else { throw new OneTableArgError(`Unknown transaction operation ${op}`) } @@ -345,12 +345,12 @@ export class Model { if (op == 'get') { let list = (ritems[this.tableName] = ritems[this.tableName] || {Keys: []}) list.Keys.push(cmd.Keys) - return this.transformReadItem(op, properties, properties, params) + return this.transformReadItem(op, properties, properties, params, expression) } else { let list = (ritems[this.tableName] = ritems[this.tableName] || []) let bop = BatchOps[op] list.push({[bop]: cmd}) - return this.transformReadItem(op, properties, properties, params) + return this.transformReadItem(op, properties, properties, params, expression) } } /* @@ -548,7 +548,7 @@ export class Model { // Special "unique" model for unique fields. Don't return in result. continue } - items[index] = model.transformReadItem(op, item, properties, params) + items[index] = model.transformReadItem(op, item, properties, params, expression) } } return items @@ -959,7 +959,7 @@ export class Model { execute: params.execute, }) } - if (this.table.warn !== false) { + if (this.table.warn) { console.warn( `Update with unique items uses transactions and cannot return the updated item.` + `Use params {return: 'none'} to squelch this warning. ` + @@ -1089,14 +1089,14 @@ export class Model { /* Map Dynamo types to Javascript types after reading data */ - transformReadItem(op, raw, properties, params) { + transformReadItem(op, raw, properties, params, expression) { if (!raw) { return raw } - return this.transformReadBlock(op, raw, properties, params, this.block.fields) + return this.transformReadBlock(op, raw, properties, params, this.block.fields, expression) } - transformReadBlock(op, raw, properties, params, fields) { + transformReadBlock(op, raw, properties, params, fields, expression) { let rec = {} for (let [name, field] of Object.entries(fields)) { // Skip hidden params. Follow needs hidden params to do the follow. @@ -1135,10 +1135,10 @@ export class Model { 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 && !params.batch && !params.fields && !field.encode && !params.noerror) { - this.table.log.error(`Required field "${name}" in model "${this.name}" not defined in table item`, { - model: this.name, raw, params, field, - }) + if (!params.transaction && !params.batch && !params.fields && !field.encode && !expression.index.project) { + if (params.warn || this.table.warn) { + 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') { @@ -1413,7 +1413,7 @@ export class Model { */ tunnelProperties(properties, params) { if (params.tunnel) { - if (this.table.warn !== false) { + if (this.table.warn) { console.warn( 'WARNING: tunnel properties should not be required for typescript and will be removed soon.' ) diff --git a/src/Schema.js b/src/Schema.js index 89eec43..eaac181 100644 --- a/src/Schema.js +++ b/src/Schema.js @@ -271,6 +271,9 @@ export class Schema { if (params.timestamps == null) { params.timestamps = false } + if (params.warn == null) { + params.warn = false + } return params } diff --git a/src/Table.js b/src/Table.js index 4a70142..95a0ba4 100644 --- a/src/Table.js +++ b/src/Table.js @@ -108,6 +108,7 @@ export class Table { this.separator = '#' this.timestamps = false this.updatedField = 'updated' + this.warn = false this.schema = new Schema(this, params.schema) @@ -186,6 +187,7 @@ export class Table { this.timestamps = params.timestamps != null ? params.timestamps : false this.typeField = params.typeField || '_type' this.updatedField = params.updatedField || 'updated' + this.warn = params.warn || false if (params.hidden != null) { this.log.warn(`Schema hidden params should be specified via the Table constructor params`, {'@stack': true}) diff --git a/test/debug.ts b/test/debug.ts index 7e821ee..69148cd 100644 --- a/test/debug.ts +++ b/test/debug.ts @@ -23,11 +23,12 @@ const schema = { isoDates: true, timestamps: true, separator: '#', + warn: false, }, models: { User: { - pk: { type: String, value: '${_type}#${email}' }, - sk: { type: String, value: '${_type}#' }, + pk: { type: String, value: 'user#' }, + sk: { type: String, value: 'user#${email}' }, email: { type: String, required: true }, } } as const, @@ -51,17 +52,16 @@ test('Create Table', async () => { }) test('Test', async () => { - let User = table.getModel('User') /* + Put your code here + + let User = table.getModel('User') let user = await User.create({ email: "user@example.com", }) dump("USER", user) let result = await User.find({}, {log: true}) */ - /* - Put your code here - */ }) test('Destroy Table', async () => {