@martinmilo/verve
Version:
TypeScript domain modeling library with field-level authorization, business rule validation, and context-aware access control
26 lines • 1.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FieldInitializer = void 0;
const constants_1 = require("../../constants");
const field_1 = require("../../field");
class FieldInitializer {
static initialize(model, schema) {
const schemaEntries = Object.entries(schema);
const fields = model[constants_1.MODEL_FIELDS]();
// Initialize fields and it's instances based on schema
for (const [fieldName, fieldDef] of schemaEntries) {
const fieldInstance = fieldDef.toField(model);
// Skip if field instance key already exists on the model instance
// Note: The field instance itself doesn't hold any state, so there's no need to re-bind it
if (fields[fieldName]) {
continue;
}
// Bind field instance and set it on the model instance
// Even when the field is undefined, we want to initialize field to be able to access it's methods
const boundField = field_1.BoundField.toBoundField(model, fieldName, fieldInstance);
fields[fieldName] = boundField;
}
}
}
exports.FieldInitializer = FieldInitializer;
//# sourceMappingURL=FieldInitializer.js.map