UNPKG

shyft

Version:

Model driven GraphQL API framework

85 lines (84 loc) 4.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.processEntityIndexes = exports.isIndex = exports.Index = exports.indexTypes = exports.INDEX_GENERIC = exports.INDEX_UNIQUE = void 0; var lodash_1 = require("lodash"); var util_1 = require("../util"); exports.INDEX_UNIQUE = 'unique'; exports.INDEX_GENERIC = 'generic'; exports.indexTypes = [exports.INDEX_UNIQUE, exports.INDEX_GENERIC]; var Index = /** @class */ (function () { function Index(setup) { if (setup === void 0) { setup = {}; } var type = setup.type, attributes = setup.attributes; util_1.passOrThrow(type, function () { return 'Missing index type'; }); util_1.passOrThrow(exports.indexTypes.indexOf(type) >= 0, function () { return "Unknown index type '" + type + "' used, try one of these: '" + exports.indexTypes.join(', ') + "'"; }); util_1.passOrThrow(util_1.isArray(attributes, true), function () { return "Index definition of type '" + type + "' needs to have a list of attributes"; }); attributes.map(function (attribute) { util_1.passOrThrow(typeof attribute === 'string', function () { return "Index definition of type '" + type + "' needs to have a list of attribute names"; }); }); util_1.passOrThrow(attributes.length === lodash_1.uniq(attributes).length, function () { return "Index definition of type '" + type + "' needs to have a list of unique attribute names"; }); this.type = type; this.attributes = attributes; } Index.prototype.toString = function () { return this.type; }; return Index; }()); exports.Index = Index; var isIndex = function (obj) { return obj instanceof Index; }; exports.isIndex = isIndex; var processEntityIndexes = function (entity, indexes) { util_1.passOrThrow(util_1.isArray(indexes), function () { return "Entity '" + entity.name + "' indexes definition needs to be an array of indexes"; }); var singleAttributeIndexes = []; var entityAttributes = entity.getAttributes(); indexes.map(function (index, idx) { util_1.passOrThrow(exports.isIndex(index), function () { return "Invalid index definition for entity '" + entity.name + "' at position '" + idx + "'"; }); index.attributes.map(function (attributeName) { util_1.passOrThrow(entityAttributes[attributeName], function () { return "Cannot use attribute '" + entity.name + "." + attributeName + "' in index as it does not exist"; }); if (index.type === exports.INDEX_UNIQUE) { util_1.passOrThrow(entityAttributes[attributeName].required, function () { return "Cannot use attribute '" + entity.name + "." + attributeName + "' in uniqueness index as it is nullable"; }); util_1.passOrThrow(!entityAttributes[attributeName].i18n, function () { return "Cannot use attribute '" + entity.name + "." + attributeName + "' in uniqueness index as it is translatable"; }); if (index.attributes.length === 1) { entityAttributes[attributeName].unique = true; } } if (index.attributes.length === 1) { singleAttributeIndexes.push(attributeName); } }); }); // add new index for single attributes with the 'index' flag set but not defined in an index object by the user Object.keys(entityAttributes).map(function (attributeName) { if (entityAttributes[attributeName].index) { if (!singleAttributeIndexes.includes(attributeName)) { indexes.push(new Index({ type: exports.INDEX_GENERIC, attributes: [attributeName], })); } } }); return indexes; }; exports.processEntityIndexes = processEntityIndexes;