{"version":3,"file":"index.mjs","sources":["../../../src/predicates/index.ts"],"sourcesContent":["import { extractPrimaryKeyFieldNames, extractPrimaryKeyValues } from '../util';\nexport { ModelSortPredicateCreator } from './sort';\nconst predicatesAllSet = new WeakSet();\nexport function isPredicatesAll(predicate) {\n    return predicatesAllSet.has(predicate);\n}\n/**\n * The valid logical grouping keys for a predicate group.\n */\nconst groupKeys = new Set(['and', 'or', 'not']);\n/**\n * Determines whether an object is a GraphQL style predicate \"group\", which must be an\n * object containing a single \"group key\", which then contains the child condition(s).\n *\n * E.g.,\n *\n * ```\n * { and: [ ... ] }\n * { not: { ... } }\n * ```\n *\n * @param o The object to test.\n */\nconst isGroup = o => {\n    const keys = [...Object.keys(o)];\n    return keys.length === 1 && groupKeys.has(keys[0]);\n};\n/**\n * Determines whether an object specifies no conditions and should match everything,\n * as would be the case with `Predicates.ALL`.\n *\n * @param o The object to test.\n */\nconst isEmpty = o => {\n    return !Array.isArray(o) && Object.keys(o).length === 0;\n};\n/**\n * The valid comparison operators that can be used as keys in a predicate comparison object.\n */\nexport const comparisonKeys = new Set([\n    'eq',\n    'ne',\n    'gt',\n    'lt',\n    'ge',\n    'le',\n    'contains',\n    'notContains',\n    'beginsWith',\n    'between',\n    'in',\n    'notIn',\n]);\n/**\n * Determines whether an object is a GraphQL style predicate comparison node, which must\n * be an object containing a single \"comparison operator\" key, which then contains the\n * operand or operands to compare against.\n *\n * @param o The object to test.\n */\nconst isComparison = o => {\n    const keys = [...Object.keys(o)];\n    return !Array.isArray(o) && keys.length === 1 && comparisonKeys.has(keys[0]);\n};\n/**\n * A light check to determine whether an object is a valid GraphQL Condition AST.\n *\n * @param o The object to test.\n */\nconst isValid = o => {\n    if (Array.isArray(o)) {\n        return o.every(v => isValid(v));\n    }\n    else {\n        return Object.keys(o).length <= 1;\n    }\n};\n// This symbol is not used at runtime, only its type (unique symbol)\nexport const PredicateAll = Symbol('A predicate that matches all records');\nexport class Predicates {\n    static get ALL() {\n        // eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n        const predicate = (c => c);\n        predicatesAllSet.add(predicate);\n        return predicate;\n    }\n}\nexport class ModelPredicateCreator {\n    /**\n     * Determines whether the given storage predicate (lookup key) is a predicate\n     * key that DataStore recognizes.\n     *\n     * @param predicate The storage predicate (lookup key) to test.\n     */\n    static isValidPredicate(predicate) {\n        return ModelPredicateCreator.predicateGroupsMap.has(predicate);\n    }\n    /**\n     * Looks for the storage predicate AST that corresponds to a given storage\n     * predicate key.\n     *\n     * The key must have been created internally by a DataStore utility\n     * method, such as `ModelPredicate.createFromAST()`.\n     *\n     * @param predicate The predicate reference to look up.\n     * @param throwOnInvalid Whether to throw an exception if the predicate\n     * isn't a valid DataStore predicate.\n     */\n    static getPredicates(predicate, throwOnInvalid = true) {\n        if (throwOnInvalid && !ModelPredicateCreator.isValidPredicate(predicate)) {\n            throw new Error('The predicate is not valid');\n        }\n        return ModelPredicateCreator.predicateGroupsMap.get(predicate);\n    }\n    /**\n     * using the PK values from the given `model` (which can be a partial of T\n     * Creates a predicate that matches an instance described by `modelDefinition`\n     * that contains only PK field values.)\n     *\n     * @param modelDefinition The model definition to create a predicate for.\n     * @param model The model instance to extract value equalities from.\n     */\n    static createForPk(modelDefinition, model) {\n        const keyFields = extractPrimaryKeyFieldNames(modelDefinition);\n        const keyValues = extractPrimaryKeyValues(model, keyFields);\n        const predicate = this.createFromAST(modelDefinition, {\n            and: keyFields.map((field, idx) => {\n                const operand = keyValues[idx];\n                return { [field]: { eq: operand } };\n            }),\n        });\n        return predicate;\n    }\n    /**\n     * Searches a `Model` table for records matching the given equalities object.\n     *\n     * This only matches against fields given in the equalities object. No other\n     * fields are tested by the predicate.\n     *\n     * @param modelDefinition The model we need a predicate for.\n     * @param flatEqualities An object holding field equalities to search for.\n     */\n    static createFromFlatEqualities(modelDefinition, flatEqualities) {\n        const ast = {\n            and: Object.entries(flatEqualities).map(([k, v]) => ({ [k]: { eq: v } })),\n        };\n        return this.createFromAST(modelDefinition, ast);\n    }\n    /**\n     * Accepts a GraphQL style filter predicate tree and transforms it into an\n     * AST that can be used for a storage adapter predicate. Example input:\n     *\n     * ```js\n     * {\n     * \tand: [\n     * \t\t{ name: { eq: \"Bob Jones\" } },\n     * \t\t{ age: { between: [32, 64] } },\n     * \t\t{ not: {\n     * \t\t\tor: [\n     * \t\t\t\t{ favoriteFood: { eq: 'pizza' } },\n     * \t\t\t\t{ favoriteFood: { eq: 'tacos' } },\n     * \t\t\t]\n     * \t\t}}\n     * \t]\n     * }\n     * ```\n     *\n     * @param gql GraphQL style filter node.\n     */\n    static transformGraphQLFilterNodeToPredicateAST(gql) {\n        if (!isValid(gql)) {\n            throw new Error('Invalid GraphQL Condition or subtree: ' + JSON.stringify(gql));\n        }\n        if (isEmpty(gql)) {\n            return {\n                type: 'and',\n                predicates: [],\n            };\n        }\n        else if (isGroup(gql)) {\n            const groupkey = Object.keys(gql)[0];\n            const children = this.transformGraphQLFilterNodeToPredicateAST(gql[groupkey]);\n            return {\n                type: groupkey,\n                predicates: Array.isArray(children) ? children : [children],\n            };\n        }\n        else if (isComparison(gql)) {\n            const operatorKey = Object.keys(gql)[0];\n            return {\n                operator: operatorKey,\n                operand: gql[operatorKey],\n            };\n        }\n        else {\n            if (Array.isArray(gql)) {\n                return gql.map(o => this.transformGraphQLFilterNodeToPredicateAST(o));\n            }\n            else {\n                const fieldKey = Object.keys(gql)[0];\n                return {\n                    field: fieldKey,\n                    ...this.transformGraphQLFilterNodeToPredicateAST(gql[fieldKey]),\n                };\n            }\n        }\n    }\n    /**\n     * Accepts a GraphQL style filter predicate tree and transforms it into a predicate\n     * that storage adapters understand. Example input:\n     *\n     * ```js\n     * {\n     * \tand: [\n     * \t\t{ name: { eq: \"Bob Jones\" } },\n     * \t\t{ age: { between: [32, 64] } },\n     * \t\t{ not: {\n     * \t\t\tor: [\n     * \t\t\t\t{ favoriteFood: { eq: 'pizza' } },\n     * \t\t\t\t{ favoriteFood: { eq: 'tacos' } },\n     * \t\t\t]\n     * \t\t}}\n     * \t]\n     * }\n     * ```\n     *\n     * @param modelDefinition The model that the AST/predicate must be compatible with.\n     * @param ast The graphQL style AST that should specify conditions for `modelDefinition`.\n     */\n    static createFromAST(modelDefinition, ast) {\n        const key = {};\n        ModelPredicateCreator.predicateGroupsMap.set(key, this.transformGraphQLFilterNodeToPredicateAST(ast));\n        return key;\n    }\n}\n/**\n * Map of storage predicates (key objects) to storage predicate AST's.\n */\nModelPredicateCreator.predicateGroupsMap = new WeakMap();\n"],"names":[],"mappings":";;;AAEA,MAAM,gBAAgB,GAAG,IAAI,OAAO,EAAE;AAC/B,SAAS,eAAe,CAAC,SAAS,EAAE;AAC3C,IAAI,OAAO,gBAAgB,CAAC,GAAG,CAAC,SAAS,CAAC;AAC1C;AACA;AACA;AACA;AACA,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC,CAAC,KAAK,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;AAC/C;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAM,OAAO,GAAG,CAAC,IAAI;AACrB,IAAI,MAAM,IAAI,GAAG,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACpC,IAAI,OAAO,IAAI,CAAC,MAAM,KAAK,CAAC,IAAI,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACtD,CAAC;AACD;AACA;AACA;AACA;AACA;AACA;AACA,MAAM,OAAO,GAAG,CAAC,IAAI;AACrB,IAAI,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,KAAK,CAAC;AAC3D,CAAC;AACD;AACA;AACA;AACY,MAAC,cAAc,GAAG,IAAI,GAAG,CAAC;AACtC,IAAI,IAAI;AACR,IAAI,IAAI;AACR,IAAI,IAAI;AACR,IAAI,IAAI;AACR,IAAI,IAAI;AACR,I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