UNPKG

shyft

Version:

Model driven GraphQL API framework

120 lines (119 loc) 5.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.processCursors = exports.processCursor = void 0; var util_1 = require("./util"); var processCursor = function (entity, cursor, orderBy, reverse) { var _a, _b; var $LT = reverse ? '$gt' : '$lt'; var $GT = reverse ? '$lt' : '$gt'; var $LTE = reverse ? '$gte' : '$lte'; var $GTE = reverse ? '$lte' : '$gte'; var where = {}; if (entity && cursor) { util_1.passOrThrow(util_1.isArray(cursor[entity.name]), function () { return 'Incompatible cursor for this entity'; }); util_1.passOrThrow(util_1.isArray(orderBy), function () { return 'orderBy needs to be an array of order definitions'; }); var orderList_1 = []; var orderMap_1 = {}; orderBy.map(function (_a) { var attribute = _a.attribute, direction = _a.direction; util_1.passOrThrow(attribute && direction, function () { return 'orderBy needs to be an array of attributes and respective sort order'; }); orderMap_1[attribute] = direction; }); var primaryAttribute_1 = entity.getPrimaryAttribute(); var attributes_1 = entity.getAttributes(); var foundUniqueAttribute_1 = false; cursor[entity.name].map(function (filter) { if (filter.length !== 2) { throw new Error('Cursor malformed'); } var attributeName = filter[0]; util_1.passOrThrow(attributes_1[attributeName], function () { return "Unknown attribute '" + attributeName + "' used in cursor"; }); if (attributeName !== primaryAttribute_1.name) { util_1.passOrThrow(orderMap_1[attributeName], function () { return "Cursor works only on sorted attributes (check: '" + attributeName + "')"; }); } var attribute = attributes_1[attributeName]; // limit where clause to the first attribute which is defined as unique if (!foundUniqueAttribute_1) { orderList_1.push(attributeName); } if (attribute.unique || attributeName === primaryAttribute_1.name) { foundUniqueAttribute_1 = true; } }); util_1.passOrThrow(foundUniqueAttribute_1, function () { return 'Cursor needs to have at least one attribute defined as unique'; }); // if more than 2 attributes are used for the cursor take only the first and the last (primary key) if (orderList_1.length > 2) { orderList_1.splice(1, orderList_1.length - 2); } // simple filter for single attributes if (orderList_1.length === 1) { var attributeName = orderList_1[0]; var value = cursor[entity.name][0][1]; if (orderMap_1[attributeName] === 'DESC') { where[attributeName] = (_a = {}, _a[$LT] = value, _a); } else { where[attributeName] = (_b = {}, _b[$GT] = value, _b); } } else { where.$not = {}; cursor[entity.name].map(function (filter) { var _a, _b, _c, _d; var attributeName = filter[0]; var value = filter[1]; // ignore attributes that obsolete due to prior unique attribute if (orderList_1.indexOf(attributeName) === -1) { return; } var attribute = attributes_1[attributeName]; if (attribute.unique) { if (orderMap_1[attributeName] === 'DESC') { where.$not[attributeName] = (_a = {}, _a[$GTE] = value, _a); } else { where.$not[attributeName] = (_b = {}, _b[$LTE] = value, _b); } } else { where.$not[attributeName] = value; if (orderMap_1[attributeName] === 'DESC') { where[attributeName] = (_c = {}, _c[$LTE] = value, _c); } else { where[attributeName] = (_d = {}, _d[$GTE] = value, _d); } } }); } } return where; }; exports.processCursor = processCursor; var processCursors = function (entity, args) { var after = args.after, before = args.before; var where = { $and: [ exports.processCursor(entity, after, args.orderBy), exports.processCursor(entity, before, args.orderBy, true), ], }; return where; }; exports.processCursors = processCursors;