Reputation: 1
Students schema has an array of objects (tests) administered. No two tests are alike, and a student can have multiple tests. Need to be able to insert/update array of [test] objects with different schema's.
Have tried placing each test into it's own Collection tying back to the Student Id, but there are hundreds of unique tests resulting in hundreds of collections. Best practice?
const StudentSchema = new SimpleSchema({
lname: {
type: String,
optional: false,
label: "Last Name",
max: 100
},
fname: {
type: String,
optional: false,
label: "First Name",
max: 100
},
tests: {
type: Object,
blackbox: true,
optional: true,
},
}, { tracker: Tracker });
//Tests Schema example 1------------------------
TestADT_Schema = new SimpleSchema({
total_ADT_score_rs: {type: Number, optional: true,},
total_ADT_score_tsc: {type: Number, optional: true,},
qualitative_score: {type: Number, optional: true,},
});
Test_ADT_Schema.attachSchema(ADT_Schema);
//Test Schema example 2 --------------------------
TestWJIVCA_Schema = new SimpleSchema({
GenIntellectualAbility_AE: {type: Number, optional: true,},
GenIntellectualAbility_GE: {type: Number, optional: true,},
GenIntellectualAbility_RPI: {type: Number, optional: true,},
GenIntellectualAbility_SS: {type: Number, optional: true,},
GFGC_COMPOSITE_AE: {type: Number, optional: true,},
GFGC_COMPOSITE_GE: {type: Number, optional: true,},
GFGC_COMPOSITE_RPI: {type: Number, optional: true,},
});
TestWJIVCA_Schema.attachSchema(TestsWJIVCASchema);
Each student having 1 to n Tests, needing to nest the Tests into the Student's Collection, but not sure if this can be done?
Upvotes: 0
Views: 1244
Reputation: 330
...or to be safe, you also can use type: SimpleSchema.oneOf(String, Number, FirstSchema)
to be able to use more schemas to validate all you test kinds.
const schemaOne = new SimpleSchema({
itemRef: String,
partNo: String,
});
const schemaTwo = new SimpleSchema({
anotherIdentifier: String,
partNo: String,
});
const combinedSchema = new SimpleSchema({
item: SimpleSchema.oneOf(schemaOne, schemaTwo),
});
Only, there can be a little problem in that case, when your test structure is dynamically created as tree or something like that. In this case, you must change your app logic to be able to validate it, or write your own validator to validate it (maybe with combination with SimpleSchema).
Upvotes: 0
Reputation: 5671
If tests is an array that contains mysterious objects, you should mark the tests
as an array and it's children as the blackbox object:
const StudentSchema = new SimpleSchema({
lname: {
type: String,
optional: false,
label: "Last Name",
max: 100
},
fname: {
type: String,
optional: false,
label: "First Name",
max: 100
},
tests: Array,
'tests.$': {
type: Object,
blackbox: true,
},
}, { tracker: Tracker });
Upvotes: 2