Skip to content

Commit

Permalink
DEV: add warn mode in schema params
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael O'Brien committed Sep 3, 2024
1 parent e725bc9 commit 20b10cd
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
3 changes: 3 additions & 0 deletions src/Model.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}

/*
Expand Down Expand Up @@ -261,6 +262,7 @@ export type OneParams = {
many?: boolean
maxPages?: number
next?: object
// DEPRECATED
noerror?: boolean
parse?: boolean
partial?: boolean
Expand All @@ -283,6 +285,7 @@ export type OneParams = {
transaction?: object
type?: string
tunnel?: object
warn?: Boolean
where?: string
profile?: string
}
Expand Down
26 changes: 13 additions & 13 deletions src/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
}
Expand All @@ -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)
}
}
/*
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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. ` +
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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.'
)
Expand Down
3 changes: 3 additions & 0 deletions src/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ export class Schema {
if (params.timestamps == null) {
params.timestamps = false
}
if (params.warn == null) {
params.warn = false
}
return params
}

Expand Down
2 changes: 2 additions & 0 deletions src/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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})
Expand Down
12 changes: 6 additions & 6 deletions test/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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: "[email protected]",
})
dump("USER", user)
let result = await User.find({}, {log: true})
*/
/*
Put your code here
*/
})

test('Destroy Table', async () => {
Expand Down

0 comments on commit 20b10cd

Please sign in to comment.