{"version":3,"file":"ModelRelationshipField.mjs","sources":["../../src/ModelRelationshipField.ts"],"sourcesContent":["import { allow } from './Authorization';\n/**\n * Used to \"attach\" auth types to ModelField without exposing them on the builder.\n */\nexport const __auth = Symbol('__auth');\nconst _brandName = 'modelRelationshipField';\n/**\n * Model relationship types\n */\nexport var ModelRelationshipTypes;\n(function (ModelRelationshipTypes) {\n    ModelRelationshipTypes[\"hasOne\"] = \"hasOne\";\n    ModelRelationshipTypes[\"hasMany\"] = \"hasMany\";\n    ModelRelationshipTypes[\"belongsTo\"] = \"belongsTo\";\n})(ModelRelationshipTypes || (ModelRelationshipTypes = {}));\nconst _relationshipModifiers = [\n    'required',\n    'valueRequired',\n    'authorization',\n];\nconst relationModifierMap = {\n    belongsTo: ['authorization'],\n    hasMany: ['valueRequired', 'authorization'],\n    hasOne: ['required', 'authorization'],\n};\nfunction _modelRelationshipField(type, relatedModel, references) {\n    const data = {\n        relatedModel,\n        type,\n        fieldType: 'model',\n        array: false,\n        valueRequired: false,\n        arrayRequired: false,\n        references,\n        authorization: [],\n    };\n    data.array = type === 'hasMany';\n    const relationshipBuilderFunctions = {\n        required() {\n            data.arrayRequired = true;\n            return this;\n        },\n        valueRequired() {\n            data.valueRequired = true;\n            return this;\n        },\n        authorization(callback) {\n            const rules = callback(allow);\n            data.authorization = Array.isArray(rules) ? rules : [rules];\n            return this;\n        },\n    };\n    const builder = Object.fromEntries(relationModifierMap[type].map((key) => [\n        key,\n        relationshipBuilderFunctions[key],\n    ]));\n    return {\n        ...builder,\n        data,\n    };\n}\n/**\n * Create one-to-one relationship between two models using the `hasOne(\"MODEL_NAME\", \"REFERENCE_FIELD(s)\")` method.\n * A hasOne relationship always uses a reference to the related model's identifier. Typically this is the `id` field\n * unless overwritten with the `identifier()` method.\n * @example\n * const schema = a.schema({\n *   Cart: a.model({\n *     items: a.string().required().array(),\n *     // 1. Create reference field\n *     customerId: a.id(),\n *     // 2. Create relationship field with the reference field\n *     customer: a.belongsTo('Customer', 'customerId'),\n *   }),\n *   Customer: a.model({\n *     name: a.string(),\n *     // 3. Create relationship field with the reference field\n *     //    from the Cart model\n *     activeCart: a.hasOne('Cart', 'customerId')\n *   }),\n * });\n * @see {@link https://docs.amplify.aws/react/build-a-backend/data/data-modeling/relationships/#model-a-one-to-one-relationship}\n * @param relatedModel the name of the related model\n * @param references the field(s) that should be used to reference the related model\n * @returns a one-to-one relationship definition\n */\nexport function hasOne(relatedModel, references) {\n    return _modelRelationshipField(ModelRelationshipTypes.hasOne, relatedModel, Array.isArray(references) ? references : [references]);\n}\n/**\n * Create a one-directional one-to-many relationship between two models using the `hasMany(\"MODEL_NAME\", \"REFERENCE_FIELD(s)\")` method.\n * @example\n * const schema = a.schema({\n *   Member: a.model({\n *     name: a.string().required(),\n *     // 1. Create a reference field\n *     teamId: a.id(),\n *     // 2. Create a belongsTo relationship with the reference field\n *     team: a.belongsTo('Team', 'teamId'),\n *   })\n *   .authorization(allow => [allow.publicApiKey()]),\n *\n *   Team: a.model({\n *     mantra: a.string().required(),\n *     // 3. Create a hasMany relationship with the reference field\n *     //    from the `Member`s model.\n *     members: a.hasMany('Member', 'teamId'),\n *   })\n *   .authorization(allow => [allow.publicApiKey()]),\n * });\n * @see {@link https://docs.amplify.aws/react/build-a-backend/data/data-modeling/relationships/#model-one-to-many-relationships}\n * @param relatedModel the name of the related model\n * @param references the field(s) that should be used to reference the related model\n * @returns a one-to-many relationship definition\n */\nexport function hasMany(relatedModel, references) {\n    return _modelRelationshipField(ModelRelationshipTypes.hasMany, relatedModel, Array.isArray(references) ? references : [references]);\n}\n/**\n * Use `belongsTo()` to create a field to query the related `hasOne()` or `hasMany()` relationship.\n * The belongsTo() method requires that a hasOne() or hasMany() relationship already exists from\n * parent to the related model.\n *\n * @example\n * // one-to-many relationship\n * const schema = a.schema({\n *   Member: a.model({\n *     name: a.string().required(),\n *     // 1. Create a reference field\n *     teamId: a.id(),\n *     // 2. Create a belongsTo relationship with the reference field\n *     team: a.belongsTo('Team', 'teamId'),\n *   })\n *   .authorization(allow => [allow.publicApiKey()]),\n *\n *   Team: a.model({\n *     mantra: a.string().required(),\n *     // 3. Create a hasMany relationship with the reference field\n *     //    from the `Member`s model.\n *     members: a.hasMany('Member', 'teamId'),\n *   })\n *   .authorization(allow => [allow.publicApiKey()]),\n * });\n * @example\n * // one-to-one relationship\n * const schema = a.schema({\n *   Cart: a.model({\n *     items: a.string().required().array(),\n *     // 1. Create reference field\n *     customerId: a.id(),\n *     // 2. Create relationship field with the reference field\n *     customer: a.belongsTo('Customer', 'customerId'),\n *   }),\n *   Customer: a.model({\n *     name: a.string(),\n *     // 3. Create relationship field with the reference field\n *     //    from the Cart model\n *     activeCart: a.hasOne('Cart', 'customerId')\n *   }),\n * });\n * @see {@link https://docs.amplify.aws/react/build-a-backend/data/data-modeling/relationships/}\n * @param relatedModel name of the related `.hasOne()` or `.hasMany()` model\n * @param references the field(s) that should be used to reference the related model\n * @returns a belong-to relationship definition\n */\nexport function belongsTo(relatedModel, references) {\n    return _modelRelationshipField(ModelRelationshipTypes.belongsTo, relatedModel, Array.isArray(references) ? references : [references]);\n}\n"],"names":[],"mappings":";;AACA;AACA;AACA;AACY,MAAC,MAAM,GAAG,MAAM,CAAC,QAAQ;AAErC;AACA;AACA;AACU,IAAC;AACX,CAAC,UAAU,sBAAsB,EAAE;AACnC,IAAI,sBAAsB,CAAC,QAAQ,CAAC,GAAG,QAAQ;AAC/C,IAAI,sBAAsB,CAAC,SAAS,CAAC,GAAG,SAAS;AACjD,IAAI,sBAAsB,CAAC,WAAW,CAAC,GAAG,WAAW;AACrD,CAAC,EAAE,sBAAsB,KAAK,sBAAsB,GAAG,EAAE,CAAC,CAAC;AAM3D,MAAM,mBAAmB,GAAG;AAC5B,IAAI,SAAS,EAAE,CAAC,eAAe,CAAC;AAChC,IAAI,OAAO,EAAE,CAAC,eAAe,EAAE,eAAe,CAAC;AAC/C,IAAI,MAAM,EAAE,CAAC,UAAU,EAAE,eAAe,CAAC;AACzC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;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,MAAM,CAAC,YAAY,EAAE,UAAU,EAAE;AACjD,IAAI,OAAO,uBAAuB,CAAC,sBAAsB,CAAC,MAAM,EAAE,YAAY,EAAE,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,GAAG,UAAU,GAAG,CAAC,UAAU,CAAC,CAAC;AACtI;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,OAAO,CAAC,YAAY,EAAE,UAAU,EAAE;AAClD,IAAI,OAAO,uBAAuB,CAAC,sBAAsB,CAAC,OAAO,EAAE,YAAY,EAAE,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,GAAG,UAAU,GAAG,CAAC,UAAU,CAAC,CAAC;AACvI;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,SAAS,CAAC,YAAY,EAAE,UAAU,EAAE;AACpD,IAAI,OAAO,uBAAuB,CAAC,sBAAsB,CAAC,SAAS,EAAE,YAAY,EAAE,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,GAAG,UAAU,GAAG,CAAC,UAAU,CAAC,CAAC;AACzI;;;;"}