diffusion
Version:
Diffusion JavaScript client
105 lines (104 loc) • 3.1 kB
JavaScript
;
/**
* @module diffusion.datatypes.RecordV2
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.SchemaImpl = void 0;
var errors_1 = require("./../../../../errors/errors");
var mutable_record_model_impl_1 = require("./../../../data/record/model/mutable-record-model-impl");
var record_impl_1 = require("./../../../data/record/schema/record-impl");
/**
* Implementation of {@link ConcreteSchema} which extends the Record V2
* {@link Schema} interface
*/
var SchemaImpl = /** @class */ (function () {
/**
* Create a new Schema
*
* @param recordV2Constructor a constructor function for creating {@link RecordV2} implementations
* @param records an array of record schema
*/
function SchemaImpl(recordV2Constructor, records) {
var _this = this;
/**
* The record schema indexed by name
*/
this.recordsMap = {};
this.recordV2Constructor = recordV2Constructor;
this.recordsArray = records;
records.forEach(function (record) {
_this.recordsMap[record.name] = record;
});
}
/**
* @inheritdoc
*/
SchemaImpl.prototype.getRecord = function (name) {
var record = this.recordsMap[name];
if (record) {
return record;
}
throw new errors_1.SchemaViolationError("Record '" + name + "' is not known");
};
/**
* @inheritdoc
*/
SchemaImpl.prototype.getRecords = function () {
return [].concat(this.recordsArray);
};
/**
* @inheritdoc
*/
SchemaImpl.prototype.records = function () {
return this.recordsArray;
};
/**
* @inheritdoc
*/
SchemaImpl.prototype.firstRecord = function () {
return this.recordsArray[0];
};
/**
* @inheritdoc
*/
SchemaImpl.prototype.lastRecord = function () {
return this.recordsArray[this.recordsArray.length - 1];
};
/**
* @inheritdoc
*/
SchemaImpl.prototype.createMutableModel = function () {
return new mutable_record_model_impl_1.MutableRecordModelImpl(this.recordV2Constructor, this);
};
/**
* @inheritdoc
*/
SchemaImpl.prototype.createModel = function () {
var model = [];
this.recordsArray.forEach(function (record) {
var recordValue = record.createModel();
if (record.min > 0) {
for (var i = 0; i < record.min; ++i) {
model.push([].concat(recordValue));
}
}
});
return model;
};
/**
* @inheritdoc
*/
SchemaImpl.prototype.asJSON = function () {
return JSON.stringify({ records: this.recordsArray.map(record_impl_1.toJSON) });
};
/**
* Convert the schema to a string
*
* @return a string representation of the schema
*/
SchemaImpl.prototype.toString = function () {
return "Schema [Records=" + this.recordsArray + "]";
};
return SchemaImpl;
}());
exports.SchemaImpl = SchemaImpl;