UNPKG

@grouparoo/core

Version:
77 lines (76 loc) 3.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.deleteProperties = exports.loadProperty = void 0; const codeConfig_1 = require("../../classes/codeConfig"); const __1 = require("../.."); // configLoader imports need to be from root const filterHelper_1 = require("../filterHelper"); const sequelize_1 = require("sequelize"); const deprecation_1 = require("../deprecation"); const configWriter_1 = require("../configWriter"); async function loadProperty(configObject, externallyValidate, validate = false) { let isNew = false; const source = await (0, codeConfig_1.getParentByName)(__1.Source, configObject.sourceId); (0, codeConfig_1.validateConfigObjectKeys)(__1.Property, configObject, [ "options", "filters", "name", "identifying", // deprecated ]); let property = await __1.Property.scope(null).findOne({ where: { id: configObject.id, }, }); // don't process bootstrapped properties again if (property && property.isPrimaryKey) return {}; if (!property) { isNew = true; property = await __1.Property.create({ id: configObject.id, //@ts-ignore key: configObject.key || configObject["name"], type: configObject.type, sourceId: source.id, }); } await property.setOptions((0, codeConfig_1.extractNonNullParts)(configObject, "options"), null, externallyValidate); await property.update({ type: configObject.type, //@ts-ignore key: configObject.key || configObject["name"], unique: configObject.unique, isArray: configObject.isArray, locked: configWriter_1.ConfigWriter.getLockKey(configObject), }); if (configObject.filters) { for (const filter of configObject.filters) { if (Object.keys(filterHelper_1.FilterHelper.deprecatedFilters).includes(filter.op)) { throw new Error(`Property filter \`${filter.op}\` has been deprecated and replaced with \`${ //@ts-ignore filterHelper_1.FilterHelper.deprecatedFilters[filter.op]}\`. Read more at https://www.grouparoo.com/docs/support/config-files#properties`); } } await property.setFilters(configObject.filters, externallyValidate); } //@ts-ignore if (!!configObject["identifying"]) { deprecation_1.Deprecation.warnRemoved("config", "Property", "identifying"); } await property.update({ state: "ready" }); (0, codeConfig_1.logModel)(property, validate ? "validated" : isNew ? "created" : "updated"); return { property: [property.id] }; } exports.loadProperty = loadProperty; async function deleteProperties(ids) { const properties = await __1.Property.scope(null).findAll({ where: { locked: (0, codeConfig_1.getCodeConfigLockKey)(), id: { [sequelize_1.Op.notIn]: ids } }, }); for (const i in properties) { const property = properties[i]; await property.update({ state: "deleted", locked: null }); (0, codeConfig_1.logModel)(property, "deleted"); } return properties.map((instance) => instance.id); } exports.deleteProperties = deleteProperties;