@grouparoo/core
Version:
The Grouparoo Core
48 lines (47 loc) • 1.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadRecord = void 0;
const codeConfig_1 = require("../../classes/codeConfig");
const __1 = require("../.."); // configLoader imports need to be from root
const runMode_1 = require("../runMode");
const propertiesCache_1 = require("../caches/propertiesCache");
async function loadRecord(configObject, externallyValidate, validate = false) {
if ((0, runMode_1.getGrouparooRunMode)() !== "cli:config")
return;
(0, codeConfig_1.validateConfigObjectKeys)(__1.GrouparooRecord, configObject, ["properties"]);
let record = await __1.GrouparooRecord.scope(null).findOne({
where: { id: configObject.id },
});
let isNew = false;
if (!record) {
isNew = true;
record = await __1.GrouparooRecord.create({
id: configObject.id,
modelId: configObject.modelId,
});
}
else {
await record.update({ modelId: configObject.modelId });
}
const nonNullProperties = (0, codeConfig_1.extractNonNullParts)(configObject, "properties");
const primaryKeyProperties = (await propertiesCache_1.PropertiesCache.findAllWithCache(record.modelId, "ready"))
.filter((p) => p.isPrimaryKey === true)
.map((p) => p.id);
let hasPrimaryKeyProperty = false;
for (const propertyId in nonNullProperties) {
if (primaryKeyProperties.includes(propertyId)) {
hasPrimaryKeyProperty = true;
break;
}
}
const serializedProps = JSON.stringify(nonNullProperties);
if (!hasPrimaryKeyProperty) {
throw new Error(`there are no directly mapped record properties provided in ${serializedProps}`);
}
await record.addOrUpdateProperties(nonNullProperties);
(0, codeConfig_1.logModel)(record, validate ? "validated" : isNew ? "created" : "updated", serializedProps);
return {
record: [record.id],
};
}
exports.loadRecord = loadRecord;