From b0980d26ac6952842f5f54b2271a1e22b7ee957b Mon Sep 17 00:00:00 2001 From: Stefan Zermatten Date: Mon, 6 Sep 2021 23:59:52 +0200 Subject: [PATCH] Iterated on migration :( --- app/imports/api/properties/Actions.js | 36 ++++-------- app/imports/api/properties/Adjustments.js | 18 +++--- .../subSchemas/AttributeConsumedSchema.js | 19 ++++--- .../InlineCalculationFieldSchema.js | 37 ------------- .../subSchemas/ItemConsumedSchema.js | 17 +++--- ...omputedFieldSchema.js => computedField.js} | 0 .../subSchemas/createPropertySchema.js | 43 +++++++++++++++ .../subSchemas/inlineCalculationField.js | 55 +++++++++++++++++++ 8 files changed, 137 insertions(+), 88 deletions(-) delete mode 100644 app/imports/api/properties/subSchemas/InlineCalculationFieldSchema.js rename app/imports/api/properties/subSchemas/{ComputedFieldSchema.js => computedField.js} (100%) create mode 100644 app/imports/api/properties/subSchemas/createPropertySchema.js create mode 100644 app/imports/api/properties/subSchemas/inlineCalculationField.js diff --git a/app/imports/api/properties/Actions.js b/app/imports/api/properties/Actions.js index d17c3d66..9a23959f 100644 --- a/app/imports/api/properties/Actions.js +++ b/app/imports/api/properties/Actions.js @@ -1,18 +1,10 @@ import SimpleSchema from 'simpl-schema'; -import { - InlineCalculationFieldToComputeSchema, - ComputedOnlyInlineCalculationFieldSchema, - InlineCalculationFieldSchema, -} from '/imports/api/properties/subSchemas/InlineCalculationFieldSchema.js'; -import { - fieldToCompute, - computedOnlyField, -} from '/imports/api/properties/subSchemas/ComputedFieldSchema.js'; import { ResourcesSchema, ResourcesComputedOnlySchema, ResourcesComputedSchema, } from '/imports/api/properties/subSchemas/ResourcesSchema.js'; +import createPropertySchema from '/imports/api/properties/subSchemas/createPropertySchema.js'; import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js'; /* @@ -21,18 +13,18 @@ import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js'; * Any actions that are children of this action will be considered alternatives * to this action */ -let ActionSchema = new SimpleSchema({ +let ActionSchema = createPropertySchema({ name: { type: String, optional: true, max: STORAGE_LIMITS.name, }, summary: { - type: InlineCalculationFieldToComputeSchema, + type: 'inlineCalculationFieldToCompute', optional: true, }, description: { - type: InlineCalculationFieldToComputeSchema, + type: 'inlineCalculationFieldToCompute', optional: true, }, // What time-resource is used to take the action in combat @@ -59,7 +51,7 @@ let ActionSchema = new SimpleSchema({ }, // Calculation of how many times this action can be used uses: { - type: Object, + type: 'fieldToCompute', optional: true, }, // Integer of how many times it has already been used @@ -73,15 +65,15 @@ let ActionSchema = new SimpleSchema({ allowedValues: ['longRest', 'shortRest'], optional: true, }, -}).extend(fieldToCompute('uses')); +}); -const ComputedOnlyActionSchema = new SimpleSchema({ +const ComputedOnlyActionSchema = createPropertySchema({ summary: { - type: ComputedOnlyInlineCalculationFieldSchema, + type: 'computedOnlyInlineCalculationField', optional: true, }, description: { - type: ComputedOnlyInlineCalculationFieldSchema, + type: 'computedOnlyInlineCalculationField', optional: true, }, resources: { @@ -94,20 +86,12 @@ const ComputedOnlyActionSchema = new SimpleSchema({ type: Boolean, optional: true, }, -}).extend(computedOnlyField('uses')); +}); const ComputedActionSchema = new SimpleSchema() .extend(ActionSchema) .extend(ComputedOnlyActionSchema) .extend({ - summary: { - type: InlineCalculationFieldSchema, - optional: true, - }, - description: { - type: InlineCalculationFieldSchema, - optional: true, - }, resources: { type: ResourcesComputedSchema, defaultValue: {}, diff --git a/app/imports/api/properties/Adjustments.js b/app/imports/api/properties/Adjustments.js index 8ab97d4f..b3d69d25 100644 --- a/app/imports/api/properties/Adjustments.js +++ b/app/imports/api/properties/Adjustments.js @@ -1,15 +1,12 @@ import SimpleSchema from 'simpl-schema'; import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js'; -import { - fieldToCompute, - computedOnlyField, -} from '/imports/api/properties/subSchemas/ComputedFieldSchema.js'; +import createPropertySchema from '/imports/api/properties/subSchemas/createPropertySchema.js'; -const AdjustmentSchema = new SimpleSchema({ +const AdjustmentSchema = createPropertySchema({ // The roll that determines how much to change the attribute // This can be simplified, but should only compute when activated amount: { - type: Object, + type: 'fieldToCompute', optional: true, }, 'amount.calculation': { @@ -37,9 +34,14 @@ const AdjustmentSchema = new SimpleSchema({ allowedValues: ['set', 'increment'], defaultValue: 'increment', }, -}).extend(fieldToCompute('amount')); +}); -const ComputedOnlyAdjustmentSchema = computedOnlyField('amount'); +const ComputedOnlyAdjustmentSchema = createPropertySchema({ + amount: { + type: 'computedOnlyField', + optional: true, + }, +}); const ComputedAdjustmentSchema = new SimpleSchema() .extend(AdjustmentSchema) diff --git a/app/imports/api/properties/subSchemas/AttributeConsumedSchema.js b/app/imports/api/properties/subSchemas/AttributeConsumedSchema.js index 0c00ade4..63f16635 100644 --- a/app/imports/api/properties/subSchemas/AttributeConsumedSchema.js +++ b/app/imports/api/properties/subSchemas/AttributeConsumedSchema.js @@ -1,12 +1,9 @@ import SimpleSchema from 'simpl-schema'; import { Random } from 'meteor/random'; -import { - fieldToCompute, - computedOnlyField, -} from '/imports/api/properties/subSchemas/ComputedFieldSchema.js'; import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js'; +import createPropertySchema from '/imports/api/properties/subSchemas/createPropertySchema.js'; -const AttributeConsumedSchema = new SimpleSchema({ +const AttributeConsumedSchema = createPropertySchema({ _id: { type: String, regEx: SimpleSchema.RegEx.Id, @@ -20,12 +17,12 @@ const AttributeConsumedSchema = new SimpleSchema({ max: STORAGE_LIMITS.variableName, }, quantity: { - type: Object, + type: 'fieldToCompute', optional: true, }, -}).extend(fieldToCompute('quantity')); +}); -const ComputedOnlyAttributeConsumedSchema = new SimpleSchema({ +const ComputedOnlyAttributeConsumedSchema = createPropertySchema({ available: { type: Number, optional: true, @@ -40,7 +37,11 @@ const ComputedOnlyAttributeConsumedSchema = new SimpleSchema({ optional: true, max: STORAGE_LIMITS.name, }, -}).extend(computedOnlyField('quantity')); + quantity: { + type: 'computedOnlyField', + optional: true, + }, +}); const ComputedAttributeConsumedSchema = new SimpleSchema() .extend(AttributeConsumedSchema) diff --git a/app/imports/api/properties/subSchemas/InlineCalculationFieldSchema.js b/app/imports/api/properties/subSchemas/InlineCalculationFieldSchema.js deleted file mode 100644 index 085347b1..00000000 --- a/app/imports/api/properties/subSchemas/InlineCalculationFieldSchema.js +++ /dev/null @@ -1,37 +0,0 @@ -import SimpleSchema from 'simpl-schema'; -import InlineComputationSchema from '/imports/api/properties/subSchemas/InlineComputationSchema.js'; -import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js'; - -const InlineCalculationFieldToComputeSchema = new SimpleSchema({ - text: { - type: String, - optional: true, - max: STORAGE_LIMITS.inlineCalculationField, - }, -}); - -const ComputedOnlyInlineCalculationFieldSchema = new SimpleSchema({ - 'inlineCalculations': { - type: Array, - defaultValue: [], - maxCount: STORAGE_LIMITS.inlineCalculationCount, - }, - 'inlineCalculations.$': { - type: InlineComputationSchema, - }, - value: { - type: String, - optional: true, - max: STORAGE_LIMITS.inlineCalculationField, - }, -}); - -const InlineCalculationFieldSchema = new SimpleSchema() - .extend(InlineCalculationFieldToComputeSchema) - .extend(ComputedOnlyInlineCalculationFieldSchema) - -export { - InlineCalculationFieldToComputeSchema, - ComputedOnlyInlineCalculationFieldSchema, - InlineCalculationFieldSchema, -}; diff --git a/app/imports/api/properties/subSchemas/ItemConsumedSchema.js b/app/imports/api/properties/subSchemas/ItemConsumedSchema.js index 762ed985..42eeb402 100644 --- a/app/imports/api/properties/subSchemas/ItemConsumedSchema.js +++ b/app/imports/api/properties/subSchemas/ItemConsumedSchema.js @@ -1,13 +1,10 @@ import SimpleSchema from 'simpl-schema'; import { Random } from 'meteor/random'; -import { - fieldToCompute, - computedOnlyField, -} from '/imports/api/properties/subSchemas/ComputedFieldSchema.js'; import { storedIconsSchema } from '/imports/api/icons/Icons.js'; import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js'; +import createPropertySchema from '/imports/api/properties/subSchemas/createPropertySchema.js'; -const ItemConsumedSchema = new SimpleSchema({ +const ItemConsumedSchema = createPropertySchema({ _id: { type: String, regEx: SimpleSchema.RegEx.Id, @@ -20,7 +17,7 @@ const ItemConsumedSchema = new SimpleSchema({ optional: true, }, quantity: { - type: Object, + type: 'fieldToCompute', optional: true, }, itemId: { @@ -28,13 +25,17 @@ const ItemConsumedSchema = new SimpleSchema({ regEx: SimpleSchema.RegEx.Id, optional: true, }, -}).extend(fieldToCompute('quantity')); +}); const ComputedOnlyItemConsumedSchema = new SimpleSchema({ available: { type: Number, optional: true, }, + quantity: { + type: 'computedOnlyField', + optional: true, + }, // This appears both in the computed and uncomputed schema because it can be // set by both a computation or a form itemId: { @@ -57,7 +58,7 @@ const ComputedOnlyItemConsumedSchema = new SimpleSchema({ optional: true, max: STORAGE_LIMITS.color, }, -}).extend(computedOnlyField('quantity')); +}); const ComputedItemConsumedSchema = new SimpleSchema() .extend(ItemConsumedSchema) diff --git a/app/imports/api/properties/subSchemas/ComputedFieldSchema.js b/app/imports/api/properties/subSchemas/computedField.js similarity index 100% rename from app/imports/api/properties/subSchemas/ComputedFieldSchema.js rename to app/imports/api/properties/subSchemas/computedField.js diff --git a/app/imports/api/properties/subSchemas/createPropertySchema.js b/app/imports/api/properties/subSchemas/createPropertySchema.js new file mode 100644 index 00000000..36c5d5e4 --- /dev/null +++ b/app/imports/api/properties/subSchemas/createPropertySchema.js @@ -0,0 +1,43 @@ +import { + inlineCalculationFieldToCompute, + computedOnlyInlineCalculationField, +} from '/imports/api/properties/subSchemas/inlineCalculationField.js'; +import { + fieldToCompute, + computedOnlyField, +} from '/imports/api/properties/subSchemas/computedField.js'; +import SimpleSchema from 'simpl-schema'; + +export default function createPropertySchema(definition){ + const computationFields = { + inlineCalculationFieldToCompute: [], + computedOnlyInlineCalculationField: [], + fieldToCompute: [], + computedOnlyField: [], + }; + const computedKeys = Object.keys(computationFields); + + for (let key in definition){ + const def = definition[key]; + if (computedKeys.includes(def.type)){ + computationFields[def.type].push(key); + def.type = Object; + } + } + + const schema = new SimpleSchema(definition); + + computationFields.inlineCalculationFieldToCompute.forEach(key => { + schema.extend(inlineCalculationFieldToCompute(key)) + }); + computationFields.computedOnlyInlineCalculationField.forEach(key => { + schema.extend(computedOnlyInlineCalculationField(key)) + }); + computationFields.fieldToCompute.forEach(key => { + schema.extend(fieldToCompute(key)) + }); + computationFields.computedOnlyField.forEach(key => { + schema.extend(computedOnlyField(key)) + }); + return schema +} diff --git a/app/imports/api/properties/subSchemas/inlineCalculationField.js b/app/imports/api/properties/subSchemas/inlineCalculationField.js new file mode 100644 index 00000000..93bfbf23 --- /dev/null +++ b/app/imports/api/properties/subSchemas/inlineCalculationField.js @@ -0,0 +1,55 @@ +import SimpleSchema from 'simpl-schema'; +import InlineComputationSchema from '/imports/api/properties/subSchemas/InlineComputationSchema.js'; +import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js'; + +// Get schemas that apply fields directly so they can be gracefully extended +// because {type: Schema} fields can't be extended +function inlineCalculationFieldToCompute(field){ + return new SimpleSchema({ + // The object should already be set, but set again just in case + [field]: { + type: Object, + optional: true, + }, + [`${field}.text`]: { + type: String, + optional: true, + max: STORAGE_LIMITS.inlineCalculationField, + }, + }); +} + +function computedOnlyInlineCalculationField(field){ + return new SimpleSchema({ + // The object should already be set, but set again just in case + [field]: { + type: Object, + optional: true, + }, + [`${field}.inlineCalculations`]: { + type: Array, + defaultValue: [], + maxCount: STORAGE_LIMITS.inlineCalculationCount, + }, + [`${field}.inlineCalculations.$`]: { + type: InlineComputationSchema, + }, + [`${field}.value`]: { + type: String, + optional: true, + max: STORAGE_LIMITS.inlineCalculationField, + }, + }); +} + +function computedInlineCalculationField(field){ + return inlineCalculationFieldToCompute(field).extend( + computedOnlyInlineCalculationField(field) + ) +} + +export { + inlineCalculationFieldToCompute, + computedOnlyInlineCalculationField, + computedInlineCalculationField, +};