UNPKG

@aws-amplify/datastore

Version:

AppSyncLocal support for aws-amplify

258 lines • 8.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var util_1 = require("../util"); var sort_1 = require("./sort"); exports.ModelSortPredicateCreator = sort_1.ModelSortPredicateCreator; var predicatesAllSet = new WeakSet(); function isPredicatesAll(predicate) { return predicatesAllSet.has(predicate); } exports.isPredicatesAll = isPredicatesAll; /** * The valid logical grouping keys for a predicate group. */ var groupKeys = new Set(['and', 'or', 'not']); /** * Determines whether an object is a GraphQL style predicate "group", which must be an * object containing a single "group key", which then contains the child condition(s). * * E.g., * * ``` * { and: [ ... ] } * { not: { ... } } * ``` * * @param o The object to test. */ var isGroup = function (o) { var keys = tslib_1.__spread(Object.keys(o)); return keys.length === 1 && groupKeys.has(keys[0]); }; /** * Determines whether an object specifies no conditions and should match everything, * as would be the case with `Predicates.ALL`. * * @param o The object to test. */ var isEmpty = function (o) { return !Array.isArray(o) && Object.keys(o).length === 0; }; /** * The valid comparison operators that can be used as keys in a predicate comparison object. */ exports.comparisonKeys = new Set([ 'eq', 'ne', 'gt', 'lt', 'ge', 'le', 'contains', 'notContains', 'beginsWith', 'between', ]); /** * Determines whether an object is a GraphQL style predicate comparison node, which must * be an object containing a single "comparison operator" key, which then contains the * operand or operands to compare against. * * @param o The object to test. */ var isComparison = function (o) { var keys = tslib_1.__spread(Object.keys(o)); return !Array.isArray(o) && keys.length === 1 && exports.comparisonKeys.has(keys[0]); }; /** * A light check to determine whether an object is a valid GraphQL Condition AST. * * @param o The object to test. */ var isValid = function (o) { if (Array.isArray(o)) { return o.every(function (v) { return isValid(v); }); } else { return Object.keys(o).length <= 1; } }; // This symbol is not used at runtime, only its type (unique symbol) exports.PredicateAll = Symbol('A predicate that matches all records'); var Predicates = /** @class */ (function () { function Predicates() { } Object.defineProperty(Predicates, "ALL", { get: function () { var predicate = (function (c) { return c; }); predicatesAllSet.add(predicate); return predicate; }, enumerable: true, configurable: true }); return Predicates; }()); exports.Predicates = Predicates; var ModelPredicateCreator = /** @class */ (function () { function ModelPredicateCreator() { } /** * Determines whether the given storage predicate (lookup key) is a predicate * key that DataStore recognizes. * * @param predicate The storage predicate (lookup key) to test. */ ModelPredicateCreator.isValidPredicate = function (predicate) { return ModelPredicateCreator.predicateGroupsMap.has(predicate); }; /** * Looks for the storage predicate AST that corresponds to a given storage * predicate key. * * The key must have been created internally by a DataStore utility * method, such as `ModelPredicate.createFromAST()`. * * @param predicate The predicate reference to look up. * @param throwOnInvalid Whether to throw an exception if the predicate * isn't a valid DataStore predicate. */ ModelPredicateCreator.getPredicates = function (predicate, throwOnInvalid) { if (throwOnInvalid === void 0) { throwOnInvalid = true; } if (throwOnInvalid && !ModelPredicateCreator.isValidPredicate(predicate)) { throw new Error('The predicate is not valid'); } return ModelPredicateCreator.predicateGroupsMap.get(predicate); }; /** * using the PK values from the given `model` (which can be a partial of T * Creates a predicate that matches an instance described by `modelDefinition` * that contains only PK field values.) * * @param modelDefinition The model definition to create a predicate for. * @param model The model instance to extract value equalities from. */ ModelPredicateCreator.createForPk = function (modelDefinition, model) { var keyFields = util_1.extractPrimaryKeyFieldNames(modelDefinition); var keyValues = util_1.extractPrimaryKeyValues(model, keyFields); var predicate = this.createFromAST(modelDefinition, { and: keyFields.map(function (field, idx) { var _a; var operand = keyValues[idx]; return _a = {}, _a[field] = { eq: operand }, _a; }), }); return predicate; }; /** * Searches a `Model` table for records matching the given equalities object. * * This only matches against fields given in the equalities object. No other * fields are tested by the predicate. * * @param modelDefinition The model we need a predicate for. * @param flatEqualities An object holding field equalities to search for. */ ModelPredicateCreator.createFromFlatEqualities = function (modelDefinition, flatEqualities) { var ast = { and: Object.entries(flatEqualities).map(function (_a) { var _b; var _c = tslib_1.__read(_a, 2), k = _c[0], v = _c[1]; return (_b = {}, _b[k] = { eq: v }, _b); }), }; return this.createFromAST(modelDefinition, ast); }; /** * Accepts a GraphQL style filter predicate tree and transforms it into an * AST that can be used for a storage adapter predicate. Example input: * * ```js * { * and: [ * { name: { eq: "Bob Jones" } }, * { age: { between: [32, 64] } }, * { not: { * or: [ * { favoriteFood: { eq: 'pizza' } }, * { favoriteFood: { eq: 'tacos' } }, * ] * }} * ] * } * ``` * * @param gql GraphQL style filter node. */ ModelPredicateCreator.transformGraphQLFilterNodeToPredicateAST = function (gql) { var _this = this; if (!isValid(gql)) { throw new Error('Invalid GraphQL Condition or subtree: ' + JSON.stringify(gql)); } if (isEmpty(gql)) { return { type: 'and', predicates: [], }; } else if (isGroup(gql)) { var groupkey = Object.keys(gql)[0]; var children = this.transformGraphQLFilterNodeToPredicateAST(gql[groupkey]); return { type: groupkey, predicates: Array.isArray(children) ? children : [children], }; } else if (isComparison(gql)) { var operatorKey = Object.keys(gql)[0]; return { operator: operatorKey, operand: gql[operatorKey], }; } else { if (Array.isArray(gql)) { return gql.map(function (o) { return _this.transformGraphQLFilterNodeToPredicateAST(o); }); } else { var fieldKey = Object.keys(gql)[0]; return tslib_1.__assign({ field: fieldKey }, this.transformGraphQLFilterNodeToPredicateAST(gql[fieldKey])); } } }; /** * Accepts a GraphQL style filter predicate tree and transforms it into a predicate * that storage adapters understand. Example input: * * ```js * { * and: [ * { name: { eq: "Bob Jones" } }, * { age: { between: [32, 64] } }, * { not: { * or: [ * { favoriteFood: { eq: 'pizza' } }, * { favoriteFood: { eq: 'tacos' } }, * ] * }} * ] * } * ``` * * @param modelDefinition The model that the AST/predicate must be compatible with. * @param ast The graphQL style AST that should specify conditions for `modelDefinition`. */ ModelPredicateCreator.createFromAST = function (modelDefinition, ast) { var key = {}; ModelPredicateCreator.predicateGroupsMap.set(key, this.transformGraphQLFilterNodeToPredicateAST(ast)); return key; }; /** * Map of storage predicates (key objects) to storage predicate AST's. */ ModelPredicateCreator.predicateGroupsMap = new WeakMap(); return ModelPredicateCreator; }()); exports.ModelPredicateCreator = ModelPredicateCreator; //# sourceMappingURL=index.js.map