167 lines
6.1 KiB
JavaScript
167 lines
6.1 KiB
JavaScript
import { allow } from './Authorization.mjs';
|
|
|
|
/**
|
|
* Used to "attach" auth types to ModelField without exposing them on the builder.
|
|
*/
|
|
const __auth = Symbol('__auth');
|
|
/**
|
|
* Model relationship types
|
|
*/
|
|
var ModelRelationshipTypes;
|
|
(function (ModelRelationshipTypes) {
|
|
ModelRelationshipTypes["hasOne"] = "hasOne";
|
|
ModelRelationshipTypes["hasMany"] = "hasMany";
|
|
ModelRelationshipTypes["belongsTo"] = "belongsTo";
|
|
})(ModelRelationshipTypes || (ModelRelationshipTypes = {}));
|
|
const relationModifierMap = {
|
|
belongsTo: ['authorization'],
|
|
hasMany: ['valueRequired', 'authorization'],
|
|
hasOne: ['required', 'authorization'],
|
|
};
|
|
function _modelRelationshipField(type, relatedModel, references) {
|
|
const data = {
|
|
relatedModel,
|
|
type,
|
|
fieldType: 'model',
|
|
array: false,
|
|
valueRequired: false,
|
|
arrayRequired: false,
|
|
references,
|
|
authorization: [],
|
|
};
|
|
data.array = type === 'hasMany';
|
|
const relationshipBuilderFunctions = {
|
|
required() {
|
|
data.arrayRequired = true;
|
|
return this;
|
|
},
|
|
valueRequired() {
|
|
data.valueRequired = true;
|
|
return this;
|
|
},
|
|
authorization(callback) {
|
|
const rules = callback(allow);
|
|
data.authorization = Array.isArray(rules) ? rules : [rules];
|
|
return this;
|
|
},
|
|
};
|
|
const builder = Object.fromEntries(relationModifierMap[type].map((key) => [
|
|
key,
|
|
relationshipBuilderFunctions[key],
|
|
]));
|
|
return {
|
|
...builder,
|
|
data,
|
|
};
|
|
}
|
|
/**
|
|
* Create one-to-one relationship between two models using the `hasOne("MODEL_NAME", "REFERENCE_FIELD(s)")` method.
|
|
* A hasOne relationship always uses a reference to the related model's identifier. Typically this is the `id` field
|
|
* unless overwritten with the `identifier()` method.
|
|
* @example
|
|
* const schema = a.schema({
|
|
* Cart: a.model({
|
|
* items: a.string().required().array(),
|
|
* // 1. Create reference field
|
|
* customerId: a.id(),
|
|
* // 2. Create relationship field with the reference field
|
|
* customer: a.belongsTo('Customer', 'customerId'),
|
|
* }),
|
|
* Customer: a.model({
|
|
* name: a.string(),
|
|
* // 3. Create relationship field with the reference field
|
|
* // from the Cart model
|
|
* activeCart: a.hasOne('Cart', 'customerId')
|
|
* }),
|
|
* });
|
|
* @see {@link https://docs.amplify.aws/react/build-a-backend/data/data-modeling/relationships/#model-a-one-to-one-relationship}
|
|
* @param relatedModel the name of the related model
|
|
* @param references the field(s) that should be used to reference the related model
|
|
* @returns a one-to-one relationship definition
|
|
*/
|
|
function hasOne(relatedModel, references) {
|
|
return _modelRelationshipField(ModelRelationshipTypes.hasOne, relatedModel, Array.isArray(references) ? references : [references]);
|
|
}
|
|
/**
|
|
* Create a one-directional one-to-many relationship between two models using the `hasMany("MODEL_NAME", "REFERENCE_FIELD(s)")` method.
|
|
* @example
|
|
* const schema = a.schema({
|
|
* Member: a.model({
|
|
* name: a.string().required(),
|
|
* // 1. Create a reference field
|
|
* teamId: a.id(),
|
|
* // 2. Create a belongsTo relationship with the reference field
|
|
* team: a.belongsTo('Team', 'teamId'),
|
|
* })
|
|
* .authorization(allow => [allow.publicApiKey()]),
|
|
*
|
|
* Team: a.model({
|
|
* mantra: a.string().required(),
|
|
* // 3. Create a hasMany relationship with the reference field
|
|
* // from the `Member`s model.
|
|
* members: a.hasMany('Member', 'teamId'),
|
|
* })
|
|
* .authorization(allow => [allow.publicApiKey()]),
|
|
* });
|
|
* @see {@link https://docs.amplify.aws/react/build-a-backend/data/data-modeling/relationships/#model-one-to-many-relationships}
|
|
* @param relatedModel the name of the related model
|
|
* @param references the field(s) that should be used to reference the related model
|
|
* @returns a one-to-many relationship definition
|
|
*/
|
|
function hasMany(relatedModel, references) {
|
|
return _modelRelationshipField(ModelRelationshipTypes.hasMany, relatedModel, Array.isArray(references) ? references : [references]);
|
|
}
|
|
/**
|
|
* Use `belongsTo()` to create a field to query the related `hasOne()` or `hasMany()` relationship.
|
|
* The belongsTo() method requires that a hasOne() or hasMany() relationship already exists from
|
|
* parent to the related model.
|
|
*
|
|
* @example
|
|
* // one-to-many relationship
|
|
* const schema = a.schema({
|
|
* Member: a.model({
|
|
* name: a.string().required(),
|
|
* // 1. Create a reference field
|
|
* teamId: a.id(),
|
|
* // 2. Create a belongsTo relationship with the reference field
|
|
* team: a.belongsTo('Team', 'teamId'),
|
|
* })
|
|
* .authorization(allow => [allow.publicApiKey()]),
|
|
*
|
|
* Team: a.model({
|
|
* mantra: a.string().required(),
|
|
* // 3. Create a hasMany relationship with the reference field
|
|
* // from the `Member`s model.
|
|
* members: a.hasMany('Member', 'teamId'),
|
|
* })
|
|
* .authorization(allow => [allow.publicApiKey()]),
|
|
* });
|
|
* @example
|
|
* // one-to-one relationship
|
|
* const schema = a.schema({
|
|
* Cart: a.model({
|
|
* items: a.string().required().array(),
|
|
* // 1. Create reference field
|
|
* customerId: a.id(),
|
|
* // 2. Create relationship field with the reference field
|
|
* customer: a.belongsTo('Customer', 'customerId'),
|
|
* }),
|
|
* Customer: a.model({
|
|
* name: a.string(),
|
|
* // 3. Create relationship field with the reference field
|
|
* // from the Cart model
|
|
* activeCart: a.hasOne('Cart', 'customerId')
|
|
* }),
|
|
* });
|
|
* @see {@link https://docs.amplify.aws/react/build-a-backend/data/data-modeling/relationships/}
|
|
* @param relatedModel name of the related `.hasOne()` or `.hasMany()` model
|
|
* @param references the field(s) that should be used to reference the related model
|
|
* @returns a belong-to relationship definition
|
|
*/
|
|
function belongsTo(relatedModel, references) {
|
|
return _modelRelationshipField(ModelRelationshipTypes.belongsTo, relatedModel, Array.isArray(references) ? references : [references]);
|
|
}
|
|
|
|
export { ModelRelationshipTypes, __auth, belongsTo, hasMany, hasOne };
|
|
//# sourceMappingURL=ModelRelationshipField.mjs.map
|