Improved migration code substantially, wrote migrations for more properties

This commit is contained in:
Stefan Zermatten
2021-09-06 17:40:57 +02:00
parent 235560eb44
commit e79b8fda3b
20 changed files with 525 additions and 303 deletions

View File

@@ -2,34 +2,53 @@ import SimpleSchema from 'simpl-schema';
import ErrorSchema from '/imports/api/properties/subSchemas/ErrorSchema.js';
import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js';
const FieldToComputeSchema = new SimpleSchema({
// This is required, if we don't have a calculation delete the whole object
calculation: {
type: String,
},
});
// Get schemas that apply fields directly so they can be gracefully extended
// because {type: Schema} fields can't be extended
function fieldToCompute(field){
return new SimpleSchema({
// The object should already be set, but set again just in case
[field]: {
type: Object,
optional: true,
},
// This is required, if we don't have a calculation delete the whole object
[`${field}.calculation`]: {
type: String,
max: STORAGE_LIMITS.calculation,
},
});
}
const ComputedOnlyFieldSchema = new SimpleSchema({
value: {
type: SimpleSchema.oneOf(String, Number),
optional: true,
},
errors: {
type: Array,
optional: true,
maxCount: STORAGE_LIMITS.errorCount,
},
'errors.$':{
type: ErrorSchema,
},
});
function computedOnlyField(field){
return new SimpleSchema({
// The object should already be set, but set again just in case
[field]: {
type: Object,
optional: true,
},
[`${field}.value`]: {
type: SimpleSchema.oneOf(String, Number),
optional: true,
},
[`${field}.errors`]: {
type: Array,
optional: true,
maxCount: STORAGE_LIMITS.errorCount,
},
[`${field}.errors.$`]:{
type: ErrorSchema,
},
});
}
const ComputedFieldSchema = new SimpleSchema()
.extend(FieldToComputeSchema)
.extend(ComputedOnlyFieldSchema)
// This should rarely be used, since the other two will merge correctly when
// uncomputed and computedOnly schemas are merged
function computedField(field){
return computedField(field).extend(computedOnlyField(field));
}
export {
FieldToComputeSchema,
ComputedOnlyFieldSchema,
ComputedFieldSchema
fieldToCompute,
computedOnlyField,
computedField,
};