0

We are migrating a template that was a mere questions array questions

[
  {
    type:text,
    name:first,
    value:What is our name
  },{
    type:radio,
    name:favourite fruits,
    value:[
      {banana},
      {apple},
      {pears}
    ]
  }
]

to a library like SurveyJS (this Json is complex, can maintain pages of questions with images and nested questions) to create a surveyJSON for one of our features (say doctor notes). Initially, we added the surveyJson column next to the questions array of that template.

Our architect said: what if tomorrow, our patient survey want to use the same template that is created for the note? By adding the surveyJS template into the notetempate table, we have not made it generic.

Ok, So we created a SurveyJSTemplate table that will now hold all templates in a generic way ie. if you have the templateID, you can use the template regardless if the feature is Notes, PatientSurvey etc.

Seems generic to me but with one concern now: The issue I have with this "lets make it generic" is that now, my data is not close to a obect oreinted approach. Before, I would have to pull my NoteTable data into a noteTemplate object and then do noteTemplate.getTemplate() which would give me my surveyJson data. Now, I would have to save the primary key of the templateID in to the NoteTemplate table and query the SurveyJSTemplate table. My service class is merely a data loading class.

Second issue I have with this is that if 5 features now all query the surveyJSTemplate table for their templates, any future migration, alter etc will impact 5 features. If I had maintained the SurveyJson into the NoteTemplate table (and other features maintain their jsons in their table) as eariler, there would be no such coupling.

I believe now that for the sake of "everyone can use everyone' template", there is a stronger couplying between all features that use template.

The only defense I find in this approach is that generally, User table is aso something all features uses but no one complains about strong coupling. So am I overthining the strong coupling caused by the SurveyJSTemplate?

1
  • 1
    not sure i understand, surely you can still have an OOP model regardless of how its stored in the db?
    – Ewan
    Commented Dec 10, 2023 at 16:13

1 Answer 1

2

So this is how i interpret your question.

Your SQL tables.

Before :

Note
 Id
 Q1
 Q2
 Q3
 surveyJSTemplateData 

After

Note
 Id
 Q1
 Q2
 Q3
 surveyTemplateId

SurveyTemplates
 Id
 Data

Your object:

noteTemplate
  Q1
  Q2
  Q3
  surveyJSTemplateData 
  getTemplate()
  {
     return surveyJSTemplateData 
  }

Seems to me that this object can be populated by a single select in both cases

Select * from Note left join SurveyTemplates st on note.surveyTemplateId = st.Id

Now you do have the issue that a reused surveyTemplate thats changed, will affect multiple noteTemplates, but this is desirable behaviour where a template is reused and can be avoided by simply making a second template.

There isn't really a coupling issue here, I think you might be using the term in a non-standard way?

Perhaps you could argue that if the goal is to replace the old style template with the new style, then your plan will be to remove all the Q1, Q2 etc fields from the Note table until you are just left with Id and SurveyJSTemplateData. In which case adding the second table now is wrong, because if you do that and remove the question columns then you are left with the unusual

Note
  Id
  surveyTemplateId

SurveyTemplates
  Id
  Data

Which obviously simplifies to a single table.

3
  • thank you for the write-up. I agree that an inner join can bring in this data. My question was more pedantic. Are we increasing cyclomatic complexity/ coupling when a template is made generic, placed in its own table as opposed to placing it closer to a feature table? Note noteID, noteName, noteDesc, templateID & SurveyJSTemplate templateID, template OR simply, Note Table (feature) noteID, noteName, noteDesc, noteTemplate Patient Table: patientID, patientName, patientTemplate CarePlan Table (care management feature) CarePlanID, PatientID, CarePlanTemplate
    – veritas
    Commented Dec 11, 2023 at 0:29
  • 3
    I think you should edit your question to show the tables and options etc. cyclomatic complexity can be measured, I don't see why it would increase. coupling is about classes and interfaces, i dont see why it would be relevant
    – Ewan
    Commented Dec 11, 2023 at 9:37
  • @veritas: Your argentation jumps between describing OOP modeling designs and database design. While some advice can apply equally to both, these are two separate concepts. Maybe you're just using some terms imprecisely but I think your question could stand with some more details on your precise problem/concern.
    – Flater
    Commented Dec 13, 2023 at 13:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.