@aws-amplify/data-schema
Advanced tools
Comparing version 0.12.6 to 0.12.7
@@ -128,2 +128,74 @@ "use strict"; | ||
} | ||
/** | ||
* Tests whether two ModelField definitions are in conflict. | ||
* | ||
* This is a shallow check intended to catch conflicts between defined fields | ||
* and fields implied by authorization rules. Hence, it only compares type | ||
* and plurality. | ||
* | ||
* @param left | ||
* @param right | ||
* @returns | ||
*/ | ||
function areConflicting(left, right) { | ||
// These are the only props we care about for this comparison, because the others | ||
// (required, arrayRequired, etc) are not specified on auth or FK directives. | ||
const relevantProps = ['array', 'fieldType']; | ||
for (const prop of relevantProps) { | ||
if (left.data[prop] !== right.data[prop]) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
/** | ||
* Merges one field defition object onto an existing one, performing | ||
* validation (conflict detection) along the way. | ||
* | ||
* @param existing An existing field map | ||
* @param additions A field map to merge in | ||
*/ | ||
function addFields(existing, additions) { | ||
for (const [k, addition] of Object.entries(additions)) { | ||
if (!existing[k]) { | ||
existing[k] = addition; | ||
} | ||
else if (areConflicting(existing[k], addition)) { | ||
throw new Error(`Field ${k} defined twice with conflicting definitions.`); | ||
} | ||
else { | ||
// fields are defined on both sides, but match. | ||
} | ||
} | ||
} | ||
/** | ||
* Produces a new field definition object from every field definition object | ||
* given as an argument. Performs validation (conflict detection) as objects | ||
* are merged together. | ||
* | ||
* @param fieldsObjects A list of field definition objects to merge. | ||
* @returns | ||
*/ | ||
function mergeFieldObjects(...fieldsObjects) { | ||
const result = {}; | ||
for (const fields of fieldsObjects) { | ||
if (fields) | ||
addFields(result, fields); | ||
} | ||
return result; | ||
} | ||
/** | ||
* Given a list of authorization rules, produces a set of the implied owner and/or | ||
* group fields, along with the associated graphql `@auth` string directive. | ||
* | ||
* This is intended to be called for each model and field to collect the implied | ||
* fields and directives from that individual "item's" auth rules. | ||
* | ||
* The computed directives are intended to be appended to the graphql field definition. | ||
* | ||
* The computed fields are intended to be aggregated and injected per model. | ||
* | ||
* @param authorization A list of authorization rules. | ||
* @returns | ||
*/ | ||
function calculateAuth(authorization) { | ||
@@ -163,6 +235,6 @@ const authFields = {}; | ||
if (rule.multiOwner) { | ||
authFields[rule.groupOrOwnerField] = (0, ModelField_1.string)().array(); | ||
addFields(authFields, { [rule.groupOrOwnerField]: (0, ModelField_1.string)().array() }); | ||
} | ||
else { | ||
authFields[rule.groupOrOwnerField] = (0, ModelField_1.string)(); | ||
addFields(authFields, { [rule.groupOrOwnerField]: (0, ModelField_1.string)() }); | ||
} | ||
@@ -336,3 +408,3 @@ } | ||
if (fieldAuthField) { | ||
Object.assign(authFields, fieldAuthField); | ||
addFields(authFields, fieldAuthField); | ||
} | ||
@@ -423,6 +495,3 @@ } | ||
else { | ||
const fields = { | ||
...typeDef.data.fields, | ||
...fkFields[typeName], | ||
}; | ||
const fields = mergeFieldObjects(typeDef.data.fields, fkFields[typeName]); | ||
const identifier = typeDef.data.identifier; | ||
@@ -435,9 +504,3 @@ const [partitionKey] = identifier; | ||
const fieldLevelAuthRules = processFieldLevelAuthRules(fields, authFields); | ||
const { gqlFields, models } = processFields({ | ||
// idFields first, so they can be overridden by customer definitions when present. | ||
...idFields(typeDef), | ||
...fields, | ||
...authFields, | ||
...implicitTimestampFields(typeDef), | ||
}, fieldLevelAuthRules, identifier, partitionKey); | ||
const { gqlFields, models } = processFields(mergeFieldObjects(idFields(typeDef), fields, authFields, implicitTimestampFields(typeDef)), fieldLevelAuthRules, identifier, partitionKey); | ||
topLevelTypes.push(...models); | ||
@@ -444,0 +507,0 @@ const joined = gqlFields.join('\n '); |
{ | ||
"name": "@aws-amplify/data-schema", | ||
"version": "0.12.6", | ||
"version": "0.12.7", | ||
"license": "Apache-2.0", | ||
@@ -5,0 +5,0 @@ "repository": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
201796
2604