diff --git a/src/metadata/FieldMetadata.js b/src/metadata/FieldMetadata.js index f72ac66e..7dff4f25 100644 --- a/src/metadata/FieldMetadata.js +++ b/src/metadata/FieldMetadata.js @@ -21,6 +21,14 @@ class FieldMetadata { field ); } + + toString() { + return `FieldMetadata: ${this.name}`; + } + + get [Symbol.toStringTag]() { + return this.toString(); + } } module.exports = FieldMetadata; diff --git a/src/metadata/ModelMetadata.js b/src/metadata/ModelMetadata.js index 42c3a340..2144fdd2 100644 --- a/src/metadata/ModelMetadata.js +++ b/src/metadata/ModelMetadata.js @@ -1,6 +1,8 @@ const FieldMetadata = require('./FieldMetadata'); -const identity = (field) => [field.name, field]; +const identity = (field) => field; +const keyValue = (field) => [field.name, field]; +const all = () => true; const createdUpdatedFields = [ { @@ -31,19 +33,20 @@ class ModelMetadata { this.tableName = tableName; // append the standard created and updated fields to all models this.fields = Object.freeze([...fields, ...createdUpdatedFields].map((field) => new FieldMetadata(field))); - this.params = Object.freeze(this.fields.filter(({ isParam }) => isParam).map(({ name }) => name)); + this.params = Object.freeze( + this.getFields( + ({ name }) => name, + ({ isParam }) => isParam + ) + ); } - getFieldHash(convertField = identity) { - return this.fields.reduce((result, field) => { - const [name, convertedField] = convertField(field); - - if (name && convertedField) { - result[name] = convertedField; - } + getFields(convertField = identity, filter = all) { + return this.fields.filter(filter).map(convertField); + } - return result; - }, {}); + getFieldHash(convertField = keyValue, filter = all) { + return Object.fromEntries(this.getFields(convertField, filter)); } getParams() { @@ -51,7 +54,7 @@ class ModelMetadata { } getObjectFields() { - return this.fields.filter(({ type, isParam }) => isParam || type === 'enum'); + return this.getFields(undefined, ({ type, isParam }) => isParam || type === 'enum'); } }