Skip to content

Commit

Permalink
Refactor ands simplify ModelMetadata
Browse files Browse the repository at this point in the history
Add toString method to FieldMetadata.
  • Loading branch information
fwextensions committed Sep 4, 2022
1 parent 4da5add commit dcad4ad
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
8 changes: 8 additions & 0 deletions src/metadata/FieldMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ class FieldMetadata {
field
);
}

toString() {
return `FieldMetadata: ${this.name}`;
}

get [Symbol.toStringTag]() {
return this.toString();
}
}

module.exports = FieldMetadata;
27 changes: 15 additions & 12 deletions src/metadata/ModelMetadata.js
Original file line number Diff line number Diff line change
@@ -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 = [
{
Expand Down Expand Up @@ -31,27 +33,28 @@ 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() {
return this.params;
}

getObjectFields() {
return this.fields.filter(({ type, isParam }) => isParam || type === 'enum');
return this.getFields(undefined, ({ type, isParam }) => isParam || type === 'enum');
}
}

Expand Down

0 comments on commit dcad4ad

Please sign in to comment.