ravendb
Version:
RavenDB client for Node.js
79 lines • 3.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TimeSeriesValuesHelper = void 0;
const index_js_1 = require("../../../Exceptions/index.js");
const TypeUtil_js_1 = require("../../../Utility/TypeUtil.js");
class TimeSeriesValuesHelper {
static _cache = new Map();
static getFieldsMapping(clazz) {
if (TimeSeriesValuesHelper._cache.has(clazz)) {
return TimeSeriesValuesHelper._cache.get(clazz);
}
if ("TIME_SERIES_VALUES" in clazz) {
const values = clazz["TIME_SERIES_VALUES"];
if (TypeUtil_js_1.TypeUtil.isArray(values)) {
const mapping = [];
for (const value of values) {
let field;
let name;
if (TypeUtil_js_1.TypeUtil.isString(value)) {
field = value;
name = value;
}
else if (TypeUtil_js_1.TypeUtil.isObject(value) && value.field && value.name) {
field = value.field;
name = value.name;
}
else {
(0, index_js_1.throwError)("InvalidOperationException", "Invalid field mapping. Expected string or { field: string, name: string } object. Got: " + value);
}
const nameAlreadyUsed = mapping.some(x => x.field === field || x.name === name);
if (nameAlreadyUsed) {
(0, index_js_1.throwError)("InvalidOperationException", "All fields and names must be unique.");
}
mapping.push({
field, name
});
}
TimeSeriesValuesHelper._cache.set(clazz, mapping);
return mapping;
}
else {
(0, index_js_1.throwError)("InvalidOperationException", "The mapping of " + clazz + " is invalid.");
}
}
return null;
}
static getValues(clazz, obj) {
const mapping = TimeSeriesValuesHelper.getFieldsMapping(clazz);
if (!mapping) {
return null;
}
return mapping.map(m => obj[m.field]);
}
static setFields(clazz, values, asRollup = false) {
if (!values) {
return null;
}
const mapping = TimeSeriesValuesHelper.getFieldsMapping(clazz);
if (!mapping) {
return null;
}
const obj = new clazz();
for (let i = 0; i < mapping.length; i++) {
let index = i;
const item = mapping[i];
let value = Number.NaN;
if (index < values.length) {
if (asRollup) {
index *= 6;
}
value = values[index];
}
obj[item.field] = value;
}
return obj;
}
}
exports.TimeSeriesValuesHelper = TimeSeriesValuesHelper;
//# sourceMappingURL=TimeSeriesValuesHelper.js.map