Required fields and default values (#5835)
* Add field options to mongo schema metadata * Add/fix test with fields options * Add required validation failing test * Add more tests * Only set default value if field is undefined * Fix redis test * Fix tests * Test for creating a new class with field options * Validate default value type * fix lint (weird) * Fix lint another way * Add tests for beforeSave trigger and solve small issue regarding the use of unset in the beforeSave trigger
This commit is contained in:
committed by
GitHub
parent
d3810c2eba
commit
fd637ff4f8
@@ -46,6 +46,17 @@ function mongoSchemaFieldsToParseSchemaFields(schema) {
|
||||
);
|
||||
var response = fieldNames.reduce((obj, fieldName) => {
|
||||
obj[fieldName] = mongoFieldToParseSchemaField(schema[fieldName]);
|
||||
if (
|
||||
schema._metadata &&
|
||||
schema._metadata.fields_options &&
|
||||
schema._metadata.fields_options[fieldName]
|
||||
) {
|
||||
obj[fieldName] = Object.assign(
|
||||
{},
|
||||
obj[fieldName],
|
||||
schema._metadata.fields_options[fieldName]
|
||||
);
|
||||
}
|
||||
return obj;
|
||||
}, {});
|
||||
response.ACL = { type: 'ACL' };
|
||||
@@ -212,7 +223,7 @@ class MongoSchemaCollection {
|
||||
// Support additional types that Mongo doesn't, like Money, or something.
|
||||
|
||||
// TODO: don't spend an extra query on finding the schema if the type we are trying to add isn't a GeoPoint.
|
||||
addFieldIfNotExists(className: string, fieldName: string, type: string) {
|
||||
addFieldIfNotExists(className: string, fieldName: string, fieldType: string) {
|
||||
return this._fetchOneSchemaFrom_SCHEMA(className)
|
||||
.then(
|
||||
schema => {
|
||||
@@ -221,7 +232,7 @@ class MongoSchemaCollection {
|
||||
return;
|
||||
}
|
||||
// The schema exists. Check for existing GeoPoints.
|
||||
if (type.type === 'GeoPoint') {
|
||||
if (fieldType.type === 'GeoPoint') {
|
||||
// Make sure there are not other geopoint fields
|
||||
if (
|
||||
Object.keys(schema.fields).some(
|
||||
@@ -247,13 +258,37 @@ class MongoSchemaCollection {
|
||||
}
|
||||
)
|
||||
.then(() => {
|
||||
const { type, targetClass, ...fieldOptions } = fieldType;
|
||||
// We use $exists and $set to avoid overwriting the field type if it
|
||||
// already exists. (it could have added inbetween the last query and the update)
|
||||
return this.upsertSchema(
|
||||
className,
|
||||
{ [fieldName]: { $exists: false } },
|
||||
{ $set: { [fieldName]: parseFieldTypeToMongoFieldType(type) } }
|
||||
);
|
||||
if (fieldOptions && Object.keys(fieldOptions).length > 0) {
|
||||
return this.upsertSchema(
|
||||
className,
|
||||
{ [fieldName]: { $exists: false } },
|
||||
{
|
||||
$set: {
|
||||
[fieldName]: parseFieldTypeToMongoFieldType({
|
||||
type,
|
||||
targetClass,
|
||||
}),
|
||||
[`_metadata.fields_options.${fieldName}`]: fieldOptions,
|
||||
},
|
||||
}
|
||||
);
|
||||
} else {
|
||||
return this.upsertSchema(
|
||||
className,
|
||||
{ [fieldName]: { $exists: false } },
|
||||
{
|
||||
$set: {
|
||||
[fieldName]: parseFieldTypeToMongoFieldType({
|
||||
type,
|
||||
targetClass,
|
||||
}),
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user